Module:Autotranslate/testcases

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Uses Template:Autotranslate/test

Code

-- Unit tests for [[Module:Autotranslate]]. Click talk page to run tests.
local p = require('Module:UnitTests')

function p:run_test_autotranslate(actualTemplate, expected)
    self:preprocess_equals(actualTemplate:format('Autotranslate'), expected)
    self:preprocess_equals(actualTemplate:format('Autotranslate/sandbox'), expected)
end

local function expandWithFrame(title, args)
    local parentFrame = mw.getCurrentFrame():newChild{ title = 'Template:Autotranslate' }
    local childFrame = parentFrame:newChild{ title = title, args = args }
    return require(title).autotranslate(childFrame)
end

function p:throws_error(args, msg)
    local success, result
    success, result = pcall(expandWithFrame, 'Module:Autotranslate', args)
    result = result:gsub('^Module:Autotranslate:%d+: ', '') -- don’t care about line numbers
    self:equals('Module:Autotranslate ' .. mw.text.jsonEncode(args), {success, result}, {false, msg})

    success, result = pcall(expandWithFrame, 'Module:Autotranslate/sandbox', args)
    result = result:gsub('^Module:Autotranslate/sandbox:%d+: ', '') -- don’t care about line numbers
    self:equals('Module:Autotranslate/sandbox ' .. mw.text.jsonEncode(args), {success, result}, {false, msg})
end

function p:test_autotranslate()
    self:run_test_autotranslate('{{#invoke: %s | autotranslate | base = Template:Autotranslate/test | lang = en }}', '[[Template:Autotranslate/test/en]] (lang=en) was called')
    self:run_test_autotranslate('{{#invoke: %s | autotranslate | base = Template:Autotranslate/test | lang = fr }}', '[[Template:Autotranslate/test/en]] (lang=fr) was called')
    self:run_test_autotranslate('{{#invoke: %s | autotranslate | base = Template:Autotranslate/test | lang = pl }}', '[[Template:Autotranslate/test/pl]] (lang=pl) was called')
    self:run_test_autotranslate('{{#invoke: %s | autotranslate | base = Template:Autotranslate/test | lang = szl }}', '[[Template:Autotranslate/test/pl]] (lang=szl) was called')
    self:run_test_autotranslate('{{#invoke: %s | autotranslate | base = Autotranslate/test | lang = szl }}', '[[Template:Autotranslate/test/pl]] (lang=szl) was called')
    self:run_test_autotranslate('{{#invoke: %s | autotranslate | base = Autotranslate/test | lang = an }}', '[[Template:Autotranslate/test/es]] (lang=an) was called')
    self:run_test_autotranslate('{{#invoke: %s | autotranslate | base = Autotranslate/test | lang = an | text=hello | 1=arg #1 }}', '[[Template:Autotranslate/test/es]] (1=arg #1, lang=an, text=hello) was called')
end

function p:test_autotranslate_errors()
    self:throws_error(
        {base = 'This template does not exist 435'},
        'No fallback page found for autotranslate (base=[[:This template does not exist 435]], lang=en)'
    )
    self:throws_error(
        {base = 'Category:This template does not exist 435'},
        'No fallback page found for autotranslate (base=[[:Category:This template does not exist 435]], lang=en)'
    )
    self:throws_error(
        {base = '[invalid title]'},
        'No fallback page found for autotranslate (base=[[:[invalid title]]], lang=en)'
    )
end

return p