Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Check DYK hook/testcases

From Wikipedia, the free encyclopedia
-- Load necessary modules
local mCheckDYKHook = require('Module:Check DYK hook')
local ScribuntoUnit = require('Module:ScribuntoUnit')

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

--------------------------------------------------------------------------------
-- Sandbox run
--------------------------------------------------------------------------------

function suite.runSandbox(...)
	mCheckDYKHook = require('Module:Check DYK hook/sandbox')
	return suite.run(...)
end

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

local function makeFrameWithArgs(args)
	local frame = mw.getCurrentFrame()
	return frame:newChild{args = args}
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:assertThrows(
		mCheckDYKHook._isValidHook,
		"bad argument #1 to '_isValidHook' (string expected, got number)",
		nil,
		7
	)
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:assertThrows(
		mCheckDYKHook.isValidHook,
		"No hook specified",
		nil, makeFrameWithArgs{nil}
	)
end

return suite