Jump to content

Module:RiskRange/testcases

From RiskiPedia

Test Cases for Module:ProbabilityFormat

Input 1 Input 2 Expected Actual Status
0.5 Error: First probability not provided Error: First probability not provided ✅ Pass
0.5 Error: Second probability not provided Error: Second probability not provided ✅ Pass
invalid 0.5 Error: Invalid first probability Error: Invalid first probability ✅ Pass
0.5 invalid Error: Invalid second probability Error: Invalid second probability ✅ Pass
0 0 no chance no chance ✅ Pass
1 1 100% chance 100% chance ✅ Pass
1.5 2 100% chance 100% chance ✅ Pass
0.96 0.99 almost certain almost certain ✅ Pass
0 1 anywhere from no chance to certain anywhere from no chance to certain ✅ Pass
0.5 0.5 about 5 in 10 about 5 in 10 ✅ Pass
0.1 0.1 about 1 in 10 about 1 in 10 ✅ Pass
0.01 0.01 about 1 in 100 about 1 in 100 ✅ Pass
0.001 0.01 1 to 10 in 1,000 1 to 10 in 1,000 ✅ Pass
0.01 0.001 1 to 10 in 1,000 1 to 10 in 1,000 ✅ Pass
0.2 0.5 2 to 5 in 10 2 to 5 in 10 ✅ Pass
0.1 0.3 1 to 3 in 10 1 to 3 in 10 ✅ Pass
0.05 0.2 1 to 2 in 10 1 to 2 in 10 ✅ Pass
0.01 0.05 1 to 5 in 100 1 to 5 in 100 ✅ Pass
0.0001 0.001 1 to 10 in 10,000 1 to 10 in 10,000 ✅ Pass
0.6 0.8 6 to 8 in 10 6 to 8 in 10 ✅ Pass
0.55 0.9 6 to 9 in 10 6 to 9 in 10 ✅ Pass
0.2 0.6 2 to 6 in 10 2 to 6 in 10 ✅ Pass
0.3 0.7 3 to 7 in 10 3 to 7 in 10 ✅ Pass
0 0.5 up to about 5 in 10 up to about 5 in 10 ✅ Pass
0 0.01 up to about 1 in 100 up to about 1 in 100 ✅ Pass
0.5 1 at least 5 in 10 at least 5 in 10 ✅ Pass
0.01 1 at least 1 in 100 at least 1 in 100 ✅ Pass
0.21 0.6 2 to 6 in 10 2 to 6 in 10 ✅ Pass
0.33 0.82 3 to 8 in 10 3 to 8 in 10 ✅ Pass
0.15 0.46 2 to 5 in 10 2 to 5 in 10 ✅ Pass
30/30 tests passed

local p = {}

-- Load the main module
local riskRange = require('Module:RiskRange')

-- Table of test cases: { input1, input2, expected_output }
local testCases = {
    -- Error cases
    { input1 = '', input2 = '0.5', expected = 'Error: First probability not provided' },
    { input1 = '0.5', input2 = '', expected = 'Error: Second probability not provided' },
    { input1 = 'invalid', input2 = '0.5', expected = 'Error: Invalid first probability' },
    { input1 = '0.5', input2 = 'invalid', expected = 'Error: Invalid second probability' },

    -- Special cases
    { input1 = 0, input2 = 0, expected = 'no chance' },
    { input1 = 1, input2 = 1, expected = '100% chance' },
    { input1 = 1.5, input2 = 2, expected = '100% chance' },
    { input1 = 0.96, input2 = 0.99, expected = 'almost certain' },
    { input1 = 0, input2 = 1, expected = 'anywhere from no chance to certain' },

    -- Equal or nearly equal probabilities
    { input1 = 0.5, input2 = 0.5, expected = 'about 5 in 10' },
    { input1 = 0.1, input2 = 0.1, expected = 'about 1 in 10' },
    { input1 = 0.01, input2 = 0.01, expected = 'about 1 in 100' },

    -- Standard ranges - both probabilities < 0.55
    { input1 = 0.001, input2 = 0.01, expected = '1 to 10 in 1,000' },
    { input1 = 0.01, input2 = 0.001, expected = '1 to 10 in 1,000' },  -- reversed order
    { input1 = 0.2, input2 = 0.5, expected = '2 to 5 in 10' },
    { input1 = 0.1, input2 = 0.3, expected = '1 to 3 in 10' },
    { input1 = 0.05, input2 = 0.2, expected = '1 to 2 in 10' },
    { input1 = 0.01, input2 = 0.05, expected = '1 to 5 in 100' },
    { input1 = 0.0001, input2 = 0.001, expected = '1 to 10 in 10,000' },

    -- Ranges where both probabilities >= 0.55 (high risk)
    { input1 = 0.6, input2 = 0.8, expected = '6 to 8 in 10' },
    { input1 = 0.55, input2 = 0.9, expected = '6 to 9 in 10' },

    -- Ranges crossing the 0.55 threshold
    { input1 = 0.2, input2 = 0.6, expected = '2 to 6 in 10' },
    { input1 = 0.3, input2 = 0.7, expected = '3 to 7 in 10' },

    -- Edge case: low end near zero
    { input1 = 0, input2 = 0.5, expected = 'up to about 5 in 10' },
    { input1 = 0, input2 = 0.01, expected = 'up to about 1 in 100' },

    -- Edge case: high end at or above 1
    { input1 = 0.5, input2 = 1, expected = 'at least 5 in 10' },
    { input1 = 0.01, input2 = 1, expected = 'at least 1 in 100' },

    -- Concert hearing loss example cases
    { input1 = 0.21, input2 = 0.6, expected = '2 to 6 in 10' },
    { input1 = 0.33, input2 = 0.82, expected = '3 to 8 in 10' },
    { input1 = 0.15, input2 = 0.46, expected = '2 to 5 in 10' },
}

-- Function to run tests and return a formatted table
function p.runTests(frame)
    local output = { '{| class="wikitable"\n! Input 1 !! Input 2 !! Expected !! Actual !! Status' }
    local passCount = 0
    local totalCount = #testCases

    for i, test in ipairs(testCases) do
        -- Simulate frame.args for the inputs
        local frameMock = { args = { [1] = test.input1, [2] = test.input2 } }
        local actual = riskRange.convert(frameMock)
        local status = actual == test.expected and '✅ Pass' or '❌ Fail'
        if actual == test.expected then
            passCount = passCount + 1
        end
        table.insert(output, '|-\n| ' .. tostring(test.input1) .. ' || ' .. tostring(test.input2) .. ' || ' .. test.expected .. ' || ' .. actual .. ' || ' .. status)
    end

    table.insert(output, '|-\n| colspan="5" | ' .. passCount .. '/' .. totalCount .. ' tests passed')
    table.insert(output, '|}')
    return table.concat(output, '\n')
end

return p