Mô đun:Math/testcases

-- Unit tests for Module:Math/sandbox. 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('Lỗi định dạng: %s', msg)
end
function suite.getLuaResult(funcName, args)
	args = args or {}
	local result = mm'_' .. funcName(unpack(args))
	return result
end
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 Module:Unsubst.
	-- 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
	end
end
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 = testTable1
	local args = testTable2 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)
	end
end
-------------------------------------------------------------------------------
-- Test random
-------------------------------------------------------------------------------
function suite:test_random()
	self:assertLuaAndInvokeTrue(function (n) return n >= 0 and n = 1 and n = 10 and n ×10−5', {0.000020004, 7}}
	}
	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 = expectedTable1, expectedTable2
	local actualNum, actualNumString = mm._cleanNumber(inputString)
	self:assertEquals(expectedNum, actualNum)
	self:assertEquals(expectedNumString, actualNumString)
end
function suite:test_cleanNumber()
	self:assertCleanNumberEquals({0, '0'}, '0')
	self:assertCleanNumberEquals({1, '1'}, 1)
	self:assertCleanNumberEquals({2, '2'}, '  2  ')
	self:assertCleanNumberEquals({3, '3'}, '4-1')
	self:assertCleanNumberEquals({4, '4'}, '2 + 2')
	self:assertCleanNumberEquals({5, '5'}, '  2 + 3  ')
	self:assertCleanNumberEquals({6, '6'}, '+6')
	self:assertCleanNumberEquals({-7, '-7'}, '-7')
	self:assertCleanNumberEquals({88, '88'}, '0x58')
	self:assertCleanNumberEquals({1000000000, '1e+9'}, '1e+9')
	self:assertCleanNumberEquals({-88, '-88'}, '-0x58')
	self:assertCleanNumberEquals({0.16667, '0.16667'}, '1/6 round 5')
	self:assertCleanNumberEquals({nil, nil}, '1 foo 2') -- invalid expression
	self:assertCleanNumberEquals({nil, nil}, '1+') -- missing expr operand
	self:assertCleanNumberEquals({nil, nil}, )
	self:assertCleanNumberEquals({nil, nil}, '  ')
	self:assertCleanNumberEquals({nil, nil}, 'string')
	self:assertCleanNumberEquals({nil, nil}, '  string with padding  ')
end
-------------------------------------------------------------------------------
-- Test divide
-------------------------------------------------------------------------------
function suite:test_divide() 
	self:assertInvokeEquals('1','divide',{1, 2, round = 'yes'}, false)
	self:assertInvokeEquals('0.5','divide',{1, 2, round = 'no'}, false)
	self:assertInvokeEquals('0','divide',{1, 3, round = 'yes', precision=2}, false)
	self:assertInvokeEquals('0.33','divide',{1, 3, precision=2}, false)
	self:assertInvokeEquals(suite.err('Không có số bị chia'),'divide',{,2},false)
	self:assertInvokeEquals(suite.err('Không có số chia'),'divide',{1,},false)
	self:assertInvokeEquals(suite.err('Không có số chia'),'divide',{},false)
	self:assertInvokeEquals(suite.err('Không có số chia'),'divide',{,},false)
	self:assertInvokeEquals(suite.err('Không phải là số: a'),'divide',{'a',2},false)
	self:assertInvokeEquals(suite.err('Không phải là số: b'),'divide',{1,'b'},false)
	self:assertInvokeEquals(suite.err('Không phải là số: b'),'divide',{'a','b'},false)
	self:assertInvokeEquals('Bad','divide',{'Bad',2},false)
	self:assertInvokeEquals('Bad','divide',{1,'Bad'},false)
	self:assertInvokeEquals('Bad2','divide',{'Bad1','Bad2'},false)
end
return suite