Module:Check DYK hook/testcases

From MOASSpedia
Jump to navigation Jump to search

Documentation for this module may be created at Module:Check DYK hook/testcases/doc

-- Load necessary modules
local mCheckDYKHook = require('Module:Check DYK hook/sandbox')
local ScribuntoUnit = require('Module:ScribuntoUnit')

-- Initialise test suite
local suite = ScribuntoUnit:new()

--------------------------------------------------------------------------------
-- Helper methods
--------------------------------------------------------------------------------

local function makeFrameWithArgs(args)
	local frame = mw.getCurrentFrame()
	return frame:newChild{args = args}
end

function suite:assertErrorMessageContains(func, expectedMessage)
	local success, actualMessage = pcall(func)
	self:assertFalse(success)
	self:assertStringContains(expectedMessage, actualMessage, true)
end

--------------------------------------------------------------------------------
-- Tests for p._isValidHook
--------------------------------------------------------------------------------

local moduleTestData = {
	{
		testName = "testIsValidHook_ReturnsTrue_ForHooksStartingWithThat",
		hook     = "... that foo bar baz?",
		expected = true,
	},
	{
		testName = "testIsValidHook_ReturnsTrue_ForHooksStartingWithAbout",
		hook     = "... about the foo bar baz?",
		expected = true,
	},
	{
		testName = "testIsValidHook_ReturnsFalse_ForHooksStartingWithOtherWords",
		hook     = "... foo bar baz?",
		expected = false,
	},
	{
		testName = "testIsValidHook_ReturnsTrue_ForHooksEndingWithAQuestionMark",
		hook     = "... that foo bar baz?",
		expected = true,
	},
	{
		testName = "testIsValidHook_ReturnsTrue_ForHooksEndingWithAQuestionMarkHTMLEncodedInsideASpan",
		hook     = '... that foo bar [[baz|<span class="something">baz&#63;</span>]]',
		expected = true,
	},
	{
		testName = "testIsValidHook_ReturnsTrue_ForHooksEndingWithYouProbablyDid",
		hook     = '... that foo bar baz? You probably did...',
		expected = true,
	},
	{
		testName = "testIsValidHook_ReturnsFalse_ForHooksNotEndingInAQuestionMark",
		hook     = '... that foo bar baz',
		expected = false,
	},
}

for _, testData in ipairs(moduleTestData) do
	suite[testData.testName] = function(self)
		local result = mCheckDYKHook._isValidHook(testData.hook)
		if testData.expected then
			self:assertTrue(result)
		else
			self:assertFalse(result)
		end
	end
end

function suite:testIsValidHook_RaisesError_WhenHookIsNotAString()
	self:assertErrorMessageContains(
		function ()
			mCheckDYKHook._isValidHook(7)
		end,
		"bad argument #1 to '_isValidHook' (string expected, got number)"
	)
end

--------------------------------------------------------------------------------
-- Tests for p.isValidHook
--------------------------------------------------------------------------------

local templateTestData = {
	{
		testName = "testTemplateIsValidHook_ReturnsYes_ForHooksStartingWithThat",
		args     = {"... that foo bar baz?"},
		expected = "yes",
	},
	{
		testName = "testTemplateIsValidHook_ReturnsBlank_ForHooksStartingWithOtherWords",
		args     = {"... foo bar baz?"},
		expected = "",
	},
	{
		testName = "testTemplateIsValidHook_ReturnsYes_ForHooksWithWhitespace",
		args     = {"\n  ... that foo bar baz?  \n"},
		expected = "yes",
	},
	{
		testName = "testTemplateIsValidHook_ReturnsBlank_ForInvalidHooksWithWhitespace",
		args     = {"\n  ... that foo bar baz  \n"},
		expected = "",
	},
	{
		testName = "testTemplateIsValidHook_ReturnsYes_ForHooksEndingWithAQuestionMarkHTMLEncodedInsideASpan",
		args     = {'... that foo bar [[baz|<span class="something">baz&#63;</span>]]'},
		expected = "yes",
	},
	{
		testName = "testTemplateIsValidHook_ReturnsYes_ForHooksEndingWithAQuestionMarkHTMLEncodedInsideASpanWithWhitespace",
		args     = {'\n  ... that foo bar [[baz|<span class="something">baz&#63;</span>]]  \n'},
		expected = "yes",
	},
	{
		testName = "testTemplateIsValidHook_ReturnsYes_WhenCalledWithValidHookWithHookKeywordArgument",
		args     = {hook = "... that foo bar baz?"},
		expected = "yes",
	},
}

for _, testData in ipairs(templateTestData) do
	suite[testData.testName] = function(self)
		self:assertEquals(
			mCheckDYKHook.isValidHook(makeFrameWithArgs(testData.args)),
			testData.expected
		)
	end
end

function suite:testTemplateIsValidHook_RaisesError_WhenNoHookSupplied()
	self:assertErrorMessageContains(
		function ()
			mCheckDYKHook.isValidHook(makeFrameWithArgs{nil})
		end,
		"No hook specified"
	)
end

return suite