summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/intl402/NumberFormat/prototype/format/value-tonumber.js
blob: 3785b291033c3a47985f8e5bb1f6f5801646aec5 (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
// Copyright (C) 2018 Ujjwal Sharma. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

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

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

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

const nf = new Intl.NumberFormat();

toNumberResults.forEach(pair => {
  const [value, result] = pair;
  assert.sameValue(nf.format(value), nf.format(result));
});

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

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

reportCompare(0, 0);