Zum Inhalt springen

„Modul:DateTime/test“ – Versionsunterschied

aus Wikipedia, der freien Enzyklopädie
[gesichtete Version][gesichtete Version]
Inhalt gelöscht Inhalt hinzugefügt
update
2015-08-04
Zeile 1: Zeile 1:
--[=[ 2014-11-04
--[=[ 2015-08-04
Debug Module:DateTime
Debug Module:DateTime
]=]
]=]
Zeile 6: Zeile 6:
-- local globals
-- local globals
local DateTime = require( "Module:DateTime" )
local DateTime = require( "Module:DateTime" )
local Tests = { }
DateTime = DateTime.DateTime
local Tests = { }
local Params = { "lang",
"bc",
local Params = { "lang",
"bc",
"year",
"year",
"month",
"month",
"month2",
"month2",
"week",
"dom",
"dom",
"dom2",
"dom2",
"hour",
"hour",
"hour2",
"hour2",
"min",
"min",
"sec",
"sec",
"msec",
"msec",
"zone" }
"mysec",
"zone" }
DateTime = DateTime.DateTime()




Zeile 31: Zeile 33:




local function freeze( about, around )
local function flat( adapt, around, assign )
-- Protect string wikisyntax
-- Format wikitext string like syntaxhighlight
-- Parameter:
-- Parameter:
-- about -- string, to be protected
-- adapt -- string, with coded text
-- around -- embed in <code>
-- around -- embed like in <code> rather than span
-- assign -- table, with CSS style, or nil
-- Returns:
-- Returns:
-- string
-- <span> string
local r = mw.getCurrentFrame():callParserFunction(
local e = mw.html.create( "span" )
local css = { ["font-family"] = "monospace" }
"#tag:syntaxhighlight",
{ about,
lang="text",
enclose="none" } )
if around then
if around then
r = string.format( "<code>%s</code>", r )
e:addClass( "mw-code" )
css["white-space"] = "nowrap"
end
end
e:wikitext( mw.text.nowiki( adapt ) )
return r
if assign then
end -- freeze()
for k, v in pairs( assign ) do
css[ k ] = v
end -- for k, v
end
e:css( css )
return tostring( e )
end -- flat()






Zeile 55: Zeile 65:
-- Parameter:
-- Parameter:
-- args -- template parameters
-- args -- template parameters
-- 2 -- string to be interpreted, or empty
-- 2 -- string, to be interpreted, or empty
-- 4 -- string, with relative interval, or empty
-- *** -- expected field values, k=v
-- *** -- expected field values, k=v
-- Returns:
-- Returns:
Zeile 61: Zeile 72:
local r
local r
local s = ( args[ 2 ] or "" )
local s = ( args[ 2 ] or "" )
local o = DateTime( s )
local o = DateTime( s, false, args[ 4 ] )
if s == "" then
if s == "" then
r = "\n==== fetcher() ====\n"
r = "\n==== fetcher() ====\n"
Zeile 115: Zeile 126:
-- args -- template parameters
-- args -- template parameters
-- template -- which template
-- template -- which template
-- 1 -- string with first template parameter
-- 1 -- string, with first template parameter
-- 2 -- string with second template parameter
-- 2 -- string, with second template parameter
-- ... -- strings with more template parameters
-- ... -- strings with more template parameters
-- r -- expected result (string)
-- r -- expected result (string)
Zeile 154: Zeile 165:
r = string.format( "%s%s !!!!!!!!!! %s",
r = string.format( "%s%s !!!!!!!!!! %s",
r,
r,
freeze( suppose, true ),
flat( suppose, true ),
fault( freeze( sort, true ) ) )
fault( flat( sort, true ) ) )
end
end
end
end
Zeile 163: Zeile 174:




