| 1 |
// Copyright 2019 Igalia, S.L. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
esid: sec-initializenumberformat |
| 6 |
description: Checks handling of the compactDisplay option to the NumberFormat constructor. |
| 7 |
info: | |
| 8 |
InitializeNumberFormat ( numberFormat, locales, options ) |
| 9 |
|
| 10 |
23. Let signDisplay be ? GetOption(options, "signDisplay", "string", « "auto", "never", "always", "exceptZero" », "auto"). |
| 11 |
24. Set numberFormat.[[SignDisplay]] to signDisplay. |
| 12 |
|
| 13 |
features: [Intl.NumberFormat-unified] |
| 14 |
---*/ |
| 15 |
|
| 16 |
const values = [ |
| 17 |
[undefined, "short"], |
| 18 |
["short"], |
| 19 |
["narrow"], |
| 20 |
["long"], |
| 21 |
]; |
| 22 |
|
| 23 |
for (const [value, expected = value] of values) { |
| 24 |
const nf = new Intl.NumberFormat([], { |
| 25 |
style: "unit", |
| 26 |
unitDisplay: value, |
| 27 |
unit: "hour", |
| 28 |
}); |
| 29 |
const resolvedOptions = nf.resolvedOptions(); |
| 30 |
assert.sameValue("unitDisplay" in resolvedOptions, true); |
| 31 |
assert.sameValue(resolvedOptions.unitDisplay, expected); |
| 32 |
} |
| 33 |
|
| 34 |
for (const [value, expected = value] of values) { |
| 35 |
const nf = new Intl.NumberFormat([], { |
| 36 |
style: "unit", |
| 37 |
unitDisplay: value, |
| 38 |
unit: "percent", |
| 39 |
}); |
| 40 |
const resolvedOptions = nf.resolvedOptions(); |
| 41 |
assert.sameValue("unitDisplay" in resolvedOptions, true); |
| 42 |
assert.sameValue(resolvedOptions.unitDisplay, expected); |
| 43 |
} |
| 44 |
|
| 45 |
for (const [value] of values) { |
| 46 |
const nf = new Intl.NumberFormat([], { |
| 47 |
style: "percent", |
| 48 |
unitDisplay: value, |
| 49 |
}); |
| 50 |
const resolvedOptions = nf.resolvedOptions(); |
| 51 |
assert.sameValue("unitDisplay" in resolvedOptions, false); |
| 52 |
assert.sameValue(resolvedOptions.unitDisplay, undefined); |
| 53 |
} |
| 54 |
|
| 55 |
const invalidValues = [ |
| 56 |
"", |
| 57 |
"Short", |
| 58 |
"s", |
| 59 |
"\u017Fhort", |
| 60 |
]; |
| 61 |
|
| 62 |
for (const unitDisplay of invalidValues) { |
| 63 |
assert.throws(RangeError, () => { |
| 64 |
new Intl.NumberFormat([], { unitDisplay }); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
reportCompare(0, 0); |