Jump to content

Module:Yesno/testcases

From Wikipedia, the free encyclopedia
local yesno = require('Module:Yesno')
local yesnoSandbox = require('Module:Yesno/sandbox')
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

local function compareLiveAndSandbox(expected, liveValue, sandboxValue)
	local message = "Expected " .. tostring(expected) .. ". Got "
	suite:assertEquals(expected, liveValue, message .. tostring(liveValue)
		.. " from live module.")
	suite:assertEquals(expected, sandboxValue, message .. tostring(sandboxValue)
		.. " from sandbox.")
end

local function runYesnoTest(expected, inputValue)
	local liveValue = yesno(inputValue)
	local sandboxValue = yesnoSandbox(inputValue)
	compareLiveAndSandbox(expected, liveValue, sandboxValue)
end

local function runYesnoTestWithDefault(expected, inputValue)
	local liveValue = yesno(inputValue, 'default')
	local sandboxValue = yesnoSandbox(inputValue, 'default')
	compareLiveAndSandbox(expected, liveValue, sandboxValue)
end

--------------------------------------------------------------------------------
-- Basic tests
--------------------------------------------------------------------------------

function suite:testYes()
	runYesnoTest(true, 'yes')
end

function suite:testY()
	runYesnoTest(true, 'y')
end

function suite:testTrueText()
	runYesnoTest(true, 'true')
end

function suite:testT()
	runYesnoTest(true, 't')
end

function suite:testTrue()
	runYesnoTest(true, true)
end

function suite:testOneText()
	runYesnoTest(true, '1')
end

function suite:testOne()
	runYesnoTest(true, 1)
end

function suite:testOn()
	runYesnoTest(true, 'on')
end

function suite:testNo()
	runYesnoTest(false, 'no')
end

function suite:testN()
	runYesnoTest(false, 'n')
end

function suite:testF()
	runYesnoTest(false, 'f')
end

function suite:testFalseText()
	runYesnoTest(false, 'false')
end

function suite:testFalse()
	runYesnoTest(false, false)
end

function suite:testZero()
	runYesnoTest(false, 0)
end

function suite:testZeroText()
	runYesnoTest(false, '0')
end

function suite:testOff()
	runYesnoTest(false, 'off')
end

function suite:testNil()
	runYesnoTest(nil, nil)
end

--------------------------------------------------------------------------------
-- Capitalisation tests
--------------------------------------------------------------------------------

function suite:testYesCaps()
	runYesnoTest(true, 'Yes')
	runYesnoTest(true, 'YES')
	runYesnoTest(true, 'yEs')
end

function suite:testYCaps()
	runYesnoTest(true, 'Y')
end

function suite:testTCaps()
	runYesnoTest(true, 'T')
end

function suite:testTrueTextCaps()
	runYesnoTest(true, 'True')
	runYesnoTest(true, 'TRUE')
	runYesnoTest(true, 'trUE')
end

function suite:testOnCaps()
	runYesnoTest(true, 'On')
	runYesnoTest(true, 'ON')
	runYesnoTest(true, 'oN')
end

function suite:testNoCaps()
	runYesnoTest(false, 'No')
	runYesnoTest(false, 'NO')
	runYesnoTest(false, 'nO')
end

function suite:testNCaps()
	runYesnoTest(false, 'N')
end

function suite:testFCaps()
	runYesnoTest(false, 'F')
end

function suite:testFalseTextCaps()
	runYesnoTest(false, 'False')
	runYesnoTest(false, 'FALSE')
	runYesnoTest(false, 'fALSe')
end

function suite:testOffCaps()
	runYesnoTest(false, 'ofF')
	runYesnoTest(false, 'oFf')
	runYesnoTest(false, 'oFF')
	runYesnoTest(false, 'Off')
	runYesnoTest(false, 'OfF')
	runYesnoTest(false, 'OFf')
	runYesnoTest(false, 'OFF')
end

--------------------------------------------------------------------------------
-- Number conversion tests
--------------------------------------------------------------------------------

function suite:testOneTextConversion()
	runYesnoTest(true, '1.0')
	runYesnoTest(true, '001')
end

function suite:testZeroTextConversion()
	runYesnoTest(false, '0.0')
	runYesnoTest(false, '000')
end

--------------------------------------------------------------------------------
-- Test no default set
--------------------------------------------------------------------------------

function suite:testStringNoDefault()
	runYesnoTest(nil, 'foo')
end

function suite:testNumberNoDefault()
	runYesnoTest(nil, 100)
end

function suite:testTableNoDefault()
	runYesnoTest(nil, {})
end

function suite:testFunctionNoDefault()
	runYesnoTest(nil, function() return true end)
end

function suite:testBlankStringNoDefault()
	runYesnoTest(nil, '')
end

--------------------------------------------------------------------------------
-- Test default set
--------------------------------------------------------------------------------

function suite:testStringDefaultSet()
	runYesnoTestWithDefault('default', 'foo')
end

function suite:testNumberDefaultSet()
	runYesnoTestWithDefault('default', 100)
end

function suite:testTableDefaultSet()
	runYesnoTestWithDefault('default', {})
end

function suite:testFunctionDefaultSet()
	runYesnoTestWithDefault('default', function() return true end)
end

function suite:testBlankStringDefaultSet()
	runYesnoTestWithDefault('default', '')
end

--------------------------------------------------------------------------------
-- Test default precedence with true, false, and nil
--------------------------------------------------------------------------------

function suite:testYesDefaultPrecedence()
	runYesnoTestWithDefault(true, 'yes')
end

function suite:testYDefaultPrecedence()
	runYesnoTestWithDefault(true, 'y')
end

function suite:testTrueTextDefaultPrecedence()
	runYesnoTestWithDefault(true, 'true')
end

function suite:testTrueDefaultPrecedence()
	runYesnoTestWithDefault(true, true)
end

function suite:testOneTextDefaultPrecedence()
	runYesnoTestWithDefault(true, '1')
end

function suite:testOneDefaultPrecedence()
	runYesnoTestWithDefault(true, 1)
end

function suite:testNoDefaultPrecedence()
	runYesnoTestWithDefault(false, 'no')
end

function suite:testNDefaultPrecedence()
	runYesnoTestWithDefault(false, 'n')
end

function suite:testFalseTextDefaultPrecedence()
	runYesnoTestWithDefault(false, 'false')
end

function suite:testFalseDefaultPrecedence()
	runYesnoTestWithDefault(false, false)
end

function suite:testZeroDefaultPrecedence()
	runYesnoTestWithDefault(false, 0)
end

function suite:testZeroTextDefaultPrecedence()
	runYesnoTestWithDefault(false, '0')
end

function suite:testNilDefaultPrecedence()
	runYesnoTestWithDefault(nil, nil)
end

return suite