blob: 558b2dc10cbc6b59aa1af088fad7a51c0465ebe3 [file] [log] [blame]
Dart Team795c4902019-08-29 10:58:28 -07001/// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2/// for details. All rights reserved. Use of this source code is governed by a
3/// BSD-style license that can be found in the LICENSE file.
[email protected]8cc28cb2013-04-09 19:58:42 +00004
alanknighta110b9c2016-01-11 09:54:18 -08005/// Tests based on the closure number formatting tests.
[email protected]8cc28cb2013-04-09 19:58:42 +00006library number_closure_test;
7
Alan Knight4ce7dc62015-01-02 16:14:50 -08008import 'dart:async';
Dart Team795c4902019-08-29 10:58:28 -07009import 'package:intl/intl.dart';
10import 'package:test/test.dart';
[email protected]8cc28cb2013-04-09 19:58:42 +000011
Dart Team795c4902019-08-29 10:58:28 -070012void main() {
13 test('testVeryBigNumber', testVeryBigNumber);
14 test('testStandardFormat', testStandardFormat);
15 test('testNegativePercentage', testNegativePercentage);
16 test('testCustomPercentage', testCustomPercentage);
17 test('testBasicFormat', testBasicFormat);
18 test('testGrouping', testGrouping);
19 test('testPerMill', testPerMill);
20 test('testQuotes', testQuotes);
21 test('testZeros', testZeros);
22 test('testExponential', testExponential);
23 test('testPlusSignInExponentPart', testPlusSignInExponentPart);
24 test('testApis', testApis);
25 test('testLocaleSwitch', testLocaleSwitch);
26 test('testLocaleSwitchAsync', testLocaleSwitchAsync);
[email protected]8cc28cb2013-04-09 19:58:42 +000027}
28
alanknighta110b9c2016-01-11 09:54:18 -080029/// Test two large numbers for equality, assuming that there may be some
30/// loss of precision in the less significant digits.
Dart Team795c4902019-08-29 10:58:28 -070031bool veryBigNumberCompare(str1, str2) {
[email protected]8cc28cb2013-04-09 19:58:42 +000032 return str1.length == str2.length &&
Kevin Moore4f4b8b42015-04-24 16:34:31 -070033 str1.substring(0, 8) == str2.substring(0, 8);
[email protected]8cc28cb2013-04-09 19:58:42 +000034}
35
Dart Team795c4902019-08-29 10:58:28 -070036void testVeryBigNumber() {
37 String str;
38 NumberFormat fmt;
[email protected]8cc28cb2013-04-09 19:58:42 +000039
Dart Team795c4902019-08-29 10:58:28 -070040 fmt = NumberFormat.decimalPattern();
[email protected]8cc28cb2013-04-09 19:58:42 +000041 str = fmt.format(1.3456E20);
[email protected]0c032bb2014-12-16 22:08:11 +000042 expect(veryBigNumberCompare('134,560,000,000,000,000,000', str), isTrue);
[email protected]8cc28cb2013-04-09 19:58:42 +000043
Dart Team795c4902019-08-29 10:58:28 -070044 fmt = NumberFormat.percentPattern();
[email protected]8cc28cb2013-04-09 19:58:42 +000045 str = fmt.format(1.3456E20);
46 expect(veryBigNumberCompare('13,456,000,000,000,000,000,000%', str), isTrue);
47
48 // TODO(alanknight): Note that this disagrees with what ICU would print
49 // for this. We need significant digit support to do this properly.
Dart Team795c4902019-08-29 10:58:28 -070050 fmt = NumberFormat.scientificPattern();
[email protected]8cc28cb2013-04-09 19:58:42 +000051 str = fmt.format(1.3456E20);
52 expect('1E20', str);
53
Dart Team795c4902019-08-29 10:58:28 -070054 fmt = NumberFormat.decimalPattern();
[email protected]8cc28cb2013-04-09 19:58:42 +000055 str = fmt.format(-1.234567890123456e306);
56 expect(1 + 1 + 306 + 306 / 3, str.length);
57 expect('-1,234,567,890,123,45', str.substring(0, 21));
58
59 str = fmt.format(1 / 0);
60 expect('∞', str);
61 str = fmt.format(-1 / 0);
62 expect('-∞', str);
63}
64
65void testStandardFormat() {
Dart Team795c4902019-08-29 10:58:28 -070066 String str;
67 NumberFormat fmt;
68 fmt = NumberFormat.decimalPattern();
[email protected]8cc28cb2013-04-09 19:58:42 +000069 str = fmt.format(1234.579);
70 expect('1,234.579', str);
Dart Team795c4902019-08-29 10:58:28 -070071 fmt = NumberFormat.percentPattern();
[email protected]8cc28cb2013-04-09 19:58:42 +000072 str = fmt.format(1234.579);
73 expect('123,458%', str);
Dart Team795c4902019-08-29 10:58:28 -070074 fmt = NumberFormat.scientificPattern();
[email protected]8cc28cb2013-04-09 19:58:42 +000075 str = fmt.format(1234.579);
76 expect('1E3', str);
77}
78
79void testNegativePercentage() {
Dart Team795c4902019-08-29 10:58:28 -070080 String str;
81 var fmt = NumberFormat('#,##0.00%');
[email protected]8cc28cb2013-04-09 19:58:42 +000082 str = fmt.format(-1234.56);
83 expect('-123,456.00%', str);
84
Dart Team795c4902019-08-29 10:58:28 -070085 fmt = NumberFormat.percentPattern();
[email protected]8cc28cb2013-04-09 19:58:42 +000086 str = fmt.format(-1234.579);
87 expect('-123,458%', str);
88}
89
90void testCustomPercentage() {
Dart Team795c4902019-08-29 10:58:28 -070091 var fmt = NumberFormat.percentPattern();
[email protected]293ee922013-04-29 20:32:04 +000092 fmt.maximumFractionDigits = 1;
93 fmt.minimumFractionDigits = 1;
[email protected]8cc28cb2013-04-09 19:58:42 +000094 var str = fmt.format(0.1291);
95 expect('12.9%', str);
[email protected]293ee922013-04-29 20:32:04 +000096 fmt.maximumFractionDigits = 2;
97 fmt.minimumFractionDigits = 1;
[email protected]8cc28cb2013-04-09 19:58:42 +000098 str = fmt.format(0.129);
99 expect('12.9%', str);
[email protected]293ee922013-04-29 20:32:04 +0000100 fmt.maximumFractionDigits = 2;
101 fmt.minimumFractionDigits = 1;
[email protected]8cc28cb2013-04-09 19:58:42 +0000102 str = fmt.format(0.12);
103 expect('12.0%', str);
[email protected]293ee922013-04-29 20:32:04 +0000104 fmt.maximumFractionDigits = 2;
105 fmt.minimumFractionDigits = 1;
[email protected]8cc28cb2013-04-09 19:58:42 +0000106 str = fmt.format(0.12911);
107 expect('12.91%', str);
108}
109
110void testBasicFormat() {
Dart Team795c4902019-08-29 10:58:28 -0700111 var fmt = NumberFormat('0.0000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000112 var str = fmt.format(123.45789179565757);
113 expect('123.4579', str);
114}
115
116void testGrouping() {
Dart Team795c4902019-08-29 10:58:28 -0700117 String str;
[email protected]8cc28cb2013-04-09 19:58:42 +0000118
Dart Team795c4902019-08-29 10:58:28 -0700119 var fmt = NumberFormat('#,###');
[email protected]8cc28cb2013-04-09 19:58:42 +0000120 str = fmt.format(1234567890);
121 expect('1,234,567,890', str);
Dart Team795c4902019-08-29 10:58:28 -0700122 fmt = NumberFormat('#,####');
[email protected]8cc28cb2013-04-09 19:58:42 +0000123 str = fmt.format(1234567890);
124 expect('12,3456,7890', str);
125
Dart Team795c4902019-08-29 10:58:28 -0700126 fmt = NumberFormat('#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000127 str = fmt.format(1234567890);
128 expect('1234567890', str);
129}
130
131void testPerMill() {
Dart Team795c4902019-08-29 10:58:28 -0700132 String str;
[email protected]8cc28cb2013-04-09 19:58:42 +0000133
Dart Team795c4902019-08-29 10:58:28 -0700134 var fmt = NumberFormat('###.###\u2030');
[email protected]8cc28cb2013-04-09 19:58:42 +0000135 str = fmt.format(0.4857);
136 expect('485.7\u2030', str);
137}
138
139void testQuotes() {
Dart Team795c4902019-08-29 10:58:28 -0700140 String str;
[email protected]8cc28cb2013-04-09 19:58:42 +0000141
Dart Team795c4902019-08-29 10:58:28 -0700142 var fmt = NumberFormat('a\'fo\'\'o\'b#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000143 str = fmt.format(123);
144 expect('afo\'ob123', str);
145
Dart Team795c4902019-08-29 10:58:28 -0700146 fmt = NumberFormat('a\'\'b#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000147 str = fmt.format(123);
148 expect('a\'b123', str);
149
Dart Team795c4902019-08-29 10:58:28 -0700150 fmt = NumberFormat('a\'fo\'\'o\'b#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000151 str = fmt.format(-123);
[email protected]69cc6092014-07-08 17:06:27 +0000152 expect('-afo\'ob123', str);
[email protected]8cc28cb2013-04-09 19:58:42 +0000153
Dart Team795c4902019-08-29 10:58:28 -0700154 fmt = NumberFormat('a\'\'b#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000155 str = fmt.format(-123);
[email protected]69cc6092014-07-08 17:06:27 +0000156 expect('-a\'b123', str);
[email protected]8cc28cb2013-04-09 19:58:42 +0000157}
158
159void testZeros() {
Dart Team795c4902019-08-29 10:58:28 -0700160 String str;
[email protected]8cc28cb2013-04-09 19:58:42 +0000161
Dart Team795c4902019-08-29 10:58:28 -0700162 var fmt = NumberFormat('#.#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000163 str = fmt.format(0);
164 expect('0', str);
Dart Team795c4902019-08-29 10:58:28 -0700165 fmt = NumberFormat('#.');
[email protected]8cc28cb2013-04-09 19:58:42 +0000166 str = fmt.format(0);
167 expect('0.', str);
Dart Team795c4902019-08-29 10:58:28 -0700168 fmt = NumberFormat('.#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000169 str = fmt.format(0);
170 expect('.0', str);
Dart Team795c4902019-08-29 10:58:28 -0700171 fmt = NumberFormat('#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000172 str = fmt.format(0);
173 expect('0', str);
174
Dart Team795c4902019-08-29 10:58:28 -0700175 fmt = NumberFormat('#0.#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000176 str = fmt.format(0);
177 expect('0', str);
Dart Team795c4902019-08-29 10:58:28 -0700178 fmt = NumberFormat('#0.');
[email protected]8cc28cb2013-04-09 19:58:42 +0000179 str = fmt.format(0);
180 expect('0.', str);
Dart Team795c4902019-08-29 10:58:28 -0700181 fmt = NumberFormat('#.0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000182 str = fmt.format(0);
183 expect('.0', str);
Dart Team795c4902019-08-29 10:58:28 -0700184 fmt = NumberFormat('#');
[email protected]8cc28cb2013-04-09 19:58:42 +0000185 str = fmt.format(0);
186 expect('0', str);
Dart Team795c4902019-08-29 10:58:28 -0700187 fmt = NumberFormat('000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000188 str = fmt.format(0);
189 expect('000', str);
190}
191
192void testExponential() {
Dart Team795c4902019-08-29 10:58:28 -0700193 String str;
194 var fmt = NumberFormat('0.####E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000195 str = fmt.format(0.01234);
196 expect('1.234E-2', str);
Dart Team795c4902019-08-29 10:58:28 -0700197 fmt = NumberFormat('00.000E00');
[email protected]8cc28cb2013-04-09 19:58:42 +0000198 str = fmt.format(0.01234);
199 expect('12.340E-03', str);
Dart Team795c4902019-08-29 10:58:28 -0700200 fmt = NumberFormat('##0.######E000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000201 str = fmt.format(0.01234);
202 expect('12.34E-003', str);
Dart Team795c4902019-08-29 10:58:28 -0700203 fmt = NumberFormat('0.###E0;[0.###E0]');
[email protected]8cc28cb2013-04-09 19:58:42 +0000204 str = fmt.format(0.01234);
205 expect('1.234E-2', str);
206
Dart Team795c4902019-08-29 10:58:28 -0700207 fmt = NumberFormat('0.####E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000208 str = fmt.format(123456789);
209 expect('1.2346E8', str);
Dart Team795c4902019-08-29 10:58:28 -0700210 fmt = NumberFormat('00.000E00');
[email protected]8cc28cb2013-04-09 19:58:42 +0000211 str = fmt.format(123456789);
212 expect('12.346E07', str);
Dart Team795c4902019-08-29 10:58:28 -0700213 fmt = NumberFormat('##0.######E000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000214 str = fmt.format(123456789);
215 expect('123.456789E006', str);
Dart Team795c4902019-08-29 10:58:28 -0700216 fmt = NumberFormat('0.###E0;[0.###E0]');
[email protected]8cc28cb2013-04-09 19:58:42 +0000217 str = fmt.format(123456789);
218 expect('1.235E8', str);
219
Dart Team795c4902019-08-29 10:58:28 -0700220 fmt = NumberFormat('0.####E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000221 str = fmt.format(1.23e300);
222 expect('1.23E300', str);
Dart Team795c4902019-08-29 10:58:28 -0700223 fmt = NumberFormat('00.000E00');
[email protected]8cc28cb2013-04-09 19:58:42 +0000224 str = fmt.format(1.23e300);
225 expect('12.300E299', str);
Dart Team795c4902019-08-29 10:58:28 -0700226 fmt = NumberFormat('##0.######E000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000227 str = fmt.format(1.23e300);
228 expect('1.23E300', str);
Dart Team795c4902019-08-29 10:58:28 -0700229 fmt = NumberFormat('0.###E0;[0.###E0]');
[email protected]8cc28cb2013-04-09 19:58:42 +0000230 str = fmt.format(1.23e300);
231 expect('1.23E300', str);
232
Dart Team795c4902019-08-29 10:58:28 -0700233 fmt = NumberFormat('0.####E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000234 str = fmt.format(-3.141592653e-271);
235 expect('-3.1416E-271', str);
Dart Team795c4902019-08-29 10:58:28 -0700236 fmt = NumberFormat('00.000E00');
[email protected]8cc28cb2013-04-09 19:58:42 +0000237 str = fmt.format(-3.141592653e-271);
238 expect('-31.416E-272', str);
Dart Team795c4902019-08-29 10:58:28 -0700239 fmt = NumberFormat('##0.######E000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000240 str = fmt.format(-3.141592653e-271);
241 expect('-314.159265E-273', str);
Dart Team795c4902019-08-29 10:58:28 -0700242 fmt = NumberFormat('0.###E0;[0.###E0]');
[email protected]8cc28cb2013-04-09 19:58:42 +0000243 str = fmt.format(-3.141592653e-271);
244 expect('[3.142E-271]', str);
245
Dart Team795c4902019-08-29 10:58:28 -0700246 fmt = NumberFormat('0.####E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000247 str = fmt.format(0);
248 expect('0E0', str);
Dart Team795c4902019-08-29 10:58:28 -0700249 fmt = NumberFormat('00.000E00');
[email protected]8cc28cb2013-04-09 19:58:42 +0000250 str = fmt.format(0);
251 expect('00.000E00', str);
Dart Team795c4902019-08-29 10:58:28 -0700252 fmt = NumberFormat('##0.######E000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000253 str = fmt.format(0);
254 expect('0E000', str);
Dart Team795c4902019-08-29 10:58:28 -0700255 fmt = NumberFormat('0.###E0;[0.###E0]');
[email protected]8cc28cb2013-04-09 19:58:42 +0000256 str = fmt.format(0);
257 expect('0E0', str);
258
Dart Team795c4902019-08-29 10:58:28 -0700259 fmt = NumberFormat('0.####E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000260 str = fmt.format(-1);
261 expect('-1E0', str);
Dart Team795c4902019-08-29 10:58:28 -0700262 fmt = NumberFormat('00.000E00');
[email protected]8cc28cb2013-04-09 19:58:42 +0000263 str = fmt.format(-1);
264 expect('-10.000E-01', str);
Dart Team795c4902019-08-29 10:58:28 -0700265 fmt = NumberFormat('##0.######E000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000266 str = fmt.format(-1);
267 expect('-1E000', str);
Dart Team795c4902019-08-29 10:58:28 -0700268 fmt = NumberFormat('0.###E0;[0.###E0]');
[email protected]8cc28cb2013-04-09 19:58:42 +0000269 str = fmt.format(-1);
270 expect('[1E0]', str);
271
Dart Team795c4902019-08-29 10:58:28 -0700272 fmt = NumberFormat('0.####E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000273 str = fmt.format(1);
274 expect('1E0', str);
Dart Team795c4902019-08-29 10:58:28 -0700275 fmt = NumberFormat('00.000E00');
[email protected]8cc28cb2013-04-09 19:58:42 +0000276 str = fmt.format(1);
277 expect('10.000E-01', str);
Dart Team795c4902019-08-29 10:58:28 -0700278 fmt = NumberFormat('##0.######E000');
[email protected]8cc28cb2013-04-09 19:58:42 +0000279 str = fmt.format(1);
280 expect('1E000', str);
Dart Team795c4902019-08-29 10:58:28 -0700281 fmt = NumberFormat('0.###E0;[0.###E0]');
[email protected]8cc28cb2013-04-09 19:58:42 +0000282 str = fmt.format(1);
283 expect('1E0', str);
284
Dart Team795c4902019-08-29 10:58:28 -0700285 fmt = NumberFormat('#E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000286 str = fmt.format(12345.0);
287 expect('1E4', str);
Dart Team795c4902019-08-29 10:58:28 -0700288 fmt = NumberFormat('0E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000289 str = fmt.format(12345.0);
290 expect('1E4', str);
Dart Team795c4902019-08-29 10:58:28 -0700291 fmt = NumberFormat('##0.###E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000292 str = fmt.format(12345.0);
293 expect('12.345E3', str);
Dart Team795c4902019-08-29 10:58:28 -0700294 fmt = NumberFormat('##0.###E0');
[email protected]8cc28cb2013-04-09 19:58:42 +0000295 str = fmt.format(12345.00001);
296 expect('12.345E3', str);
Dart Team795c4902019-08-29 10:58:28 -0700297 fmt = NumberFormat('##0.###E0');