summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/intl402/NumberFormat/prototype/formatToParts/value-tonumber.js
blob: c2601b026fd86ef4e37e6d9921aed13889a0b9f7 (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
// Copyright 2016 Mozilla Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.

/*---
esid: sec-intl.numberformat.prototype.formattoparts
description: >
  Tests that Intl.NumberFormat.prototype.formatToParts converts
  its argument (called value) to a number using ToNumber (7.1.3).
info: |
  11.1.4 Number Format Functions

  4. Let x be ? ToNumber(value).
features: [Symbol]
---*/

const toNumberResults = [
  [undefined, NaN],
  [null, +0],
  [true, 1],
  [false, +0],
  ['42', 42],
  ['foo', NaN]
];

const nf = new Intl.NumberFormat();

function assertSameParts(actual, expected) {
  assert.sameValue(actual.length, expected.length);
  for (let i = 0; i < expected.length; ++i) {
    assert.sameValue(actual[i].type, expected[i].type);
    assert.sameValue(actual[i].value, expected[i].value);
  }
}

toNumberResults.forEach(pair => {
  const [value, result] = pair;
  assertSameParts(nf.formatToParts(value), nf.formatToParts(result));
});

let count = 0;
const dummy = {};
dummy[Symbol.toPrimitive] = hint => (hint === 'number' ? ++count : NaN);
assertSameParts(nf.formatToParts(dummy), nf.formatToParts(count));
assert.sameValue(count, 1);

assert.throws(
  TypeError,
  () => nf.formatToParts(Symbol()),
  "ToNumber(arg) throws a TypeError when arg is of type 'Symbol'"
);

reportCompare(0, 0);