OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 void main() { |
| 6 bool checkedMode = false; |
| 7 assert(checkedMode = true); |
| 8 |
| 9 for (int i = 0; i < 36 * 36 + 1; i++) { |
| 10 for (int r = 2; r <= 36; r++) { |
| 11 String radixString = i.toRadixString(r); |
| 12 Expect.equals(i, int.parse(radixString, radix: r), ""); |
| 13 Expect.equals(i, int.parse(" $radixString", radix: r), ""); |
| 14 Expect.equals(i, int.parse("$radixString ", radix: r), ""); |
| 15 Expect.equals(i, int.parse(" $radixString ", radix: r), ""); |
| 16 Expect.equals(i, int.parse("+$radixString", radix: r), ""); |
| 17 Expect.equals(i, int.parse(" +$radixString", radix: r), ""); |
| 18 Expect.equals(i, int.parse("+$radixString ", radix: r), ""); |
| 19 Expect.equals(i, int.parse(" +$radixString ", radix: r), ""); |
| 20 Expect.equals(-i, int.parse("-$radixString", radix: r), ""); |
| 21 Expect.equals(-i, int.parse(" -$radixString", radix: r), ""); |
| 22 Expect.equals(-i, int.parse("-$radixString ", radix: r), ""); |
| 23 Expect.equals(-i, int.parse(" -$radixString ", radix: r), ""); |
| 24 } |
| 25 } |
| 26 // Allow both upper- and lower-case letters. |
| 27 Expect.equals(0xABCD, int.parse("ABCD", radix: 16)); |
| 28 Expect.equals(0xABCD, int.parse("abcd", radix: 16)); |
| 29 Expect.equals(15628859, int.parse("09azAZ", radix: 36)); |
| 30 |
| 31 // Allow whitespace before and after the number. |
| 32 Expect.equals(1, int.parse(" 1", radix: 2)); |
| 33 Expect.equals(1, int.parse("1 ", radix: 2)); |
| 34 Expect.equals(1, int.parse(" 1 ", radix: 2)); |
| 35 Expect.equals(1, int.parse("\n1", radix: 2)); |
| 36 Expect.equals(1, int.parse("1\n", radix: 2)); |
| 37 Expect.equals(1, int.parse("\n1\n", radix: 2)); |
| 38 Expect.equals(1, int.parse("+1", radix: 2)); |
| 39 |
| 40 void testFails(String source, int radix) { |
| 41 Expect.throws(() { throw int.parse(source, radix: radix, |
| 42 onError: (s) { throw "FAIL"; }); }, |
| 43 (e) => e == "FAIL", |
| 44 "$source/$radix"); |
| 45 Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999)); |
| 46 } |
| 47 for (int i = 2; i < 36; i++) { |
| 48 testFails(i.toRadixString(36), i); |
| 49 } |
| 50 testFails("", 2); |
| 51 testFails("0x10", 16); // No 0x specially allowed. |
| 52 testFails("+ 1", 2); // No space between sign and digits. |
| 53 testFails("- 1", 2); // No space between sign and digits. |
| 54 |
| 55 testBadTypes(var source, var radix) { |
| 56 if (!checkedMode) { |
| 57 // No promises on what error is thrown if the type doesn't match. |
| 58 // Likely either ArgumentError or NoSuchMethodError. |
| 59 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0)); |
| 60 return; |
| 61 } |
| 62 // In checked mode, it's always a TypeError. |
| 63 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0), |
| 64 (e) => e is TypeError); |
| 65 } |
| 66 |
| 67 testBadTypes(9, 10); |
| 68 testBadTypes(true, 10); |
| 69 testBadTypes("0", true); |
| 70 testBadTypes("0", "10"); |
| 71 |
| 72 testBadArguments(String source, int radix) { |
| 73 // If the types match, it should be an ArgumentError of some sort. |
| 74 Expect.throws(() => int.parse(source, radix: radix, onError: (s) => 0), |
| 75 (e) => e is ArgumentError); |
| 76 } |
| 77 |
| 78 testBadArguments("0", -1); |
| 79 testBadArguments("0", 0); |
| 80 testBadArguments("0", 1); |
| 81 testBadArguments("0", 37); |
| 82 |
| 83 // If handleError isn't an unary function, and it's called, it also throws |
| 84 // (either TypeError in checked mode, or some failure in unchecked mode). |
| 85 Expect.throws(() => int.parse("9", radix: 8, onError: "not a function")); |
| 86 Expect.throws(() => int.parse("9", radix: 8, onError: () => 42)); |
| 87 Expect.throws(() => int.parse("9", radix: 8, onError: (v1, v2) => 42)); |
| 88 } |
OLD | NEW |