Module:Class mask/testcases

local mClassMask = require('Module:Class mask') local ScribuntoUnit = require('Module:ScribuntoUnit') local suite = ScribuntoUnit:new local frame = mw.getCurrentFrame local lang = mw.language.getContentLanguage

--- -- Helper functions ---

local function classMask(args, title) if title then title = mw.title.new(title) end return mClassMask._main(args, title) -- return frame:expandTemplate{title = 'Class mask', args = args} end

local function copyTable(t1, t2) for k, v in pairs(t2) do		t1[k] = v	end end

local function unity(t1, t2) local ret = {} copyTable(ret, t1) copyTable(ret, t2) return ret end

--- -- Custom assert methods ---

function suite:assertGradeEquals(expected, grade, args, title) self:assertEquals(expected, classMask(unity(args, {grade}), title)) self:assertEquals(expected, classMask(unity(args, {grade:upper}), title)) self:assertEquals(expected, classMask(unity(args, {grade:lower}), title)) end

--- -- Default scale tests ---

local function makeDefaultScaleTests(t) suite['test_' .. t.name] = function (self) self:assertGradeEquals(t.expected, t.trigger, {}) end

suite['test_' .. t.name .. '_no'] = function (self) self:assertGradeEquals('', t.trigger, {[t.param] = 'no'}) end end

makeDefaultScaleTests{name = 'FA', expected = 'FA', trigger = 'FA', param = 'fa'} makeDefaultScaleTests{name = 'FL', expected = 'FL', trigger = 'FL', param = 'fl'} makeDefaultScaleTests{name = 'A', expected = 'A', trigger = 'A', param = 'a'} makeDefaultScaleTests{name = 'GA', expected = 'GA', trigger = 'GA', param = 'ga'} makeDefaultScaleTests{name = 'C', expected = 'C', trigger = 'C', param = 'c'} makeDefaultScaleTests{name = 'start', expected = 'Start', trigger = 'Start', param = 'start'} makeDefaultScaleTests{name = 'stub', expected = 'Stub', trigger = 'Stub', param = 'stub'} makeDefaultScaleTests{name = 'list', expected = 'List', trigger = 'List', param = 'list'}

--- -- B grade tests ---

function suite:test_B_normal self:assertGradeEquals('B', 'B', {}) end

function suite:test_B_normal_no self:assertGradeEquals('', 'B', {b = 'no'}) end

