| 1 |
// |reftest| skip-if(!this.hasOwnProperty('Intl')) |
| 2 |
|
| 3 |
const tests = { |
| 4 |
"en": { |
| 5 |
long: { |
| 6 |
"Latn": "Latin", |
| 7 |
"Hant": "Traditional Han", |
| 8 |
"Hans": "Simplified Han", |
| 9 |
"Cans": "Unified Canadian Aboriginal Syllabics", |
| 10 |
}, |
| 11 |
short: { |
| 12 |
"Hant": "Traditional", |
| 13 |
"Hans": "Simplified", |
| 14 |
"Cans": "UCAS", |
| 15 |
}, |
| 16 |
narrow: {}, |
| 17 |
}, |
| 18 |
"de": { |
| 19 |
long: { |
| 20 |
"Latn": "Lateinisch", |
| 21 |
"Hant": "Traditionelles Chinesisch", |
| 22 |
"Hans": "Vereinfachtes Chinesisch", |
| 23 |
}, |
| 24 |
short: { |
| 25 |
"Hant": "Traditionell", |
| 26 |
"Hans": "Vereinfacht", |
| 27 |
}, |
| 28 |
narrow: {}, |
| 29 |
}, |
| 30 |
"fr": { |
| 31 |
long: { |
| 32 |
"Latn": "latin", |
| 33 |
"Hant": "sinogrammes traditionnels", |
| 34 |
"Hans": "sinogrammes simplifiés", |
| 35 |
}, |
| 36 |
short: { |
| 37 |
"Hant": "traditionnel", |
| 38 |
"Hans": "simplifié", |
| 39 |
}, |
| 40 |
narrow: {}, |
| 41 |
}, |
| 42 |
"zh": { |
| 43 |
long: { |
| 44 |
"Latn": "拉丁文", |
| 45 |
"Hant": "繁体中文", |
| 46 |
"Hans": "简体中文", |
| 47 |
}, |
| 48 |
short: { |
| 49 |
"Hant": "繁体", |
| 50 |
"Hans": "简体", |
| 51 |
}, |
| 52 |
narrow: {}, |
| 53 |
}, |
| 54 |
"ar": { |
| 55 |
long: { |
| 56 |
"Latn": "اللاتينية", |
| 57 |
"Arab": "العربية", |
| 58 |
"Hant": "الهان التقليدية", |
| 59 |
"Hans": "الهان المبسطة", |
| 60 |
}, |
| 61 |
short: { |
| 62 |
"Hant": "التقليدية", |
| 63 |
"Hans": "المبسطة", |
| 64 |
}, |
| 65 |
narrow: {}, |
| 66 |
}, |
| 67 |
}; |
| 68 |
|
| 69 |
for (let [locale, localeTests] of Object.entries(tests)) { |
| 70 |
for (let [style, styleTests] of Object.entries(localeTests)) { |
| 71 |
let dn = new Intl.DisplayNames(locale, {type: "script", style}); |
| 72 |
|
| 73 |
let resolved = dn.resolvedOptions(); |
| 74 |
assertEq(resolved.locale, locale); |
| 75 |
assertEq(resolved.style, style); |
| 76 |
assertEq(resolved.type, "script"); |
| 77 |
assertEq(resolved.fallback, "code"); |
| 78 |
|
| 79 |
let inheritedTests = {...localeTests.long, ...localeTests.short, ...localeTests.narrow}; |
| 80 |
for (let [script, expected] of Object.entries({...inheritedTests, ...styleTests})) { |
| 81 |
assertEq(dn.of(script), expected); |
| 82 |
|
| 83 |
// Also works with objects. |
| 84 |
assertEq(dn.of(Object(script)), expected); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
{ |
| 90 |
let dn = new Intl.DisplayNames("en", {type: "script"}); |
| 91 |
|
| 92 |
// Performs ToString on the input and then validates the stringified result. |
| 93 |
assertThrowsInstanceOf(() => dn.of(), RangeError); |
| 94 |
assertThrowsInstanceOf(() => dn.of(Symbol()), TypeError); |
| 95 |
assertThrowsInstanceOf(() => dn.of(0), RangeError); |
| 96 |
|
| 97 |
// ToString(null) = "null", which passes `unicode_script_subtag`. |
| 98 |
dn.of(null); // no error |
| 99 |
|
| 100 |
// Throws an error if |code| can't be parsed as a `unicode_script_subtag` production. |
| 101 |
assertThrowsInstanceOf(() => dn.of("latn-"), RangeError); |
| 102 |
assertThrowsInstanceOf(() => dn.of("en-latn"), RangeError); |
| 103 |
} |
| 104 |
|
| 105 |
// Test fallback behaviour. |
| 106 |
{ |
| 107 |
let dn1 = new Intl.DisplayNames("en", {type: "script"}); |
| 108 |
let dn2 = new Intl.DisplayNames("en", {type: "script", fallback: "code"}); |
| 109 |
let dn3 = new Intl.DisplayNames("en", {type: "script", fallback: "none"}); |
| 110 |
|
| 111 |
assertEq(dn1.resolvedOptions().fallback, "code"); |
| 112 |
assertEq(dn2.resolvedOptions().fallback, "code"); |
| 113 |
assertEq(dn3.resolvedOptions().fallback, "none"); |
| 114 |
|
| 115 |
// "Aaaa" is not a registered script code. |
| 116 |
assertEq(dn1.of("Aaaa"), "Aaaa"); |
| 117 |
assertEq(dn2.of("Aaaa"), "Aaaa"); |
| 118 |
assertEq(dn3.of("Aaaa"), undefined); |
| 119 |
|
| 120 |
// The returned fallback is in canonical case. |
| 121 |
assertEq(dn1.of("aaaa"), "Aaaa"); |
| 122 |
assertEq(dn2.of("aaaa"), "Aaaa"); |
| 123 |
assertEq(dn3.of("aaaa"), undefined); |
| 124 |
} |
| 125 |
|
| 126 |
// Test when case isn't canonical. |
| 127 |
{ |
| 128 |
let dn = new Intl.DisplayNames("en", {type: "script", fallback: "none"}); |
| 129 |
|
| 130 |
assertEq(dn.of("LATN"), "Latin"); |
| 131 |
} |
| 132 |
|
| 133 |
if (typeof reportCompare === "function") |
| 134 |
reportCompare(true, true); |