Zum Inhalt springen

„Modul:UnitTests/invoke“ – Versionsunterschied

aus Wikipedia, der freien Enzyklopädie
[gesichtete Version][gesichtete Version]
Inhalt gelöscht Inhalt hinzugefügt
Setup
 
updates
Zeile 1: Zeile 1:
--[=[ 2014-10-03
--[=[ 2014-10-19
UnitTests/invoke
UnitTests/invoke
Debug any Module from which this is required as a submodule.
Debug any Module from which this is required as a submodule.
Zeile 11: Zeile 11:
return string.format( "<span class=\"error\">%s</span>", a )
return string.format( "<span class=\"error\">%s</span>", a )
end -- fault()
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()




Zeile 23: Zeile 51:
-- Returns:
-- Returns:
-- string
-- string
local set, story
local r, set, story
for k, v in pairs( args ) do
for k, v in pairs( args ) do
if type( k) == "number" then
if type( k ) == "number" then
set = string.format( "%d", k )
set = string.format( "%d", k )
else
else
Zeile 42: Zeile 70:
story = ""
story = ""
end
end
local r = "* ''" .. action .. "''<br />" ..
r = "* ''" .. action .. "''<br />"
frame:callParserFunction( "#tag:syntaxhighlight",
.. fade( frame, story ) .. "<br />"
{ story,
lang="text",
enclose="none" } )
.. "<br />"
if accept and accept ~= "" then
if accept and accept ~= "" then
r = r .. frame:callParserFunction( "#tag:syntaxhighlight",
r = r .. fade( frame, accept )
{ accept,
lang="text",
enclose="none" } )
end
end
if not accept and accept ~= "" then
if not accept and accept ~= "" then
local s = frame:callParserFunction( "#tag:syntaxhighlight",
{ approve,
lang="text",
enclose="none",
style="color: #8B008B" } )
r = string.format( "%s<br /><code>|_r_=</code>%s",
r = string.format( "%s<br /><code>|_r_=</code>%s",
r, s )
r, fade( frame, approve, "color:#8B008B" ) )
elseif not approve and accept and accept ~= "" then
elseif not approve and accept and accept ~= "" then
r = r .. "<br />" .. fault( "''no result''" )
r = r .. "<br />" .. fault( "''no result''" )
elseif accept and approve and accept ~= approve then
elseif accept and approve and accept ~= approve then
local i, sub
local j = -1
local j = -1
local m = mw.ustring.len( accept )
local m = mw.ustring.len( accept )
local n = mw.ustring.len( approve )
local n = mw.ustring.len( approve )
local i, sub
r = r .. "<br />»"
r = r .. "<br />»"
for i = 1, m do
for i = 1, m do
Zeile 79: Zeile 95:
if j > 0 then
if j > 0 then
sub = mw.ustring.sub( accept, 1, j )
sub = mw.ustring.sub( accept, 1, j )
r = r .. frame:callParserFunction( "#tag:syntaxhighlight",
r = r .. fade( frame, sub )
{ sub,
lang="text",
enclose="none" } )
if n > j then
if n > j then
r = r .. "&#182;"
r = r .. "&#182;"
Zeile 88: Zeile 101:
end
end
if n > j then
if n > j then
local style = "color: #FF0000; font-weight: bold;"
sub = mw.ustring.sub( approve, j + 1 )
sub = mw.ustring.sub( approve, j + 1 )
r = r .. frame:callParserFunction( "#tag:syntaxhighlight",
r = string.format( "%s%s",
{ sub,
r, fade( frame, sub, style ) )
lang="text",
enclose="none",
style="color: red" } )
end
end
r = r .. "«"
r = r .. "«"
Zeile 118: Zeile 129:
local params = { }
local params = { }
for k, v in pairs( frame.args ) do
for k, v in pairs( frame.args ) do
if type( k ) == "string"
if type( k ) == "string"
and k:match( "^_%l_$" ) then
and k:match( "^_%l_$" ) then
k = false
k = false
Zeile 126: Zeile 137:
end
end
end -- for k, v
end -- for k, v
frame.args = params
frame.args = params
lucky, r = pcall( funny, frame )
lucky, r = pcall( funny, frame )
if lucky then
if lucky then

Version vom 19. Oktober 2014, 17:30 Uhr

Die Dokumentation für dieses Modul kann unter Modul:UnitTests/invoke/Doku erstellt werden

--[=[ 2014-10-19
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, accept, approve )
    -- Show result; append string wikisyntax if differing
    -- Parameter:
    --     action   -- string, function
    --     args     -- table, with parameters
    --     accept   -- string, with expectation, or nil
    --     approve  -- string, with result
    -- Returns:
    --     string
    local r, set, story
    for k, v in pairs( args ) 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 .. "&#182;"
            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 = { }
                for k, v in pairs( frame.args ) do
                    if type( k ) == "string"
                       and  k:match( "^_%l_$" ) then
                        k = false
                    end
                    if k then
                        params[ k ] = v
                    end
                end -- for k, v
                frame.args = params
                lucky, r = pcall( funny, frame )
                if lucky then
                    r = fidelity( frame, fun, params, should, r )
                else
                    r = fault( self .. " " .. r )
                end
            else
                r = fault( subject ..
                           " * function " .. fun .. " unknown" )
            end
        else
            r = fault( self .. " * no fun" )
        end
    else
        r = fault( self .. " " .. modt )
    end
    return r
end -- p.f

return p