Difference between revisions of "Module:ConvertNumeric/testcases"

From blackwiki
Jump to navigation Jump to search
blackwiki>Dcoetzee
(Generalize run_tests in preparation for refactoring by looping over methods)
blackwiki>Dcoetzee
(Refactor into UnitTests module)
Line 1: Line 1:
 
-- Unit tests for [[Module:ConvertNumeric]]. Click talk page to run tests.
 
-- Unit tests for [[Module:ConvertNumeric]]. Click talk page to run tests.
 
+
require('Module:UnitTests')
local p = {}
+
local p = UnitTester:new()
 
 
local frame, tick, cross
 
local result_table = "{|class=\"wikitable\"\n! !! Text !! Expected !! Actual\n|-\n"
 
local num_failures = 0
 
 
 
function preprocess_equals(text, expected)
 
    local actual = frame:preprocess(text)
 
    if actual == expected then
 
        result_table = result_table .. '| ' .. tick
 
    else
 
        result_table = result_table .. '| ' .. cross
 
        num_failures = num_failures + 1
 
    end
 
    result_table = result_table .. ' || <nowiki>' .. text:gsub('%|', '&#124;') .. '</nowiki> || ' .. expected .. ' || ' .. actual .. "\n|-\n"
 
end
 
  
 
function p.test_numeral_to_english()
 
function p.test_numeral_to_english()
Line 31: Line 16:
 
     preprocess_equals(prefix .. '76 | pl=on}}', 'seventy-sixes')
 
     preprocess_equals(prefix .. '76 | pl=on}}', 'seventy-sixes')
 
     preprocess_equals(prefix .. '-123456789.25 }}', 'negative one hundred twenty-three million four hundred fifty-six thousand seven hundred and eighty-nine point two five')
 
     preprocess_equals(prefix .. '-123456789.25 }}', 'negative one hundred twenty-three million four hundred fifty-six thousand seven hundred and eighty-nine point two five')
end
 
 
function p.run_tests(frame_arg)
 
    frame = frame_arg
 
    tick = frame:preprocess('{{Tick}}')
 
    cross = frame:preprocess('{{Cross}}')
 
   
 
    for key,value in pairs(p) do
 
        if key:find('^test') then value() end
 
    end
 
 
    result_table = result_table .. "|}"
 
    return (num_failures == 0 and "<font color=\"#008000\">'''All tests passed.'''</font>" or "<font color=\"#800000\">'''" .. num_failures .. " tests failed.'''</font>") .. "\n\n" .. frame:preprocess(result_table)
 
 
end
 
end
  
 
return p
 
return p

Revision as of 00:10, 25 February 2013

Documentation for this module may be created at Module:ConvertNumeric/testcases/doc

-- Unit tests for [[Module:ConvertNumeric]]. Click talk page to run tests.
require('Module:UnitTests')
local p = UnitTester:new()

function p.test_numeral_to_english()
    local prefix = '{{#invoke:ConvertNumeric | numeral_to_english |'
    preprocess_equals(prefix .. '0}}', 'zero')
    preprocess_equals(prefix .. '1}}', 'one')
    preprocess_equals(prefix .. ' 1 }}', 'one') -- test whitespace around number
    preprocess_equals(prefix .. '-1}}', 'negative one')
    preprocess_equals(prefix .. '-1 | negative=minus}}', 'minus one')
    preprocess_equals(prefix .. '70}}', 'seventy')
    preprocess_equals(prefix .. '42}}', 'forty-two')
    preprocess_equals(prefix .. '33 | ord=on}}', 'thirty-third')
    preprocess_equals(prefix .. '40 | ord=on}}', 'fortieth')
    preprocess_equals(prefix .. '76 | pl=on}}', 'seventy-sixes')
    preprocess_equals(prefix .. '-123456789.25 }}', 'negative one hundred twenty-three million four hundred fifty-six thousand seven hundred and eighty-nine point two five')
end

return p