Module:Lua banner/testcases: Difference between revisions

From NARA
Jump to navigation Jump to search
NARA>ExE Boss
(Ignore TemplateStyles and other strip markers (except <nowiki>))
(No difference)

Revision as of 09:52, 6 June 2022

Documentation for this module may be created at Module:Lua banner/testcases/doc

local mList = require('Module:List')
local mMessageBox = require('Module:Message box')

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

local noWishConfig = {
	subpage_blacklist = {
		['doc'] = true
	},
	allow_wishes = false,
	error_category = 'ERRCAT_TITLE',
	-- wish_category = nil, -- should not be accessed
	default_category = 'DEFCAT_TITLE',
	module_categories = {
		['String'] = 'STRINGCAT_TITLE'
	}
}
local withWishConfig = {
	subpage_blacklist = {
		['doc'] = true
	},
	allow_wishes = true,
	-- error_category = nil, -- should not be accessed
	wish_category = 'WISHCAT_TITLE',
	default_category = 'DEFCAT_TITLE',
	module_categories = {
		['String'] = 'STRINGCAT_TITLE'
	}
}
local configMetatable = {
	__index = function(t, k) error("Unexpected field '" .. k .. "' accessed.") end
}
setmetatable(noWishConfig, configMetatable)
setmetatable(withWishConfig, configMetatable)

-- Allow test runner to use both the Module:XXX itself and Module:XXX/sandbox with the same testcases
function p:module()
	return self.frame and self.frame.args and self.frame.args.module or 'Lua banner'
end

function p:box(text)
	local boxArgs = {
		text = text,
		type = 'notice',
		small = true,
		image = '[[File:Lua-logo-nolabel.svg|30px|alt=Lua logo|link=wikipedia:Wikipedia:Lua]]'
	}
	return mMessageBox.main('mbox', boxArgs)
end

function p:boxFromList(moduleLinks, withWishes)
	if #moduleLinks == 0 then
		if withWishes then
			return self:box('This template should use [[wikipedia:Wikipedia:Lua|Lua]]')
		else
			return self:box('<strong class="error">Error: no modules specified</strong>')
		end
	else
		return self:box('Uses [[wikipedia:Wikipedia:Lua|Lua]]:\n' .. mList.makeList('bulleted', moduleLinks))
	end
end

function p:assertTCBoth(args, modules, titleObj, expected)
	local func = require('Module:' .. p:module()).renderTrackingCategories
	self:assertEquals(func(args, modules, titleObj, noWishConfig), expected)
	self:assertEquals(func(args, modules, titleObj, withWishConfig), expected)
end

local function ignoreStripMarkersExceptNoWiki(s)
	-- return mw.text.unstrip(s);
	return mw.text.killMarkers(mw.text.unstripNoWiki(s));
end

function p:assertEqualsIgnoreStripMarkers(a, b, ...)
	if (type(a) == "string") then
		a = ignoreStripMarkersExceptNoWiki(a);
	end
	if (type(b) == "string") then
		b = ignoreStripMarkersExceptNoWiki(b);
	end
	return self:assertEquals(a, b, ...);
end

function p:testRenderBox()
	local func = require('Module:' .. p:module()).renderBox
	self:assertEqualsIgnoreStripMarkers(func({}, noWishConfig), self:boxFromList{})
	self:assertEqualsIgnoreStripMarkers(func({}, withWishConfig), self:boxFromList({}, true))
	self:assertEqualsIgnoreStripMarkers(func({'Module:Lua banner'}, noWishConfig), self:boxFromList{'[[:Module:Lua banner]]'})
	self:assertEqualsIgnoreStripMarkers(func({'Module:Lua banner'}, withWishConfig), self:boxFromList{'[[:Module:Lua banner]]'})
end

function p:testRenderTrackingCategories()
	local func = require('Module:' .. p:module()).renderTrackingCategories
	local moduleTitle = mw.title.new('Module:Lua banner')
	local templateTitle = mw.title.new('Template:Lua')
	local templateDocTitle = mw.title.new('Template:Lua/doc')
	self:assertEquals(func({}, {}, moduleTitle, noWishConfig), '[[Category:ERRCAT_TITLE]]')
	self:assertEquals(func({}, {}, moduleTitle, withWishConfig), '')
	self:assertEquals(func({}, {}, templateTitle, noWishConfig), '[[Category:ERRCAT_TITLE]][[Category:DEFCAT_TITLE]]')
	self:assertEquals(func({}, {}, templateTitle, withWishConfig), '[[Category:WISHCAT_TITLE]]')
	self:assertEquals(func({}, {}, templateDocTitle, noWishConfig), '[[Category:ERRCAT_TITLE]]')
	self:assertEquals(func({}, {}, templateDocTitle, withWishConfig), '')
	self:assertTCBoth({}, {'Module:Lua banner'}, moduleTitle, '')
	self:assertTCBoth({}, {'Module:Lua banner'}, templateTitle, '[[Category:DEFCAT_TITLE]]')
	self:assertTCBoth({}, {'Module:Lua banner'}, templateDocTitle, '')
	self:assertTCBoth({}, {'Module:Lua banner', 'Module:String'}, moduleTitle, '')
	self:assertTCBoth({}, {'Module:Lua banner', 'Module:String'}, templateTitle, '[[Category:DEFCAT_TITLE]]')
	self:assertTCBoth({}, {'Module:Lua banner', 'Module:String'}, templateDocTitle, '')
	self:assertTCBoth({}, {'Module:String', 'Module:Lua banner'}, moduleTitle, '')
	self:assertTCBoth({}, {'Module:String', 'Module:Lua banner'}, templateTitle, '[[Category:STRINGCAT_TITLE]]')
	self:assertTCBoth({}, {'Module:String', 'Module:Lua banner'}, templateDocTitle, '')
