Module:Reply to/testcases

From Bonkipedia

Module:Reply to

Yes All tests passed.

Name Expected Actual
Yes testBlankConjunction
Yes testBlankPrefix
Yes testBlankPunctuation
Yes testConjunction
Yes testFourUsernames
Yes testInvalidUsernameError
Yes testLabel
Yes testLabel1
Yes testNoFirstParam
Yes testNoFirstParamWithLabel
Yes testNoFirstParamWithLabel2
Yes testNoUsernamesError
Yes testOneUsername
Yes testPrefix
Yes testPunctuation
Yes testThreeLabels
Yes testThreeUsernames
Yes testTooManyUsernamesError
Yes testTwoLabels
Yes testTwoUsernames



local libName = 'Module:Reply to'
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()
local frame = mw.getCurrentFrame()

-- Helper to run all tests using sandbox version of the library from the debug console. To run against main lib, use  =p.run()
function suite.runSandbox()
	return suite.run(frame:newChild{
		title = libName .. '/testcases',
		args = {
			module = libName .. '/sandbox',
			displayMode = 'log'
		},
	})
end

-- Allow test runner to use both the main library and the sandbox of the library with the same testcases
function suite:module()
	return self.frame and self.frame.args.module or libName
end

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

local function makeFrameWithParentArgs(args)
	local parent = frame:newChild{title = 'Template:Reply to', args = args}
	local child = parent:newChild{title = 'Module:Reply to'}
	return child
end

local function replyTo(args)
	return require(suite:module()).replyto(makeFrameWithParentArgs(args))
end

-- Returns an array of n unique strings.
local function makeNUniqueStrings(n)
	local ret = {}
	for i = 1, n do
		ret[i] = tostring(i)
	end
	return ret
end

function suite:assertHtmlError(pattern, output)
	pattern = '^<strong class="error">Error in %<span.-%>%&%#123%;%&%#123%;%[%[Template:reply to%|reply to%]%]%&%#125%;%&%#125%;%<%/span%>: ' .. pattern .. '.</strong>$'
	self:assertStringContains(pattern, output)
end

function suite:assertContentEquals(expected, actual)
	expected = '<span class="template-ping">' .. expected .. '</span>'
	self:assertEquals(mw.text.trim(expected), mw.text.trim(actual))
end

--------------------------------------------------------------------------------
-- Error tests
--------------------------------------------------------------------------------

function suite:testNoUsernamesError()
    self:assertHtmlError('Username not given', replyTo{})
end

function suite:testInvalidUsernameError()
    self:assertHtmlError("Input contains forbidden characters", replyTo{'Examp|le'})
end

function suite:testTooManyUsernamesError()
	self:assertHtmlError(
		'More than %d+ names specified',
		replyTo(makeNUniqueStrings(1000)) -- The limit is probably always going to be lower than 1000
	)
end

--------------------------------------------------------------------------------
-- MediaWiki messages:
--------------------------------------------------------------------------------

local msgComma	= mw.message.new('comma-separator'):plain()
local msgAnd	= mw.message.new('and'):plain()
local msgSpace	= mw.message.new('word-separator'):plain()
local msgColon	= mw.message.new('colon-separator'):plain()

--------------------------------------------------------------------------------
-- Test defaults
--------------------------------------------------------------------------------

function suite:testOneUsername()
	self:assertContentEquals(
		'@[[User:Example|Example]]' .. msgColon,
		replyTo{'Example'}
	)
end

function suite:testTwoUsernames()
	self:assertContentEquals(
		'@[[User:Example|Example]]' .. msgAnd .. msgSpace
			.. '[[User:Example2|Example2]]' .. msgColon,
		replyTo{'Example', 'Example2'}
	)
end

function suite:testThreeUsernames()
	self:assertContentEquals(
		'@[[User:Example|Example]]' .. msgComma
			.. '[[User:Example2|Example2]]' .. msgComma .. msgAnd .. msgSpace
			.. '[[User:Example3|Example3]]' .. msgColon,
		replyTo{'Example', 'Example2', 'Example3'}
	)
