Module:Devmodule/testcases

From Commons Wiki
Jump to navigation Jump to search

Template:Testcases module


-- <nowiki>
local testcases = {
    {
        '#mod=delink|#fname=delink|[[test]]',
        'test'
    },
    {
        '#mod=delink|#fname=delink|test',
        'test'
    },
    {
        '#mod=autochangecat|test2|test3|days=0',
        '[[Category:test3]]'
    },
    {
        '#modulename=autochangecat|test2|test3|days=0',
        '[[Category:test3]]'
    },
    {
        '#modulename=autochangecat|#fname=main|test2|test3|days=0',
        '[[Category:test3]]'
    },
    {
        '#mod=Module:Delink|#fname=delink|[[test]]',
        'test'
    },
    {
        '#mod=Dev:Delink|#fname=delink|[[test]]',
        'test'
    },
    {
        '#mod=dev:delink|#fname=delink|[[test]]',
        'test'
    },
    {
        '#mod=devif',
        '<strong class="error">Error: could not find a module called `Devif` on dev.wikia.</strong>'
    },
    {
        '#mod=moduleif',
        '<strong class="error">Error: could not find a module called `Moduleif` on dev.wikia.</strong>'
    },
    {
        '#mod=Devmodule/testmodule|#fname=isleapyear',
        '<strong class="error">Error: that module does not have a function called `isleapyear`.</strong>'
    },
    {
        '#mod=x_x',
        '<strong class="error">Error: could not find a module called `X_x` on dev.wikia.</strong>'
    },
    {
        '#mod=delink|fname=x_x',
        '<strong class="error">Error: that module does not have a function called `main`.</strong>'
    },
    {
        '',
        '<strong class="error">Error: no `#mod` parameter given (should be the name of a module on dev.wikia).</strong>'
    },
    {
        '#mod=Devmodule/testmodule|#fname=dontCallMe',
        '<strong class="error">Error: leave me alone.</strong>'
    },
}

-- Add or remove new testcases here
local testSandbox = {
    {
        '#mod=delink|#fname=delink|[[test]]',
        'test'
    },
    {
        '#mod=delink|#fname=delink|test',
        'test'
    },
    {
        '#mod=autochangecat|test2|test3|days=0',
        '[[Category:test3]]'
    },
    {
        '#modulename=autochangecat|test2|test3|days=0',
        '[[Category:test3]]'
    },
    {
        '#modulename=autochangecat|#fname=main|test2|test3|days=0',
        '[[Category:test3]]'
    },
    {
        '#mod=Module:Delink|#fname=delink|[[test]]',
        'test'
    },
    {
        '#mod=Dev:Delink|#fname=delink|[[test]]',
        'test'
    },
    {
        '#mod=dev:Delink|#fname=delink|[[test]]',
        'test'
    },
    {
        '#mod=devif',
        '<strong class="error">Error: could not find a module called `Devif` on dev.wikia.</strong>'
    },
    {
        '#mod=moduleif',
        '<strong class="error">Error: could not find a module called `Moduleif` on dev.wikia.</strong>'
    },
    {
        '#mod=Devmodule/testmodule|#fname=isleapyear',
        '<strong class="error">Error: that module does not have a function called `isleapyear`.</strong>'
    },
    {
        '#mod=x_x',
        '<strong class="error">Error: could not find a module called `X_x` on dev.wikia.</strong>'
    },
    {
        '#mod=delink|fname=x_x',
        '<strong class="error">Error: that module does not have a function called `main`.</strong>'
    },
    {
        '',
        '<strong class="error">Error: no `#mod` parameter given (should be the name of a module on dev.wikia).</strong>'
    },
    {
        '#mod=Devmodule/testmodule|#fname=dontCallMe',
        '<strong class="error">Error: leave me alone.</strong>'
    },
}

local p = require('Dev:ScribuntoUnit')
local suite = p:new()

-- Helper function for invoking
local function invoke(moduleName, args)
    local invokeCmd = '{{#invoke:' .. moduleName

    for i, v in pairs(args) do
        invokeCmd = invokeCmd .. '|' .. i .. '=' .. v
    end

    invokeCmd = invokeCmd .. '}}'

    return mw.getCurrentFrame():preprocess(invokeCmd)
end

-- Tests deliberate errors. This is needed because of strip markers.
local function create_errors(moduleName)
    local moduleName = moduleName or 'devmodule'
    local frame = mw.getCurrentFrame()

    suite:assertStringContains(
        '<strong class="error">',
        invoke(moduleName, {
            ['#mod'] = 'Devmodule/testmodule'
        }),
        true,
        'This test only passes if testmodule does not contain function "main".'
    )

    suite:assertStringContains(
        '<strong class="error">',
        invoke(moduleName, {
            ['#mod'] = 'Devmodule/testmodule',
            ['#fname'] = 'error2'
        }),
        true,
        'Function error2 does not have any errors (add some).'
    )
end

-- tests for sandbox
function suite:test_errors()
    suite:test_localized_module_namespace()
    create_errors()
end

function p.tests(testsuite, moduleName, testTable)
    for _, testData in pairs(testTable or testcases) do
        local expected = testData[2]
        local processedActual = '{{#invoke:' .. moduleName .. '|main|' .. testData[1] .. '}}'

        testsuite:assertResultEquals(expected, processedActual)
    end
end

-- Runs sandbox tests from table "testsandbox"
function suite:test_sandbox_module()
    create_errors(moduleName)
    p.tests(self, 'devmodule/sandbox', testSandbox)
end

-- Runs all tests in table testcases
function suite:test_main_module()
    p.tests(self, 'devmodule', testcases)
end

function suite:test_localized_module_namespace()
    local moduleNames = {
        '모듈:Sandbox',
        'Módulo:Sandbox'
    }

    for _, moduleName in ipairs(moduleNames) do
        suite:assertTrue(invoke('Devmodule', {
            ['#mod'] = moduleName
        }))
    end
end

return suite