summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/intl402/RelativeTimeFormat/prototype/formatToParts/en-us-numeric-auto.js
blob: fdf9ab648770de9d0e8c38aebbaef2c3d5dfc510 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-Intl.RelativeTimeFormat.prototype.formatToParts
description: Checks the behavior of Intl.RelativeTimeFormat.prototype.formatToParts() in English.
features: [Intl.RelativeTimeFormat]
locale: [en-US]
---*/

function verifyFormatParts(actual, expected, message) {
  assert.sameValue(actual.length, expected.length, `${message}: length`);

  for (let i = 0; i < actual.length; ++i) {
    assert.sameValue(actual[i].type, expected[i].type, `${message}: parts[${i}].type`);
    assert.sameValue(actual[i].value, expected[i].value, `${message}: parts[${i}].value`);
    assert.sameValue(actual[i].unit, expected[i].unit, `${message}: parts[${i}].unit`);
  }
}

function expected(key, unit, default_) {
  // https://www.unicode.org/cldr/charts/33/summary/en.html#1530
  const exceptions = {
    "year": {
      "-1": "last year",
      "0": "this year",
      "1": "next year",
    },
    "quarter": {
      "-1": "last quarter",
      "0": "this quarter",
      "1": "next quarter",
    },
    "month": {
      "-1": "last month",
      "0": "this month",
      "1": "next month",
    },
    "week": {
      "-1": "last week",
      "0": "this week",
      "1": "next week",
    },
    "day": {
      "-1": "yesterday",
      "0": "today",
      "1": "tomorrow",
    },
    "hour": {
      "0": "this hour",
    },
    "minute": {
      "0": "this minute",
    },
    "second": {
      "0": "now",
    },
  };

  const exception = exceptions[unit] || {};
  if (key in exception) {
    return [
      { "type": "literal", "value": exception[key] },
    ]
  }

  return default_;
}

const units = [
  "second",
  "minute",
  "hour",
  "day",
  "week",
  "month",
  "quarter",
  "year",
];

const rtf = new Intl.RelativeTimeFormat("en-US", { "numeric": "auto" });

assert.sameValue(typeof rtf.formatToParts, "function", "formatToParts should be supported");

for (const unit of units) {
  verifyFormatParts(rtf.formatToParts(1000, unit), [
    { "type": "literal", "value": "in " },
    { "type": "integer", "value": "1", "unit": unit },
    { "type": "group", "value": ",", "unit": unit },
    { "type": "integer", "value": "000", "unit": unit },
    { "type": "literal", "value": ` ${unit}s` },
  ], `formatToParts(1000, ${unit})`);

  verifyFormatParts(rtf.formatToParts(10, unit), [
    { "type": "literal", "value": "in " },
    { "type": "integer", "value": "10", "unit": unit },
    { "type": "literal", "value": ` ${unit}s` },
  ], `formatToParts(10, ${unit})`);

  verifyFormatParts(rtf.formatToParts(2, unit), [
    { "type": "literal", "value": "in " },
    { "type": "integer", "value": "2", "unit": unit },
    { "type": "literal", "value": ` ${unit}s` },
  ], `formatToParts(2, ${unit})`);

  verifyFormatParts(rtf.formatToParts(1, unit), expected("1", unit, [
    { "type": "literal", "value": "in " },
    { "type": "integer", "value": "1", "unit": unit },
    { "type": "literal", "value": ` ${unit}` },
  ]), `formatToParts(1, ${unit})`);

  verifyFormatParts(rtf.formatToParts(0, unit), expected("0", unit, [
    { "type": "literal", "value": "in " },
    { "type": "integer", "value": "0", "unit": unit },
    { "type": "literal", "value": ` ${unit}s` },
  ]), `formatToParts(0, ${unit})`);

  verifyFormatParts(rtf.formatToParts(-0, unit), expected("0", unit, [
    { "type": "integer", "value": "0", "unit": unit },
    { "type": "literal", "value": ` ${unit}s ago` },
  ]), `formatToParts(-0, ${unit})`);

  verifyFormatParts(rtf.formatToParts(-1, unit), expected("-1", unit, [
    { "type": "integer", "value": "1", "unit": unit },
    { "type": "literal", "value": ` ${unit} ago` },
  ]), `formatToParts(-1, ${unit})`);

  verifyFormatParts(rtf.formatToParts(-2, unit), [
    { "type": "integer", "value": "2", "unit": unit },
    { "type": "literal", "value": ` ${unit}s ago` },
  ], `formatToParts(-2, ${unit})`);

  verifyFormatParts(rtf.formatToParts(-10, unit), [
    { "type": "integer", "value": "10", "unit": unit },
    { "type": "literal", "value": ` ${unit}s ago` },
  ], `formatToParts(-10, ${unit})`);

  verifyFormatParts(rtf.formatToParts(-1000, unit), [
    { "type": "integer", "value": "1", "unit": unit },
    { "type": "group", "value": ",", "unit": unit },
    { "type": "integer", "value": "000", "unit": unit },
    { "type": "literal", "value": ` ${unit}s ago` },
  ], `formatToParts(-1000, ${unit})`);

  verifyFormatParts(rtf.formatToParts(123456.78, unit), [
    { "type": "literal", "value": "in " },
    { "type": "integer", "value": "123", "unit": unit },
    { "type": "group", "value": ",", "unit": unit },
    { "type": "integer", "value": "456", "unit": unit },
    { "type": "decimal", "value": ".", "unit": unit },
    { "type": "fraction", "value": "78", "unit": unit },
    { "type": "literal", "value": ` ${unit}s` },
  ], `formatToParts(123456.78, ${unit})`);
}

reportCompare(0, 0);