end

function suite:testFourUsernames()
	self:assertContentEquals(
		'@[[User:Example|Example]]' .. msgComma
			.. '[[User:Example2|Example2]]' .. msgComma
			.. '[[User:Example3|Example3]]' .. msgComma .. msgAnd .. msgSpace
			.. '[[User:Example4|Example4]]' .. msgColon,
		replyTo{'Example', 'Example2', 'Example3', 'Example4'}
	)
end

--------------------------------------------------------------------------------
-- Test labels
--------------------------------------------------------------------------------

function suite:testLabel()
	self:assertContentEquals(
		'@[[User:Example|Foo]]' .. msgColon,
		replyTo{[1] = 'Example', label = 'Foo'}
	)
end

function suite:testLabel1()
	self:assertContentEquals(
		'@[[User:Example|Foo]]' .. msgColon,
		replyTo{[1] = 'Example', label1 = 'Foo'}
	)
end

function suite:testTwoLabels()
	self:assertContentEquals(
		'@[[User:Example|Foo]]' .. msgAnd .. msgSpace
			.. '[[User:Example2|Bar]]' .. msgColon,
		replyTo{[1] = 'Example', label1 = 'Foo', [2] = 'Example2', label2 = 'Bar'}
	)
end

function suite:testThreeLabels()
	self:assertContentEquals(
		'@[[User:Example|Foo]]' .. msgComma
			.. '[[User:Example2|Bar]]' .. msgComma .. msgAnd .. msgSpace
			.. '[[User:Example3|Baz]]' .. msgColon,
		replyTo{[1] = 'Example', label1 = 'Foo', [2] = 'Example2', label2 = 'Bar', [3] = 'Example3', label3 = 'Baz'}
	)
end

--------------------------------------------------------------------------------
-- Test special formatting
--------------------------------------------------------------------------------

function suite:testPrefix()
	self:assertContentEquals(
		'foo[[User:Example|Example]]' .. msgColon,
		replyTo{'Example', prefix = 'foo'}
	)
end

function suite:testBlankPrefix()
	self:assertContentEquals(
		'[[User:Example|Example]]' .. msgColon,
		replyTo{'Example', prefix = ''}
	)
end

function suite:testConjunction()
	self:assertContentEquals(
		'@[[User:Example|Example]]' .. msgComma
			.. '[[User:Example2|Example2]]' .. msgComma
			.. msgSpace .. 'foo' .. msgSpace
			.. '[[User:Example3|Example3]]' .. msgColon,
		replyTo{'Example', 'Example2', 'Example3', c = 'foo'}
	)
end

function suite:testBlankConjunction()
	self:assertContentEquals(
		'@[[User:Example|Example]]' .. msgComma
			.. '[[User:Example2|Example2]]' .. msgComma
			.. '[[User:Example3|Example3]]' .. msgColon,
		replyTo{'Example', 'Example2', 'Example3', c = ''}
	)
end

function suite:testPunctuation()
	self:assertContentEquals(
		'@[[User:Example|Example]]foo',
		replyTo{'Example', p = 'foo'}
	)
end

function suite:testBlankPunctuation()
	self:assertContentEquals(
		'@[[User:Example|Example]]',
		replyTo{'Example', p = ''}
	)
end

--------------------------------------------------------------------------------
-- Test non-standard parameter orders
--------------------------------------------------------------------------------

function suite:testNoFirstParam()
	self:assertContentEquals(
		'@[[User:Example|Example]]' .. msgColon,
		replyTo{[2] = 'Example'}
	)
end

function suite:testNoFirstParamWithLabel()
	self:assertContentEquals(
		'@[[User:Example|Example]]' .. msgColon,
		replyTo{[2] = 'Example', label = 'Foo'}
	)
end

function suite:testNoFirstParamWithLabel2()
	self:assertContentEquals(
		'@[[User:Example|Foo]]' .. msgColon,
		replyTo{[2] = 'Example', label2 = 'Foo'}
	)
end

return suite