Module:Roman/testcases: Difference between revisions

Content deleted Content added
this really should be no problem
ASiplas (talk | contribs)
Withdrawing sandbox code / request for edit—duplicate of Module:ConvertNumeric's roman_to_numeral
 
(40 intermediate revisions by 2 users not shown)
Line 2:
 
local moduleName = 'Roman/sandbox' -- assigning this to a variable as it is later used to generate an #invoke statement.
local mainFuncName = 'main'
local mm = require('Module:' .. moduleName)
local ScribuntoUnitsuite = require('Module:ScribuntoUnitUnitTests')
local suite = ScribuntoUnit:new()
 
function suite.buildInvocation(funcName, args)
-------------------------------------------------------------------------------
args = args or {}
-- Helper functions
local argsClone = mw.clone(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(argsClone) do
v = tostring(v)
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
argsClone[k] = nil
end
for k, v in pairs(argsClone) do
k = tostring(k)
v = tostring(v)
ret = ret .. '|' .. k .. '=' .. v
end
return 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.
function suite.getLuaResult(funcName, args)
args = args or {}
local resultinvocation = mm['_' self.. funcName](unpackbuildInvocation(funcName, args))
local result = self.frame:preprocess(invocation)
return result
if convertNumber ~= false and tonumber(result) then
return tonumber(result)
else
return result
end
end
 
function suite:assertLuaEqualsassertInvokeEquals(expected, funcName, args, convertNumber)
args = args or {}
local invokeResult = self:assertEquals(expected, self.getLuaResultgetInvokeResult(funcName, args), convertNumber)
self:preprocess_equals(invokeResult, expected)
end
 
function suite.buildInvocation:assertInvokeEqual(funcName, argstestTable, convertNumber)
args testTable = argstestTable or {}
local argsCloneexpected = mw.clone(args)testTable[1]
local args = testTable[2] or {}
-- Build a module invocation equivalent to the args table. Taken from [[Module:Unsubst]].
self:assertInvokeEquals(expected, funcName, args, convertNumber)
-- Numbered args first.
local ret = '{{#invoke:' .. moduleName .. '|' .. funcName
for k, v in ipairs(argsClone) do
v = tostring(v)
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
argsClone[k] = nil
end
for k, v in pairs(argsClone) do
k = tostring(k)
v = tostring(v)
ret = ret .. '|' .. k .. '=' .. v
end
return ret .. '}}'
end
 
function suite:getInvokeResultassertInvokeEqualMany(funcName, argstestTables, convertNumber) -- Unless convertNumber is false, the number is converted to a number, if possible, on re-entry to Lua.
for i, testTable in ipairs(testTables) do
args = args or {}
local invocation = self.buildInvocation:assertInvokeEqual(funcName, argstestTable, convertNumber)
end
local result = self.frame:preprocess(invocation)
if convertNumber ~= false and tonumber(result) then
return tonumber(result)
else
return result
end
end
 
function suite:assertInvokeEqualstest_genericNumbers(expected, funcName, args, convertNumber)
args = local args or= {}
{'N', {'0'}}, {'I', {'1'}}, {'II', {'2'}}, {'IV', {'4'}}, {'V', {'5'}},
local invokeResult = self:getInvokeResult(funcName, args, convertNumber)
{'VI', {'6'}}, {'VIII', {'8'}}, {'IX', {'9'}}, {'X', {'10'}},
self:assertEquals(expected, invokeResult)
{'XI', {'11'}}, {'XIV', {'14'}}, {'XV', {'15'}}, {'XVI', {'16'}},
{'XIX', {'19'}}, {'XX', {'20'}}, {'XXI', {'21'}}, {'XXIV', {'24'}},
{'XXV', {'25'}}, {'XXVI', {'26'}}, {'XXXIV', {'34'}}, {'XXXV', {'35'}},
{'XXXVIII', {'38'}}, {'XXXIX', {'39'}}, {'XL', {'40'}}, {'XLI', {'41'}},
{'XLIV', {'44'}}, {'XLV', {'45'}}, {'XLIX', {'49'}}, {'L', {'50'}},
{'LXXXVIII', {'88'}}, {'LXXXIX', {'89'}}, {'XC', {'90'}}, {'XCI', {'91'}},
{'XCIV', {'94'}}, {'XCV', {'95'}}, {'XCVIII', {'98'}}, {'XCIX', {'99'}},
{'C', {'100'}}, {'CI', {'101'}}, {'CIV', {'104'}}, {'CV', {'105'}},
{'CIX', {'109'}}, {'CX', {'110'}},
{'MCCXXXIV', {'1234'}},
{'MDCXLVIII', {'1648'}},
{'MMCMXCIX', {'2999'}},
{'MMM', {'3000'}},
}
-- unsure how to test cases with overline
self:assertInvokeEqualMany('main', args)
end
 
