Debian Codegraph @

js/src/tests/non262/Intl/DisplayNames/region.js

raw
1 // |reftest| skip-if(!this.hasOwnProperty('Intl'))
2
3 const tests = {
4 "en": {
5 long: {
6 "DE": "Germany",
7 "GB": "United Kingdom",
8 "US": "United States",
9 "FR": "France",
10 },
11 short: {
12 "GB": "UK",
13 "US": "US",
14 },
15 narrow: {},
16 },
17 "de": {
18 long: {
19 "DE": "Deutschland",
20 "GB": "Vereinigtes Königreich",
21 "US": "Vereinigte Staaten",
22 "FR": "Frankreich",
23 },
24 short: {
25 "GB": "UK",
26 "US": "USA",
27 },
28 narrow: {},
29 },
30 "fr": {
31 long: {
32 "DE": "Allemagne",
33 "GB": "Royaume-Uni",
34 "US": "États-Unis",
35 "FR": "France",
36 },
37 short: {
38 "GB": "R.-U.",
39 "US": "É.-U.",
40 },
41 narrow: {},
42 },
43 "zh": {
44 long: {
45 "CN": "中国",
46 "HK": "中国香港特别行政区",
47 },
48 short: {
49 "HK": "香港"
50 },
51 narrow: {},
52 },
53 "ar": {
54 long: {
55 "SA": "المملكة العربية السعودية",
56 "MO": "منطقة ماكاو الإدارية الخاصة",
57 },
58 short: {
59 "MO": "مكاو",
60 },
61 narrow: {},
62 },
63 };
64
65 for (let [locale, localeTests] of Object.entries(tests)) {
66 for (let [style, styleTests] of Object.entries(localeTests)) {
67 let dn = new Intl.DisplayNames(locale, {type: "region", style});
68
69 let resolved = dn.resolvedOptions();
70 assertEq(resolved.locale, locale);
71 assertEq(resolved.style, style);
72 assertEq(resolved.type, "region");
73 assertEq(resolved.fallback, "code");
74
75 let inheritedTests = {...localeTests.long, ...localeTests.short, ...localeTests.narrow};
76 for (let [region, expected] of Object.entries({...inheritedTests, ...styleTests})) {
77 assertEq(dn.of(region), expected);
78
79 // Also works with objects.
80 assertEq(dn.of(Object(region)), expected);
81 }
82 }
83 }
84
85 {
86 let dn = new Intl.DisplayNames("en", {type: "region"});
87
88 // Performs ToString on the input and then validates the stringified result.
89 assertThrowsInstanceOf(() => dn.of(), RangeError);
90 assertThrowsInstanceOf(() => dn.of(null), RangeError);
91 assertThrowsInstanceOf(() => dn.of(Symbol()), TypeError);
92 assertThrowsInstanceOf(() => dn.of(0), RangeError);
93
94 // Throws an error if |code| can't be parsed as a `unicode_region_subtag` production.
95 assertThrowsInstanceOf(() => dn.of("CA-"), RangeError);
96 assertThrowsInstanceOf(() => dn.of("en-CA"), RangeError);
97 }
98
99 // Test fallback behaviour.
100 {
101 let dn1 = new Intl.DisplayNames("en", {type: "region"});
102 let dn2 = new Intl.DisplayNames("en", {type: "region", fallback: "code"});
103 let dn3 = new Intl.DisplayNames("en", {type: "region", fallback: "none"});
104
105 assertEq(dn1.resolvedOptions().fallback, "code");
106 assertEq(dn2.resolvedOptions().fallback, "code");
107 assertEq(dn3.resolvedOptions().fallback, "none");
108
109 // "AA" is not a registered region code.
110 assertEq(dn1.of("AA"), "AA");
111 assertEq(dn2.of("AA"), "AA");
112 assertEq(dn3.of("AA"), undefined);
113
114 // The returned fallback is in canonical case.
115 assertEq(dn1.of("aa"), "AA");
116 assertEq(dn2.of("aa"), "AA");
117 assertEq(dn3.of("aa"), undefined);
118
119 // "998" is canonicalised to "XZ", but "XZ" has no localised names.
120 assertEq(new Intl.Locale("und-998").region, "XZ");
121
122 // Ensure we return the input and not the canonicalised input.
123 assertEq(dn1.of("998"), "998");
124 assertEq(dn2.of("998"), "998");
125 assertEq(dn3.of("998"), undefined);
126
127 // "XZ" should be consistent with "998".
128 assertEq(dn1.of("XZ"), "XZ");
129 assertEq(dn2.of("XZ"), "XZ");
130 assertEq(dn3.of("XZ"), undefined);
131 }
132
133 // Ensure language tag canonicalisation is performed.
134 {
135 let dn = new Intl.DisplayNames("en", {type: "region", fallback: "none"});
136
137 assertEq(dn.of("RU"), "Russia");
138
139 // ICU's canonicalisation supports "SU" -> "RU".
140 assertEq(Intl.getCanonicalLocales("ru-SU")[0], "ru-RU");
141 assertEq(dn.of("SU"), "Russia");
142
143 // ICU's canonicalisation doesn't support "172" -> "RU".
144 assertEq(Intl.getCanonicalLocales("ru-172")[0], "ru-RU");
145 assertEq(dn.of("172"), "Russia");
146 }
147
148 // Test when case isn't canonical.
149 {
150 let dn = new Intl.DisplayNames("en", {type: "region", fallback: "none"});
151
152 assertEq(dn.of("IT"), "Italy");
153 assertEq(dn.of("it"), "Italy");
154 }
155
156 if (typeof reportCompare === "function")
157 reportCompare(true, true);