Difference between revisions of "Module:Math/testcases"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (preprocess the invoke args and fix rand10_20) |
blackwiki>Mr. Stradivarius (save progress writing the helper functions) |
||
| Line 1: | Line 1: | ||
| − | -- Unit tests for [[Module:Math]]. Click talk page to run tests. | + | -- Unit tests for [[Module:Math/sandbox]]. Click talk page to run tests. |
| − | local mm = require('Module: | + | 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 ScribuntoUnit = require('Module:ScribuntoUnit') | ||
local suite = ScribuntoUnit:new() | local suite = ScribuntoUnit:new() | ||
| − | + | ------------------------------------------------------------------------------- | |
| − | + | -- Helper functions | |
| − | + | ------------------------------------------------------------------------------- | |
| − | + | ||
| + | function suite.getLuaResult(funcName, args) | ||
| + | return mm[funcName](unpack(args)) | ||
| + | end | ||
| + | |||
| + | function suite:assertLuaEquals(expected, funcName, args) | ||
| + | self:assertEquals(expected, self.getLuaResult(funcName, args)) | ||
end | end | ||
| − | + | function suite.buildInvocation(funcName, args) | |
| − | for | + | -- Build a module invocation equivalent to the args table. Taken from [[Module:Unsubst]]. |
| − | + | -- Numbered args first. | |
| + | local ret = '{{#invoke:' .. moduleName .. '|' .. funcName | ||
| + | for k, v in ipairs(args) do | ||
| + | if string.find(v, '=', 1, true) then | ||
| + | -- likely something like 1=foo=bar, we need to do it as a named arg | ||
| + | break | ||
| + | end | ||
| + | ret = ret .. '|' .. v | ||
| + | args[k] = nil | ||
| + | end | ||
| + | for k, v in pairs(args) do | ||
| + | ret = ret .. '|' .. k .. '=' .. v | ||
end | end | ||
| + | return ret .. '}}' | ||
| + | end | ||
| + | |||
| + | function suite:getInvokeResult(funcName, args) | ||
| + | local invocation = self.buildInvocation(funcName, args) | ||
| + | return self.frame:preprocess(invocation) | ||
| + | end | ||
| + | |||
| + | function suite:assertInvokeEquals(expected, funcName, args) | ||
| + | local invokeResult = self:getInvokeResult(funcName, args) | ||
| + | self:assertEquals(expected, invokeResult) | ||
| + | end | ||
| + | |||
| + | function suite:assertLuaAndInvokeTrue(trueFunc, funcName, args) | ||
| + | local invokeResult = self:getInvokeResult(funcName, args) | ||
| + | local luaResult = self.getLuaResult(funcName, args) | ||
| + | self:assertTrue(trueFunc(invokeResult)) | ||
| + | self:assertTrue(trueFunc(luaResult)) | ||
end | end | ||
| − | + | function suite:assertLuaAndInvokeEqual(funcName, testTable) | |
| − | + | local expected = testTable[1] | |
| + | local args = testTable[2] | ||
| + | self:assertLuaEquals(expected, funcName, args) | ||
| + | self:assertInvokeEquals(expected, funcName, args) | ||
end | end | ||
| − | + | ||
| + | function suite:assertLuaAndInvokeEqualMany(funcName, testTables) | ||
| + | for i, testTable in ipairs(testTables) do | ||
| + | self:assertLuaAndInvokeEqual(funcName, testTable) | ||
| + | end | ||
| + | end | ||
| + | |||
| + | ------------------------------------------------------------------------------- | ||
| + | -- Test random | ||
| + | ------------------------------------------------------------------------------- | ||
| + | |||
function suite:test_random() | function suite:test_random() | ||
local rand0 = tonumber(invokeMath('random')) | local rand0 = tonumber(invokeMath('random')) | ||
| Line 32: | Line 81: | ||
suite:assertTrue(rand10_20 >= 10 and rand10_20 <= 20 and math.floor(rand10_20) == rand10_20) | suite:assertTrue(rand10_20 >= 10 and rand10_20 <= 20 and math.floor(rand10_20) == rand10_20) | ||
end | end | ||
| + | |||
| + | ------------------------------------------------------------------------------- | ||
| + | -- Test max | ||
| + | ------------------------------------------------------------------------------- | ||
function suite:test_max() | function suite:test_max() | ||
| Line 42: | Line 95: | ||
end | end | ||
| + | ------------------------------------------------------------------------------- | ||
| + | -- Test average | ||
| + | ------------------------------------------------------------------------------- | ||
| + | |||
function suite:test_average() | function suite:test_average() | ||
local tests = { | local tests = { | ||
| Line 51: | Line 108: | ||
end | end | ||
| + | ------------------------------------------------------------------------------- | ||
| + | -- Test min | ||
| + | ------------------------------------------------------------------------------- | ||
| + | |||
function suite:test_min() | function suite:test_min() | ||
local tests = { | local tests = { | ||
| Line 60: | Line 121: | ||
end | end | ||
| + | ------------------------------------------------------------------------------- | ||
| + | -- Test order | ||
| + | ------------------------------------------------------------------------------- | ||
| + | |||
function suite:test_order() | function suite:test_order() | ||
local tests = { | local tests = { | ||
| Line 71: | Line 136: | ||
end | end | ||
| + | ------------------------------------------------------------------------------- | ||
| + | -- Test precision | ||
| + | ------------------------------------------------------------------------------- | ||
| + | |||
function suite:test_precison() | function suite:test_precison() | ||
local tests = { | local tests = { | ||
| Line 82: | Line 151: | ||
end | end | ||
| + | ------------------------------------------------------------------------------- | ||
| + | -- Test round | ||
| + | ------------------------------------------------------------------------------- | ||
| + | |||
function suite:test_round() | function suite:test_round() | ||
local tests = { | local tests = { | ||
| Line 93: | Line 166: | ||
end | end | ||
| + | ------------------------------------------------------------------------------- | ||
| + | -- Test precision format | ||
| + | ------------------------------------------------------------------------------- | ||
| + | |||
function suite:test_precison_format() | function suite:test_precison_format() | ||
local tests = { | local tests = { | ||
Revision as of 12:02, 10 December 2013
Documentation for this module may be created at Module:Math/testcases/doc
-- 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.getLuaResult(funcName, args)
return mm[funcName](unpack(args))
end
function suite:assertLuaEquals(expected, funcName, args)
self:assertEquals(expected, self.getLuaResult(funcName, args))
end
function suite.buildInvocation(funcName, args)
-- Build a module invocation equivalent to the args table. Taken from [[Module:Unsubst]].
-- Numbered args first.
local ret = '{{#invoke:' .. moduleName .. '|' .. funcName
for k, v in ipairs(args) do
if string.find(v, '=', 1, true) then
-- likely something like 1=foo=bar, we need to do it as a named arg
break
end
ret = ret .. '|' .. v
args[k] = nil
end
for k, v in pairs(args) do
ret = ret .. '|' .. k .. '=' .. v
end
return ret .. '}}'
end
function suite:getInvokeResult(funcName, args)
local invocation = self.buildInvocation(funcName, args)
return self.frame:preprocess(invocation)
end
function suite:assertInvokeEquals(expected, funcName, args)
local invokeResult = self:getInvokeResult(funcName, args)
self:assertEquals(expected, invokeResult)
end
function suite:assertLuaAndInvokeTrue(trueFunc, funcName, args)
local invokeResult = self:getInvokeResult(funcName, args)
local luaResult = self.getLuaResult(funcName, args)
self:assertTrue(trueFunc(invokeResult))
self:assertTrue(trueFunc(luaResult))
end
function suite:assertLuaAndInvokeEqual(funcName, testTable)
local expected = testTable[1]
local args = testTable[2]
self:assertLuaEquals(expected, funcName, args)
self:assertInvokeEquals(expected, funcName, args)
end
function suite:assertLuaAndInvokeEqualMany(funcName, testTables)
for i, testTable in ipairs(testTables) do
self:assertLuaAndInvokeEqual(funcName, testTable)
end
end
-------------------------------------------------------------------------------
-- Test random
-------------------------------------------------------------------------------
function suite:test_random()
local rand0 = tonumber(invokeMath('random'))
local rand2 = tonumber(invokeMath('random', '2'))
local rand10 = tonumber(invokeMath('random', '10'))
local rand10_20 = tonumber(invokeMath('random', '10|20'))
suite:assertTrue(rand0 >= 0 and rand0 < 1)
suite:assertTrue(rand2 == 1 or rand2 == 2)
suite:assertTrue(rand10 >= 1 and rand10 <= 10 and math.floor(rand10) == rand10)
mw.log(rand10_20)
suite:assertTrue(rand10_20 >= 10 and rand10_20 <= 20 and math.floor(rand10_20) == rand10_20)
end
-------------------------------------------------------------------------------
-- Test max
-------------------------------------------------------------------------------
function suite:test_max()
local tests = {
{'', ''},
{'9', '5|6|9'},
{'-5', '-5|-6|-9'},
}
resultEqualsMany(self, 'max', tests)
end
-------------------------------------------------------------------------------
-- Test average
-------------------------------------------------------------------------------
function suite:test_average()
local tests = {
{'6', '5|6|7'},
{'-7', '-7'},
{'10000000002', '10000000001|10000000002|10000000003'},
}
resultEqualsMany(self, 'average', tests)
end
-------------------------------------------------------------------------------
-- Test min
-------------------------------------------------------------------------------
function suite:test_min()
local tests = {
{'', ''},
{'1', '1|2|3'},
{'-3', '-1|-2|-3'},
}
resultEqualsMany(self, 'min', tests)
end
-------------------------------------------------------------------------------
-- Test order
-------------------------------------------------------------------------------
function suite:test_order()
local tests = {
{'0', '2'},
{'1', '20'},
{'2', '200'},
{'0', 'x = 5'},
{'<strong class="error">Formatting error: order of magnitude input appears non-numeric</strong>', 'string'},
}
resultEqualsMany(self, 'order', tests)
end
-------------------------------------------------------------------------------
-- Test precision
-------------------------------------------------------------------------------
function suite:test_precison()
local tests = {
{'4', '1.9856'},
{'1', '1.1'},
{'10', '1.9999999999'},
{'4', 'x = 1.9888'},
{'<strong class="error">Formatting error: precision input appears non-numeric</strong>', 'letra'},
}
resultEqualsMany(self, 'precision', tests)
end
-------------------------------------------------------------------------------
-- Test round
-------------------------------------------------------------------------------
function suite:test_round()
local tests = {
{'2', '1.99999'},
{'2', '1.99999|0'},
{'1.9', '1.94|1'},
{'20', '15|-1'},
{'3', 'value = 2.99999|precision = 2'},
}
resultEqualsMany(self, 'round', tests)
end
-------------------------------------------------------------------------------
-- Test precision format
-------------------------------------------------------------------------------
function suite:test_precison_format()
local tests = {
{'10.00', '10|2'}
}
resultEqualsMany(self, 'precision_format', tests)
end
return suite