Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | // Copyright 2011-2012 Norbert Lindenberg. All rights reserved. |
michael@0 | 2 | // Copyright 2012 Mozilla Corporation. All rights reserved. |
michael@0 | 3 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * @description Tests that language tags are canonicalized in return values. |
michael@0 | 7 | * @author Norbert Lindenberg |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | $INCLUDE("testIntl.js"); |
michael@0 | 11 | |
michael@0 | 12 | var canonicalizedTags = { |
michael@0 | 13 | "de": ["de"], |
michael@0 | 14 | "de-DE": ["de-DE", "de"], |
michael@0 | 15 | "DE-de": ["de-DE", "de"], |
michael@0 | 16 | "cmn": ["cmn"], |
michael@0 | 17 | "CMN-hANS": ["cmn-Hans", "cmn"], |
michael@0 | 18 | "cmn-hans-cn": ["cmn-Hans-CN", "cmn-Hans", "cmn"], |
michael@0 | 19 | "es-419": ["es-419", "es"], |
michael@0 | 20 | "es-419-u-nu-latn": ["es-419-u-nu-latn", "es-419", "es", "es-u-nu-latn"], |
michael@0 | 21 | // -u-ca is incomplete, so it will not show up in resolvedOptions().locale |
michael@0 | 22 | "cmn-hans-cn-u-ca-t-ca-x-t-u": ["cmn-Hans-CN-t-ca-u-ca-x-t-u", "cmn-Hans-CN-t-ca-x-t-u", "cmn-Hans-CN-t-ca-x-t", "cmn-Hans-CN-t-ca", "cmn-Hans-CN", "cmn-Hans", "cmn"], |
michael@0 | 23 | "enochian-enochian": ["enochian-enochian", "enochian"], |
michael@0 | 24 | "de-gregory-u-ca-gregory": ["de-gregory-u-ca-gregory", "de-gregory", "de-u-ca-gregory", "de"], |
michael@0 | 25 | "no-nyn": ["nn"], |
michael@0 | 26 | "i-klingon": ["tlh"], |
michael@0 | 27 | "sgn-GR": ["gss"], |
michael@0 | 28 | "ji": ["yi"], |
michael@0 | 29 | "de-DD": ["de-DE", "de"], |
michael@0 | 30 | "zh-hak-CN": ["hak-CN", "hak"], |
michael@0 | 31 | "sgn-ils": ["ils"], |
michael@0 | 32 | "in": ["id"], |
michael@0 | 33 | "x-foo": ["x-foo"] |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | // make sure the data above is correct |
michael@0 | 37 | Object.getOwnPropertyNames(canonicalizedTags).forEach(function (tag) { |
michael@0 | 38 | canonicalizedTags[tag].forEach(function (canonicalTag) { |
michael@0 | 39 | if (!isCanonicalizedStructurallyValidLanguageTag(canonicalTag)) { |
michael@0 | 40 | $ERROR("Test data \"" + canonicalTag + "\" is not canonicalized and structurally valid language tag."); |
michael@0 | 41 | } |
michael@0 | 42 | }); |
michael@0 | 43 | }); |
michael@0 | 44 | |
michael@0 | 45 | // now the actual test |
michael@0 | 46 | testWithIntlConstructors(function (Constructor) { |
michael@0 | 47 | var defaultLocale = new Constructor().resolvedOptions().locale; |
michael@0 | 48 | Object.getOwnPropertyNames(canonicalizedTags).forEach(function (tag) { |
michael@0 | 49 | // use lookup locale matcher to keep the set of possible return values predictable |
michael@0 | 50 | |
michael@0 | 51 | // Variant 1: construct an object and see whether its locale is canonicalized. |
michael@0 | 52 | // In this variant, shortened forms or the default locale may be returned |
michael@0 | 53 | var object = new Constructor([tag], {localeMatcher: "lookup"}); |
michael@0 | 54 | var locale = object.resolvedOptions().locale; |
michael@0 | 55 | if (canonicalizedTags[tag].indexOf(locale) === -1 && locale !== defaultLocale) { |
michael@0 | 56 | $ERROR("For " + tag + " got " + locale + "; expected one of " + |
michael@0 | 57 | canonicalizedTags[tag].join(", ") + "."); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | // Variant 2: get the supported locales. If the tag is supported, it should be returned canonicalized but unshortened |
michael@0 | 61 | var supported = Constructor.supportedLocalesOf([tag]); |
michael@0 | 62 | if (supported.length > 0 && supported[0] !== canonicalizedTags[tag][0]) { |
michael@0 | 63 | $ERROR("For " + tag + " got " + supported[0] + "; expected " + |
michael@0 | 64 | canonicalizedTags[tag][0] + "."); |
michael@0 | 65 | } |
michael@0 | 66 | }); |
michael@0 | 67 | return true; |
michael@0 | 68 | }); |
michael@0 | 69 |