summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/intl402/NumberFormat/prototype/formatRangeToParts/en-US.js
blob: a23cd17f402e338a905d23c47fb028a9b1151162 (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
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-Intl.NumberFormat-formatRangeToParts
description: Basic tests for the en-US output of formatRangeToParts()
locale: [en-US]
features: [Intl.NumberFormat-v3]
includes: [propertyHelper.js]
---*/

// Utils functions
function* zip(a, b) {
  assert.sameValue(a.length, b.length);
  for (let i = 0; i < a.length; ++i) {
    yield [i, a[i], b[i]];
  }
}

function compare(actual, expected) {
  for (const [i, actualEntry, expectedEntry] of zip(actual, expected)) {
    // assertions
    assert.sameValue(actualEntry.type, expectedEntry.type, `type for entry ${i}`);
    assert.sameValue(actualEntry.value, expectedEntry.value, `value for entry ${i}`);
    assert.sameValue(actualEntry.source, expectedEntry.source, `source for entry ${i}`);

    //  1.1.25_4.a  Let O be ObjectCreate(%ObjectPrototype%).
    assert.sameValue(Object.getPrototypeOf(actualEntry), Object.prototype, `prototype for entry ${i}`);
    //  1.1.25_4.b Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]])
    verifyProperty(actualEntry, 'type', {  enumerable: true, writable: true, configurable: true });
    //  1.1.25_4.c Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
    verifyProperty(actualEntry, 'value', {  enumerable: true, writable: true, configurable: true });
    //  1.1.25_4.d Perform ! CreateDataPropertyOrThrow(O, "source", part.[[Source]]).
    verifyProperty(actualEntry, 'source', {  enumerable: true, writable: true, configurable: true });
  }
}

// Basic example test en-US
const nf = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
  maximumFractionDigits: 0,
});

compare(nf.formatRangeToParts(3, 5), [
  {type: "currency", value: "$", source: "startRange"},
  {type: "integer", value: "3", source: "startRange"},
  {type: "literal", value: " – ", source: "shared"},
  {type: "currency", value: "$", source: "endRange"},
  {type: "integer", value: "5", source: "endRange"}
]);

compare(nf.formatRangeToParts(1, 1), [
  {type: 'approximatelySign', value: '~', source: 'shared'},
  {type: 'currency', value: '$', source: 'shared'},
  {type: 'integer', value: '1', source: 'shared'}
]);

compare(nf.formatRangeToParts(2.999, 3.001), [
  {type: 'approximatelySign', value: '~', source: 'shared'},
  {type: 'currency', value: '$', source: 'shared'},
  {type: 'integer', value: '3', source: 'shared'}
]);



reportCompare(0, 0);