Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: packages/intl/test/number_format_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/intl/test/number_closure_test.dart ('k') | packages/intl/test/number_test_data.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 = {"3.142": PI,};
42
43 /** Test numbers that won't work in Javascript because they're too big. */
44 var testNumbersOnlyForTheVM = {
45 "10,000,000,000,000,000,000,000,000,000,000":
46 10000000000000000000000000000000,
47 };
48
49 get allTestNumbers => new Map.from(testNumbersWeCanReadBack)
50 ..addAll(testNumbersWeCannotReadBack)
51 ..addAll(inJavaScript() ? {} : testNumbersOnlyForTheVM);
52
53 var testExponential = const {"1E-3": 0.001, "1E-2": 0.01, "1.23E0": 1.23};
54
55 // TODO(alanknight): Test against currency, which requires generating data
56 // for the three different forms that this now supports.
57 // TODO(alanknight): Test against scientific, which requires significant
58 // digit support.
59 List<NumberFormat> standardFormats(String locale) {
60 return [
61 new NumberFormat.decimalPattern(locale),
62 new NumberFormat.percentPattern(locale),
63 ];
64 }
65
66 // Pay no attention to the hint. This is here deliberately to have different
67 // behavior in the Dart VM versus Javascript so we can distinguish the two.
68 inJavaScript() => 1 is double;
69
70 main() {
71 // For data from a list of locales, run each locale's data as a separate
72 // test so we can see exactly which ones pass or fail. The test data is
73 // hard-coded as printing 123, -12.3, %12,300, -1,230% in each locale.
74 var mainList = numberTestData;
75 var sortedLocales = new List.from(numberFormatSymbols.keys);
76 sortedLocales.sort((a, b) => a.compareTo(b));
77 for (var locale in sortedLocales) {
78 var testFormats = standardFormats(locale);
79 var testLength = (testFormats.length * 3) + 1;
80 var list = mainList.take(testLength).iterator;
81 mainList = mainList.skip(testLength);
82 var nextLocaleFromList = (list..moveNext()).current;
83 test("Test against ICU data for $locale", () {
84 expect(locale, nextLocaleFromList);
85 for (var format in testFormats) {
86 var formatted = format.format(123);
87 var negative = format.format(-12.3);
88 var large = format.format(1234567890);
89 var expected = (list..moveNext()).current;
90 expect(formatted, expected);
91 var expectedNegative = (list..moveNext()).current;
92 // Some of these results from CLDR have a leading LTR/RTL indicator,
93 // which we don't want. We also treat the difference between Unicode
94 // minus sign (2212) and hyphen-minus (45) as not significant.
95 expectedNegative = expectedNegative
96 .replaceAll("\u200e", "")
97 .replaceAll("\u200f", "")
98 .replaceAll("\u2212", "-");
99 expect(negative, expectedNegative);
100 var expectedLarge = (list..moveNext()).current;
101 expect(large, expectedLarge);
102 var readBack = format.parse(formatted);
103 expect(readBack, 123);
104 var readBackNegative = format.parse(negative);
105 expect(readBackNegative, -12.3);
106 var readBackLarge = format.parse(large);
107 expect(readBackLarge, 1234567890);
108 }
109 });
110 }
111
112 test('Simple set of numbers', () {
113 var number = new NumberFormat();
114 for (var x in allTestNumbers.keys) {
115 var formatted = number.format(allTestNumbers[x]);
116 expect(formatted, x);
117 if (!testNumbersWeCannotReadBack.containsKey(x)) {
118 var readBack = number.parse(formatted);
119 // Even among ones we can read back, we can't test NaN for equality.
120 if (allTestNumbers[x].isNaN) {
121 expect(readBack.isNaN, isTrue);
122 } else {
123 expect(readBack, allTestNumbers[x]);
124 }
125 }
126 }
127 });
128
129 test('Exponential form', () {
130 var number = new NumberFormat("#.###E0");
131 for (var x in testExponential.keys) {
132 var formatted = number.format(testExponential[x]);
133 expect(formatted, x);
134 var readBack = number.parse(formatted);
135 expect(testExponential[x], readBack);
136 }
137 });
138
139 test('Percent with no decimals and no integer part', () {
140 var number = new NumberFormat("#%");
141 var formatted = number.format(0.12);
142 expect(formatted, "12%");
143 var readBack = number.parse(formatted);
144 expect(0.12, readBack);
145 });
146
147 // We can't do these in the normal tests because those also format the
148 // numbers and we're reading them in a format where they won't print
149 // back the same way.
150 test('Parsing modifiers,e.g. percent, in the base format', () {
151 var number = new NumberFormat();
152 var modified = {"12%": 0.12, "12\u2030": 0.012};
153 modified.addAll(testExponential);
154 for (var x in modified.keys) {
155 var parsed = number.parse(x);
156 expect(parsed, modified[x]);
157 }
158 });
159
160 test('Explicit currency name', () {
161 var amount = 1000000.32;
162 var usConvention = new NumberFormat.currencyPattern('en_US', '€');
163 var formatted = usConvention.format(amount);
164 expect(formatted, '€1,000,000.32');
165 var readBack = usConvention.parse(formatted);
166 expect(readBack, amount);
167 var swissConvention = new NumberFormat.currencyPattern('de_CH', r'$');
168 formatted = swissConvention.format(amount);
169 var nbsp = new String.fromCharCode(0xa0);
170 expect(formatted, r"$" + nbsp + "1'000'000.32");
171 readBack = swissConvention.parse(formatted);
172 expect(readBack, amount);
173
174 /// Verify we can leave off the currency and it gets filled in.
175 var plainSwiss = new NumberFormat.currencyPattern('de_CH');
176 formatted = plainSwiss.format(amount);
177 expect(formatted, r"CHF" + nbsp + "1'000'000.32");
178 readBack = plainSwiss.parse(formatted);
179 expect(readBack, amount);
180
181 // Verify that we can pass null in order to specify the currency symbol
182 // but use the default locale.
183 var defaultLocale = new NumberFormat.currencyPattern(null, 'Smurfs');
184 formatted = defaultLocale.format(amount);
185 // We don't know what the exact format will be, but it should have Smurfs.
186 expect(formatted.contains('Smurfs'), isTrue);
187 readBack = defaultLocale.parse(formatted);
188 expect(readBack, amount);
189 });
190
191 test("Delta percent format", () {
192 var f = new NumberFormat("+#,##0%;-#,##0%");
193 expect(f.format(-0.07), "-7%");
194 expect(f.format(0.12), "+12%");
195 });
196
197 test('Unparseable', () {
198 var format = new NumberFormat.currencyPattern();
199 expect(() => format.parse("abcdefg"), throwsFormatException);
200 expect(() => format.parse(""), throwsFormatException);
201 expect(() => format.parse("1.0zzz"), throwsFormatException);
202 expect(() => format.parse("-∞+1"), throwsFormatException);
203 });
204 }
OLDNEW
« no previous file with comments | « packages/intl/test/number_closure_test.dart ('k') | packages/intl/test/number_test_data.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698