Zum Inhalt springen

„Modul:UnitTests/invoke“ – Versionsunterschied

aus Wikipedia, der freien Enzyklopädie
[gesichtete Version][gesichtete Version]
Inhalt gelöscht Inhalt hinzugefügt
K Schützte „Modul:UnitTests/invoke“: Häufig eingebundenes Modul ([Bearbeiten=Nur angemeldete, nicht neue Benutzer] (unbeschränkt) [Verschieben=Nur Administratoren] (unbeschränkt))
2019-03-25
Zeile 1: Zeile 1:
--[=[ 2014-10-19
--[=[ 2019-03-25
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 42: Zeile 42:




local function fidelity( frame, action, args, accept, approve )
local function fidelity( frame, action, args, alt, accept, approve )
-- Show result; append string wikisyntax if differing
-- Show result; append string wikisyntax if differing
-- Parameter:
-- Parameter:
-- action -- string, function
-- action -- string, function
-- args -- table, with parameters
-- args -- table, with forwarded parameter values
-- alt -- table, with displayed parameter values
-- accept -- string, with expectation, or nil
-- accept -- string, with expectation, or nil
-- approve -- string, with result
-- approve -- string, with result
Zeile 52: Zeile 53:
-- string
-- string
local r, set, story
local r, set, story
for k, v in pairs( args ) do
for k, v in pairs( alt ) do
if type( k ) == "number" then
if type( k ) == "number" then
set = string.format( "%d", k )
set = string.format( "%d", k )
Zeile 128: Zeile 129:
local should = frame.args[ "_r_" ]
local should = frame.args[ "_r_" ]
local params = { }
local params = { }
local views = { }
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:sub( 1, 1 ) == "_" then
k = false
if k:match( "^_%l_$" ) then
k = false
else
k = k:match( "^__(.+)__$" )
if k then
if k:match( "^[0-9]%d*$" ) then
k = tonumber( k )
end
views[ k ] = v
k = false
end
end
end
end
if k then
if k then
Zeile 140: Zeile 153:
lucky, r = pcall( funny, frame )
lucky, r = pcall( funny, frame )
if lucky then
if lucky then
r = fidelity( frame, fun, params, should, r )
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
else
r = fault( self .. " " .. r )
r = fault( string.format( "%s %s", self, r ) )
end
end
else
else
r = fault( subject ..
r = fault( string.format( "%s * function %s unknown",
" * function " .. fun .. " unknown" )
subject, fun ) )
end
end
else
else

Version vom 26. März 2019, 22:10 Uhr

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

--[=[ 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 .. "&#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 = { }
                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
                                if k:match( "^[0-9]%d*$" ) then
                                    k = tonumber( k )
                                end
                                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