Module:Math/testcases explained

-- Unit tests for . Click talk page to run tests.

local moduleName = 'Math/sandbox' -- assigning this to a variable as it is later used to generate an #invoke statement.local mm = require('Module:' .. moduleName)local ScribuntoUnit = require('Module:ScribuntoUnit')local suite = ScribuntoUnit:new

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

function suite.err(msg) return mw.ustring.format('Formatting error: %s', msg)end

function suite.getLuaResult(funcName, args) args = args or local result = mm['_' .. funcName](unpack(args)) return resultend

function suite:assertLuaEquals(expected, funcName, args) args = args or self:assertEquals(expected, self.getLuaResult(funcName, args))end

function suite.buildInvocation(funcName, args) args = args or local argsClone = mw.clone(args) -- Build a module invocation equivalent to the args table. Taken from . -- Numbered args first. local ret = ''end

function suite:getInvokeResult(funcName, args, convertNumber) -- Unless convertNumber is false, the number is converted to a number, if possible, on re-entry to Lua. args = args or local invocation = self.buildInvocation(funcName, args) local result = self.frame:preprocess(invocation) if convertNumber ~= false and tonumber(result) then return tonumber(result) else return result endend

function suite:assertInvokeEquals(expected, funcName, args, convertNumber) args = args or local invokeResult = self:getInvokeResult(funcName, args, convertNumber) self:assertEquals(expected, invokeResult)end

function suite:assertLuaAndInvokeTrue(trueFunc, funcName, args, convertNumber) args = args or local invokeResult = self:getInvokeResult(funcName, args, convertNumber) local luaResult = self.getLuaResult(funcName, args) self:assertTrue(trueFunc(invokeResult)) self:assertTrue(trueFunc(luaResult))end

function suite:assertLuaAndInvokeEqual(funcName, testTable, convertNumber) local expected = testTable[1] local args = testTable[2] or self:assertLuaEquals(expected, funcName, args) self:assertInvokeEquals(expected, funcName, args, convertNumber)end

function suite:assertLuaAndInvokeEqualMany(funcName, testTables, convertNumber) for i, testTable in ipairs(testTables) do self:assertLuaAndInvokeEqual(funcName, testTable, convertNumber) endend

--------------------------------------------------------------------------------- Test random-------------------------------------------------------------------------------

function suite:test_random self:assertLuaAndInvokeTrue(function (n) return n >= 0 and n < 1 end, 'random') self:assertLuaAndInvokeTrue(function (n) return n

1 or n

2 end, 'random',) self:assertLuaAndInvokeTrue(function (n) return n >= 1 and n <= 10 and math.floor(n)

n end, 'random',) self:assertLuaAndInvokeTrue(function (n) return n >= 10 and n <= 20 and math.floor(n)

n end, 'random',)end

--------------------------------------------------------------------------------- Test max------------------------------------------------------------------------------- function suite:test_max local tests = self:assertLuaAndInvokeEqualMany('max', tests) self:assertLuaEquals(nil, 'max',) self:assertInvokeEquals(, 'max',)end

--------------------------------------------------------------------------------- Test average-------------------------------------------------------------------------------

function suite:test_average local tests = self:assertLuaAndInvokeEqualMany('average', tests)end --------------------------------------------------------------------------------- Test min-------------------------------------------------------------------------------

function suite:test_min local tests = self:assertLuaAndInvokeEqualMany('min', tests) self:assertLuaEquals(nil, 'min',) self:assertInvokeEquals(, 'min',)end

--------------------------------------------------------------------------------- Test gcd-------------------------------------------------------------------------------

function suite:test_gcd local tests = self:assertLuaAndInvokeEqualMany('gcd', tests)end

--------------------------------------------------------------------------------- Test order-------------------------------------------------------------------------------

