Difference between revisions of "Module:Math/testcases"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (test using Module:ScribuntoUnit for test cases) |
blackwiki>Mr. Stradivarius (tests converted) |
||
| Line 6: | Line 6: | ||
local function invokeMath(funcName, argString) | local function invokeMath(funcName, argString) | ||
| − | return '{{#invoke:math/sandbox|' .. funcName .. argString or '' .. '}}' | + | return '{{#invoke:math/sandbox|' .. funcName .. '|' .. (argString or '') .. '}}' |
| + | end | ||
| + | |||
| + | local function testTable(obj, funcName, mathFunc, tests) | ||
| + | for i, t in ipairs(tests) do | ||
| + | obj[funcName](obj, t[1], invokeMath(mathFunc, t[2])) | ||
| + | end | ||
| + | end | ||
| + | |||
| + | local function resultEqualsMany(obj, mathFunc, tests) | ||
| + | return testTable(obj, 'assertResultEquals', mathFunc, tests) | ||
end | end | ||
| − | |||
function suite:test_random() | function suite:test_random() | ||
| − | + | local tests = { | |
| − | + | {'0.00047147460303804', ''}, | |
| − | + | {'8', '10'}, | |
| − | + | {'2', '1|2'}, | |
| − | }) | + | } |
| + | resultEqualsMany(self, 'random', tests) | ||
end | end | ||
| − | |||
function suite:test_max() | function suite:test_max() | ||
local tests = { | local tests = { | ||
{'', ''}, | {'', ''}, | ||
| − | + | {'9', '5|6|9'}, | |
| − | {'9', ' | + | {'-5', '-5|-6|-9'}, |
| − | {'-5', ' | ||
} | } | ||
| − | + | resultEqualsMany(self, 'max', tests) | |
| − | |||
| − | |||
end | end | ||
| − | |||
function suite:test_average() | function suite:test_average() | ||
| − | + | local tests = { | |
| − | + | {'6', '5|6|7'}, | |
| − | + | {'-7', '-7'}, | |
| − | + | {'10000000002', '10000000001|10000000002|10000000003'}, | |
| − | + | } | |
| + | resultEqualsMany(self, 'average', tests) | ||
end | end | ||
function suite:test_min() | function suite:test_min() | ||
| − | + | local tests = { | |
| − | + | {'', ''}, | |
| − | + | {'1', '1|2|3'}, | |
| − | + | {'-3', '-1|-2|-3'}, | |
| − | }) | + | } |
| + | resultEqualsMany(self, 'min', tests) | ||
end | end | ||
function suite: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 | end | ||
function suite:test_precison() | function suite:test_precison() | ||
| − | + | local tests = { | |
| − | + | {'4', '1.9856'}, | |
| − | {' | + | {'1', '1.1'}, |
| − | {'1.9999999999', ' | + | {'10', '1.9999999999'}, |
| − | + | {'4', 'x = 1.9888'}, | |
| − | { | + | {'<strong class="error">Formatting error: precision input appears non-numeric</strong>', 'letra'}, |
| − | }) | + | } |
| + | resultEqualsMany(self, 'precision', tests) | ||
end | end | ||
function suite:test_round() | function suite:test_round() | ||
| − | + | local tests = { | |
| − | + | {'2', '1.99999'}, | |
| − | + | {'2', '1.99999|0'}, | |
| − | {'1. | + | {'1.9', '1.94|1'}, |
| − | {'15|-1', ' | + | {'20', '15|-1'}, |
| − | + | {'3', 'value = 2.99999|precision = 2'}, | |
| − | + | } | |
| + | resultEqualsMany(self, 'round', tests) | ||
end | end | ||
function suite:test_precison_format() | function suite:test_precison_format() | ||
| − | + | local tests = { | |
| + | {'10.00', '10|2'} | ||
| + | } | ||
| + | resultEqualsMany(self, 'precision_format', tests) | ||
end | end | ||
| − | + | ||
| − | |||
return suite | return suite | ||
Revision as of 11:23, 6 December 2013
Documentation for this module may be created at Module:Math/testcases/doc
-- Unit tests for [[Module:Math]]. Click talk page to run tests.
local mm = require('Module:Math/sandbox') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()
local function invokeMath(funcName, argString)
return '{{#invoke:math/sandbox|' .. funcName .. '|' .. (argString or '') .. '}}'
end
local function testTable(obj, funcName, mathFunc, tests)
for i, t in ipairs(tests) do
obj[funcName](obj, t[1], invokeMath(mathFunc, t[2]))
end
end
local function resultEqualsMany(obj, mathFunc, tests)
return testTable(obj, 'assertResultEquals', mathFunc, tests)
end
function suite:test_random()
local tests = {
{'0.00047147460303804', ''},
{'8', '10'},
{'2', '1|2'},
}
resultEqualsMany(self, 'random', tests)
end
function suite:test_max()
local tests = {
{'', ''},
{'9', '5|6|9'},
{'-5', '-5|-6|-9'},
}
resultEqualsMany(self, 'max', tests)
end
function suite:test_average()
local tests = {
{'6', '5|6|7'},
{'-7', '-7'},
{'10000000002', '10000000001|10000000002|10000000003'},
}
resultEqualsMany(self, 'average', tests)
end
function suite:test_min()
local tests = {
{'', ''},
{'1', '1|2|3'},
{'-3', '-1|-2|-3'},
}
resultEqualsMany(self, 'min', tests)
end
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
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
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
function suite:test_precison_format()
local tests = {
{'10.00', '10|2'}
}
resultEqualsMany(self, 'precision_format', tests)
end
return suite