FANDOM


--<pre> Unit tests for [[Module:Formatnum]]. Click talk page to run tests.
local p = require('Module:UnitTests')
local invokeText = '{{#invoke:Formatnum|main'
 
function p:test_1_English_basic()
    self:preprocess_equals(invokeText .. '|0|en}}', '0')
    self:preprocess_equals(invokeText .. '|-123|en}}', '-123')
end
 
function p:test_2_unnecessary_signs()
    self:preprocess_equals(invokeText .. '|-0|en}}', '0')
    self:preprocess_equals(invokeText .. '|+123|en}}', '123')
end
 
function p:test_3_non_numbers_preserved()
    self:preprocess_equals(invokeText .. '|12:34:45|en}}', '12:34:45')
    self:preprocess_equals(invokeText .. '|12 h 34|fr}}', '12 h 34')
    self:preprocess_equals(invokeText .. '|12:34:45|th}}', '12:34:45')
    self:preprocess_equals(invokeText .. '|12:34:45.00|en}}', '12:34:45.00')
    self:preprocess_equals(invokeText .. '|12°34′45.00″|en}}', '12°34′45.00″')
    self:preprocess_equals(invokeText .. '|some words...|en}}', 'some words...')
end
 
function p:test_4_English_precision_rounding()
    self:preprocess_equals_preprocess_many(
            invokeText .. '|', '}}',
        invokeText .. '|', '}}',
        {
            {'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(
            invokeText .. '|', '}}',
        invokeText .. '|', '}}',
        {
            {'.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(invokeText .. '|200.1|en|prec=3,5}}', '200.1')
    self:preprocess_equals(invokeText .. '|200.1|en|prec=a}}', '200.1')
end
 
function p:test_6_decimal_separator()
    self:preprocess_equals(invokeText .. '|.12345|en}}', '0.12345')
    self:preprocess_equals(invokeText .. '|.12345|fr}}', '0,12345')
    self:preprocess_equals(invokeText .. '|123.45|en}}', '123.45')
    self:preprocess_equals(invokeText .. '|123.45|fr}}', '123,45')
    self:preprocess_equals(invokeText .. '|123.|en}}', '123')
    self:preprocess_equals(invokeText .. '|123.|fr}}', '123')
    self:preprocess_equals(invokeText .. '|123.00|en}}', '123')
    self:preprocess_equals(invokeText .. '|123.00|fr}}', '123')
end
 
function p:test_7_no_grouping_separators()
    self:preprocess_equals(invokeText .. '|100000|en|sep=1}}', '100000')
    self:preprocess_equals(invokeText .. '|100000|fr|sep=1}}', '100000')
end
 
function p:test_8_HTML_entity_or_native_UTF8()
    self:preprocess_equals(invokeText .. '|12345|en}}', '12,345')
    self:preprocess_equals(invokeText .. '|12345|en}}', '12,345')
    self:preprocess_equals(invokeText .. '|12345|fr}}', '12\194\160345') -- "\194\160" in Lua litterals is NBSP (U+00A0) encoded in UTF-8 (0xC2,0xA0)
    self:preprocess_equals(invokeText .. '|12345|fr}}', '12&nbsp;345') -- this difference should be corrected: the old template version generated HTML entities
    self:preprocess_equals_preprocess_many(
            invokeText .. '|12345|', '}}',
        invokeText .. '|12345|', '}}',
        {
            {'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(
            invokeText .. '|1234567890|', '}}',
        invokeText .. '|1234567890|', '}}',
        {   -- this test  '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(invokeText .. '|100000|ab|sep=1}}', '100000')
    -- self:preprocess_equals(invokeText .. '|100000|ace|sep=1}}', '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(invokeText .. '|100000||sep=1}}', '100000') -- unspecified Wikimedia default
    -- self:preprocess_equals(invokeText .. '|100000|aa|sep=1}}', '100000') -- Afar
    self:preprocess_equals(invokeText .. '|100000|qq|sep=1}}', '100000') -- Private-use
    self:preprocess_equals(invokeText .. '|100000|qqa|sep=1}}', '100000') -- Private-use
    self:preprocess_equals(invokeText .. '|100000|mul|sep=1}}', '100000') -- Multilingual
    self:preprocess_equals(invokeText .. '|100000|und|sep=1}}', '100000') -- Undetermined
    self:preprocess_equals(invokeText .. '|100000|root|sep=1}}', '100000') -- CLDR default
    self:preprocess_equals(invokeText .. '|100000|user|sep=1}}', '100000')
    self:preprocess_equals(invokeText .. '|100000|invalid|sep=1}}', '100000')
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.