OLD | NEW |
(Empty) | |
| 1 // for details. All rights reserved. Use of this source code is governed by a |
| 2 // BSD-style license that can be found in the LICENSE file. |
| 3 |
| 4 /** |
| 5 * Tests based on the closure number formatting tests. |
| 6 */ |
| 7 library number_closure_test; |
| 8 |
| 9 import 'dart:async'; |
| 10 import "package:intl/intl.dart"; |
| 11 import "package:unittest/unittest.dart"; |
| 12 |
| 13 main() { |
| 14 test("testVeryBigNumber", testVeryBigNumber); |
| 15 test("testStandardFormat", testStandardFormat); |
| 16 test("testNegativePercentage", testNegativePercentage); |
| 17 test("testCustomPercentage", testCustomPercentage); |
| 18 test("testBasicFormat", testBasicFormat); |
| 19 test("testGrouping", testGrouping); |
| 20 test("testPerMill", testPerMill); |
| 21 test("testQuotes", testQuotes); |
| 22 test("testZeros", testZeros); |
| 23 test("testExponential", testExponential); |
| 24 test("testPlusSignInExponentPart", testPlusSignInExponentPart); |
| 25 test("testApis", testApis); |
| 26 test("testLocaleSwitch", testLocaleSwitch); |
| 27 test("testLocaleSwitchAsync", testLocaleSwitchAsync); |
| 28 } |
| 29 |
| 30 /** |
| 31 * Test two large numbers for equality, assuming that there may be some |
| 32 * loss of precision in the less significant digits. |
| 33 */ |
| 34 veryBigNumberCompare(str1, str2) { |
| 35 return str1.length == str2.length && |
| 36 str1.substring(0, 8) == str2.substring(0, 8); |
| 37 } |
| 38 |
| 39 testVeryBigNumber() { |
| 40 var str; |
| 41 var fmt; |
| 42 |
| 43 fmt = new NumberFormat.decimalPattern(); |
| 44 str = fmt.format(1.3456E20); |
| 45 expect(veryBigNumberCompare('134,560,000,000,000,000,000', str), isTrue); |
| 46 |
| 47 fmt = new NumberFormat.percentPattern(); |
| 48 str = fmt.format(1.3456E20); |
| 49 expect(veryBigNumberCompare('13,456,000,000,000,000,000,000%', str), isTrue); |
| 50 |
| 51 // TODO(alanknight): Note that this disagrees with what ICU would print |
| 52 // for this. We need significant digit support to do this properly. |
| 53 fmt = new NumberFormat.scientificPattern(); |
| 54 str = fmt.format(1.3456E20); |
| 55 expect('1E20', str); |
| 56 |
| 57 fmt = new NumberFormat.decimalPattern(); |
| 58 str = fmt.format(-1.234567890123456e306); |
| 59 expect(1 + 1 + 306 + 306 / 3, str.length); |
| 60 expect('-1,234,567,890,123,45', str.substring(0, 21)); |
| 61 |
| 62 str = fmt.format(1 / 0); |
| 63 expect('∞', str); |
| 64 str = fmt.format(-1 / 0); |
| 65 expect('-∞', str); |
| 66 } |
| 67 |
| 68 void testStandardFormat() { |
| 69 var str; |
| 70 var fmt; |
| 71 fmt = new NumberFormat.decimalPattern(); |
| 72 str = fmt.format(1234.579); |
| 73 expect('1,234.579', str); |
| 74 fmt = new NumberFormat.percentPattern(); |
| 75 str = fmt.format(1234.579); |
| 76 expect('123,458%', str); |
| 77 fmt = new NumberFormat.scientificPattern(); |
| 78 str = fmt.format(1234.579); |
| 79 expect('1E3', str); |
| 80 } |
| 81 |
| 82 void testNegativePercentage() { |
| 83 var str; |
| 84 var fmt = new NumberFormat('#,##0.00%'); |
| 85 str = fmt.format(-1234.56); |
| 86 expect('-123,456.00%', str); |
| 87 |
| 88 fmt = new NumberFormat.percentPattern(); |
| 89 str = fmt.format(-1234.579); |
| 90 expect('-123,458%', str); |
| 91 } |
| 92 |
| 93 void testCustomPercentage() { |
| 94 var fmt = new NumberFormat.percentPattern(); |
| 95 fmt.maximumFractionDigits = 1; |
| 96 fmt.minimumFractionDigits = 1; |
| 97 var str = fmt.format(0.1291); |
| 98 expect('12.9%', str); |
| 99 fmt.maximumFractionDigits = 2; |
| 100 fmt.minimumFractionDigits = 1; |
| 101 str = fmt.format(0.129); |
| 102 expect('12.9%', str); |
| 103 fmt.maximumFractionDigits = 2; |
| 104 fmt.minimumFractionDigits = 1; |
| 105 str = fmt.format(0.12); |
| 106 expect('12.0%', str); |
| 107 fmt.maximumFractionDigits = 2; |
| 108 fmt.minimumFractionDigits = 1; |
| 109 str = fmt.format(0.12911); |
| 110 expect('12.91%', str); |
| 111 } |
| 112 |
| 113 void testBasicFormat() { |
| 114 var fmt = new NumberFormat('0.0000'); |
| 115 var str = fmt.format(123.45789179565757); |
| 116 expect('123.4579', str); |
| 117 } |
| 118 |
| 119 void testGrouping() { |
| 120 var str; |
| 121 |
| 122 var fmt = new NumberFormat('#,###'); |
| 123 str = fmt.format(1234567890); |
| 124 expect('1,234,567,890', str); |
| 125 fmt = new NumberFormat('#,####'); |
| 126 str = fmt.format(1234567890); |
| 127 expect('12,3456,7890', str); |
| 128 |
| 129 fmt = new NumberFormat('#'); |
| 130 str = fmt.format(1234567890); |
| 131 expect('1234567890', str); |
| 132 } |
| 133 |
| 134 void testPerMill() { |
| 135 var str; |
| 136 |
| 137 var fmt = new NumberFormat('###.###\u2030'); |
| 138 str = fmt.format(0.4857); |
| 139 expect('485.7\u2030', str); |
| 140 } |
| 141 |
| 142 void testQuotes() { |
| 143 var str; |
| 144 |
| 145 var fmt = new NumberFormat('a\'fo\'\'o\'b#'); |
| 146 str = fmt.format(123); |
| 147 expect('afo\'ob123', str); |
| 148 |
| 149 fmt = new NumberFormat('a\'\'b#'); |
| 150 str = fmt.format(123); |
| 151 expect('a\'b123', str); |
| 152 |
| 153 fmt = new NumberFormat('a\'fo\'\'o\'b#'); |
| 154 str = fmt.format(-123); |
| 155 expect('-afo\'ob123', str); |
| 156 |
| 157 fmt = new NumberFormat('a\'\'b#'); |
| 158 str = fmt.format(-123); |
| 159 expect('-a\'b123', str); |
| 160 } |
| 161 |
| 162 void testZeros() { |
| 163 var str; |
| 164 var fmt; |
| 165 |
| 166 fmt = new NumberFormat('#.#'); |
| 167 str = fmt.format(0); |
| 168 expect('0', str); |
| 169 fmt = new NumberFormat('#.'); |
| 170 str = fmt.format(0); |
| 171 expect('0.', str); |
| 172 fmt = new NumberFormat('.#'); |
| 173 str = fmt.format(0); |
| 174 expect('.0', str); |
| 175 fmt = new NumberFormat('#'); |
| 176 str = fmt.format(0); |
| 177 expect('0', str); |
| 178 |
| 179 fmt = new NumberFormat('#0.#'); |
| 180 str = fmt.format(0); |
| 181 expect('0', str); |
| 182 fmt = new NumberFormat('#0.'); |
| 183 str = fmt.format(0); |
| 184 expect('0.', str); |
| 185 fmt = new NumberFormat('#.0'); |
| 186 str = fmt.format(0); |
| 187 expect('.0', str); |
| 188 fmt = new NumberFormat('#'); |
| 189 str = fmt.format(0); |
| 190 expect('0', str); |
| 191 fmt = new NumberFormat('000'); |
| 192 str = fmt.format(0); |
| 193 expect('000', str); |
| 194 } |
| 195 |
| 196 void testExponential() { |
| 197 var str; |
| 198 var fmt; |
| 199 |
| 200 fmt = new NumberFormat('0.####E0'); |
| 201 str = fmt.format(0.01234); |
| 202 expect('1.234E-2', str); |
| 203 fmt = new NumberFormat('00.000E00'); |
| 204 str = fmt.format(0.01234); |
| 205 expect('12.340E-03', str); |
| 206 fmt = new NumberFormat('##0.######E000'); |
| 207 str = fmt.format(0.01234); |
| 208 expect('12.34E-003', str); |
| 209 fmt = new NumberFormat('0.###E0;[0.###E0]'); |
| 210 str = fmt.format(0.01234); |
| 211 expect('1.234E-2', str); |
| 212 |
| 213 fmt = new NumberFormat('0.####E0'); |
| 214 str = fmt.format(123456789); |
| 215 expect('1.2346E8', str); |
| 216 fmt = new NumberFormat('00.000E00'); |
| 217 str = fmt.format(123456789); |
| 218 expect('12.346E07', str); |
| 219 fmt = new NumberFormat('##0.######E000'); |
| 220 str = fmt.format(123456789); |
| 221 expect('123.456789E006', str); |
| 222 fmt = new NumberFormat('0.###E0;[0.###E0]'); |
| 223 str = fmt.format(123456789); |
| 224 expect('1.235E8', str); |
| 225 |
| 226 fmt = new NumberFormat('0.####E0'); |
| 227 str = fmt.format(1.23e300); |
| 228 expect('1.23E300', str); |
| 229 fmt = new NumberFormat('00.000E00'); |
| 230 str = fmt.format(1.23e300); |
| 231 expect('12.300E299', str); |
| 232 fmt = new NumberFormat('##0.######E000'); |
| 233 str = fmt.format(1.23e300); |
| 234 expect('1.23E300', str); |
| 235 fmt = new NumberFormat('0.###E0;[0.###E0]'); |
| 236 str = fmt.format(1.23e300); |
| 237 expect('1.23E300', str); |
| 238 |
| 239 fmt = new NumberFormat('0.####E0'); |
| 240 str = fmt.format(-3.141592653e-271); |
| 241 expect('-3.1416E-271', str); |
| 242 fmt = new NumberFormat('00.000E00'); |
| 243 str = fmt.format(-3.141592653e-271); |
| 244 expect('-31.416E-272', str); |
| 245 fmt = new NumberFormat('##0.######E000'); |
| 246 str = fmt.format(-3.141592653e-271); |
| 247 expect('-314.159265E-273', str); |
| 248 fmt = new NumberFormat('0.###E0;[0.###E0]'); |
| 249 str = fmt.format(-3.141592653e-271); |
| 250 expect('[3.142E-271]', str); |
| 251 |
| 252 fmt = new NumberFormat('0.####E0'); |
| 253 str = fmt.format(0); |
| 254 expect('0E0', str); |
| 255 fmt = new NumberFormat('00.000E00'); |
| 256 str = fmt.format(0); |
| 257 expect('00.000E00', str); |
| 258 fmt = new NumberFormat('##0.######E000'); |
| 259 str = fmt.format(0); |
| 260 expect('0E000', str); |
| 261 fmt = new NumberFormat('0.###E0;[0.###E0]'); |
| 262 str = fmt.format(0); |
| 263 expect('0E0', str); |
| 264 |
| 265 fmt = new NumberFormat('0.####E0'); |
| 266 str = fmt.format(-1); |
| 267 expect('-1E0', str); |
| 268 fmt = new NumberFormat('00.000E00'); |
| 269 str = fmt.format(-1); |
| 270 expect('-10.000E-01', str); |
| 271 fmt = new NumberFormat('##0.######E000'); |
| 272 str = fmt.format(-1); |
| 273 expect('-1E000', str); |
| 274 fmt = new NumberFormat('0.###E0;[0.###E0]'); |
| 275 str = fmt.format(-1); |
| 276 expect('[1E0]', str); |
| 277 |
| 278 fmt = new NumberFormat('0.####E0'); |
| 279 str = fmt.format(1); |
| 280 expect('1E0', str); |
| 281 fmt = new NumberFormat('00.000E00'); |
| 282 str = fmt.format(1); |
| 283 expect('10.000E-01', str); |
| 284 fmt = new NumberFormat('##0.######E000'); |
| 285 str = fmt.format(1); |
| 286 expect('1E000', str); |
| 287 fmt = new NumberFormat('0.###E0;[0.###E0]'); |
| 288 str = fmt.format(1); |
| 289 expect('1E0', str); |
| 290 |
| 291 fmt = new NumberFormat('#E0'); |
| 292 str = fmt.format(12345.0); |
| 293 expect('1E4', str); |
| 294 fmt = new NumberFormat('0E0'); |
| 295 str = fmt.format(12345.0); |
| 296 expect('1E4', str); |
| 297 fmt = new NumberFormat('##0.###E0'); |
| 298 str = fmt.format(12345.0); |
| 299 expect('12.345E3', str); |
| 300 fmt = new NumberFormat('##0.###E0'); |
| 301 str = fmt.format(12345.00001); |
| 302 expect('12.345E3', str); |
| 303 fmt = new NumberFormat('##0.###E0'); |
| 304 str = fmt.format(12345); |
| 305 expect('12.345E3', str); |
| 306 |
| 307 fmt = new NumberFormat('##0.####E0'); |
| 308 str = fmt.format(789.12345e-9); |
| 309 fmt = new NumberFormat('##0.####E0'); |
| 310 str = fmt.format(780e-9); |
| 311 expect('780E-9', str); |
| 312 fmt = new NumberFormat('.###E0'); |
| 313 str = fmt.format(45678.0); |
| 314 expect('.457E5', str); |
| 315 fmt = new NumberFormat('.###E0'); |
| 316 str = fmt.format(0); |
| 317 expect('.0E0', str); |
| 318 |
| 319 fmt = new NumberFormat('#E0'); |
| 320 str = fmt.format(45678000); |
| 321 expect('5E7', str); |
| 322 fmt = new NumberFormat('##E0'); |
| 323 str = fmt.format(45678000); |
| 324 expect('46E6', str); |
| 325 fmt = new NumberFormat('####E0'); |
| 326 str = fmt.format(45678000); |
| 327 expect('4568E4', str); |
| 328 fmt = new NumberFormat('0E0'); |
| 329 str = fmt.format(45678000); |
| 330 expect('5E7', str); |
| 331 fmt = new NumberFormat('00E0'); |
| 332 str = fmt.format(45678000); |
| 333 expect('46E6', str); |
| 334 fmt = new NumberFormat('000E0'); |
| 335 str = fmt.format(45678000); |
| 336 expect('457E5', str); |
| 337 fmt = new NumberFormat('###E0'); |
| 338 str = fmt.format(0.0000123); |
| 339 expect('12E-6', str); |
| 340 fmt = new NumberFormat('###E0'); |
| 341 str = fmt.format(0.000123); |
| 342 expect('123E-6', str); |
| 343 fmt = new NumberFormat('###E0'); |
| 344 str = fmt.format(0.00123); |
| 345 expect('1E-3', str); |
| 346 fmt = new NumberFormat('###E0'); |
| 347 str = fmt.format(0.0123); |
| 348 expect('12E-3', str); |
| 349 fmt = new NumberFormat('###E0'); |
| 350 str = fmt.format(0.123); |
| 351 expect('123E-3', str); |
| 352 fmt = new NumberFormat('###E0'); |
| 353 str = fmt.format(1.23); |
| 354 expect('1E0', str); |
| 355 fmt = new NumberFormat('###E0'); |
| 356 str = fmt.format(12.3); |
| 357 expect('12E0', str); |
| 358 fmt = new NumberFormat('###E0'); |
| 359 str = fmt.format(123.0); |
| 360 expect('123E0', str); |
| 361 fmt = new NumberFormat('###E0'); |
| 362 str = fmt.format(1230.0); |
| 363 expect('1E3', str); |
| 364 } |
| 365 |
| 366 void testPlusSignInExponentPart() { |
| 367 var fmt; |
| 368 fmt = new NumberFormat('0E+0'); |
| 369 var str = fmt.format(45678000); |
| 370 expect('5E+7', str); |
| 371 } |
| 372 |
| 373 void testApis() { |
| 374 var fmt; |
| 375 var str; |
| 376 |
| 377 fmt = new NumberFormat('#,###'); |
| 378 str = fmt.format(1234567890); |
| 379 expect('1,234,567,890', str); |
| 380 } |
| 381 |
| 382 testLocaleSwitch() { |
| 383 Intl.withLocale("fr", verifyFrenchLocale); |
| 384 } |
| 385 |
| 386 testLocaleSwitchAsync() { |
| 387 Intl.withLocale("fr", () { |
| 388 new Timer(new Duration(milliseconds: 10), expectAsync(verifyFrenchLocale)); |
| 389 }); |
| 390 // Verify that things running outside the zone still get en_US. |
| 391 testStandardFormat(); |
| 392 } |
| 393 |
| 394 void verifyFrenchLocale() { |
| 395 var fmt = new NumberFormat('#,###'); |
| 396 var str = fmt.format(1234567890); |
| 397 expect(str, '1\u00a0234\u00a0567\u00a0890'); |
| 398 } |
OLD | NEW |