| 1 |
// |reftest| skip-if(!this.hasOwnProperty('Intl')) |
| 2 |
|
| 3 |
const tests = { |
| 4 |
"en": { |
| 5 |
long: { |
| 6 |
"USD": "US Dollar", |
| 7 |
"EUR": "Euro", |
| 8 |
"FRF": "French Franc", |
| 9 |
"CNY": "Chinese Yuan", |
| 10 |
"XAU": "Gold", |
| 11 |
}, |
| 12 |
short: { |
| 13 |
"USD": "$", |
| 14 |
"EUR": "€", |
| 15 |
"FRF": "FRF", |
| 16 |
"CNY": "CN¥", |
| 17 |
"XAU": "XAU", |
| 18 |
}, |
| 19 |
narrow: { |
| 20 |
"CNY": "¥", |
| 21 |
}, |
| 22 |
}, |
| 23 |
"de": { |
| 24 |
long: { |
| 25 |
"USD": "US-Dollar", |
| 26 |
"EUR": "Euro", |
| 27 |
"FRF": "Französischer Franc", |
| 28 |
"CNY": "Renminbi Yuan", |
| 29 |
"XAU": "Unze Gold", |
| 30 |
}, |
| 31 |
short: { |
| 32 |
"USD": "$", |
| 33 |
"EUR": "€", |
| 34 |
"FRF": "FRF", |
| 35 |
"CNY": "CN¥", |
| 36 |
"XAU": "XAU", |
| 37 |
}, |
| 38 |
narrow: { |
| 39 |
"CNY": "¥", |
| 40 |
}, |
| 41 |
}, |
| 42 |
"fr": { |
| 43 |
long: { |
| 44 |
"USD": "dollar des États-Unis", |
| 45 |
"EUR": "euro", |
| 46 |
"FRF": "franc français", |
| 47 |
"CNY": "yuan renminbi chinois", |
| 48 |
"XAU": "or", |
| 49 |
}, |
| 50 |
short: { |
| 51 |
"USD": "$US", |
| 52 |
"EUR": "€", |
| 53 |
"FRF": "F", |
| 54 |
"CNY": "CNY", |
| 55 |
"XAU": "XAU", |
| 56 |
}, |
| 57 |
narrow: { |
| 58 |
"USD": "$", |
| 59 |
"CNY": "¥", |
| 60 |
}, |
| 61 |
}, |
| 62 |
"zh": { |
| 63 |
long: { |
| 64 |
"USD": "美元", |
| 65 |
"EUR": "欧元", |
| 66 |
"FRF": "法国法郎", |
| 67 |
"CNY": "人民币", |
| 68 |
"XAU": "黄金", |
| 69 |
}, |
| 70 |
short: { |
| 71 |
"USD": "US$", |
| 72 |
"EUR": "€", |
| 73 |
"FRF": "FRF", |
| 74 |
"CNY": "¥", |
| 75 |
"XAU": "XAU", |
| 76 |
}, |
| 77 |
narrow: { |
| 78 |
"USD": "$", |
| 79 |
}, |
| 80 |
}, |
| 81 |
}; |
| 82 |
|
| 83 |
for (let [locale, localeTests] of Object.entries(tests)) { |
| 84 |
for (let [style, styleTests] of Object.entries(localeTests)) { |
| 85 |
let dn = new Intl.DisplayNames(locale, {type: "currency", style}); |
| 86 |
|
| 87 |
let resolved = dn.resolvedOptions(); |
| 88 |
assertEq(resolved.locale, locale); |
| 89 |
assertEq(resolved.style, style); |
| 90 |
assertEq(resolved.type, "currency"); |
| 91 |
assertEq(resolved.fallback, "code"); |
| 92 |
|
| 93 |
let inheritedTests = {...localeTests.long, ...localeTests.short, ...localeTests.narrow}; |
| 94 |
for (let [currency, expected] of Object.entries({...inheritedTests, ...styleTests})) { |
| 95 |
assertEq(dn.of(currency), expected); |
| 96 |
|
| 97 |
// Also works with objects. |
| 98 |
assertEq(dn.of(Object(currency)), expected); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
{ |
| 104 |
let dn = new Intl.DisplayNames("en", {type: "currency"}); |
| 105 |
|
| 106 |
// Performs ToString on the input and then validates the stringified result. |
| 107 |
assertThrowsInstanceOf(() => dn.of(), RangeError); |
| 108 |
assertThrowsInstanceOf(() => dn.of(null), RangeError); |
| 109 |
assertThrowsInstanceOf(() => dn.of(Symbol()), TypeError); |
| 110 |
assertThrowsInstanceOf(() => dn.of(0), RangeError); |
| 111 |
|
| 112 |
// Throws an error if |code| isn't a well-formed currency code. |
| 113 |
assertThrowsInstanceOf(() => dn.of("us"), RangeError); |
| 114 |
assertThrowsInstanceOf(() => dn.of("euro"), RangeError); |
| 115 |
assertThrowsInstanceOf(() => dn.of("€uro"), RangeError); |
| 116 |
} |
| 117 |
|
| 118 |
// Test fallback behaviour. |
| 119 |
{ |
| 120 |
let dn1 = new Intl.DisplayNames("en", {type: "currency"}); |
| 121 |
let dn2 = new Intl.DisplayNames("en", {type: "currency", fallback: "code"}); |
| 122 |
let dn3 = new Intl.DisplayNames("en", {type: "currency", fallback: "none"}); |
| 123 |
|
| 124 |
assertEq(dn1.resolvedOptions().fallback, "code"); |
| 125 |
assertEq(dn2.resolvedOptions().fallback, "code"); |
| 126 |
assertEq(dn3.resolvedOptions().fallback, "none"); |
| 127 |
|
| 128 |
// "AAA" is not a registered currency code. |
| 129 |
assertEq(dn1.of("AAA"), "AAA"); |
| 130 |
assertEq(dn2.of("AAA"), "AAA"); |
| 131 |
assertEq(dn3.of("AAA"), undefined); |
| 132 |
|
| 133 |
// The returned fallback is in canonical case. |
| 134 |
assertEq(dn1.of("aaa"), "AAA"); |
| 135 |
assertEq(dn2.of("aaa"), "AAA"); |
| 136 |
assertEq(dn3.of("aaa"), undefined); |
| 137 |
} |
| 138 |
|
| 139 |
// Test when case isn't canonical. |
| 140 |
{ |
| 141 |
let dn = new Intl.DisplayNames("en", {type: "currency", fallback: "none"}); |
| 142 |
|
| 143 |
assertEq(dn.of("USD"), "US Dollar"); |
| 144 |
assertEq(dn.of("usd"), "US Dollar"); |
| 145 |
} |
| 146 |
|
| 147 |
if (typeof reportCompare === "function") |
| 148 |
reportCompare(true, true); |