summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/NumberFormat/formatToParts.js
blob: 3ac7c3f4d389c6c775bb7c6683f2c4e6e21c5b04 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// |reftest| skip-if(!this.hasOwnProperty("Intl"))
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

//-----------------------------------------------------------------------------
var BUGNUMBER = 1289882;
var summary = "Implement Intl.NumberFormat.prototype.formatToParts";

print(BUGNUMBER + ": " + summary);

//-----------------------------------------------------------------------------

assertEq("formatToParts" in Intl.NumberFormat(), true);

// NOTE: Some of these tests exercise standard behavior (e.g. that format and
//       formatToParts expose the same formatted string).  But much of this,
//       like the exact-formatted-string expectations, is technically
//       implementation-dependent.  This is necessary as a practical matter to
//       properly test the conversion from ICU's nested-field exposure to
//       ECMA-402's sequential-parts exposure.

var {
  Nan, Inf, Integer, Group, Decimal, Fraction,
  MinusSign, PlusSign, PercentSign, Currency, Literal,
} = NumberFormatParts;

//-----------------------------------------------------------------------------

// Test -0's partitioning now that it's not treated like +0.
// https://github.com/tc39/ecma402/pull/232

var deadSimpleFormatter = new Intl.NumberFormat("en-US");

assertParts(deadSimpleFormatter, -0,
            [MinusSign("-"), Integer("0")]);

// Test behavior of a currency with code formatting.
var usdCodeOptions =
  {
    style: "currency",
    currency: "USD",
    currencyDisplay: "code",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  };
var usDollarsCode = new Intl.NumberFormat("en-US", usdCodeOptions);

assertParts(usDollarsCode, 25,
            [Currency("USD"), Literal("\xA0"), Integer("25")]);

// ISO 4217 currency codes are formed from an ISO 3166-1 alpha-2 country code
// followed by a third letter.  ISO 3166 guarantees that no country code
// starting with "X" will ever be assigned.  Stepping carefully around a few
// 4217-designated special "currencies", XQQ will never have a representation.
// Thus, yes: this really is specified to work, as unrecognized or unsupported
// codes pass into the string unmodified.
var xqqCodeOptions =
  {
    style: "currency",
    currency: "XQQ",
    currencyDisplay: "code",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  };
var xqqMoneyCode = new Intl.NumberFormat("en-US", xqqCodeOptions);

assertParts(xqqMoneyCode, 25,
            [Currency("XQQ"), Literal("\xA0"), Integer("25")]);

// Test currencyDisplay: "name".
var usdNameOptions =
  {
    style: "currency",
    currency: "USD",
    currencyDisplay: "name",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  };
var usDollarsName = new Intl.NumberFormat("en-US", usdNameOptions);

assertParts(usDollarsName, 25,
            [Integer("25"), Literal(" "), Currency("US dollars")]);

var usdNameGroupingOptions =
  {
    style: "currency",
    currency: "USD",
    currencyDisplay: "name",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  };
var usDollarsNameGrouping =
  new Intl.NumberFormat("en-US", usdNameGroupingOptions);

assertParts(usDollarsNameGrouping, 12345678,
            [Integer("12"),
             Group(","),
             Integer("345"),
             Group(","),
             Integer("678"),
             Literal(" "),
             Currency("US dollars")]);

// But if the implementation doesn't recognize the currency, the provided code
// is used in place of a proper name, unmolested.
var xqqNameOptions =
  {
    style: "currency",
    currency: "XQQ",
    currencyDisplay: "name",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  };
var xqqMoneyName = new Intl.NumberFormat("en-US", xqqNameOptions);

assertParts(xqqMoneyName, 25,
            [Integer("25"), Literal(" "), Currency("XQQ")]);

// Test some currencies with fractional components.

var usdNameFractionOptions =
  {
    style: "currency",
    currency: "USD",
    currencyDisplay: "name",
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  };
var usdNameFractionFormatter =
  new Intl.NumberFormat("en-US", usdNameFractionOptions);

// The US national surplus (i.e. debt) as of October 18, 2016.  (Replicating
// data from a comment in builtin/Intl/NumberFormat.cpp.)
var usNationalSurplus = -19766580028249.41;

assertParts(usdNameFractionFormatter, usNationalSurplus,
            [MinusSign("-"),
             Integer("19"),
             Group(","),
             Integer("766"),
             Group(","),
             Integer("580"),
             Group(","),
             Integer("028"),
             Group(","),
             Integer("249"),
             Decimal("."),
             Fraction("41"),
             Literal(" "),
             Currency("US dollars")]);

// Percents in various forms.

var usPercentOptions =
  {
    style: "percent",
    minimumFractionDigits: 1,
    maximumFractionDigits: 1,
  };
var usPercentFormatter =
  new Intl.NumberFormat("en-US", usPercentOptions);

assertParts(usPercentFormatter, 0.375,
            [Integer("37"), Decimal("."), Fraction("5"), PercentSign("%")]);

assertParts(usPercentFormatter, -1284.375,
            [MinusSign("-"),
             Integer("128"),
             Group(","),
             Integer("437"),
             Decimal("."),
             Fraction("5"),
             PercentSign("%")]);

assertParts(usPercentFormatter, NaN,
            [Nan("NaN"), PercentSign("%")]);

assertParts(usPercentFormatter, Infinity,
            [Inf("∞"), PercentSign("%")]);

