OLD | NEW |
| (Empty) |
1 /** | |
2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
3 * for details. All rights reserved. Use of this source code is governed by a | |
4 * BSD-style license that can be found in the LICENSE file. | |
5 */ | |
6 | |
7 library number_format_test; | |
8 | |
9 import 'package:unittest/unittest.dart'; | |
10 import 'package:intl/number_symbols_data.dart'; | |
11 import 'package:intl/intl.dart'; | |
12 import 'number_test_data.dart'; | |
13 import 'dart:math'; | |
14 | |
15 /** | |
16 * Tests the Numeric formatting library in dart. | |
17 */ | |
18 var testNumbersWeCanReadBack = { | |
19 "-1" : -1, | |
20 "-2" : -2.0, | |
21 "-0.01" : -0.01, | |
22 "0.001": 0.001, | |
23 "0.01": 0.01, | |
24 "0.1": 0.1, | |
25 "1": 1, | |
26 "2": 2.0, | |
27 "10": 10, | |
28 "100": 100, | |
29 "1,000": 1000, | |
30 "2,000,000,000,000": 2000000000000, | |
31 "0.123": 0.123, | |
32 "1,234": 1234.0, | |
33 "1.234": 1.234, | |
34 "1.23": 1.230, | |
35 "NaN": double.NAN, | |
36 "∞": double.INFINITY, | |
37 "-∞": double.NEGATIVE_INFINITY, | |
38 }; | |
39 | |
40 /** Test numbers that we can't parse because we lose precision in formatting.*/ | |
41 var testNumbersWeCannotReadBack = { | |
42 "3.142" : PI, | |
43 }; | |
44 | |
45 /** Test numbers that won't work in Javascript because they're too big. */ | |
46 var testNumbersOnlyForTheVM = { | |
47 "10,000,000,000,000,000,000,000,000,000,000" : | |
48 10000000000000000000000000000000, | |
49 }; | |
50 | |
51 get allTestNumbers => | |
52 new Map.from(testNumbersWeCanReadBack) | |
53 ..addAll(testNumbersWeCannotReadBack) | |
54 ..addAll(inJavaScript() ? {} : testNumbersOnlyForTheVM); | |
55 | |
56 var testExponential = const { | |
57 "1E-3" : 0.001, | |
58 "1E-2": 0.01, | |
59 "1.23E0" : 1.23 | |
60 }; | |
61 | |
62 // TODO(alanknight): Test against currency, which requires generating data | |
63 // for the three different forms that this now supports. | |
64 // TODO(alanknight): Test against scientific, which requires significant | |
65 // digit support. | |
66 List<NumberFormat> standardFormats(String locale) { | |
67 return [ | |
68 new NumberFormat.decimalPattern(locale), | |
69 new NumberFormat.percentPattern(locale), | |
70 ]; | |
71 } | |
72 | |
73 // Pay no attention to the hint. This is here deliberately to have different | |
74 // behavior in the Dart VM versus Javascript so we can distinguish the two. | |
75 inJavaScript() => 1 is double; | |
76 | |
77 main() { | |
78 // For data from a list of locales, run each locale's data as a separate | |
79 // test so we can see exactly which ones pass or fail. The test data is | |
80 // hard-coded as printing 123, -12.3, %12,300, -1,230% in each locale. | |
81 var mainList = numberTestData; | |
82 var sortedLocales = new List.from(numberFormatSymbols.keys); | |
83 sortedLocales.sort((a, b) => a.compareTo(b)); | |
84 for (var locale in sortedLocales) { | |
85 var testFormats = standardFormats(locale); | |
86 var testLength = (testFormats.length * 3) + 1; | |
87 var list = mainList.take(testLength).iterator; | |
88 mainList = mainList.skip(testLength); | |
89 var nextLocaleFromList = (list..moveNext()).current; | |
90 test("Test against ICU data for $locale", () { | |
91 expect(locale, nextLocaleFromList); | |
92 for (var format in testFormats) { | |
93 var formatted = format.format(123); | |
94 var negative = format.format(-12.3); | |
95 var large = format.format(1234567890); | |
96 var expected = (list..moveNext()).current; | |
97 expect(formatted, expected); | |
98 var expectedNegative = (list..moveNext()).current; | |
99 // Some of these results from CLDR have a leading LTR/RTL indicator, | |
100 // which we don't want. We also treat the difference between Unicode | |
101 // minus sign (2212) and hyphen-minus (45) as not significant. | |
102 expectedNegative = expectedNegative | |
103 .replaceAll("\u200e", "") | |
104 .replaceAll("\u200f", "") | |
105 .replaceAll("\u2212", "-"); | |
106 expect(negative, expectedNegative); | |
107 var expectedLarge = (list..moveNext()).current; | |
108 expect(large, expectedLarge); | |
109 var readBack = format.parse(formatted); | |
110 expect(readBack, 123); | |
111 var readBackNegative = format.parse(negative); | |
112 expect(readBackNegative, -12.3); | |
113 var readBackLarge = format.parse(large); | |
114 expect(readBackLarge, 1234567890); | |
115 } | |
116 }); | |
117 } | |
118 | |
119 test('Simple set of numbers', () { | |
120 var number = new NumberFormat(); | |
121 for (var x in allTestNumbers.keys) { | |
122 var formatted = number.format(allTestNumbers[x]); | |
123 expect(formatted, x); | |
124 if (!testNumbersWeCannotReadBack.containsKey(x)) { | |
125 var readBack = number.parse(formatted); | |
126 // Even among ones we can read back, we can't test NaN for equality. | |
127 if (allTestNumbers[x].isNaN) { | |
128 expect(readBack.isNaN, isTrue); | |
129 } else { | |
130 expect(readBack, allTestNumbers[x]); | |
131 } | |
132 } | |
133 } | |
134 }); | |
135 | |
136 test('Exponential form', () { | |
137 var number = new NumberFormat("#.###E0"); | |
138 for (var x in testExponential.keys) { | |
139 var formatted = number.format(testExponential[x]); | |
140 expect(formatted, x); | |
141 var readBack = number.parse(formatted); | |
142 expect(testExponential[x], readBack); | |
143 } | |
144 }); | |
145 | |
146 // We can't do these in the normal tests because those also format the | |
147 // numbers and we're reading them in a format where they won't print | |
148 // back the same way. | |
149 test('Parsing modifiers,e.g. percent, in the base format', () { | |
150 var number = new NumberFormat(); | |
151 var modified = { "12%" : 0.12, "12\u2030" : 0.012}; | |
152 modified.addAll(testExponential); | |
153 for (var x in modified.keys) { | |
154 var parsed = number.parse(x); | |
155 expect(parsed, modified[x]); | |
156 } | |
157 }); | |
158 | |
159 test('Explicit currency name', () { | |
160 var amount = 1000000.32; | |
161 var usConvention = new NumberFormat.currencyPattern('en_US', '€'); | |
162 var formatted = usConvention.format(amount); | |
163 expect(formatted, '€1,000,000.32'); | |
164 var readBack = usConvention.parse(formatted); | |
165 expect(readBack, amount); | |
166 var swissConvention = new NumberFormat.currencyPattern('de_CH', r'$'); | |
167 formatted = swissConvention.format(amount); | |
168 var nbsp = new String.fromCharCode(0xa0); | |
169 expect(formatted, r"$" + nbsp + "1'000'000.32"); | |
170 readBack = swissConvention.parse(formatted); | |
171 expect(readBack, amount); | |
172 | |
173 /// Verify we can leave off the currency and it gets filled in. | |
174 var plainSwiss = new NumberFormat.currencyPattern('de_CH'); | |
175 formatted = plainSwiss.format(amount); | |
176 expect(formatted, r"CHF" + nbsp + "1'000'000.32"); | |
177 readBack = plainSwiss.parse(formatted); | |
178 expect(readBack, amount); | |
179 | |
180 // Verify that we can pass null in order to specify the currency symbol | |
181 // but use the default locale. | |
182 var defaultLocale = new NumberFormat.currencyPattern(null, 'Smurfs'); | |
183 formatted = defaultLocale.format(amount); | |
184 // We don't know what the exact format will be, but it should have Smurfs. | |
185 expect(formatted.contains('Smurfs'), isTrue); | |
186 readBack = defaultLocale.parse(formatted); | |
187 expect(readBack, amount); | |
188 }); | |
189 | |
190 test("Delta percent format", () { | |
191 var f = new NumberFormat("+#,##0%;-#,##0%"); | |
192 expect(f.format(-0.07), "-7%"); | |
193 expect(f.format(0.12), "+12%"); | |
194 }); | |
195 | |
196 test('Unparseable', () { | |
197 var format = new NumberFormat.currencyPattern(); | |
198 expect(() => format.parse("abcdefg"), throwsFormatException); | |
199 expect(() => format.parse(""), throwsFormatException); | |
200 expect(() => format.parse("1.0zzz"), throwsFormatException); | |
201 expect(() => format.parse("-∞+1"), throwsFormatException); | |
202 }); | |
203 } | |
OLD | NEW |