Debian Codegraph @

js/src/tests/non262/Intl/NumberFormat/formatToParts.js

raw
1 // |reftest| skip-if(!this.hasOwnProperty("Intl"))
2 // Any copyright is dedicated to the Public Domain.
3 // http://creativecommons.org/licenses/publicdomain/
4
5 //-----------------------------------------------------------------------------
6 var BUGNUMBER = 1289882;
7 var summary = "Implement Intl.NumberFormat.prototype.formatToParts";
8
9 print(BUGNUMBER + ": " + summary);
10
11 //-----------------------------------------------------------------------------
12
13 assertEq("formatToParts" in Intl.NumberFormat(), true);
14
15 // NOTE: Some of these tests exercise standard behavior (e.g. that format and
16 // formatToParts expose the same formatted string). But much of this,
17 // like the exact-formatted-string expectations, is technically
18 // implementation-dependent. This is necessary as a practical matter to
19 // properly test the conversion from ICU's nested-field exposure to
20 // ECMA-402's sequential-parts exposure.
21
22 var {
23 Nan, Inf, Integer, Group, Decimal, Fraction,
24 MinusSign, PlusSign, PercentSign, Currency, Literal,
25 } = NumberFormatParts;
26
27 //-----------------------------------------------------------------------------
28
29 // Test -0's partitioning now that it's not treated like +0.
30 // https://github.com/tc39/ecma402/pull/232
31
32 var deadSimpleFormatter = new Intl.NumberFormat("en-US");
33
34 assertParts(deadSimpleFormatter, -0,
35 [MinusSign("-"), Integer("0")]);
36
37 // Test behavior of a currency with code formatting.
38 var usdCodeOptions =
39 {
40 style: "currency",
41 currency: "USD",
42 currencyDisplay: "code",
43 minimumFractionDigits: 0,
44 maximumFractionDigits: 0,
45 };
46 var usDollarsCode = new Intl.NumberFormat("en-US", usdCodeOptions);
47
48 assertParts(usDollarsCode, 25,
49 [Currency("USD"), Literal("\xA0"), Integer("25")]);
50
51 // ISO 4217 currency codes are formed from an ISO 3166-1 alpha-2 country code
52 // followed by a third letter. ISO 3166 guarantees that no country code
53 // starting with "X" will ever be assigned. Stepping carefully around a few
54 // 4217-designated special "currencies", XQQ will never have a representation.
55 // Thus, yes: this really is specified to work, as unrecognized or unsupported
56 // codes pass into the string unmodified.
57 var xqqCodeOptions =
58 {
59 style: "currency",
60 currency: "XQQ",
61 currencyDisplay: "code",
62 minimumFractionDigits: 0,
63 maximumFractionDigits: 0,
64 };
65 var xqqMoneyCode = new Intl.NumberFormat("en-US", xqqCodeOptions);
66
67 assertParts(xqqMoneyCode, 25,
68 [Currency("XQQ"), Literal("\xA0"), Integer("25")]);
69
70 // Test currencyDisplay: "name".
71 var usdNameOptions =
72 {
73 style: "currency",
74 currency: "USD",
75 currencyDisplay: "name",
76 minimumFractionDigits: 0,
77 maximumFractionDigits: 0,
78 };
79 var usDollarsName = new Intl.NumberFormat("en-US", usdNameOptions);
80
81 assertParts(usDollarsName, 25,
82 [Integer("25"), Literal(" "), Currency("US dollars")]);
83
84 var usdNameGroupingOptions =
85 {
86 style: "currency",
87 currency: "USD",
88 currencyDisplay: "name",
89 minimumFractionDigits: 0,
90 maximumFractionDigits: 0,
91 };
92 var usDollarsNameGrouping =
93 new Intl.NumberFormat("en-US", usdNameGroupingOptions);
94
95 assertParts(usDollarsNameGrouping, 12345678,
96 [Integer("12"),
97 Group(","),
98 Integer("345"),
99 Group(","),
100 Integer("678"),
101 Literal(" "),
102 Currency("US dollars")]);
103
104 // But if the implementation doesn't recognize the currency, the provided code
105 // is used in place of a proper name, unmolested.
106 var xqqNameOptions =
107 {
108 style: "currency",
109 currency: "XQQ",
110 currencyDisplay: "name",
111 minimumFractionDigits: 0,
112 maximumFractionDigits: 0,
113 };
114 var xqqMoneyName = new Intl.NumberFormat("en-US", xqqNameOptions);
115
116 assertParts(xqqMoneyName, 25,
117 [Integer("25"), Literal(" "), Currency("XQQ")]);
118
119 // Test some currencies with fractional components.
120
121 var usdNameFractionOptions =
122 {
123 style: "currency",
124 currency: "USD",
125 currencyDisplay: "name",
126 minimumFractionDigits: 2,
127 maximumFractionDigits: 2,
128 };
129 var usdNameFractionFormatter =
130 new Intl.NumberFormat("en-US", usdNameFractionOptions);
131
132 // The US national surplus (i.e. debt) as of October 18, 2016. (Replicating
133 // data from a comment in builtin/Intl/NumberFormat.cpp.)
134 var usNationalSurplus = -19766580028249.41;
135
136 assertParts(usdNameFractionFormatter, usNationalSurplus,
137 [MinusSign("-"),
138 Integer("19"),
139 Group(","),
140 Integer("766"),
141 Group(","),
142 Integer("580"),
143 Group(","),
144 Integer("028"),
145 Group(","),
146 Integer("249"),
147 Decimal("."),
148 Fraction("41"),
149 Literal(" "),
150 Currency("US dollars")]);
151
152 // Percents in various forms.
153
154 var usPercentOptions =
155 {
156 style: "percent",
157 minimumFractionDigits: 1,
158 maximumFractionDigits: 1,
159 };
160 var usPercentFormatter =
161 new Intl.NumberFormat("en-US", usPercentOptions);
162
163 assertParts(usPercentFormatter, 0.375,
164 [Integer("37"), Decimal("."), Fraction("5"), PercentSign("%")]);
165
166 assertParts(usPercentFormatter, -1284.375,
167 [MinusSign("-"),
168 Integer("128"),
169 Group(","),
170 Integer("437"),
171 Decimal("."),
172 Fraction("5"),
173 PercentSign("%")]);
174
175 assertParts(usPercentFormatter, NaN,
176 [Nan("NaN"), PercentSign("%")]);
177
178 assertParts(usPercentFormatter, Infinity,
179 [Inf("∞"), PercentSign("%")]);
180
181 assertParts(usPercentFormatter, -Infinity,
182 [MinusSign("-"), Inf("∞"), PercentSign("%")]);
183
184 var dePercentOptions =
185 {
186 style: "percent",
187 minimumFractionDigits: 1,
188 maximumFractionDigits: 1,
189 };
190 var dePercentFormatter =
191 new Intl.NumberFormat("de", dePercentOptions);
192
193 assertParts(dePercentFormatter, 0.375,
194 [Integer("37"), Decimal(","), Fraction("5"), Literal("\xA0"), PercentSign("%")]);
195
196 assertParts(dePercentFormatter, -1284.375,
197 [MinusSign("-"),
198 Integer("128"),
199 Group("."),
200 Integer("437"),
201 Decimal(","),
202 Fraction("5"),
203 Literal("\xA0"),
204 PercentSign("%")]);
205
206 assertParts(dePercentFormatter, NaN,
207 [Nan("NaN"), Literal("\xA0"), PercentSign("%")]);
208
209 assertParts(dePercentFormatter, Infinity,
210 [Inf("∞"), Literal("\xA0"), PercentSign("%")]);
211
212 assertParts(dePercentFormatter, -Infinity,
213 [MinusSign("-"), Inf("∞"), Literal("\xA0"), PercentSign("%")]);
214
215 var arPercentOptions =
216 {
217 style: "percent",
218 minimumFractionDigits: 2,
219 };
220 var arPercentFormatter =
221 new Intl.NumberFormat("ar-IQ", arPercentOptions);
222
223 assertParts(arPercentFormatter, -135.32,
224 [Literal("\u{061C}"),
225 MinusSign("-"),
226 Integer("١٣"),
227 Group("٬"),
228 Integer("٥٣٢"),
229 Decimal("٫"),
230 Fraction("٠٠"),
231 PercentSign("٪"),
232 Literal("\u{061C}")]);
233
234 // Decimals.
235
236 var usDecimalOptions =
237 {
238 style: "decimal",
239 maximumFractionDigits: 7 // minimum defaults to 0
240 };
241 var usDecimalFormatter =
242 new Intl.NumberFormat("en-US", usDecimalOptions);
243
244 assertParts(usDecimalFormatter, 42,
245 [Integer("42")]);
246
247 assertParts(usDecimalFormatter, 1337,
248 [Integer("1"), Group(","), Integer("337")]);
249
250 assertParts(usDecimalFormatter, -6.25,
251 [MinusSign("-"), Integer("6"), Decimal("."), Fraction("25")]);
252
253 assertParts(usDecimalFormatter, -1376.25,
254 [MinusSign("-"),
255 Integer("1"),
256 Group(","),
257 Integer("376"),
258 Decimal("."),
259 Fraction("25")]);
260
261 assertParts(usDecimalFormatter, 124816.8359375,
262 [Integer("124"),
263 Group(","),
264 Integer("816"),
265 Decimal("."),
266 Fraction("8359375")]);
267
268 var usNoGroupingDecimalOptions =
269 {
270 style: "decimal",
271 useGrouping: false,
272 maximumFractionDigits: 7 // minimum defaults to 0
273 };
274 var usNoGroupingDecimalFormatter =
275 new Intl.NumberFormat("en-US", usNoGroupingDecimalOptions);
276
277 assertParts(usNoGroupingDecimalFormatter, 1337,
278 [Integer("1337")]);
279
280 assertParts(usNoGroupingDecimalFormatter, -6.25,
281 [MinusSign("-"), Integer("6"), Decimal("."), Fraction("25")]);
282
283 assertParts(usNoGroupingDecimalFormatter, -1376.25,
284 [MinusSign("-"),
285 Integer("1376"),
286 Decimal("."),
287 Fraction("25")]);
288
289 assertParts(usNoGroupingDecimalFormatter, 124816.8359375,
290 [Integer("124816"),
291 Decimal("."),
292 Fraction("8359375")]);
293
294 var deDecimalOptions =
295 {
296 style: "decimal",
297 maximumFractionDigits: 7 // minimum defaults to 0
298 };
299 var deDecimalFormatter =
300 new Intl.NumberFormat("de-DE", deDecimalOptions);
301
302 assertParts(deDecimalFormatter, 42,
303 [Integer("42")]);
304
305 assertParts(deDecimalFormatter, 1337,
306 [Integer("1"), Group("."), Integer("337")]);
307
308 assertParts(deDecimalFormatter, -6.25,
309 [MinusSign("-"), Integer("6"), Decimal(","), Fraction("25")]);
310
311 assertParts(deDecimalFormatter, -1376.25,
312 [MinusSign("-"),
313 Integer("1"),
314 Group("."),
315 Integer("376"),
316 Decimal(","),
317 Fraction("25")]);
318
319 assertParts(deDecimalFormatter, 124816.8359375,
320 [Integer("124"),
321 Group("."),
322 Integer("816"),
323 Decimal(","),
324 Fraction("8359375")]);
325
326 var deNoGroupingDecimalOptions =
327 {
328 style: "decimal",
329 useGrouping: false,
330 maximumFractionDigits: 7 // minimum defaults to 0
331 };
332 var deNoGroupingDecimalFormatter =
333 new Intl.NumberFormat("de-DE", deNoGroupingDecimalOptions);
334
335 assertParts(deNoGroupingDecimalFormatter, 1337,
336 [Integer("1337")]);
337
338 assertParts(deNoGroupingDecimalFormatter, -6.25,
339 [MinusSign("-"), Integer("6"), Decimal(","), Fraction("25")]);
340
341 assertParts(deNoGroupingDecimalFormatter, -1376.25,
342 [MinusSign("-"),
343 Integer("1376"),
344 Decimal(","),
345 Fraction("25")]);
346
347 assertParts(deNoGroupingDecimalFormatter, 124816.8359375,
348 [Integer("124816"),
349 Decimal(","),
350 Fraction("8359375")]);
351
352 //-----------------------------------------------------------------------------
353
354 if (typeof reportCompare === "function")
355 reportCompare(0, 0, 'ok');
356
357 print("Tests complete");