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_format.dart'; | 10 import 'package:intl/number_symbols.dart'; |
| 11 import 'package:intl/number_symbols_data.dart'; |
11 import 'package:intl/intl.dart'; | 12 import 'package:intl/intl.dart'; |
| 13 import 'number_test_data.dart'; |
| 14 import 'dart:math'; |
12 | 15 |
13 /** | 16 /** |
14 * Tests the Numeric formatting library in dart. | 17 * Tests the Numeric formatting library in dart. |
15 */ | 18 */ |
16 var testNumbers = const { | 19 var testNumbers = { |
17 "0.001": 0.001, | 20 "0.001": 0.001, |
18 "0.01": 0.01, | 21 "0.01": 0.01, |
19 "0.1": 0.1, | 22 "0.1": 0.1, |
20 "1": 1, | 23 "1": 1, |
21 "2": 2.0, | 24 "2": 2.0, |
22 "10": 10, | 25 "10": 10, |
23 "100": 100, | 26 "100": 100, |
24 "1,000": 1000, | 27 "1,000": 1000, |
25 "2,000,000,000,000": 2000000000000, | 28 "2,000,000,000,000": 2000000000000, |
26 "10,000,000,000,000,000,000,000,000,000,000": | |
27 10000000000000000000000000000000, | |
28 "0.123": 0.123, | 29 "0.123": 0.123, |
29 "1,234": 1234.0, | 30 "1,234": 1234.0, |
30 "1.234": 1.234, | 31 "1.234": 1.234, |
31 "1.23": 1.230, | 32 "1.23": 1.230, |
32 "NaN": 0/0, | 33 "NaN": double.NAN, |
33 "∞": 1/0, | 34 "∞": double.INFINITY, |
34 "-∞": -1/0}; | 35 "-∞": double.NEGATIVE_INFINITY, |
| 36 "3.142": PI}; |
| 37 |
| 38 var testExponential = const { |
| 39 "1E-3" : 0.001, |
| 40 "1E-2": 0.01, |
| 41 "1.23E0" : 1.23 |
| 42 }; |
| 43 |
| 44 // TODO(alanknight): Test against currency, which requires generating data |
| 45 // for the three different forms that this now supports. |
| 46 // TODO(alanknight): Test against scientific, which requires significant |
| 47 // digit support. |
| 48 List<NumberFormat> standardFormats(String locale) { |
| 49 return [ |
| 50 new NumberFormat.DECIMAL_PATTERN(locale), |
| 51 new NumberFormat.PERCENT_PATTERN(locale) |
| 52 ]; |
| 53 } |
| 54 |
| 55 inJavaScript() => 1 is double; |
35 | 56 |
36 main() { | 57 main() { |
37 test('Basic number printing', () { | 58 if (!inJavaScript()) { |
38 var number = new NumberFormat(); | 59 testNumbers["10,000,000,000,000,000,000,000,000,000,000"] = |
39 expect(number.format(3.14),"3.14"); | 60 10000000000000000000000000000000; |
40 }); | 61 } |
| 62 |
| 63 // For data from a list of locales, run each locale's data as a separate |
| 64 // test so we can see exactly which ones pass or fail. |
| 65 var mainList = numberTestData; |
| 66 var sortedLocales = new List.from(numberFormatSymbols.keys); |
| 67 sortedLocales.sort((a, b) => a.compareTo(b)); |
| 68 for (var locale in sortedLocales) { |
| 69 var testFormats = standardFormats(locale); |
| 70 var list = mainList.take(testFormats.length + 1).iterator; |
| 71 mainList = mainList.skip(testFormats.length + 1); |
| 72 var nextLocaleFromList = (list..moveNext()).current; |
| 73 test("Test against ICU data for $locale", () { |
| 74 expect(locale, nextLocaleFromList); |
| 75 for (var format in testFormats) { |
| 76 var formatted = format.format(123); |
| 77 var expected = (list..moveNext()).current; |
| 78 expect(formatted, expected); |
| 79 } |
| 80 }); |
| 81 } |
41 | 82 |
42 test('Simple set of numbers', () { | 83 test('Simple set of numbers', () { |
43 var number = new NumberFormat(); | 84 var number = new NumberFormat(); |
44 for (var x in testNumbers.keys) { | 85 for (var x in testNumbers.keys) { |
45 var formatted = number.format(testNumbers[x]); | 86 var formatted = number.format(testNumbers[x]); |
46 expect(formatted, x); | 87 expect(formatted, x); |
47 } | 88 } |
48 }); | 89 }); |
49 } | 90 |
| 91 test('Exponential form', () { |
| 92 var number = new NumberFormat("#.###E0"); |
| 93 for (var x in testExponential.keys) { |
| 94 var formatted = number.format(testExponential[x]); |
| 95 expect(formatted, x); |
| 96 } |
| 97 }); |
| 98 } |
OLD | NEW |