Difference between revisions of "Module:Math/testcases"

From blackwiki
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()
    self:assertResultEquals ('{{#Invoke:math/sandbox|random', '}}', {
+
local tests = {
    {'', '0.00047147460303804'},
+
{'0.00047147460303804', ''},
    {'|10','8'},
+
{'8', '10'},
    {'|1|2','2'},
+
{'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|6|9'},
+
{'-5', '-5|-6|-9'},
{'-5', '|-5|-6|-9'},
 
 
}
 
}
for i, t in ipairs(tests) do
+
resultEqualsMany(self, 'max', tests)
self:assertResultEquals(t[1], invokeMath('max', t[2]))
 
end
 
 
end
 
end
 
   
 
   
--[[
 
 
function suite:test_average()
 
function suite:test_average()
    self:assertResultEquals ('{{#Invoke:math/sandbox|average|', '}}', {
+
local tests = {
    {'5|6|7', '6'},
+
{'6', '5|6|7'},
    {'-7', '-7'},
+
{'-7', '-7'},
    {'10000000001|10000000002|10000000003', '10000000002'},
+
{'10000000002', '10000000001|10000000002|10000000003'},
    })
+
}
 +
resultEqualsMany(self, 'average', tests)
 
end
 
end
 
   
 
   
 
function suite:test_min()
 
function suite:test_min()
    self:assertResultEquals ('{{#Invoke:math/sandbox|min|', '}}', {
+
local tests = {
    {'',''},
+
{'', ''},
    {'1|2|3','1'},
+
{'1', '1|2|3'},
    {'-1|-2|-3','-3'},
+
{'-3', '-1|-2|-3'},
     })
+
     }
 +
resultEqualsMany(self, 'min', tests)
 
end
 
end
 
   
 
   
 
function suite:test_order()
 
function suite:test_order()
    self:assertResultEquals ('{{#Invoke:math/sandbox|order|', '}}', {
+
local tests = {
    {'2','0'},
+
{'0', '2'},
    {'20','1'},
+
{'1', '20'},
    {'200','2'},
+
{'2', '200'},
    {'x = 5','0'},
+
{'0', 'x = 5'},
    {'string','<strong class="error">Formatting error: Order of magnitude input appears non-numeric</strong>'},
+
{'<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()
    self:assertResultEquals ('{{#Invoke:math/sandbox|precision|', '}}', {
+
local tests = {
{'1.9856', '4'},
+
{'4', '1.9856'},
{'1.1', '1'},
+
{'1', '1.1'},
{'1.9999999999', '10'},
+
{'10', '1.9999999999'},
{'x = 1.9888', '4'},
+
{'4', 'x = 1.9888'},
{'letra', '<strong class="error">Formatting error: Precision input appears non-numeric</strong>'},
+
{'<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()
    self:assertResultEquals ('{{#Invoke:math/sandbox|round|', '}}', {
+
local tests = {
{'1.99999', '2'},
+
{'2', '1.99999'},
{'1.99999|0', '2'},
+
{'2', '1.99999|0'},
{'1.94|1', '1.9'},
+
{'1.9', '1.94|1'},
{'15|-1', '20'},
+
{'20', '15|-1'},
{'value = 2.99999|precision = 2', '3'},
+
{'3', 'value = 2.99999|precision = 2'},
})
+
}
 +
resultEqualsMany(self, 'round', tests)
 
end
 
end
 
   
 
   
 
function suite:test_precison_format()
 
function suite:test_precison_format()
    self:preprocess_equals('{{#Invoke:math/sandbox|precision_format|10|2}}', '10.00')
+
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