Modul:UnitTests
Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | |||
---|---|---|---|---|---|---|
Modul | Deutsch | English
|
Modul: | Dokumentation |
--[=[ 2019-03-25
UnitTests/invoke
Debug any Module from which this is required as a submodule.
Compare result after invoke and expectation.
]=]
local function fault( a )
-- Format error by class=error
return string.format( "<span class=\"error\">%s</span>", a )
end -- fault()
local function fade( frame, apply, assign, allow )
-- Escape wikisyntax into one line
-- Parameter:
-- apply -- string, to be escaped
-- assign -- apply CSS style, if string
-- allow -- permit regular text, if true; else code
-- Returns:
-- string, without line break
local r = apply:gsub( string.char( 10 ),
mw.ustring.char( 182 ) )
if allow then
if assign then
r = string.format( "<span style='%s'>%s</span>", assign, r )
end
else
local params = { r,
lang="text",
enclose="none" }
if assign then
params.style = assign
end
r = frame:callParserFunction( "#tag:syntaxhighlight", params )
end
return r
end -- fade()
local function fidelity( frame, action, args, alt, accept, approve )
-- Show result; append string wikisyntax if differing
-- Parameter:
-- action -- string, function
-- args -- table, with forwarded parameter values
-- alt -- table, with displayed parameter values
-- accept -- string, with expectation, or nil
-- approve -- string, with result
-- Returns:
-- string
local r, set, story
for k, v in pairs( alt ) do
if type( k ) == "number" then
set = string.format( "%d", k )
else
set = k
end
if story then
story = story .. "|"
else
story = ""
end
story = string.format( "%s%s=%s", story, set, v )
end -- for k, v
if story then
story = story .. "}}"
else
story = ""
end
r = "* ''" .. action .. "''<br />"
.. fade( frame, story ) .. "<br />"
if accept and accept ~= "" then
r = r .. fade( frame, accept )
end
if not accept and accept ~= "" then
r = string.format( "%s<br /><code>|_r_=</code>%s",
r, fade( frame, approve, "color:#8B008B" ) )
elseif not approve and accept and accept ~= "" then
r = r .. "<br />" .. fault( "''no result''" )
elseif accept and approve and accept ~= approve then
local j = -1
local m = mw.ustring.len( accept )
local n = mw.ustring.len( approve )
local i, sub
r = r .. "<br />»"
for i = 1, m do
if mw.ustring.codepoint( accept, i, i ) ~=
mw.ustring.codepoint( approve, i, i ) then
j = i - 1
break -- for i
end
end -- for i
if j > 0 then
sub = mw.ustring.sub( accept, 1, j )
r = r .. fade( frame, sub )
if n > j then
r = r .. "¶"
end
end
if n > j then
local style = "color: #FF0000; font-weight: bold;"
sub = mw.ustring.sub( approve, j + 1 )
r = string.format( "%s%s",
r, fade( frame, sub, style ) )
end
r = r .. "«"
end
return r
end -- fidelity()
-- Export
local p = { }
function p.f( frame )
local self = frame:getTitle()
local subject = self:match( "^(.+)/[^/]+$" )
local lucky, modt, r
lucky, modt = pcall( require, subject )
if type( modt ) == "table" then
local fun = frame.args[ "_f_" ]
if fun then
local funny = modt[ fun ]
if funny then
local should = frame.args[ "_r_" ]
local params = { }
local views = { }
for k, v in pairs( frame.args ) do
if type( k ) == "string"
and k:sub( 1, 1 ) == "_" then
if k:match( "^_%l_$" ) then
k = false
else
k = k:match( "^__(.+)__$" )
if k then
views[ k ] = v
k = false
end
end
end
if k then
params[ k ] = v
end
end -- for k, v
frame.args = params
lucky, r = pcall( funny, frame )
if lucky then
for k, v in pairs( params ) do
if type( views[ k ] ) ~= "string" then
views[ k ] = v
end
end -- for k, v
r = fidelity( frame, fun, params, views, should, r )
else
r = fault( string.format( "%s %s", self, r ) )
end
else
r = fault( string.format( "%s * function %s unknown",
subject, fun ) )
end
else
r = fault( self .. " * no fun" )
end
else
r = fault( self .. " " .. modt )
end
return r
end -- p.f
return p