function suite:test_B_extended_all_yes self:assertGradeEquals('B', 'B', {b1 = 'yes', b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) end

function suite:test_B_extended_yes_aliases self:assertGradeEquals('B', 'B', {b1 = 'YES', b2 = 'y', b3 = 'Y', b4 = '1', b5 = 'pass', b6 = 'PASS'}) self:assertGradeEquals('B', 'B', {b1 = 'n/a', b2 = 'N/A', b3 = 'na', b4 = 'NA', b5 = '¬', b6 = 'unused'}) self:assertGradeEquals('B', 'B', {b1 = 'UNUSED', b2 = nil, b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) end

function suite:test_B_extended_all_no self:assertGradeEquals('C', 'B', {b1 = 'no', b2 = 'no', b3 = 'no', b4 = 'no', b5 = 'no', b6 = 'no'}) end

function suite:test_B_extended_no_aliases self:assertGradeEquals('C', 'B', {b1 = 'NO',      b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = 'n',       b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = 'N',       b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = '0',       b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = 'fail',    b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = 'FAIL',    b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = '', b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = '', b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = 'yes/no',  b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = 'YES/NO',  b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = '',        b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) self:assertGradeEquals('C', 'B', {b1 = 'foo',     b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) end

function suite:test_B_extended_YYYYYN self:assertGradeEquals('C', 'B', {b1 = 'yes', b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'yes', b6 = 'no'}) end

function suite:test_B_extended_YYYYNN self:assertGradeEquals('C', 'B', {b1 = 'yes', b2 = 'yes', b3 = 'yes', b4 = 'yes', b5 = 'no', b6 = 'no'}) end

function suite:test_B_extended_YYYNNN self:assertGradeEquals('C', 'B', {b1 = 'yes', b2 = 'yes', b3 = 'yes', b4 = 'no', b5 = 'no', b6 = 'no'}) end

function suite:test_B_extended_YYNNNN self:assertGradeEquals('C', 'B', {b1 = 'yes', b2 = 'yes', b3 = 'no', b4 = 'no', b5 = 'no', b6 = 'no'}) end

function suite:test_B_extended_YNNNNN self:assertGradeEquals('C', 'B', {b1 = 'yes', b2 = 'no', b3 = 'no', b4 = 'no', b5 = 'no', b6 = 'no'}) end

function suite:test_B_extended_YYNYYY self:assertGradeEquals('C', 'B', {b1 = 'yes', b2 = 'yes', b3 = 'no', b4 = 'yes', b5 = 'yes', b6 = 'yes'}) end

--- -- Full quality scale grade tests ---

local function makeFQSTests(t) suite['test_' .. t.name .. '_default'] = function (self) self:assertGradeEquals('NA', t.trigger, {}) end

suite['test_' .. t.name .. '_activated'] = function (self) self:assertGradeEquals(t.expected, t.trigger, {[t.param] = 'yes'}) end

if t.aliases then suite['test_' .. t.name .. '_aliases'] = function (self) for _, alias in ipairs(t.aliases) do				self:assertGradeEquals(t.expected, alias, {[t.param] = 'yes'}) end end end

suite['test_' .. t.name .. '_FQS'] = function (self) self:assertGradeEquals(t.expected, t.trigger, {FQS = 'yes'}) end end

makeFQSTests{name = 'file',    expected = 'File',     param = 'file',     trigger = 'File',     aliases = {'Image', 'Img'}} makeFQSTests{name = 'category', expected = 'Category', param = 'category', trigger = 'Category', aliases = {'Cat', 'Categ'}} makeFQSTests{name = 'disambig', expected = 'Disambig', param = 'disambig', trigger = 'Disambig', aliases = {'Disambiguation', 'Disamb', 'Dab'}} makeFQSTests{name = 'portal',  expected = 'Portal',   param = 'portal',   trigger = 'Portal'} makeFQSTests{name = 'template', expected = 'Template', param = 'template', trigger = 'Template', aliases = {'Temp', 'Tpl', 'Templ'}} makeFQSTests{name = 'project', expected = 'Project',  param = 'project',  trigger = 'Project'} makeFQSTests{name = 'draft',   expected = 'Draft',    param = 'draft',    trigger = 'Draft'}

function suite:test_FQS_override self:assertGradeEquals('NA', 'File', {file = '', FQS = 'yes'}) end

--- -- Other standard grade tests ---

local function makeOtherTests(t) suite['test_' .. t.name .. '_default'] = function (self) self:assertGradeEquals('NA', t.trigger, {}) end

suite['test_' .. t.name .. '_activated'] = function (self) self:assertGradeEquals(t.expected, t.trigger, {[t.param] = 'yes'}) end

if t.aliases then suite['test_' .. t.name .. '_aliases'] = function (self) for _, alias in ipairs(t.aliases) do				self:assertGradeEquals(t.expected, alias, {[t.param] = 'yes'}) end end end end

makeOtherTests{name = 'redirect', expected = 'Redirect', param = 'redirect', trigger = 'Redirect', aliases = {'Red', 'Redir'}} makeOtherTests{name = 'book',    expected = 'Book',     param = 'book',     trigger = 'Book'}

--- -- FM tests ---

function suite:test_FM_default self:assertGradeEquals('NA', 'FM', {}) end

function suite:test_FM_activated self:assertGradeEquals('FM', 'FM', {fm = 'yes'}) end

function suite:test_FM_file_activated self:assertGradeEquals('File', 'FM', {file = 'yes'}) end

function suite:test_FM_file_FQS self:assertGradeEquals('File', 'FM', {FQS = 'yes'}) end

function suite:test_FM_file_overridden self:assertGradeEquals('NA', 'FM', {file = '', FQS = 'yes'}) end

--- -- Undefined tests ---

function suite:test_undefined self:assertEquals('¬', classMask{}) end

function suite:test_undefined_explicit self:assertEquals('¬', classMask{'¬'}) end

--- -- NA grade tests ---

local function makeNAForceTests(t) suite['test_NA_force_' .. t.name .. '_default'] = function (self) self:assertGradeEquals('NA', 'NA', {forceNA = 'yes'}, t.title) end

suite['test_NA_force_' .. t.name .. '_activated'] = function (self) self:assertGradeEquals(t.expected, 'NA', {forceNA = 'yes', [t.param] = 'yes'}, t.title) end

suite['test_NA_force_' .. t.name .. '_FQS'] = function (self) self:assertGradeEquals(t.expected, 'NA', {forceNA = 'yes', FQS = 'yes'}, t.title) end

suite['test_NA_force_' .. t.name .. '_FQS_overridden'] = function (self) self:assertGradeEquals('NA', 'NA', {forceNA = 'yes', [t.param] = '', FQS = 'yes'}, t.title) end end

makeNAForceTests{name = 'file',    title = 'File talk:Foo.png', expected = 'File',     param = 'file'} makeNAForceTests{name = 'category', title = 'Category talk:Foo', expected = 'Category', param = 'category'} makeNAForceTests{name = 'portal',  title = 'Portal talk:Foo',   expected = 'Portal',   param = 'portal'} makeNAForceTests{name = 'template', title = 'Template talk:Foo', expected = 'Template', param = 'template'} makeNAForceTests{name = 'project', title = 'Project talk:Foo',  expected = 'Project',  param = 'project'} makeNAForceTests{name = 'draft',   title = 'Draft talk:Foo',    expected = 'Draft',    param = 'draft'}

function suite:test_NA_no_force self:assertGradeEquals('NA', 'NA', {}) self:assertGradeEquals('NA', 'NA', {forceNA = 'no'}) self:assertGradeEquals('NA', 'NA', {forceNA = ''}) end

function suite:test_NA_force_default self:assertGradeEquals('NA', 'NA', {forceNA = 'yes'}, 'Talk:Foo') end

--- -- Default grade tests ---

local function makeDefaultTests(t) suite['test_default_' .. t.name .. '_inactive'] = function (self) self:assertGradeEquals('NA', 'Foo', {}, t.title) end

suite['test_default_' .. t.name .. '_active'] = function (self) self:assertGradeEquals(t.expected, 'Foo', {[t.param] = 'yes'}, t.title) end

suite['test_default_' .. t.name .. '_FQS'] = function (self) self:assertGradeEquals(t.expected, 'Foo', {FQS = 'yes'}, t.title) end

suite['test_default_' .. t.name .. '_FQS_overridden'] = function (self) self:assertGradeEquals('NA', 'Foo', {[t.param] = '', FQS = 'yes'}, t.title) end end

makeDefaultTests{name = 'user',     expected = 'NA',       param = 'user',      title = 'User talk:Foo'} makeDefaultTests{name = 'mediawiki', expected = 'NA',      param = 'mediawiki', title = 'MediaWiki talk:Foo'} makeDefaultTests{name = 'timedtext', expected = 'NA',      param = 'timedtext', title = 'TimedText talk:Foo'} makeDefaultTests{name = 'module',   expected = 'NA',       param = 'module',    title = 'Module talk:Foo'} makeDefaultTests{name = 'help',     expected = 'NA',       param = 'help',      title = 'Help talk:Foo'} makeDefaultTests{name = 'file',     expected = 'File',     param = 'file',      title = 'File talk:Foo.png'} makeDefaultTests{name = 'category', expected = 'Category', param = 'category',  title = 'Category talk:Foo'} makeDefaultTests{name = 'portal',   expected = 'Portal',   param = 'portal',    title = 'Portal talk:Foo'} makeDefaultTests{name = 'template', expected = 'Template', param = 'template',  title = 'Template talk:Foo'} makeDefaultTests{name = 'project',  expected = 'Project',  param = 'project',   title = 'Wikipedia talk:Foo'} makeDefaultTests{name = 'draft',    expected = 'Draft',    param = 'draft',     title = 'Draft talk:Foo'}

function suite:test_default_book_inactive self:assertGradeEquals('NA', 'Foo', {}, 'Book talk:Foo') end

function suite:test_default_book_active self:assertGradeEquals('Book', 'Foo', {book = 'yes'}, 'Book talk:Foo') end

function suite:test_default_other_namespace self:assertGradeEquals('', 'Foo', {}, 'Talk:Foo') end

function suite:test_default_upper_exists self:assertGradeEquals('Some grade', 'Foo', {FOO = 'Some grade'}) end

function suite:test_default_lower_exists self:assertGradeEquals('Foo', 'foO', {foo = 'yes'}) end

function suite:test_default_lower_overridden self:assertGradeEquals('Some grade', 'Foo', {foo = 'yes', FOO = 'Some grade'}) end

function suite:test_default_empty_exists self:assertGradeEquals('Some grade', , {[] = 'Some grade'}) end

function suite:test_default_blank_exists self:assertGradeEquals('Some grade', ' ', {[''] = 'Some grade'}) end

function suite:test_default_unicode_upper_exists self:assertEquals('Some value', classMask{'ğÖÖ', ['ĞÖÖ'] = 'Some value'}) end

function suite:test_default_unicode_lower_exists self:assertEquals('Ğöö', classMask{'ğÖÖ', ['ğöö'] = 'yes'}) end

function suite:test_default_unicode_lower_overridden self:assertEquals('Some value', classMask{'ğÖÖ', ['ğöö'] = 'yes', ['ĞÖÖ'] = 'Some value'}) end

--- -- Doc warning tests ---

function suite:test_doc_warning self:assertStringContains(		frame:expandTemplate{title = 'Class mask/doc warning'},		classMask({}, 'Template:Foo/class'),		true	) end

--- -- Other tests ---

function suite:test_whitespace self:assertEquals('FA', classMask{' FA  '}) end

return suite