summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/intl402/NumberFormat/constructor-unit.js
blob: b14981cf7a80967b31f33e28e80e9a5c02af1145 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2019 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-initializenumberformat
description: Checks handling of the unit style.
features: [Intl.NumberFormat-unified]
---*/

assert.throws(TypeError, () => {
  new Intl.NumberFormat([], {
    style: "unit",
  })
});

for (const unit of ["test", "MILE", "kB"]) {
  // Throws RangeError for invalid unit identifier.
  for (const style of [undefined, "decimal", "unit"]) {
    assert.throws(RangeError, () => {
      new Intl.NumberFormat([], { style, unit })
    }, `{ style: ${style}, unit: ${unit} }`);
  }

  const style = "currency";

  // Throws TypeError because "currency" option is missing.
  assert.throws(TypeError, () => {
    new Intl.NumberFormat([], { style, unit })
  }, `{ style: ${style}, unit: ${unit} }`);

  // Throws RangeError for invalid unit identifier.
  assert.throws(RangeError, () => {
    new Intl.NumberFormat([], { style, unit, currency: "USD" })
  }, `{ style: ${style}, unit: ${unit} }`);
}

const nf = new Intl.NumberFormat([], {
  style: "percent",
});
assert.sameValue(nf.resolvedOptions().style, "percent");
assert.sameValue("unit" in nf.resolvedOptions(), false);
assert.sameValue(nf.resolvedOptions().unit, undefined);

function check(unit) {
  const nf = new Intl.NumberFormat([], {
    style: "unit",
    unit,
  });
  const options = nf.resolvedOptions();
  assert.sameValue(options.style, "unit");
  assert.sameValue(options.unit, unit);
}

const units = [
  "acre",
  "bit",
  "byte",
  "celsius",
  "centimeter",
  "day",
  "degree",
  "fahrenheit",
  "fluid-ounce",
  "foot",
  "gallon",
  "gigabit",
  "gigabyte",
  "gram",
  "hectare",
  "hour",
  "inch",
  "kilobit",
  "kilobyte",
  "kilogram",
  "kilometer",
  "liter",
  "megabit",
  "megabyte",
  "meter",
  "mile",
  "mile-scandinavian",
  "millimeter",
  "milliliter",
  "millisecond",
  "minute",
  "month",
  "ounce",
  "percent",
  "petabyte",
  "pound",
  "second",
  "stone",
  "terabit",
  "terabyte",
  "week",
  "yard",
  "year",
];

for (const simpleUnit of units) {
  check(simpleUnit);
  for (const simpleUnit2 of units) {
    check(simpleUnit + "-per-" + simpleUnit2);
    check(simpleUnit2 + "-per-" + simpleUnit);
  }
}

reportCompare(0, 0);