aboutsummaryrefslogtreecommitdiffstats
path: root/test/suite/intl402/ch09/9.2/9.2.1_3.js
blob: e403b17653eb123db126f3f5e85a5684f6c874e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2012 Mozilla Corporation. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/**
 * @description Tests that a single string instead of a locale list is treated
 *     as the locale list containing that string.
 * @author Norbert Lindenberg
 */

$INCLUDE("testIntl.js");

var validAndInvalidLanguageTags = [
    "de", // ISO 639 language code
    "de-DE", // + ISO 3166-1 country code
    "DE-de", // tags are case-insensitive
    "cmn", // ISO 639 language code
    "cmn-Hans", // + script code
    "CMN-hANS", // tags are case-insensitive
    "cmn-hans-cn", // + ISO 3166-1 country code
    "es-419", // + UN M.49 region code
    "es-419-u-nu-latn-cu-bob", // + Unicode locale extension sequence
    "i-klingon", // grandfathered tag
    "cmn-hans-cn-t-ca-u-ca-x-t-u", // singleton subtags can also be used as private use subtags
    "enochian-enochian", // language and variant subtags may be the same
    "de-gregory-u-ca-gregory", // variant and extension subtags may be the same
    "de_DE",
    "DE_de",
    "cmn_Hans",
    "cmn-hans_cn",
    "es_419",
    "es-419-u-nu-latn-cu_bob",
    "i_klingon",
    "cmn-hans-cn-t-ca-u-ca-x_t-u",
    "enochian_enochian",
    "de-gregory_u-ca-gregory",
    "i", // singleton alone
    "x", // private use without subtag
    "u", // extension singleton in first place
    "419", // region code in first place
    "u-nu-latn-cu-bob", // extension sequence without language
    "hans-cmn-cn", // "hans" could theoretically be a 4-letter language code,
                   // but those can't be followed by extlang codes.
    "cmn-hans-cn-u-u", // duplicate singleton
    "cmn-hans-cn-t-u-ca-u", // duplicate singleton
    "de-gregory-gregory" // duplicate variant
];

testWithIntlConstructors(function (Constructor) {
    validAndInvalidLanguageTags.forEach(function (locale) {
        var obj1, obj2, locale1, locale2, error1, error2;
        try {
            obj1 = new Constructor(locale);
            locale1 = obj1.resolvedOptions().locale;
        } catch (e) {
            error1 = e;
        }
        try {
            obj2 = new Constructor([locale]);
            locale2 = obj2.resolvedOptions().locale;
        } catch (e) {
            error2 = e;
        }

        if ((error1 === undefined) !== (error2 === undefined)) {
            if (error1 === undefined) {
                $ERROR("Single locale string " + locale +
                    " was accepted, but locale list containing that string wasn't.");
            } else {
                $ERROR("Single locale string " + locale +
                    " was rejected, but locale list containing that string wasn't.");
            }
        } else if (error1 === undefined) {
             if (locale1 !== locale2) {
                $ERROR("Single locale string " + locale + " results in " + locale1 +
                    ", but locale list [" + locale + "] results in " + locale2 + ".");
             }
        } else {
            if (error1.name !== error2.name) {
                $ERROR("Single locale string " + locale + " results in error " + error1.name +
                    ", but locale list [" + locale + "] results in error " + error2.name + ".");
             }
        }
    });
    
    return true;
});