end

function p:testRenderTrackingCategoriesWithNocat()
	local moduleTitle = mw.title.new('Module:Lua banner')
	local templateTitle = mw.title.new('Template:Lua')
	local templateDocTitle = mw.title.new('Template:Lua/doc')
	self:assertTCBoth({nocat='true'}, {}, moduleTitle, '')
	self:assertTCBoth({nocat='true'}, {}, templateTitle, '')
	self:assertTCBoth({nocat='true'}, {}, templateDocTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:Lua banner'}, moduleTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:Lua banner'}, templateTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:Lua banner'}, templateDocTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:Lua banner', 'Module:String'}, moduleTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:Lua banner', 'Module:String'}, templateTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:Lua banner', 'Module:String'}, templateDocTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:String', 'Module:Lua banner'}, moduleTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:String', 'Module:Lua banner'}, templateTitle, '')
	self:assertTCBoth({nocat='true'}, {'Module:String', 'Module:Lua banner'}, templateDocTitle, '')
end

function p:testRenderTrackingCategoriesWithCategory()
	local func = require('Module:' .. p:module()).renderTrackingCategories
	local moduleTitle = mw.title.new('Module:Lua banner')
	local templateTitle = mw.title.new('Template:Lua')
	local templateDocTitle = mw.title.new('Template:Lua/doc')
	self:assertEquals(func({category='catname'}, {}, moduleTitle, noWishConfig), '[[Category:ERRCAT_TITLE]]')
	self:assertEquals(func({category='catname'}, {}, moduleTitle, withWishConfig), '')
	self:assertEquals(func({category='catname'}, {}, templateTitle, noWishConfig), '[[Category:ERRCAT_TITLE]][[Category:catname]]')
	self:assertEquals(func({category='catname'}, {}, templateTitle, withWishConfig), '[[Category:catname]]')
	self:assertEquals(func({category='catname'}, {}, templateDocTitle, noWishConfig), '[[Category:ERRCAT_TITLE]]')
	self:assertEquals(func({category='catname'}, {}, templateDocTitle, withWishConfig), '')
	self:assertTCBoth({category='catname'}, {'Module:Lua banner'}, moduleTitle, '')
	self:assertTCBoth({category='catname'}, {'Module:Lua banner'}, templateTitle, '[[Category:catname]]')
	self:assertTCBoth({category='catname'}, {'Module:Lua banner'}, templateDocTitle, '')
	self:assertTCBoth({category='catname'}, {'Module:Lua banner', 'Module:String'}, moduleTitle, '')
	self:assertTCBoth({category='catname'}, {'Module:Lua banner', 'Module:String'}, templateTitle, '[[Category:catname]]')
	self:assertTCBoth({category='catname'}, {'Module:Lua banner', 'Module:String'}, templateDocTitle, '')
	self:assertTCBoth({category='catname'}, {'Module:String', 'Module:Lua banner'}, moduleTitle, '')
	self:assertTCBoth({category='catname'}, {'Module:String', 'Module:Lua banner'}, templateTitle, '[[Category:catname]]')
	self:assertTCBoth({category='catname'}, {'Module:String', 'Module:Lua banner'}, templateDocTitle, '')
end

function p:testMain()
	local func = require('Module:' .. p:module())._main
	self:assertEqualsIgnoreStripMarkers(func({}, noWishConfig), self:boxFromList{} .. '[[Category:ERRCAT_TITLE]]')
	self:assertEqualsIgnoreStripMarkers(func({}, withWishConfig), self:boxFromList({}, true))
	self:assertEqualsIgnoreStripMarkers(func({nil}, noWishConfig), self:boxFromList{} .. '[[Category:ERRCAT_TITLE]]')
	self:assertEqualsIgnoreStripMarkers(func({nil}, withWishConfig), self:boxFromList({}, true))
	self:assertEqualsIgnoreStripMarkers(func({'Module:Lua banner'}, noWishConfig), self:boxFromList{'[[:Module:Lua banner]]'})
	self:assertEqualsIgnoreStripMarkers(func({'Module:Lua banner'}, withWishConfig), self:boxFromList{'[[:Module:Lua banner]]'})
	self:assertEqualsIgnoreStripMarkers(func({nil, 'Module:Lua banner'}, noWishConfig), self:boxFromList{'[[:Module:Lua banner]]'})
	self:assertEqualsIgnoreStripMarkers(func({nil, 'Module:Lua banner'}, withWishConfig), self:boxFromList{'[[:Module:Lua banner]]'})
end

return p