| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 | 6 |
| 7 main() { | 7 main() { |
| 8 // Test replaceFirst. | 8 // Test replaceFirst. |
| 9 Expect.equals( | 9 Expect.equals("AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to")); |
| 10 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to")); | |
| 11 | 10 |
| 12 // Test with the replaced string at the begining. | 11 // Test with the replaced string at the begining. |
| 13 Expect.equals( | 12 Expect.equals("toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to")); |
| 14 "toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to")); | |
| 15 | 13 |
| 16 // Test with the replaced string at the end. | 14 // Test with the replaced string at the end. |
| 17 Expect.equals( | 15 Expect.equals("toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to")); |
| 18 "toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to")); | |
| 19 | 16 |
| 20 // Test when there are no occurence of the string to replace. | 17 // Test when there are no occurence of the string to replace. |
| 21 Expect.equals("ABC", "ABC".replaceFirst("from", "to")); | 18 Expect.equals("ABC", "ABC".replaceFirst("from", "to")); |
| 22 | 19 |
| 23 // Test when the string to change is the empty string. | 20 // Test when the string to change is the empty string. |
| 24 Expect.equals("", "".replaceFirst("from", "to")); | 21 Expect.equals("", "".replaceFirst("from", "to")); |
| 25 | 22 |
| 26 // Test when the string to change is a substring of the string to | 23 // Test when the string to change is a substring of the string to |
| 27 // replace. | 24 // replace. |
| 28 Expect.equals("fro", "fro".replaceFirst("from", "to")); | 25 Expect.equals("fro", "fro".replaceFirst("from", "to")); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 49 | 46 |
| 50 // Test startIndex skipping one case at the begining. | 47 // Test startIndex skipping one case at the begining. |
| 51 Expect.equals( | 48 Expect.equals( |
| 52 "foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1)); | 49 "foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1)); |
| 53 | 50 |
| 54 // Test startIndex skipping one case at the begining. | 51 // Test startIndex skipping one case at the begining. |
| 55 Expect.equals( | 52 Expect.equals( |
| 56 "foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5)); | 53 "foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5)); |
| 57 | 54 |
| 58 // Test startIndex replacing with the empty string. | 55 // Test startIndex replacing with the empty string. |
| 59 Expect.equals( | 56 Expect.equals("foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1)); |
| 60 "foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1)); | |
| 61 | 57 |
| 62 // Test startIndex with a RegExp with carat | 58 // Test startIndex with a RegExp with carat |
| 63 Expect.equals( | 59 Expect.equals("foo-bar-foo-bar", |
| 64 "foo-bar-foo-bar", | |
| 65 "foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8)); | 60 "foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8)); |
| 66 | 61 |
| 67 // Test startIndex with a RegExp | 62 // Test startIndex with a RegExp |
| 68 Expect.equals( | 63 Expect.equals( |
| 69 "aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1)); | 64 "aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1)); |
| 70 | 65 |
| 71 // Test startIndex with regexp-looking String | 66 // Test startIndex with regexp-looking String |
| 72 Expect.equals( | 67 Expect.equals("aaa{3}aaX", "aaa{3}aaa{3}".replaceFirst("a{3}", "X", 3)); |
| 73 "aaa{3}aaX", "aaa{3}aaa{3}".replaceFirst("a{3}", "X", 3)); | |
| 74 | 68 |
| 75 // Test negative startIndex | 69 // Test negative startIndex |
| 76 Expect.throws( | 70 Expect.throws( |
| 77 () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError); | 71 () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError); |
| 78 | 72 |
| 79 // Test startIndex too large | 73 // Test startIndex too large |
| 80 Expect.throws( | 74 Expect.throws( |
| 81 () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError); | 75 () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError); |
| 82 | 76 |
| 83 // Test null startIndex | 77 // Test null startIndex |
| 84 Expect.throws( | 78 Expect.throws( |
| 85 () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError); | 79 () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError); |
| 86 | 80 |
| 87 // Test object startIndex | 81 // Test object startIndex |
| 88 Expect.throws( | 82 Expect.throws(() => "hello".replaceFirst("h", "X", new Object())); |
| 89 () => "hello".replaceFirst("h", "X", new Object())); | |
| 90 | |
| 91 | 83 |
| 92 // Test replaceFirstMapped. | 84 // Test replaceFirstMapped. |
| 93 | 85 |
| 94 Expect.equals( | 86 Expect.equals( |
| 95 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to")); | 87 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to")); |
| 96 | 88 |
| 97 // Test with the replaced string at the begining. | 89 // Test with the replaced string at the begining. |
| 98 Expect.equals( | 90 Expect.equals( |
| 99 "toABtoCDtoE", "fromABtoCDtoE".replaceFirstMapped("from", (_) => "to")); | 91 "toABtoCDtoE", "fromABtoCDtoE".replaceFirstMapped("from", (_) => "to")); |
| 100 | 92 |
| 101 // Test with the replaced string at the end. | 93 // Test with the replaced string at the end. |
| 102 Expect.equals( | 94 Expect.equals("toABtoCDtoEto", |
| 103 "toABtoCDtoEto", | |
| 104 "fromABtoCDtoEto".replaceFirstMapped("from", (_) => "to")); | 95 "fromABtoCDtoEto".replaceFirstMapped("from", (_) => "to")); |
| 105 | 96 |
| 106 // Test when there are no occurence of the string to replace. | 97 // Test when there are no occurence of the string to replace. |
| 107 Expect.equals("ABC", "ABC".replaceFirstMapped("from", (_) => "to")); | 98 Expect.equals("ABC", "ABC".replaceFirstMapped("from", (_) => "to")); |
| 108 | 99 |
| 109 // Test when the string to change is the empty string. | 100 // Test when the string to change is the empty string. |
| 110 Expect.equals("", "".replaceFirstMapped("from", (_) => "to")); | 101 Expect.equals("", "".replaceFirstMapped("from", (_) => "to")); |
| 111 | 102 |
| 112 // Test when the string to change is a substring of the string to | 103 // Test when the string to change is a substring of the string to |
| 113 // replace. | 104 // replace. |
| 114 Expect.equals("fro", "fro".replaceFirstMapped("from", (_) => "to")); | 105 Expect.equals("fro", "fro".replaceFirstMapped("from", (_) => "to")); |
| 115 | 106 |
| 116 // Test when the string to change is the replaced string. | 107 // Test when the string to change is the replaced string. |
| 117 Expect.equals("to", "from".replaceFirstMapped("from", (_) => "to")); | 108 Expect.equals("to", "from".replaceFirstMapped("from", (_) => "to")); |
| 118 | 109 |
| 119 // Test when the string to change is the replacement string. | 110 // Test when the string to change is the replacement string. |
| 120 Expect.equals("to", "to".replaceFirstMapped("from", (_) => "to")); | 111 Expect.equals("to", "to".replaceFirstMapped("from", (_) => "to")); |
| 121 | 112 |
| 122 // Test replacing by the empty string. | 113 // Test replacing by the empty string. |
| 123 Expect.equals("", "from".replaceFirstMapped("from", (_) => "")); | 114 Expect.equals("", "from".replaceFirstMapped("from", (_) => "")); |
| 124 Expect.equals("AB", "AfromB".replaceFirstMapped("from", (_) => "")); | 115 Expect.equals("AB", "AfromB".replaceFirstMapped("from", (_) => "")); |
| 125 | 116 |
| 126 // Test changing the empty string. | 117 // Test changing the empty string. |
| 127 Expect.equals("to", "".replaceFirstMapped("", (_) => "to")); | 118 Expect.equals("to", "".replaceFirstMapped("", (_) => "to")); |
| 128 | 119 |
| 129 // Test replacing the empty string. | 120 // Test replacing the empty string. |
| 130 Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirstMapped("", (_) => "to")); | 121 Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirstMapped("", (_) => "to")); |
| 131 | 122 |
| 132 // Test startIndex. | 123 // Test startIndex. |
| 133 Expect.equals( | 124 Expect.equals("foo-AAA-foo-bar", |
| 134 "foo-AAA-foo-bar", | |
| 135 "foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 4)); | 125 "foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 4)); |
| 136 | 126 |
| 137 // Test startIndex skipping one case at the begining. | 127 // Test startIndex skipping one case at the begining. |
| 138 Expect.equals( | 128 Expect.equals("foo-bar-AAA-bar", |
| 139 "foo-bar-AAA-bar", | |
| 140 "foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "AAA", 1)); | 129 "foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "AAA", 1)); |
| 141 | 130 |
| 142 // Test startIndex skipping one case at the begining. | 131 // Test startIndex skipping one case at the begining. |
| 143 Expect.equals( | 132 Expect.equals("foo-bar-foo-AAA", |
| 144 "foo-bar-foo-AAA", | |
| 145 "foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 5)); | 133 "foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 5)); |
| 146 | 134 |
| 147 // Test startIndex replacing with the empty string. | 135 // Test startIndex replacing with the empty string. |
| 148 Expect.equals( | 136 Expect.equals("foo-bar--bar", |
| 149 "foo-bar--bar", "foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "", 1))
; | 137 "foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "", 1)); |
| 150 | 138 |
| 151 // Test startIndex with a RegExp with carat | 139 // Test startIndex with a RegExp with carat |
| 152 Expect.equals( | 140 Expect.equals("foo-bar-foo-bar", |
| 153 "foo-bar-foo-bar", | |
| 154 "foo-bar-foo-bar".replaceFirstMapped(new RegExp(r"^foo"), (_) => "", 8)); | 141 "foo-bar-foo-bar".replaceFirstMapped(new RegExp(r"^foo"), (_) => "", 8)); |
| 155 | 142 |
| 156 // Test startIndex with a RegExp | 143 // Test startIndex with a RegExp |
| 157 Expect.equals( | 144 Expect.equals("aaa{3}X{3}", |
| 158 "aaa{3}X{3}", | |
| 159 "aaa{3}aaa{3}".replaceFirstMapped(new RegExp(r"a{3}"), (_) => "X", 1)); | 145 "aaa{3}aaa{3}".replaceFirstMapped(new RegExp(r"a{3}"), (_) => "X", 1)); |
| 160 | 146 |
| 161 // Test startIndex with regexp-looking String | 147 // Test startIndex with regexp-looking String |
| 162 Expect.equals( | 148 Expect.equals( |
| 163 "aaa{3}aaX", "aaa{3}aaa{3}".replaceFirstMapped("a{3}", (_) => "X", 3)); | 149 "aaa{3}aaX", "aaa{3}aaa{3}".replaceFirstMapped("a{3}", (_) => "X", 3)); |
| 164 | 150 |
| 165 // Test negative startIndex | 151 // Test negative startIndex |
| 166 Expect.throws( | 152 Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", -1), |
| 167 () => "hello".replaceFirstMapped("h", (_) => "X", -1), | |
| 168 (e) => e is RangeError); | 153 (e) => e is RangeError); |
| 169 | 154 |
| 170 // Test startIndex too large | 155 // Test startIndex too large |
| 171 Expect.throws( | 156 Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", 6), |
| 172 () => "hello".replaceFirstMapped("h", (_) => "X", 6), | |
| 173 (e) => e is RangeError); | 157 (e) => e is RangeError); |
| 174 | 158 |
| 175 // Test null startIndex | 159 // Test null startIndex |
| 176 Expect.throws( | 160 Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", null), |
| 177 () => "hello".replaceFirstMapped("h", (_) => "X", null), | |
| 178 (e) => e is ArgumentError); | 161 (e) => e is ArgumentError); |
| 179 | 162 |
| 180 // Test object startIndex | 163 // Test object startIndex |
| 181 Expect.throws( | 164 Expect |
| 182 () => "hello".replaceFirstMapped("h", (_) => "X", new Object())); | 165 .throws(() => "hello".replaceFirstMapped("h", (_) => "X", new Object())); |
| 183 | 166 |
| 184 // Test replacement depending on argument. | 167 // Test replacement depending on argument. |
| 185 Expect.equals( | 168 Expect.equals("foo-BAR-foo-bar", |
| 186 "foo-BAR-foo-bar", | |
| 187 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0].toUpperCase())); | 169 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0].toUpperCase())); |
| 188 | 170 |
| 189 Expect.equals( | 171 Expect.equals("foo-[bar]-foo-bar", |
| 190 "foo-[bar]-foo-bar", | |
| 191 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => "[${v[0]}]")); | 172 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => "[${v[0]}]")); |
| 192 | 173 |
| 193 Expect.equals("foo-foo-bar-foo-bar-foo-bar", | 174 Expect.equals("foo-foo-bar-foo-bar-foo-bar", |
| 194 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v.input)); | 175 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v.input)); |
| 195 | 176 |
| 196 // Test replacement throwing. | 177 // Test replacement throwing. |
| 197 Expect.throws( | 178 Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) => throw 42), |
| 198 () => "foo-bar".replaceFirstMapped("bar", (v) => throw 42), | 179 (e) => e == 42); |
| 199 (e) => e == 42); | |
| 200 | 180 |
| 201 // Test replacement returning non-String. | 181 // Test replacement returning non-String. |
| 202 var o = new Object(); | 182 var o = new Object(); |
| 203 Expect.equals("foo-$o", "foo-bar".replaceFirstMapped("bar", | 183 Expect.equals( |
| 204 (v) { return o; })); | 184 "foo-$o", |
| 185 "foo-bar".replaceFirstMapped("bar", (v) { |
| 186 return o; |
| 187 })); |
| 205 | 188 |
| 206 Expect.equals("foo-42", "foo-bar".replaceFirstMapped("bar", | 189 Expect.equals( |
| 207 (v) { return 42; })); | 190 "foo-42", |
| 191 "foo-bar".replaceFirstMapped("bar", (v) { |
| 192 return 42; |
| 193 })); |
| 208 | 194 |
| 209 // Test replacement returning object throwing on string-conversion. | 195 // Test replacement returning object throwing on string-conversion. |
| 210 var n = new Naughty(); | 196 var n = new Naughty(); |
| 211 Expect.throws( | 197 Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) { |
| 212 () => "foo-bar".replaceFirstMapped("bar", (v) { return n; })); | 198 return n; |
| 199 })); |
| 213 | 200 |
| 214 for (var string in ["", "x", "foo", "x\u2000z"]) { | 201 for (var string in ["", "x", "foo", "x\u2000z"]) { |
| 215 for (var replacement in ["", "foo", string]) { | 202 for (var replacement in ["", "foo", string]) { |
| 216 for (int start = 0; start <= string.length; start++) { | 203 for (int start = 0; start <= string.length; start++) { |
| 217 var expect; | 204 var expect; |
| 218 for (int end = start; end <= string.length; end++) { | 205 for (int end = start; end <= string.length; end++) { |
| 219 expect = string.substring(0, start) + | 206 expect = |
| 220 replacement + | 207 string.substring(0, start) + replacement + string.substring(end); |
| 221 string.substring(end); | |
| 222 Expect.equals(expect, string.replaceRange(start, end, replacement), | 208 Expect.equals(expect, string.replaceRange(start, end, replacement), |
| 223 '"$string"[$start:$end]="$replacement"'); | 209 '"$string"[$start:$end]="$replacement"'); |
| 224 } | 210 } |
| 225 // Reuse expect from "end == string.length" case when omitting end. | 211 // Reuse expect from "end == string.length" case when omitting end. |
| 226 Expect.equals(expect, string.replaceRange(start, null, replacement), | 212 Expect.equals(expect, string.replaceRange(start, null, replacement), |
| 227 '"$string"[$start:]="$replacement"'); | 213 '"$string"[$start:]="$replacement"'); |
| 228 } | 214 } |
| 229 } | 215 } |
| 230 Expect.throws(() => string.replaceRange(0, 0, null)); | 216 Expect.throws(() => string.replaceRange(0, 0, null)); |
| 231 Expect.throws(() => string.replaceRange(0, 0, 42)); | 217 Expect.throws(() => string.replaceRange(0, 0, 42)); |
| 232 Expect.throws(() => string.replaceRange(0, 0, ["x"])); | 218 Expect.throws(() => string.replaceRange(0, 0, ["x"])); |
| 233 Expect.throws(() => string.replaceRange(-1, 0, "x")); | 219 Expect.throws(() => string.replaceRange(-1, 0, "x")); |
| 234 Expect.throws(() => string.replaceRange(0, string.length + 1, "x")); | 220 Expect.throws(() => string.replaceRange(0, string.length + 1, "x")); |
| 235 } | 221 } |
| 236 } | 222 } |
| 237 | 223 |
| 238 // Fails to return a String on toString, throws if converted by "$naughty". | 224 // Fails to return a String on toString, throws if converted by "$naughty". |
| 239 class Naughty { | 225 class Naughty { |
| 240 toString() => this; | 226 toString() => this; |
| 241 } | 227 } |
| OLD | NEW |