assertParts(usPercentFormatter, -Infinity,
            [MinusSign("-"), Inf("∞"), PercentSign("%")]);

var dePercentOptions =
  {
    style: "percent",
    minimumFractionDigits: 1,
    maximumFractionDigits: 1,
  };
var dePercentFormatter =
  new Intl.NumberFormat("de", dePercentOptions);

assertParts(dePercentFormatter, 0.375,
            [Integer("37"), Decimal(","), Fraction("5"), Literal("\xA0"), PercentSign("%")]);

assertParts(dePercentFormatter, -1284.375,
            [MinusSign("-"),
             Integer("128"),
             Group("."),
             Integer("437"),
             Decimal(","),
             Fraction("5"),
             Literal("\xA0"),
             PercentSign("%")]);

assertParts(dePercentFormatter, NaN,
            [Nan("NaN"), Literal("\xA0"), PercentSign("%")]);

assertParts(dePercentFormatter, Infinity,
            [Inf("∞"), Literal("\xA0"), PercentSign("%")]);

assertParts(dePercentFormatter, -Infinity,
            [MinusSign("-"), Inf("∞"), Literal("\xA0"), PercentSign("%")]);

var arPercentOptions =
  {
    style: "percent",
    minimumFractionDigits: 2,
  };
var arPercentFormatter =
  new Intl.NumberFormat("ar-IQ", arPercentOptions);

assertParts(arPercentFormatter, -135.32,
            [Literal("\u{061C}"),
             MinusSign("-"),
             Integer("١٣"),
             Group("٬"),
             Integer("٥٣٢"),
             Decimal("٫"),
             Fraction("٠٠"),
             PercentSign("٪"),
             Literal("\u{061C}")]);

// Decimals.

var usDecimalOptions =
  {
    style: "decimal",
    maximumFractionDigits: 7 // minimum defaults to 0
  };
var usDecimalFormatter =
  new Intl.NumberFormat("en-US", usDecimalOptions);

assertParts(usDecimalFormatter, 42,
            [Integer("42")]);

assertParts(usDecimalFormatter, 1337,
            [Integer("1"), Group(","), Integer("337")]);

assertParts(usDecimalFormatter, -6.25,
            [MinusSign("-"), Integer("6"), Decimal("."), Fraction("25")]);

assertParts(usDecimalFormatter, -1376.25,
            [MinusSign("-"),
             Integer("1"),
             Group(","),
             Integer("376"),
             Decimal("."),
             Fraction("25")]);

assertParts(usDecimalFormatter, 124816.8359375,
            [Integer("124"),
             Group(","),
             Integer("816"),
             Decimal("."),
             Fraction("8359375")]);

var usNoGroupingDecimalOptions =
  {
    style: "decimal",
    useGrouping: false,
    maximumFractionDigits: 7 // minimum defaults to 0
  };
var usNoGroupingDecimalFormatter =
  new Intl.NumberFormat("en-US", usNoGroupingDecimalOptions);

assertParts(usNoGroupingDecimalFormatter, 1337,
            [Integer("1337")]);

assertParts(usNoGroupingDecimalFormatter, -6.25,
            [MinusSign("-"), Integer("6"), Decimal("."), Fraction("25")]);

assertParts(usNoGroupingDecimalFormatter, -1376.25,
            [MinusSign("-"),
             Integer("1376"),
             Decimal("."),
             Fraction("25")]);

assertParts(usNoGroupingDecimalFormatter, 124816.8359375,
            [Integer("124816"),
             Decimal("."),
             Fraction("8359375")]);

var deDecimalOptions =
  {
    style: "decimal",
    maximumFractionDigits: 7 // minimum defaults to 0
  };
var deDecimalFormatter =
  new Intl.NumberFormat("de-DE", deDecimalOptions);

assertParts(deDecimalFormatter, 42,
            [Integer("42")]);

assertParts(deDecimalFormatter, 1337,
            [Integer("1"), Group("."), Integer("337")]);

assertParts(deDecimalFormatter, -6.25,
            [MinusSign("-"), Integer("6"), Decimal(","), Fraction("25")]);

assertParts(deDecimalFormatter, -1376.25,
            [MinusSign("-"),
             Integer("1"),
             Group("."),
             Integer("376"),
             Decimal(","),
             Fraction("25")]);

assertParts(deDecimalFormatter, 124816.8359375,
            [Integer("124"),
             Group("."),
             Integer("816"),
             Decimal(","),
             Fraction("8359375")]);

var deNoGroupingDecimalOptions =
  {
    style: "decimal",
    useGrouping: false,
    maximumFractionDigits: 7 // minimum defaults to 0
  };
var deNoGroupingDecimalFormatter =
  new Intl.NumberFormat("de-DE", deNoGroupingDecimalOptions);

assertParts(deNoGroupingDecimalFormatter, 1337,
            [Integer("1337")]);

assertParts(deNoGroupingDecimalFormatter, -6.25,
            [MinusSign("-"), Integer("6"), Decimal(","), Fraction("25")]);

assertParts(deNoGroupingDecimalFormatter, -1376.25,
            [MinusSign("-"),
             Integer("1376"),
             Decimal(","),
             Fraction("25")]);

assertParts(deNoGroupingDecimalFormatter, 124816.8359375,
            [Integer("124816"),
             Decimal(","),
             Fraction("8359375")]);

//-----------------------------------------------------------------------------

if (typeof reportCompare === "function")
    reportCompare(0, 0, 'ok');

print("Tests complete");