OLD | NEW |
1 /** | 1 /** |
2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 4 * BSD-style license that can be found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 library number_format_test; | 7 library number_format_test; |
8 | 8 |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 import 'package:intl/number_symbols_data.dart'; | 10 import 'package:intl/number_symbols_data.dart'; |
11 import 'package:intl/intl.dart'; | 11 import 'package:intl/intl.dart'; |
12 import 'number_test_data.dart'; | 12 import 'number_test_data.dart'; |
13 import 'dart:math'; | 13 import 'dart:math'; |
14 | 14 |
15 /** | 15 /** |
16 * Tests the Numeric formatting library in dart. | 16 * Tests the Numeric formatting library in dart. |
17 */ | 17 */ |
18 var testNumbersWeCanReadBack = { | 18 var testNumbersWeCanReadBack = { |
19 "-1" : -1, | 19 "-1" : -1, |
20 "-2" : -2.0, | 20 "-2" : -2.0, |
21 "-0.01" : -0.01, | 21 "-0.01" : -0.01, |
22 "0.001": 0.001, | 22 "0.001": 0.001, |
23 "0.01": 0.01, | 23 "0.01": 0.01, |
24 "0.1": 0.1, | 24 "0.1": 0.1, |
25 "1": 1, | 25 "1": 1, |
26 "2": 2.0, | 26 "2": 2.0, |
27 "10": 10, | 27 "10": 10, |
28 "100": 100, | 28 "100": 100, |
29 "1,000": 1000, | 29 "1,000": 1000, |
30 "2,000,000,000,000": 2000000000000, | 30 "2,000,000,000,000": 2000000000000, |
31 "0.123": 0.123, | 31 "0.123": 0.123, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 new NumberFormat.percentPattern(locale), | 69 new NumberFormat.percentPattern(locale), |
70 ]; | 70 ]; |
71 } | 71 } |
72 | 72 |
73 // Pay no attention to the hint. This is here deliberately to have different | 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. | 74 // behavior in the Dart VM versus Javascript so we can distinguish the two. |
75 inJavaScript() => 1 is double; | 75 inJavaScript() => 1 is double; |
76 | 76 |
77 main() { | 77 main() { |
78 // For data from a list of locales, run each locale's data as a separate | 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 | 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. | 80 // hard-coded as printing 123, -12.3, %12,300, -1,230% in each locale. |
81 var mainList = numberTestData; | 81 var mainList = numberTestData; |
82 var sortedLocales = new List.from(numberFormatSymbols.keys); | 82 var sortedLocales = new List.from(numberFormatSymbols.keys); |
83 sortedLocales.sort((a, b) => a.compareTo(b)); | 83 sortedLocales.sort((a, b) => a.compareTo(b)); |
84 for (var locale in sortedLocales) { | 84 for (var locale in sortedLocales) { |
85 var testFormats = standardFormats(locale); | 85 var testFormats = standardFormats(locale); |
86 var testLength = (testFormats.length * 3) + 1; | 86 var testLength = (testFormats.length * 3) + 1; |
87 var list = mainList.take(testLength).iterator; | 87 var list = mainList.take(testLength).iterator; |
88 mainList = mainList.skip(testLength); | 88 mainList = mainList.skip(testLength); |
89 var nextLocaleFromList = (list..moveNext()).current; | 89 var nextLocaleFromList = (list..moveNext()).current; |
90 test("Test against ICU data for $locale", () { | 90 test("Test against ICU data for $locale", () { |
91 expect(locale, nextLocaleFromList); | 91 expect(locale, nextLocaleFromList); |
92 for (var format in testFormats) { | 92 for (var format in testFormats) { |
93 var formatted = format.format(123); | 93 var formatted = format.format(123); |
94 var negative = format.format(-12.3); | 94 var negative = format.format(-12.3); |
95 var large = format.format(1234567890); | 95 var large = format.format(1234567890); |
96 var expected = (list..moveNext()).current; | 96 var expected = (list..moveNext()).current; |
97 expect(formatted, expected); | 97 expect(formatted, expected); |
98 var expectedNegative = (list..moveNext()).current; | 98 var expectedNegative = (list..moveNext()).current; |
99 // Some of these results from CLDR have a leading LTR/RTL indicator, | 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 | 100 // which we don't want. We also treat the difference between Unicode |
101 // minus sign (2212) and hyphen-minus (45) as not significant. | 101 // minus sign (2212) and hyphen-minus (45) as not significant. |
102 expectedNegative = expectedNegative | 102 expectedNegative = expectedNegative |
103 .replaceAll("\u200e", "") | 103 .replaceAll("\u200e", "") |
104 .replaceAll("\u200f", "") | 104 .replaceAll("\u200f", "") |
105 .replaceAll("\u2212", "-"); | 105 .replaceAll("\u2212", "-"); |
106 expect(negative, expectedNegative); | 106 expect(negative, expectedNegative); |
107 var expectedLarge = (list..moveNext()).current; | 107 var expectedLarge = (list..moveNext()).current; |
108 expect(large, expectedLarge); | 108 expect(large, expectedLarge); |
109 var readBack = format.parse(formatted); | 109 var readBack = format.parse(formatted); |
110 expect(readBack, 123); | 110 expect(readBack, 123); |
(...skipping 25 matching lines...) Expand all Loading... |
136 test('Exponential form', () { | 136 test('Exponential form', () { |
137 var number = new NumberFormat("#.###E0"); | 137 var number = new NumberFormat("#.###E0"); |
138 for (var x in testExponential.keys) { | 138 for (var x in testExponential.keys) { |
139 var formatted = number.format(testExponential[x]); | 139 var formatted = number.format(testExponential[x]); |
140 expect(formatted, x); | 140 expect(formatted, x); |
141 var readBack = number.parse(formatted); | 141 var readBack = number.parse(formatted); |
142 expect(testExponential[x], readBack); | 142 expect(testExponential[x], readBack); |
143 } | 143 } |
144 }); | 144 }); |
145 | 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 |
146 test('Explicit currency name', () { | 159 test('Explicit currency name', () { |
147 var amount = 1000000.32; | 160 var amount = 1000000.32; |
148 var usConvention = new NumberFormat.currencyPattern('en_US', '€'); | 161 var usConvention = new NumberFormat.currencyPattern('en_US', '€'); |
149 var formatted = usConvention.format(amount); | 162 var formatted = usConvention.format(amount); |
150 expect(formatted, '€1,000,000.32'); | 163 expect(formatted, '€1,000,000.32'); |
151 var readBack = usConvention.parse(formatted); | 164 var readBack = usConvention.parse(formatted); |
152 expect(readBack, amount); | 165 expect(readBack, amount); |
153 var swissConvention = new NumberFormat.currencyPattern('de_CH', r'$'); | 166 var swissConvention = new NumberFormat.currencyPattern('de_CH', r'$'); |
154 formatted = swissConvention.format(amount); | 167 formatted = swissConvention.format(amount); |
155 var nbsp = new String.fromCharCode(0xa0); | 168 var nbsp = new String.fromCharCode(0xa0); |
(...skipping 19 matching lines...) Expand all Loading... |
175 }); | 188 }); |
176 | 189 |
177 test('Unparseable', () { | 190 test('Unparseable', () { |
178 var format = new NumberFormat.currencyPattern(); | 191 var format = new NumberFormat.currencyPattern(); |
179 expect(() => format.parse("abcdefg"), throwsFormatException); | 192 expect(() => format.parse("abcdefg"), throwsFormatException); |
180 expect(() => format.parse(""), throwsFormatException); | 193 expect(() => format.parse(""), throwsFormatException); |
181 expect(() => format.parse("1.0zzz"), throwsFormatException); | 194 expect(() => format.parse("1.0zzz"), throwsFormatException); |
182 expect(() => format.parse("-∞+1"), throwsFormatException); | 195 expect(() => format.parse("-∞+1"), throwsFormatException); |
183 }); | 196 }); |
184 } | 197 } |
OLD | NEW |