Tests.follow = function ( args )
Tests.flower = function ( args )
-- Evaluate comparison function
-- Parameter:
-- args -- template parameters
-- 2 -- string, with comparison function name
-- 3 -- string, with first timestamp, or empty
-- 4 -- string, with second timestamp, or empty
-- r -- expected result (number)
local r
local select = args[ 2 ]
if select then
local suppose = args.r or ""
local o = DateTime( args[ 3 ] )
local slip = o:flow( args[ 4 ], select ) and "1" or ""
r = string.format( "* %s ''%s'' %s &#160; »<code>%s</code>«",
args[ 3 ] or "",
select,
args[ 4 ] or "",
slip )
if slip ~= suppose then
r = string.format( "%s %s »<code>%s</code>«",
r, fault( "~=" ), suppose )
end
else
r = fault( "No comparison function name" )
end
return r
end -- Tests.flower()



Tests.follower = function ( args )
-- Evaluate sort function
-- Evaluate sort function
-- Parameter:
-- Parameter:
-- args -- template parameters
-- args -- template parameters
-- 2 -- string to be interpreted
-- 2 -- string, to be interpreted
-- 3 -- string with heading cell, or empty
-- 3 -- string, with heading cell, or empty
-- r -- expected result (number)
-- r -- expected result (number)
-- Returns:
-- Returns:
Zeile 191: Zeile 233:
end
end
return r
return r
end -- Tests.follow()
end -- Tests.follower()




Zeile 199: Zeile 241:
-- Parameter:
-- Parameter:
-- args -- template parameters
-- args -- template parameters
-- 2 -- string to be interpreted, or empty
-- 2 -- string, to be interpreted, or empty
-- 3 -- string with format, or empty
-- 3 -- string, with format, or empty
-- 4 -- string with language, or empty
-- 4 -- string, with language, or empty
-- r -- expected result
-- r -- expected result
-- Returns:
-- Returns:
Zeile 207: Zeile 249:
local r
local r
local source = ( args[ 2 ] or "" )
local source = ( args[ 2 ] or "" )
local o = DateTime( source )
local shower = ( args[ 3 ] or "" )
local shower = ( args[ 3 ] or "" )
local slang = args[ 4 ]
local slang = args[ 4 ]
local o = DateTime( source, slang )
local p
local p
if slang then
if slang then
Zeile 218: Zeile 260:
end
end
if r then
if r then
local syntax = freeze( r, false )
local syntax = flat( r, false )
if r ~= args.r then
if r ~= args.r then
local should = ( args.r or "" )
local should = ( args.r or "" )
Zeile 238: Zeile 280:
return r
return r
end -- Tests.formatter()
end -- Tests.formatter()



Tests.futurist = function ( args )
-- Evaluate formatting function
-- Parameter:
-- args -- template parameters
-- 2 -- string, to be interpreted, or empty
-- 3 -- string, with shift
-- r -- expected result
-- Returns:
-- string
local r
local source = ( args[ 2 ] or "" )
local shift = ( args[ 3 ] or "" )
local o = DateTime( source )
if o then
r = o:future( shift ):tostring()
if r ~= args.r then
local should = ( args.r or "" )
r = string.format( "\"%s\" ./. \"%s\"",
fault( r ), should )
end
else
r = fault( "??????" )
end
r = string.format( "\n; \"%s\"\n: \"%s\" &rArr; %s",
shift, source, r )
return r
end -- Tests.futurist()





Version vom 5. August 2015, 22:42 Uhr

Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


--[=[ 2015-08-04
Debug Module:DateTime
]=]


-- local globals
local DateTime = require( "Module:DateTime" )
local Tests    = { }
local Params   = { "lang",
                   "bc",
                   "year",
                   "month",
                   "month2",
                   "week",
                   "dom",
                   "dom2",
                   "hour",
                   "hour2",
                   "min",
                   "sec",
                   "msec",
                   "mysec",
                   "zone" }
DateTime = DateTime.DateTime()



local function fault( a )
    -- Formatiere Fehler mit class=error
    return string.format( "<span class=\"error\">%s</span>", a )
end -- fault()



