Module:Formatnum/testcases
Lua
Documentation for this module may be created at Module:Formatnum/testcases/doc
Code
-- Unit tests for [[Module:Formatnum]]. Click talk page to run tests.
local p = require('Module:UnitTests')
function p:test_01_English_basic()
self:preprocess_equals('{{Formatnum|0|en}}', '0')
self:preprocess_equals('{{Formatnum|-123|en}}', '−123')
end
function p:test_02_English_precision_rounding()
self:heading('Comparing Formatnum with Formatnum/old: "Expected" means using Formatnum/old')
self:preprocess_equals_preprocess_many(
'{{Formatnum|', '}}',
'{{Formatnum/old|', '}}',
{
{'100|en|prec=1'},
{'100.0|en|prec=1'},
{'100.01|en|prec=2'},
{'100.01|en|prec=3'},
{'100.010|en|prec=2'},
})
end
function p:test_03_English_bad_parameters()
self:heading('Comparing Formatnum with Formatnum/old: "Expected" means using Formatnum/old')
self:preprocess_equals_preprocess_many(
'{{Formatnum|', '}}',
'{{Formatnum/old|', '}}',
{
{'.10,00|en'},
{'.10,00|en|sep='},
{'100,0.|en'},
{'100,0.|en|sep='},
{'100.01|en|prec=-2'},
{'200.1|en|prec=3.5'},
})
self:preprocess_equals('{{Formatnum|200.1|en|prec=3,5}}', '200.1')
self:preprocess_equals('{{Formatnum|200.1|en|prec=a}}', '200.1')
end
function p:test_04_English_unnecessary_signs_removed()
self:preprocess_equals('{{Formatnum|-0|en}}', '0')
self:preprocess_equals('{{Formatnum|+123|en}}', '123')
end
function p:test_05_Non_numbers_preserved()
self:preprocess_equals('{{Formatnum|12:34:45|en}}', '12:34:45')
self:preprocess_equals('{{Formatnum|12 h 34|fr}}', '12 h 34')
self:preprocess_equals('{{Formatnum|12:34:45|th}}', '12:34:45')
self:preprocess_equals('{{Formatnum|12:34:45.00|en}}', '12:34:45.00')
self:preprocess_equals('{{Formatnum|12°34′45.00″|en}}', '12°34′45.00″')
self:preprocess_equals('{{Formatnum|some words...|en}}', 'some words...')
end
function p:test_06_Styles_of_decimal_digits()
self:heading('The choice of decimal system may vary in Malayalam, Sinhalese, Mongolian and Uyghur languages.<br/> Using an explicit script extension to the language code should disambiguate this, but this still does not work.<br/ > In these languages, differences will not be counted as test failures.')
self:preprocess_equals('{{Formatnum|450|en}}', '450')
self:preprocess_equals('{{Formatnum|450|as}}', '৪৫০')
self:preprocess_equals('{{Formatnum|450|bn}}', '৪৫০')
self:preprocess_equals('{{Formatnum|450|gu}}', '૪૫૦')
self:preprocess_equals('{{Formatnum|450|hi}}', '४५०')
self:preprocess_equals('{{Formatnum|450|kn}}', '೪೫೦')
self:preprocess_equals('{{Formatnum|450|ml-Latn}}', '450') -- displayed with Hindu-Arabic/Latin digits
self:preprocess_equals('{{Formatnum|450|ml}}', '450', {varying=1}) -- displayed with Hindu-Arabic/Latin digits (for modern use)
self:preprocess_equals('{{Formatnum|450|ml-Mlym}}', '൪൫൦', {varying=1}) -- displayed INCORRECTLY with Hindu-Arabic/Latin digits
self:preprocess_equals('{{Formatnum|450|or}}', '୪୫୦')
self:preprocess_equals('{{Formatnum|450|si-Latn}}', '450') -- displayed with Hindu-Arabic/Latin digits
self:preprocess_equals('{{Formatnum|450|si}}', '450', {varying=1}) -- displayed with Hindu-Arabic/Latin digits (is it really preferred in modern use?)
self:preprocess_equals('{{Formatnum|450|si-Sinh}}', '෪෫෦', {varying=1}) -- displayed INCORRECTLY with Hindu-Arabic/Latin digits
self:preprocess_equals('{{Formatnum|450|ta}}', '௪௫௦')
self:preprocess_equals('{{Formatnum|450|te}}', '౪౫౦')
self:preprocess_equals('{{Formatnum|450|bo}}', '༤༥༠')
self:preprocess_equals('{{Formatnum|450|dz}}', '༤༥༠')
self:preprocess_equals('{{Formatnum|450|mn-Tibt}}', '༤༥༠', {varying=1}) -- displayed INCORRECTLY with Hindu-Arabic/Latin/Cyrillic digits
self:preprocess_equals('{{Formatnum|450|mn-Mong}}', '᠔᠕᠐', {varying=1}) -- displayed INCORRECTLY with Hindu-Arabic/Latin/Cyrillic digits
self:preprocess_equals('{{Formatnum|450|mn}}', '᠔᠕᠐', {varying=1}) -- displayed with Mongolian digits (is it really preferred in modern use?)
self:preprocess_equals('{{Formatnum|450|mn-Cyrl}}', '450') -- displayed with with Hindu-Arabic/Latin/Cyrillic digits
self:preprocess_equals('{{Formatnum|450|th}}', '๔๕๐')
self:preprocess_equals('{{Formatnum|450|km}}', '៤៥០')
self:preprocess_equals('{{Formatnum|450|lo}}', '໔໕໐')
self:preprocess_equals('{{Formatnum|450|ar}}', '٤٥٠')
self:preprocess_equals('{{Formatnum|450|fa}}', '۴۵۰')
self:preprocess_equals('{{Formatnum|450|ps}}', '۴۵۰')
self:preprocess_equals('{{Formatnum|450|ks}}', '۴۵۰')
self:preprocess_equals('{{Formatnum|450|ug-Cyrl}}', '450') -- displayed with Hindu-Arabic/Latin/Cyrillic digits
self:preprocess_equals('{{Formatnum|450|ug-Latn}}', '450') -- displayed with Hindu-Arabic/Latin/Cyrillic digits
self:preprocess_equals('{{Formatnum|450|ug}}', '450', {varying=1}) -- displayed with Hindu-Arabic/Latin/Cyrillic digits (is it really preferred in modern use?)
self:preprocess_equals('{{Formatnum|450|ug-Arab}}', '۴۵۰', {varying=1}) -- displayed INCORRECTLY with Hindu-Arabic/Latin/Cyrillic digits
end
function p:test_07_Decimal_separator()
self:heading('Basic cases generating a decimal separator appropriate to each language')
self:preprocess_equals('{{Formatnum|123.45|de}}', '123,45')
self:preprocess_equals('{{Formatnum|123.45|en}}', '123.45')
self:preprocess_equals('{{Formatnum|123.45|fr}}', '123,45')
self:preprocess_equals('{{Formatnum|123.45|sv}}', '123,45')
self:heading('There should be no trailing decimal separator for integer values')
self:preprocess_equals('{{Formatnum|123.|en}}', '123')
self:preprocess_equals('{{Formatnum|123.|fr}}', '123')
self:preprocess_equals('{{Formatnum|123.00|en}}', '123')
self:preprocess_equals('{{Formatnum|123.00|fr}}', '123')
self:heading('There should be at least a leading zero before the decimal separator for small non-zero absolute values')
self:preprocess_equals('{{Formatnum|.12345|en}}', '0.12345')
self:preprocess_equals('{{Formatnum|.12345|fr}}', '0,12345')
self:preprocess_equals('{{Formatnum|-.12345|en}}', '−0.12345')
self:preprocess_equals('{{Formatnum|-.12345|fr}}', '−0,12345')
end
function p:test_08_Grouping_separators()
self:heading('There should be grouping separators appropriate to each language')
self:preprocess_equals('{{Formatnum|123456789|de}}', '123.456.789')
self:preprocess_equals('{{Formatnum|123456789|en}}', '123,456,789')
-- self:preprocess_equals('{{Formatnum|123456789|fr}}', '123 456 789') -- see test 08 below
self:preprocess_equals('{{Formatnum|123456789|ml}}', '12,34,56,789')
self:heading('Below there should be no grouping separators in any language')
self:preprocess_equals('{{Formatnum|123456789|de|sep=1}}', '123456789')
self:preprocess_equals('{{Formatnum|123456789|en|sep=1}}', '123456789')
self:preprocess_equals('{{Formatnum|123456789|fr|sep=1}}', '123456789')
self:preprocess_equals('{{Formatnum|123456789|ml|sep=1}}', '123456789')
end
function p:test_09_Legacy_HTML_entity_or_native_UTF8()
self:heading('Comparing Formatnum with Formatnum/old: "Expected" means using Formatnum/old.<br /> Actual and expected results are HTMLized in wikitext source format.<br /> Differences will not be counted as test failures.')
self:preprocess_equals_preprocess_many(
'{{Formatnum|123456789|', '}}',
'{{Formatnum/old|123456789|', '}}',
{
{'en'}, -- this is OK (see the 2 first lines above)
{'fr'}, -- this fails when comparing them (difference of encoding for NBSP; see the next test)
{'br'}, -- this fails when comparing them (the old template had a problem, unexpectedly generating a comma; see the following 2 lines above)
{'co'}, -- this fails when comparing them (the old template had a problem, unexpectedly generating a comma)
{'oc'}, -- this fails when comparing them (the old template had a problem, unexpectedly generating a comma)
{'ty'}, -- this fails when comparing them (the old template had a problem, unexpectedly generating a comma)
{'pl'} -- this fails when comparing them (difference of encoding for NBSP; see the next test)
}, {htmlize=1, varying=1} -- don't fail, just warn
)
end
function p:test_10_Legacy_HTML_entity_or_native_UTF8_asserts()
self:heading('Actual and expected results are HTMLized in wikitext source format.')
self:preprocess_equals('{{Formatnum|12345|en}}', '12,345', {htmlize=1})
self:preprocess_equals('{{Formatnum/old|12345|en}}', '12,345', {htmlize=1})
self:heading('Grouping separators may be non-breaking spaces (preferably narrow), they have several HTML representations.<br /> Possible differences below will not be counted as failures.')
self:preprocess_equals('{{Formatnum|12345|fr}}', '12\194\160345', {htmlize=1, varying=1}) -- "\194\160" in Lua litterals is NBSP (U+00A0) encoded in UTF-8 (0xC2,0xA0)
self:preprocess_equals('{{Formatnum/old|12345|fr}}', '12 345', {htmlize=1, varying=1}) -- this difference should be corrected: the old template version generated HTML entities
self:preprocess_equals('{{Formatnum|12345|pl}}', '12\194\160345', {htmlize=1, varying=1}) -- "\194\160" in Lua litterals is NBSP (U+00A0) encoded in UTF-8 (0xC2,0xA0)
self:preprocess_equals('{{Formatnum/old|12345|pl}}', '12 345', {htmlize=1, varying=1}) -- this difference should be corrected: the old template version generated HTML entities
end
function p:test_11_Legacy_grouping_separators()
self:heading('Comparing Formatnum with Formatnum/old: "Expected" means using Formatnum/old.<br /> Actual and expected results are HTMLized in wikitext source format.<br /> Differences will not be counted as test failures.')
self:preprocess_equals_preprocess_many(
'{{Formatnum|1234567890|', '}}',
'{{Formatnum/old|1234567890|', '}}',
{ -- for each case below, this uses the same value to compare the preprocessed (prefix1..case..suffix1) with the preprocessed (prefix2..case..suffix2)
{'en'},
{'ru'},
{'hy'},
{'ka'},
{'mn'},
{'as'},
{'bn'},
{'gu'},
{'hi'},
{'kn'},
{'ml'},
{'or'},
{'pa'},
{'ta'},
{'te'},
{'bo'},
{'th'},
{'km'},
{'lo'},
{'ja'},
{'ko'},
{'zh'},
{'zh-hans'},
{'zh-hant'},
{'wuu'},
{'yue'},
{'he'},
{'yi'},
},
{varying=1} -- don't fail, just warn
)
self:heading('The Arabic script normally uses its own grouping separator U+066C (not to be confused with U+066B for its decimal separator).<br /> It also uses two different styles of digits: Western digits for standard Arabic and related languages in Africa, and Eastern digits for Farsi and related languages in Central Asia.<br /> In India or China, Latin-Arabic digits may be used with the ASCII comma U+0044 for the grouping separator.')
self:preprocess_equals_preprocess_many(
'{{Formatnum|1234567890|', '}}',
'{{Formatnum/old|1234567890|', '}}',
{ -- for each case below, this uses the same value to compare the preprocessed (prefix1..case..suffix1) with the preprocessed (prefix2..case..suffix2)
{'ar'},
{'fa'},
{'ps'},
{'ks'},
{'ur'},
{'ug'},
},
{varying=1} -- don't fail, just warn
)
end
function p:test_99_Unsupported_languages_using_user_default_language_instead()
self:heading('Results may vary depending on supported language of the user if it uses non-ASCII digits.<br /> Differences will not be counted as test failures.')
self:preprocess_equals('{{Formatnum|100000|sep=1}}', '100000', {varying=1} )
self:preprocess_equals('{{Formatnum|100000||sep=1}}', '100000', {varying=1} )
self:preprocess_equals('{{Formatnum|100000|qq|sep=1}}', '100000', {varying=1} )
self:preprocess_equals('{{Formatnum|100000|qqa|sep=1}}', '100000', {varying=1} )
self:preprocess_equals('{{Formatnum|100000|mul|sep=1}}', '100000', {varying=1} )
self:preprocess_equals('{{Formatnum|100000|und|sep=1}}', '100000', {varying=1} )
self:preprocess_equals('{{Formatnum|100000|root|sep=1}}', '100000', {varying=1} )
self:preprocess_equals('{{Formatnum|100000|user|sep=1}}', '100000', {varying=1} )
self:preprocess_equals('{{Formatnum|100000|invalid|sep=1}}', '100000', {varying=1} )
self:heading('If too many languages are used (more than 20?), they may behave like default languages.<br /> Differences will not be counted as test failures.')
self:preprocess_equals('{{Formatnum|100000|aa|sep=1}}', '100000', {varying=1} ) -- Afar
self:preprocess_equals('{{Formatnum|100000|ab|sep=1}}', '100000', {varying=1} ) -- Abkhaz
self:preprocess_equals('{{Formatnum|100000|ace|sep=1}}', '100000', {varying=1} ) -- Acehnese
self:preprocess_equals('{{Formatnum|100000|mwl|sep=1}}', '100000', {varying=1} ) -- Mirandese
self:preprocess_equals('{{Formatnum|100000|ydd|sep=1}}', '100000', {varying=1} ) -- Eastern Yiddish
self:preprocess_equals('{{Formatnum|100000|zea|sep=1}}', '100000', {varying=1} ) -- Zealandic
end
return p