function suite:test_order local tests = self:assertLuaAndInvokeEqualMany('order', tests) self:assertInvokeEquals(suite.err('order of magnitude input appears non-numeric'), 'order',) self:assertInvokeEquals(0, 'order',)end

--------------------------------------------------------------------------------- Test precision-------------------------------------------------------------------------------

function suite:test_precison local tests = self:assertLuaAndInvokeEqualMany('precision', tests) self:assertInvokeEquals(suite.err('precision input appears non-numeric'), 'precision',) self:assertInvokeEquals(4, 'precision',)end

--------------------------------------------------------------------------------- Test round-------------------------------------------------------------------------------

function suite:test_round local tests = self:assertLuaAndInvokeEqualMany('round', tests) self:assertInvokeEquals(3, 'round',)end

--------------------------------------------------------------------------------- Test mod-------------------------------------------------------------------------------

function suite:test_mod local tests = self:assertLuaAndInvokeEqualMany('mod', tests) self:assertInvokeEquals(suite.err('first argument to mod appears non-numeric'), 'mod',) self:assertInvokeEquals(suite.err('second argument to mod appears non-numeric'), 'mod',) local successNoArgs = pcall(mm._mod) self:assertFalse(successNoArgs) local successOneArg = pcall(mm._mod, 1) self:assertFalse(successOneArg)end

--------------------------------------------------------------------------------- Test precision format-------------------------------------------------------------------------------

function suite:test_precison_format local tests = self:assertLuaAndInvokeEqualMany('precision_format', tests, false) -- the "false" stops string values being converted to numbers on re-entry from #invoke.end

--------------------------------------------------------------------------------- Test cleanNumber-------------------------------------------------------------------------------

function suite:assertCleanNumberEquals(expectedTable, inputString) local expectedNum, expectedNumString = expectedTable[1], expectedTable[2] local actualNum, actualNumString = mm._cleanNumber(inputString) self:assertEquals(expectedNum, actualNum) self:assertEquals(expectedNumString, actualNumString)end

function suite:test_cleanNumber self:assertCleanNumberEquals('0') self:assertCleanNumberEquals(1) self:assertCleanNumberEquals(' 2 ') self:assertCleanNumberEquals('4-1') self:assertCleanNumberEquals('2 + 2') self:assertCleanNumberEquals(' 2 + 3 ') self:assertCleanNumberEquals('+6') self:assertCleanNumberEquals('-7') self:assertCleanNumberEquals('0x58') self:assertCleanNumberEquals('1e+9') self:assertCleanNumberEquals('-0x58') self:assertCleanNumberEquals('1/6 round 5') self:assertCleanNumberEquals('1 foo 2') -- invalid expression self:assertCleanNumberEquals('1+') -- missing expr operand self:assertCleanNumberEquals() self:assertCleanNumberEquals(' ') self:assertCleanNumberEquals('string') self:assertCleanNumberEquals(' string with padding ')end--------------------------------------------------------------------------------- Test divide-------------------------------------------------------------------------------

function suite:test_divide self:assertInvokeEquals('1','divide',, false) self:assertInvokeEquals('0.5','divide',, false) self:assertInvokeEquals('0','divide',, false) self:assertInvokeEquals('0.33','divide',, false) self:assertInvokeEquals(suite.err('Empty dividend'),'divide',,false) self:assertInvokeEquals(suite.err('Empty divisor'),'divide',,false) self:assertInvokeEquals(suite.err('Empty divisor'),'divide',,false) self:assertInvokeEquals(suite.err('Empty divisor'),'divide',,false) self:assertInvokeEquals(suite.err('Not a number: a'),'divide',,false) self:assertInvokeEquals(suite.err('Not a number: b'),'divide',,false) self:assertInvokeEquals(suite.err('Not a number: b'),'divide',,false) self:assertInvokeEquals('Bad','divide',,false) self:assertInvokeEquals('Bad','divide',,false) self:assertInvokeEquals('Bad2','divide',,false)endreturn suite