local function flat( adapt, around, assign )
    -- Format wikitext string like syntaxhighlight
    -- Parameter:
    --     adapt   -- string, with coded text
    --     around  -- embed like in <code> rather than span
    --     assign  -- table, with CSS style, or nil
    -- Returns:
    --     <span> string
    local e   = mw.html.create( "span" )
    local css = { ["font-family"] = "monospace" }
    if around then
        e:addClass( "mw-code" )
        css["white-space"] = "nowrap"
    end
    e:wikitext( mw.text.nowiki( adapt ) )
    if assign then
        for k, v in pairs( assign ) do
            css[ k ] = v
        end -- for k, v
    end
    e:css( css )
    return tostring( e )
end -- flat()





Tests.fetcher = function ( args )
    -- Evaluate constructor function
    -- Parameter:
    --     args  -- template parameters
    --              2  -- string, to be interpreted, or empty
    --              4  -- string, with relative interval, or empty
    --              ***  --  expected field values, k=v
    -- Returns:
    --     string
    local r
    local s = ( args[ 2 ] or "" )
    local o = DateTime( s, false, args[ 4 ] )
    if s == "" then
        r = "\n==== fetcher() ====\n"
    else
        r = string.format( "\n==== %s ====\n", s )
    end
    if o then
        local n = table.maxn( Params )
        local score, single, suppose
        s = mw.text.trim( s )
        if s == "" then
            s = "''empty''"
        end
        r = r ..
            "{| class='wikitable'\n" ..
            "|-\n" ..
            "! item !! expect !! got !! diff\n"
        for i = 1, n do
            single  = Params[ i ]
            score   = o[ single ]
            suppose = args[ single ]
            if score ~= nil  or  suppose ~= nil then
                r = string.format( "%s\n|-\n| %s || ", r, single )
                if suppose then
                    r = string.format( "%s<code>%s</code>", r, suppose )
                end
                r = r .. " || "
                if score ~= nil then
                    score = tostring( score )
                    r     = string.format( "%s<code>%s</code>",
                                           r, score )
                end
                if score ~= suppose then
                    r = string.format( "%s || %s",
                                       r,  fault( "!!!!!!!!!!" ) )
                end
            end
        end -- for i
        r = r .. "\n|}\n"
    elseif args.failed then
        r = r .. "'''failed'''"
    else
        r = r .. "<span class=\"error\">FAILED</span>"
    end
    return r
end -- Tests.fetcher()



Tests.field = function ( args )
    -- Evaluate template function
    -- Parameter:
    --     args  -- template parameters
    --              template  -- which template
    --              1         -- string, with first template parameter
    --              2         -- string, with second template parameter
    --              ...       -- strings with more template parameters
    --              r         -- expected result (string)
    -- Returns:
    --     string with table row
    local r = "\n|-\n| "
    local l, d = pcall( require, "Module:DateTime/sort" )
    local source = args.template
    if l and source then
        local sort, v
        local suppose = args.r or ""
        source = mw.text.trim( source )
        r = string.format( "%s<code>{{%s",
                           r, source )
        for i = 1, 9 do
            v = args[ i ]
            if v then
                r = string.format( "%s&#124;%s",
                                   r, v )
            end
        end -- for i
        for k, v in pairs( args ) do
            if type( k ) == "string"  and
               k ~= "f"  and  k ~= "r"  and  k ~= "template" then
                r = string.format( "%s&#124;%s=%s",
                                   r, k, v )
            end
        end -- for k, v
        r    = r ..  "}}</code> || "
        r    = r:gsub( "{{", "&#123;&#123;" )
                :gsub( "}}", "&#125;&#125;" )
        sort = d.test( source, args )
        r    = string.format( "%s%s ||", r, sort )
        if sort == suppose then
            r = string.format( "%s<code>%s</code>", r, suppose )
        else
            r = string.format( "%s%s !!!!!!!!!! %s",
                               r,
                               flat( suppose, true ),
                               fault( flat( sort, true ) ) )
        end
    end
    return r
end -- Tests.field()



