Module:TNT/testcases

local ScribuntoUnit = require('Module:ScribuntoUnit') local p = ScribuntoUnit:new

-- Allow test runner to use both Module:TNT and Module:TNT/sandbox with the same testcases function p:module return self.frame and self.frame.args.module or 'TNT' end

function p:invokeTNT(wikicode) return '' end

function p:test_msg -- dataset resolution self:assertResultEquals('text message', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text'), 'msg01') self:assertResultEquals('text message', p:invokeTNT('msg|I18n/Module:TNT/testcases|text'), 'msg02') self:assertResultEquals('text message', p:invokeTNT('msg| I18n/Module:TNT/testcases.tab |text'), 'msg03') self:assertResultEquals('text message', p:invokeTNT('msg| I18n/Module:TNT/testcases |text'), 'msg04')

-- spacing self:assertResultEquals('text message', p:invokeTNT(' msg | I18n/Module:TNT/testcases.tab | text '), 'msg11') self:assertResultEquals('text message', p:invokeTNT(' \n msg \n | \n I18n/Module:TNT/testcases.tab \n | \n text \n '), 'msg12') self:assertResultEquals('текст', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text|lang=ru'), 'msg13') self:assertResultEquals('text message', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text|lang=fr'), 'msg14')

-- 1 parameter self:assertResultEquals('text message a', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_1|a'), 'msg21') self:assertResultEquals('text message a', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_1| a '), 'msg22') self:assertResultEquals('text message a', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_1| \na \n '), 'msg23') self:assertResultEquals('текст абв', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_1|абв|lang=ru'), 'msg24')

-- multiple parameters self:assertResultEquals('text message a and b', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_2|a|b'), 'msg31') self:assertResultEquals('text message a and b', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_2| a | b '), 'msg32') self:assertResultEquals('text message a and b', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_2| \na \n | \nb \n '), 'msg33') self:assertResultEquals('текст абв и где', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_2|абв|где|lang=ru'), 'msg34')

-- parameters as part of wiki text self:assertResultEquals('text message a|b', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_link|a|b'), 'msg41') self:assertResultEquals('text message a|b', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_link| a | b '), 'msg42') self:assertResultEquals('текст где', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_link|абв|где|lang=ru'), 'msg43')

-- fallbacks self:assertResultEquals('текст', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_no_en'), 'msg51') self:assertResultEquals('текст', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_no_en|lang=ru'), 'msg52') self:assertResultEquals('текст', p:invokeTNT('msg|I18n/Module:TNT/testcases.tab|text_no_en|lang=fr'), 'msg53') end

function p:test_msg_format local tnt = require('Module:' .. p:module) self:assertEquals('text message', tnt.format('I18n/Module:TNT/testcases', 'text'), 'format01') self:assertEquals('text message a', tnt.format('I18n/Module:TNT/testcases', 'text_1', {'a'}), 'format02') self:assertEquals('текст абв', tnt.format('I18n/Module:TNT/testcases', 'text_1', {'абв'}, 'ru'), 'format03') self:assertEquals('text message foo and bar', tnt.format('I18n/Module:TNT/testcases', 'text_2', {'foo', 'bar'}), 'format04')

self:assertThrows(function tnt.format end, "bad argument #1 to 'format' (string expected, got nil)", 'format11') self:assertThrows(function tnt.format('Module:TNT/testcases') end, "bad argument #2 to 'format' (string expected, got nil)", 'format12') self:assertThrows(function tnt.format('_bad_name_', 'foo') end, "Dataset commons:Data:_bad_name_.tab is not valid", 'format13') self:assertThrows(function tnt.format('I18n/Module:TNT/testcases', '_bad_msg') end, "Message '_bad_msg' does not exist in dataset commons:Data:I18n/Module:TNT/testcases", 'format14') end

function p:assertResultError(errorText, text, message) local actual = self.frame:preprocess(text) self:assertStringContains(' <span class="scribunto-error" id="mw-scribunto-error-', actual, true, message)	self:assertStringContains(errorText .. '. ', actual, true, message) end

function p:test_msg_errors -- The error messages might change if https://commons.wikimedia.org/wiki/Data:I18n/Module:TNT.tab changes for English self:assertResultError('First parameter must be the name of the Commons dataset', p:invokeTNT('msg'), 'msgerr01') self:assertResultError('First parameter must be the name of the Commons dataset', p:invokeTNT('msg|lang=pl'), 'msgerr02') self:assertResultError('Dataset commons:Data:_bad_name_.tab is not valid', p:invokeTNT('msg|_bad_name_'), 'msgerr03') self:assertResultError("Message '_bad_msg_' does not exist in dataset commons:Data:I18n/Module:TNT", p:invokeTNT('msg|I18n/Module:TNT|_bad_msg_'), 'msgerr04') end

function p:test_link self:assertResultEquals('commons:Data:abc.tab', p:invokeTNT('link|abc.tab'), 'link01') self:assertResultEquals('commons:Data:abc.tab', p:invokeTNT(' link | abc.tab '), 'link02') self:assertResultEquals('commons:Data:abc.tab', p:invokeTNT(' link |\n abc.tab\n '), 'link03') end

function p:test_doc local tnt = require('Module:' .. p:module) self:assertEquals(			'{"paramOrder":["1","2"],"description":"DO NOT EDIT OR TRANSLATE unless you are changing unit tests","params":{"1":{"suggested":false,"type":"string","required":true,"label":"en_label_1","example":"en_example_1","description":"en_desc_1"},"2":{"suggested":true,"type":"string","required":false,"label":"fr_label_2","example":"pl_example_2","description":"en_desc_2"}}}',			tnt.getTemplateData('Module:TNT/testcases templatedata 1'),			'doc01') self:assertEquals(			'{"paramOrder":["param1","param2"],"description":"DO NOT EDIT OR TRANSLATE unless you are changing unit tests","params":{"param2":{"suggested":true,"type":"string","required":false,"label":"fr_label_2","example":"pl_example_2","description":"en_desc_2"},"param1":{"suggested":false,"type":"string","required":true,"label":"en_label_1","example":"en_example_1","description":"en_desc_1"}}}',			tnt.getTemplateData('Module:TNT/testcases templatedata 2'),			'doc02') end

return p