| 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 import "package:expect/expect.dart"; | |
| 6 | |
| 7 // Short string. | |
| 8 var s = "0123456789"; | |
| 9 // Long string. | |
| 10 var l = "$s$s$s$s$s$s$s$s$s$s"; | |
| 11 // Very long string. | |
| 12 var v = "$l$l$l$l$l$l$l$l$l$l"; | |
| 13 | |
| 14 | |
| 15 testSliceSuccess() { | |
| 16 // Test different ways to get the same slice from a string. | |
| 17 testSlice(String string, int start, int end) { | |
| 18 int length = string.length; | |
| 19 String expect = string.substring(start, end); | |
| 20 Expect.equals(expect, string.slice(start, end), "#${length}[$start:$end]"); | |
| 21 if (start < length) { | |
| 22 // If start == length, there is no negative representation of the position
. | |
| 23 Expect.equals(expect, string.slice(start - length, end), | |
| 24 "#${length}[${start - length}:$end]"); | |
| 25 } | |
| 26 if (end < length) { | |
| 27 Expect.equals(expect, string.slice(start, end - length), | |
| 28 "#${length}[$start:${end - length}]"); | |
| 29 Expect.equals(expect, string.slice(start - length, end - length), | |
| 30 "#${length}[${start-length}:${end-length}]"); | |
| 31 } else { | |
| 32 Expect.equals(expect, string.slice(start), | |
| 33 "#${length}[$start]"); | |
| 34 if (start < length) { | |
| 35 Expect.equals(expect, string.slice(start - length), | |
| 36 "#${length}[${start-length}]"); | |
| 37 if (start == 0) { | |
| 38 Expect.equals(string, string.slice(), "#$length[:]"); | |
| 39 } | |
| 40 } | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 testSliceCombinations(String string) { | |
| 45 int length = string.length; | |
| 46 List<int> positions = [0, 1, string.length >> 1, length - 1, length]; | |
| 47 for (int i = 0; i < positions.length; i++) { | |
| 48 for (int j = i; j < positions.length; j++) { | |
| 49 testSlice(string, positions[i], positions[j]); | |
| 50 } | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 testSliceCombinations(s); | |
| 55 testSliceCombinations(l); | |
| 56 testSliceCombinations(v); | |
| 57 } | |
| 58 | |
| 59 testSliceError() { | |
| 60 function expectRangeError(void thunk()) { | |
| 61 Expect.throws(thunk, (e) => e is RangeError); | |
| 62 }; | |
| 63 function expectArgumentError(void thunk()) { | |
| 64 Expect.throws(thunk, (e) => e is ArgumentError); | |
| 65 }; | |
| 66 function badType(void thunk()) { | |
| 67 bool checkedMode = false; | |
| 68 assert(checkedMode = true); | |
| 69 Expect.throws(thunk, | |
| 70 (e) => checkedMode ? e is TypeError : e is ArgumentError); | |
| 71 } | |
| 72 | |
| 73 // Invalid start: | |
| 74 expectRangeError(() => s.slice(11)); | |
| 75 expectRangeError(() => s.slice(-11)); | |
| 76 // Invalid end: | |
| 77 expectRangeError(() => s.slice(0, 11)); | |
| 78 expectRangeError(() => s.slice(0, -11)); | |
| 79 // Non-int: | |
| 80 badType(() => s.slice(1.5)); | |
| 81 badType(() => s.slice(0, 1.5)); | |
| 82 badType(() => s.slice("1")); | |
| 83 badType(() => s.slice(0, "1")); | |
| 84 // Bad order: | |
| 85 expectArgumentError(() => s.slice(5, 4)); | |
| 86 expectArgumentError(() => s.slice(-5, 4)); | |
| 87 expectArgumentError(() => s.slice(5, -6)); | |
| 88 expectArgumentError(() => s.slice(-5, -6)); | |
| 89 } | |
| 90 | |
| 91 | |
| 92 main() { | |
| 93 testSliceSuccess(); | |
| 94 testSliceError(); | |
| 95 } | |
| OLD | NEW |