Tests.flower = function ( args )
    -- Evaluate comparison function
    -- Parameter:
    --     args  -- template parameters
    --              2  -- string, with comparison function name
    --              3  -- string, with first timestamp, or empty
    --              4  -- string, with second timestamp, or empty
    --              r  -- expected result (number)
    local r
    local select = args[ 2 ]
    if select then
        local suppose = args.r or ""
        local o       = DateTime( args[ 3 ] )
        local slip    = o:flow( args[ 4 ], select )  and  "1"   or   ""
        r = string.format( "* %s ''%s'' %s &#160; »<code>%s</code>«",
                           args[ 3 ] or "",
                           select,
                           args[ 4 ] or "",
                           slip )
        if slip ~= suppose then
             r = string.format( "%s %s »<code>%s</code>«",
                                r,  fault( "~=" ),  suppose )
        end
    else
        r = fault( "No comparison function name" )
    end
    return r
end -- Tests.flower()



Tests.follower = function ( args )
    -- Evaluate sort function
    -- Parameter:
    --     args  -- template parameters
    --              2  -- string, to be interpreted
    --              3  -- string, with heading cell, or empty
    --              r  -- expected result (number)
    -- Returns:
    --     string with table row
    local r = "|-\n| "
    local l, d = pcall( require, "Module:DateTime/sort" )
    if l then
        local source  = args[ 2 ] or ""
        local sort    = d.fruit( source, false )
        local suppose = args.r or ""
        if args[ 3 ] then
            r = string.format( "%s%s || ", r, args[ 3 ] )
        end
        r = string.format( "%s%s || <code>%s</code> || ",
                           r, source, sort )
        if sort == suppose then
            r = string.format( "%s<code>%s</code>", r, suppose )
        else
            r = string.format( "%s%s !!!!!!!!!!",
                               r, fault( suppose ) )
        end
    end
    return r
end -- Tests.follower()



Tests.formatter = function ( args )
    -- Evaluate formatting function
    -- Parameter:
    --     args  -- template parameters
    --              2  -- string, to be interpreted, or empty
    --              3  -- string, with format, or empty
    --              4  -- string, with language, or empty
    --              r  -- expected result
    -- Returns:
    --     string
    local r
    local source = ( args[ 2 ] or "" )
    local shower = ( args[ 3 ] or "" )
    local slang  = args[ 4 ]
    local o = DateTime( source, slang )
    local p
    if slang then
        p = { lang = slang }
    end
    if o then
        r = o:format( shower, p )
    end
    if r then
        local syntax = flat( r, false )
        if r ~= args.r then
            local should = ( args.r or "" )
            r = string.format( "%s<br />%s",
                               fault( r ),  should )
        end
        r = string.format( "%s<br />%s",
                           r, syntax )
    else
        r = fault( "???????" )
    end
    if slang then
        slang = string.format( "<code>%s</code>", slang )
    else
        slang = ""
    end
    r = string.format( "\n==== \"%s\" %s ====\n%s<br />%s",
                       shower, slang, source, r )
    return r
end -- Tests.formatter()



Tests.futurist = function ( args )
    -- Evaluate formatting function
    -- Parameter:
    --     args  -- template parameters
    --              2  -- string, to be interpreted, or empty
    --              3  -- string, with shift
    --              r  -- expected result
    -- Returns:
    --     string
    local r
    local source = ( args[ 2 ] or "" )
    local shift  = ( args[ 3 ] or "" )
    local o      = DateTime( source )
    if o then
        r = o:future( shift ):tostring()
        if r ~= args.r then
            local should = ( args.r or "" )
            r = string.format( "\"%s\" ./. \"%s\"",
                               fault( r ),  should )
        end
    else
        r = fault( "??????" )
    end
    r = string.format( "\n; \"%s\"\n: \"%s\" &rArr; %s",
                       shift, source, r )
    return r
end -- Tests.futurist()



-- Export
local p = { }

function p.f( frame )
    local r
    local params = frame:getParent().args
    local fun    = params.f or params[ 1 ]
    if fun then
        local l
        l, r = pcall( Tests[ fun ], params  )
    end
    return r
end -- p.f

return p