Module:Formatnum/testcases

-- Unit tests for Module:Formatnum. Click talk page to run tests. local p = require('Module:UnitTests') function p:test_1_English_basic self:preprocess_equals('0', '0') self:preprocess_equals('-123', '-123') end

function p:test_2_unnecessary_signs self:preprocess_equals('-0', '0') self:preprocess_equals('123', '123') end

function p:test_3_non_numbers_preserved self:preprocess_equals('12', '12:34:45') self:preprocess_equals('NaN', '12 h 34') self:preprocess_equals('12', '12:34:45') self:preprocess_equals('12', '12:34:45.00') self:preprocess_equals('NaN', '12°34′45.00″') self:preprocess_equals('NaN', 'some words...') end

function p:test_4_English_precision_rounding self:preprocess_equals_preprocess_many(           'NaN',        '',        {            {'100|en|prec=1'},            {'100.01|en|prec=1'},            {'100.01|en|prec=2'},            {'100.01|en|prec=3'},            {'100.010|en|prec=2'},        }) end

function p:test_5_English_bad_parameters self:preprocess_equals_preprocess_many(           'NaN',        '',        {            {'.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('200.1', '200.1') self:preprocess_equals('200.1', '200.1') end

function p:test_6_decimal_separator self:preprocess_equals('0.123', '0.12345') self:preprocess_equals('0.123', '0,12345') self:preprocess_equals('123.45', '123.45') self:preprocess_equals('123.45', '123,45') self:preprocess_equals('123', '123') self:preprocess_equals('123', '123') self:preprocess_equals('123', '123') self:preprocess_equals('123', '123') end

function p:test_7_no_grouping_separators self:preprocess_equals('100,000', '100000') self:preprocess_equals('100,000', '100000') end

function p:test_8_HTML_entity_or_native_UTF8 self:preprocess_equals('12,345', '12,345') self:preprocess_equals('', '12,345') self:preprocess_equals('12,345', '12\194\160345') -- "\194\160" in Lua litterals is NBSP (U+00A0) encoded in UTF-8 (0xC2,0xA0) self:preprocess_equals('', '12 345') -- this difference should be corrected: the old template version generated HTML entities self:preprocess_equals_preprocess_many(           '12,345',        '',        {            {'en'}, -- this is OK            {'fr'}, -- this fails when comparing them            {'br'}, -- this fails when comparing them            {'co'}, -- this fails when comparing them            {'oc'}, -- this fails when comparing them            {'ty'}, -- this fails when comparing them        }    ) end

function p:test_9_grouping_separators self:preprocess_equals_preprocess_many(           '1,234,567,890',        '',        {   -- this test will 'fr' will fail as long as test 8 fails            {'en'},            {'ka'},            {'mn'},            {'ar'},            {'fa'},            {'ks'},            {'hi'},            {'bn'},            {'ta'},            {'te'},            {'kn'},            {'or'},            {'gu'},            {'pa'},            {'bo'},            {'th'},            {'km'},            {'lo'},        }) end

function p:test_Error_more_than_20_languages self:preprocess_equals('100,000', '100000') self:preprocess_equals('100,000', '100000') end

function p:test_Unsupported_languages_using_user_default_language_instead_may_fail -- these may fail depending on supported language of the user if it uses non-ASCII digits self:preprocess_equals('100,000', '100000') -- unspecified Wikimedia default self:preprocess_equals('100,000', '100000') -- Afar self:preprocess_equals('100,000', '100000') -- Private-use self:preprocess_equals('100,000', '100000') -- Private-use self:preprocess_equals('100,000', '100000') -- Multilingual self:preprocess_equals('100,000', '100000') -- Undetermined self:preprocess_equals('100,000', '100000') -- CLDR default self:preprocess_equals('100,000', '100000') self:preprocess_equals('100,000', '100000') end

return p