summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/NumberFormat/use-grouping.js
blob: b10398dc67720dde709440da6ceef98c4abcd7ff (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
// |reftest| skip-if(!this.hasOwnProperty("Intl"))

const tests = {
  // minimumGroupingDigits is one for "en" (English).
  "en": {
    values: [
      {
        value: 1000,
        useGroupings: {
          auto: "1,000",
          always: "1,000",
          min2: "1000",
          "": "1000",
        },
      },
      {
        value: 10000,
        useGroupings: {
          auto: "10,000",
          always: "10,000",
          min2: "10,000",
          "": "10000",
        },
      },
    ],
  },

  // minimumGroupingDigits is two for "pl" (Polish).
  "pl": {
    values: [
      {
        value: 1000,
        useGroupings: {
          auto: "1000",
          always: "1 000",
          min2: "1000",
          "": "1000",
        },
      },
      {
        value: 10000,
        useGroupings: {
          auto: "10 000",
          always: "10 000",
          min2: "10 000",
          "": "10000",
        },
      },
    ],
  },
};

for (let [locale, {options = {}, values}] of Object.entries(tests)) {
  for (let {value, useGroupings} of values) {
    for (let [useGrouping, expected] of Object.entries(useGroupings)) {
      let nf = new Intl.NumberFormat(locale, {...options, useGrouping});
      assertEq(nf.format(value), expected, `locale=${locale}, value=${value}, useGrouping=${useGrouping}`);
    }
  }
}

// Resolved options.
for (let [useGrouping, expected] of [
  [false, false],
  ["", false],
  [0, false],
  [null, false],

  ["auto", "auto"],
  [undefined, "auto"],
  ["true", "auto"],
  ["false", "auto"],

  ["always", "always"],
  [true, "always"],

  ["min2", "min2"],
]) {
  let nf = new Intl.NumberFormat("en", {useGrouping});
  assertEq(nf.resolvedOptions().useGrouping , expected);
}

// Throws a RangeError for unsupported values.
for (let useGrouping of [
  "none",
  "yes",
  "no",
  {},
  123,
  123n,
]) {
  assertThrowsInstanceOf(() => new Intl.NumberFormat("en", {useGrouping}), RangeError);
}

// Throws a TypeError if ToString fails.
for (let useGrouping of [Object.create(null), Symbol()]) {
  assertThrowsInstanceOf(() => new Intl.NumberFormat("en", {useGrouping}), TypeError);
}

if (typeof reportCompare === "function")
  reportCompare(true, true);