-- Unit tests for [[Module:Roman/sandbox]]. Click talk page to run tests.

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 suite = require('Module:UnitTests')

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 = '((#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.
    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:preprocess_equals(invokeResult, expected)
end

function suite:assertInvokeEqual(funcName, testTable, convertNumber)
    testTable = testTable or {}
    local expected = testTable[1]
    local args = testTable[2] or {}
    self:assertInvokeEquals(expected, funcName, args, convertNumber)
end

function suite:assertInvokeEqualMany(funcName, testTables, convertNumber)
    for i, testTable in ipairs(testTables) do
        self:assertInvokeEqual(funcName, testTable, convertNumber)
    end
end

function suite:test_genericNumbers()
    local args = {
        {'N', {'0')), {'I', {'1')), {'II', {'2')), {'IV', {'4')), {'V', {'5')),
        {'VI', {'6')), {'VIII', {'8')), {'IX', {'9')), {'X', {'10')),
        {'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:test_outOfRangeNumbers()
    local args = {
        {'N/A', {'5000000')),
        {'N/A', {'5000010')),
        {'N/A', {'3000000000')),
    }
    self:assertInvokeEqualMany('main', args)
end

function suite:test_MessageArg()
    local args = {
        {'N', {'0', 'too big')),
        {'I', {'1', 'too big')),
        {'V', {'5', 'too big')),
        {'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:test_MessageArgWithFractionArg()
    -- Note, 'fraction=yes' before 'too big' cannot happen with the template
    local args = {
        {'N', {'0', 'too big', 'fraction=yes')),
        {'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()
    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_fractionsBetweenZeroAndOne()
    local args = {
        {'S', {'1/2', 'hi', 'fraction=yes'))
    }
    self:assertInvokeEqualMany('main', args)
end
--]]

return suite