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

local UnitTests = { suite  = "UnitTests",
                    serial = "2020-04-21",
                    sub    = "invoke",
                    item   = 0 }
--[=[
UnitTests/invoke
Debug any Module from which this is required as a submodule.
Compare result after invoke and expectation.
]=]
local Failsafe = UnitTests



local function fault( alert )
    -- Format error by class=error
    return tostring( mw.html.create( "span" )
                            :addClass( "error" )
                            :wikitext( alert ) )
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
            local e = mw.html.create( "span" )
                             :attr( "style", assign )
                             :wikitext( r )
            r = tostring( e )
        end
    else
        local params = { r,
                         lang="text",
                         inline="" }
        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
        local e = mw.html.create( "code" )
                         :wikitext( "|_r_=" )
        r = string.format( "%s<br />%s%s",
                           r,
                           tostring( e ),
                           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()



Failsafe.failsafe = function ( atleast )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --     atleast  -- string, with required version or "wikidata" or "~"
    --                 or false
    -- Postcondition:
    --     Returns  string  -- with queried version, also if problem
    --              false   -- if appropriate
    -- 2019-10-15
    local last  = ( atleast == "~" )
    local since = atleast
    local r
    if last  or  since == "wikidata" then
        local item = Failsafe.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local entity = mw.wikibase.getEntity( string.format( "Q%d",
                                                                 item ) )
            if type( entity ) == "table" then
                local seek = Failsafe.serialProperty or "P348"
                local vsn  = entity:formatPropertyValues( seek )
                if type( vsn ) == "table"  and
                   type( vsn.value ) == "string"  and
                   vsn.value ~= "" then
                    if last  and  vsn.value == Failsafe.serial then
                        r = false
                    else
                        r = vsn.value
                    end
                end
            end
        end
    end
    if type( r ) == "nil" then
        if not since  or  since <= Failsafe.serial then
            r = Failsafe.serial
        else
            r = false
        end
    end
    return r
end -- Failsafe.failsafe()



-- 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

p.failsafe = function ( frame )
    -- Versioning interface
    local s = type( frame )
    local since
    if s == "table" then
        since = frame.args[ 1 ]
    elseif s == "string" then
        since = frame
    end
    if since then
        since = mw.text.trim( since )
        if since == "" then
            since = false
        end
    end
    return Failsafe.failsafe( since )  or  ""
end -- p.failsafe()

return p