Module:Fallback/testcases

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Dependencies[edit]

  • Module:Fallbacklist catalogs old fallback chains for each language as it was set up on Commons a decade ago. Lua allows now to access the default MediaWiki fallback chains which are used by most templates.

Code

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

function p:test_1_translatelua()
    self:preprocess_equals('{{#invoke: Fallback | translatelua |i18n/oil on canvas| lang = en }}', '[[:en:oil on canvas|oil on canvas]]')
    self:preprocess_equals('{{#invoke: Fallback | translatelua |i18n/oil on canvas| lang = fr }}', "[[:fr:Peinture à l'huile|huile]] sur [[:fr:Toile (peinture)|toile]]")
    self:preprocess_equals('{{#invoke: Fallback | translatelua |i18n/oil on canvas| lang = frc }}', "[[:fr:Peinture à l'huile|huile]] sur [[:fr:Toile (peinture)|toile]]")
    self:preprocess_equals('{{#invoke: Fallback | translatelua |i18n/oil on canvas| lang = pl }}', 'olej na płótnie')
    self:preprocess_equals('{{#invoke: Fallback | translatelua |i18n/coordinates|CameraLocation| lang = en }}', 'Camera location')
end

function p:test_2_compare__fallbacks()
    local function mwlist(lang) -- expected(?)
        local function has(list, value)
            for _, val in ipairs(list) do
                if val == value then
                    return true
                end
            end
            return false
        end
        local function remove(list, value)
            for key, val in ipairs(list) do
                if val == value then
                    table.remove(list, key)
                end
            end
        end
        local list = mw.language.getFallbacksFor(lang)
        -- Modifications in order to compare successfully with actual in most cases:
        -- 1. Remove English completely if English is not requested.
        if lang ~= 'en' and lang:match('^en%-') == nil then
            remove(list, 'en')
        end
        -- 2. add or move the code of the language itself to the top
        remove(list, lang)
        table.insert(list, 1, lang)
        -- 3. add 'default' at end
        table.insert(list, 'default') -- this must come before 'en'
        -- 4. add 'en' after 'default', if 'en' was removed in step 1
        if not has(list, 'en') then
            table.insert(list, 'en') -- this must come last after 'default'
        end
        return list
    end
    self:heading('Expected means using only builtin <code>mw.language.getFallbacksFor(langcode)</code>, with English placed after default.<br /> Actual means using <code>fallback.fblist(langcode)</code>.')
	for _, lang in ipairs({
		'aa', 'ab', 'ace', 'af', 'ak', 'aln', 'als', 'am', 'an', 'ang', 'anp',
		'ar', 'arc', 'arn', 'ary', 'arz', 'as', 'ast', 'av', 'avk', 'ay',
		'az', 'azb',
		'ba', 'bar', 'bat-smg', 'bbc', 'bbc-latn', 'bcc', 'bcl',
		'be', 'be-tarask', 'be-x-old', 'bg', 'bh', 'bho', 'bi', 'bjn', 'bm',
		'bn', 'bo', 'bpy', 'bqi', 'br', 'brh', 'bs', 'bug', 'bxr',
		'ca', 'cbk-zam', 'cdo', 'ce', 'ceb', 'ch', 'cho', 'chr', 'chy', 'ckb',
		'co', 'co-fr', 'co-it', 'cps', 'cr', 'crh', 'crh-latn', 'crh-cyrl',
		'cs', 'csb', 'cu', 'cv', 'cy',
		'da', 'de', 'de-at', 'de-ch', 'de-formal', 'diq', 'dsb', 'dtp', 'dv',
		'dz',
		'ee', 'egl', 'el', 'eml',
		'en', 'en-ca', 'en-gb', 'en-in', 'en-us', 'en-za', 'eo', 'es', 'et',
		'eu', 'ext',
		'fa', 'ff', 'fi', 'fit', 'fiu-vro', 'fj', 'fo', 'fr', 'frc', 'frp',
		'frr', 'fur', 'fy',
		'ga', 'gag', 'gan', 'gan-hans', 'gan-hant', 'gd', 'gl', 'glk', 'gn',
		'gom-latn', 'got', 'grc', 'gsw', 'gu', 'gv',
		'ha', 'hak', 'haw', 'he', 'hi', 'hif', 'hif-latn', 'hil', 'ho', 'hr',
		'hsb', 'ht', 'hu', 'hy', 'hz',
		'ia', 'id', 'ie', 'ig', 'ii', 'ik', 'ike-cans', 'ike-latn', 'ilo',
		'inh', 'io', 'is', 'it', 'iu',
		'ja', 'jam', 'jbo', 'jut', 'jv',
		'ka', 'kaa', 'kab', 'kbd', 'kbd-cyrl', 'kg', 'khw', 'ki', 'kiu', 'kj',
		'kk', 'kk-arab', 'kk-cyrl', 'kk-latn', 'kk-cn', 'kk-kz', 'kk-tr', 'kl',
		'km', 'kn', 'ko', 'ko-kp', 'koi', 'kr', 'krc', 'kri', 'krj',
		'ks', 'ks-arab', 'ks-deva', 'ksh', 'ku', 'ku-latn', 'ku-arab', 'kv',
		'kw', 'ky',
		'la', 'lad', 'lb', 'lbe', 'lez', 'lfn', 'lg', 'li', 'lij', 'liv',
		'lmo', 'ln', 'lo', 'lrc', 'loz', 'lt', 'ltg', 'lus', 'lv', 'lzh',
		'lzz',
		'mai', 'map-bms', 'mdf', 'mg', 'mh', 'mhr', 'mi', 'min', 'mk', 'ml',
		'mn', 'mo', 'mr', 'mrj', 'ms', 'mt', 'mus', 'mwl', 'my', 'myv', 'mzn',
		'na', 'nah', 'nan', 'nap', 'nb', 'nds', 'nds-nl', 'ne', 'new', 'ng',
		'niu', 'nl', 'nl-informal', 'nn', 'no', 'nov', 'nrm', 'nso', 'nv',
		'ny',
		'oc', 'om', 'or', 'os',
		'pa', 'pag', 'pam', 'pap', 'pcd', 'pdc', 'pdt', 'pfl', 'pi', 'pih',
		'pl', 'pms', 'pnb', 'pnt', 'prg', 'ps', 'pt', 'pt-br',
		'qu', 'qug',
		'rgn', 'rif', 'rm', 'rmy', 'rn', 'ro', 'roa-rup', 'roa-tara',
		'ru', 'rue', 'rup', 'ruq', 'ruq-cyrl', 'ruq-grek', 'ruq-latn', 'rw',
		'sa', 'sah', 'sat', 'sc', 'scn', 'sco', 'sd', 'sdc', 'se', 'sei',
		'sg', 'sgs', 'sh', 'shi', 'shi-tfng', 'shi-latn', 'si', 'simple',
		'sk', 'sl', 'sli', 'sm', 'sma', 'sn', 'so', 'sq',
		'sr', 'sr-cyrl', 'sr-ec', 'sr-el', 'sr-latn', 'srn', 'ss', 'st',
		'stq', 'su', 'sv', 'sw', 'szl',
		'ta', 'tcy', 'te', 'tet', 'tg', 'tg-cyrl', 'tg-latn', 'th', 'ti',
		'tk', 'tl', 'tly', 'tn', 'to', 'tokipona', 'tpi', 'tr', 'tru', 'ts',
		'tt', 'tt-cyrl', 'tt-latn', 'tum', 'tw', 'ty', 'tyv',
		'udm', 'ug', 'ug-arab', 'ug-latn', 'uk', 'ur', 'uz', 've', 'vec',
		'vep', 'vi', 'vls', 'vmf', 'vo', 'vot', 'vro',
		'wa', 'war', 'wo', 'wuu',
		'xal', 'xh', 'xmf',
		'yi', 'yo', 'yue',
		'za', 'zea', 'zh', 'zh-classical', 'zh-cn', 'zh-hans', 'zh-hant',
		'zh-hk', 'zh-min-nan', 'zh-mo', 'zh-my', 'zh-sg', 'zh-tw', 'zh-yue',
		'zu',
	}) do
        local expected = fallback.fblist(lang)
        local actual = mwlist(lang)
        self:equals(lang, expected, actual, {varying = 1})
	end
end

return p