Zum Inhalt springen

Modul:UnitTests/invoke

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 4. Oktober 2014 um 16:06 Uhr durch PerfektesChaos (Diskussion | Beiträge) (Setup). Sie kann sich erheblich von der aktuellen Version unterscheiden.
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

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

--[=[ 2014-10-03
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 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 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
    local r = "* ''" .. action .. "''<br />" ..
              frame:callParserFunction( "#tag:syntaxhighlight",
                                        { story,
                                          lang="text",
                                          enclose="none" } )
              .. "<br />"
    if accept  and  accept ~= "" then
        r = r .. frame:callParserFunction( "#tag:syntaxhighlight",
                                           { accept,
                                             lang="text",
                                             enclose="none" } )
    end
    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, s )
    elseif not approve  and  accept  and  accept ~= "" then
        r = r .. "<br />" .. fault( "''no result''" )
    elseif accept  and  approve  and  accept ~= approve then
        local i, sub
        local j = -1
        local m = mw.ustring.len( accept )
        local n = mw.ustring.len( approve )
        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 .. frame:callParserFunction( "#tag:syntaxhighlight",
                                                 { sub,
                                                   lang="text",
                                                   enclose="none" } )
            if n > j then
                r = r .. "&#182;"
            end
        end
        if n > j then
            sub = mw.ustring.sub( approve,  j + 1 )
            r   = r .. frame:callParserFunction( "#tag:syntaxhighlight",
                                                 { sub,
                                                   lang="text",
                                                   enclose="none",
                                                   style="color: red" } )
        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