Jump to content

Module:Check DYK hook/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 10:13, 29 November 2020 (add a test for hooks with whitespace + HTML-encoded question mark). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Load necessary modules
local mCheckDYKHook = require('Module:Check DYK hook/sandbox')
local ScribuntoUnit = require('Module:ScribuntoUnit')

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

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

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

for _, testData in ipairs(templateTestData) do
	suite[testData.testName] = function(self)
		local frame = mw.getCurrentFrame()
		local child = frame:newChild{args = {testData.hook}}
		self:assertEquals(mCheckDYKHook.isValidHook(child), testData.expected)
	end
end

return suite