function suite:assertLuaAndInvokeTruetest_outOfRangeNumbers(trueFunc, funcName, args, convertNumber)
args = local args or= {}
{'N/A', {'5000000'}},
local invokeResult = self:getInvokeResult(funcName, args, convertNumber)
{'N/A', {'5000010'}},
local luaResult = self.getLuaResult(funcName, args)
{'N/A', {'3000000000'}},
self:assertTrue(trueFunc(invokeResult))
}
self:assertTrue(trueFunc(luaResult))
self:assertInvokeEqualMany('main', args)
end
 
function suite:assertLuaAndInvokeEqualtest_MessageArg(funcName, testTable, convertNumber)
local expectedargs = testTable[1]{
{'N', {'0', 'too big'}},
local args = testTable[2] or {}
{'I', {'1', 'too big'}},
self:assertLuaEquals(expected, funcName, args)
{'V', {'5', 'too big'}},
self:assertInvokeEquals(expected, funcName, args, convertNumber)
{'X', {'10', 'too big'}},
{'L', {'50', 'too big'}},
{'C', {'100', 'too big'}},
{'D', {'500', 'too big'}},
{'M', {'1000', 'too big'}},
{'too big', {'5000000', 'too big'}},
{'too big', {'5000010', 'too big'}},
{'too big', {'3000000000', 'too big'}},
}
self:assertInvokeEqualMany('main', args)
end
 
function suite:assertLuaAndInvokeEqualManytest_MessageArgWithFractionArg(funcName, testTables, convertNumber)
-- Note, 'fraction=yes' before 'too big' cannot happen with the template
for i, testTable in ipairs(testTables) do
local args = {
self:assertLuaAndInvokeEqual(funcName, testTable, convertNumber)
{'N', {'0', 'too big', 'fraction=yes'}},
end
{'I', {'1', 'too big', 'fraction=yes'}},
{'M', {'1000', 'too big', 'fraction=yes'}},
{'too big', {'5000000', 'too big', 'fraction=yes'}},
{'too big', {'5000010', 'too big', 'fraction=yes'}},
{'too big', {'3000000000', 'too big', 'fraction=yes'}},
}
self:assertInvokeEqualMany('main', args)
end
 
--[[
-------------------------------------------------------------------------------
function suite:test_decimalsBetweenZeroAndOne()
-- Tests
local args = {
-------------------------------------------------------------------------------
{'»', {'0.0001', '', 'fraction=yes'}},
{'»', {'0.0005', '', 'fraction=yes'}},
{'»', {'0.000578703', '', 'fraction=yes'}},
{'»', {'0.000578704', '', 'fraction=yes'}},
{'℈', {'0.00347222', '', 'fraction=yes'}},
{'℈', {'0.00347223', '', 'fraction=yes'}},
{'ƻ', {'0.007', '', 'fraction=yes'}},
{'Ƨ', {'0.0139', '', 'fraction=yes'}},
{'Ɔ', {'0.02084', '', 'fraction=yes'}},
{'ƧƧ', {'0.0278', '', 'fraction=yes'}},
{'Є', {'0.04167', '', 'fraction=yes'}},
{'•', {'0.08334', '', 'fraction=yes'}},
{'•ЄƧ℈»', {'0.142858', '', 'fraction=yes'}},
{"''':'''", {'0.1667', '', 'fraction=yes'}},
{"''':'''•", {'0.25', '', 'fraction=yes'}},
{"'''::'''", {'0.3333', '', 'fraction=yes'}},
{"''':'''•''':'''", {'0.41666', '', 'fraction=yes'}},
{'S', {0.5, '', 'fraction=yes'}},
{"S''':'''•", {'0.75', '', 'fraction=yes'}},
{"S''':'''•''':'''ЄƧƧƻ℈»»»»", {'0.9999', '', 'fraction=yes'}},
}
self:assertInvokeEqualMany('main', args)
end
--]]
 
--[[
function suite:test_integersZeroToTen()
function suite:test_fractionsBetweenZeroAndOne()
local tests = {
local args = {
{'zero', {'0'}},
{'IS', {'1/2', 'hi', 'fraction=yes'}},
}
}
self:assertLuaEqualsassertInvokeEqualMany('N', 'main', {'0'}args)
end
--]]
 
return suite