| 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 class StringReplaceTest { | 7 main() { |
| 8 static testMain() { | 8 // Test replaceFirst. |
| 9 Expect.equals( | 9 Expect.equals( |
| 10 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to")); | 10 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to")); |
| 11 | 11 |
| 12 // Test with the replaced string at the begining. | 12 // Test with the replaced string at the begining. |
| 13 Expect.equals( | 13 Expect.equals( |
| 14 "toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to")); | 14 "toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to")); |
| 15 | 15 |
| 16 // Test with the replaced string at the end. | 16 // Test with the replaced string at the end. |
| 17 Expect.equals( | 17 Expect.equals( |
| 18 "toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to")); | 18 "toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to")); |
| 19 | 19 |
| 20 // Test when there are no occurence of the string to replace. | 20 // Test when there are no occurence of the string to replace. |
| 21 Expect.equals("ABC", "ABC".replaceFirst("from", "to")); | 21 Expect.equals("ABC", "ABC".replaceFirst("from", "to")); |
| 22 | 22 |
| 23 // Test when the string to change is the empty string. | 23 // Test when the string to change is the empty string. |
| 24 Expect.equals("", "".replaceFirst("from", "to")); | 24 Expect.equals("", "".replaceFirst("from", "to")); |
| 25 | 25 |
| 26 // Test when the string to change is a substring of the string to | 26 // Test when the string to change is a substring of the string to |
| 27 // replace. | 27 // replace. |
| 28 Expect.equals("fro", "fro".replaceFirst("from", "to")); | 28 Expect.equals("fro", "fro".replaceFirst("from", "to")); |
| 29 | 29 |
| 30 // Test when the string to change is the replaced string. | 30 // Test when the string to change is the replaced string. |
| 31 Expect.equals("to", "from".replaceFirst("from", "to")); | 31 Expect.equals("to", "from".replaceFirst("from", "to")); |
| 32 | 32 |
| 33 // Test when the string to change is the replacement string. | 33 // Test when the string to change is the replacement string. |
| 34 Expect.equals("to", "to".replaceFirst("from", "to")); | 34 Expect.equals("to", "to".replaceFirst("from", "to")); |
| 35 | 35 |
| 36 // Test replacing by the empty string. | 36 // Test replacing by the empty string. |
| 37 Expect.equals("", "from".replaceFirst("from", "")); | 37 Expect.equals("", "from".replaceFirst("from", "")); |
| 38 Expect.equals("AB", "AfromB".replaceFirst("from", "")); | 38 Expect.equals("AB", "AfromB".replaceFirst("from", "")); |
| 39 | 39 |
| 40 // Test changing the empty string. | 40 // Test changing the empty string. |
| 41 Expect.equals("to", "".replaceFirst("", "to")); | 41 Expect.equals("to", "".replaceFirst("", "to")); |
| 42 | 42 |
| 43 // Test replacing the empty string. | 43 // Test replacing the empty string. |
| 44 Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirst("", "to")); | 44 Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirst("", "to")); |
| 45 | 45 |
| 46 // Test startIndex. | 46 // Test startIndex. |
| 47 Expect.equals( | 47 Expect.equals( |
| 48 "foo-AAA-foo-bar", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 4)); | 48 "foo-AAA-foo-bar", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 4)); |
| 49 | 49 |
| 50 // Test startIndex skipping one case at the begining. | 50 // Test startIndex skipping one case at the begining. |
| 51 Expect.equals( | 51 Expect.equals( |
| 52 "foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1)); | 52 "foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1)); |
| 53 | 53 |
| 54 // Test startIndex skipping one case at the begining. | 54 // Test startIndex skipping one case at the begining. |
| 55 Expect.equals( | 55 Expect.equals( |
| 56 "foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5)); | 56 "foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5)); |
| 57 | 57 |
| 58 // Test startIndex replacing with the empty string. | 58 // Test startIndex replacing with the empty string. |
| 59 Expect.equals( | 59 Expect.equals( |
| 60 "foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1)); | 60 "foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1)); |
| 61 | 61 |
| 62 // Test startIndex with a RegExp with carat | 62 // Test startIndex with a RegExp with carat |
| 63 Expect.equals( | 63 Expect.equals( |
| 64 "foo-bar-foo-bar", | 64 "foo-bar-foo-bar", |
| 65 "foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8)); | 65 "foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8)); |
| 66 | 66 |
| 67 // Test startIndex with a RegExp | 67 // Test startIndex with a RegExp |
| 68 Expect.equals( | 68 Expect.equals( |
| 69 "aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1)); | 69 "aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1)); |
| 70 | 70 |
| 71 // Test startIndex with regexp-looking String | 71 // Test startIndex with regexp-looking String |
| 72 Expect.equals( | 72 Expect.equals( |
| 73 "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 | 74 |
| 75 // Test negative startIndex | 75 // Test negative startIndex |
| 76 Expect.throws( | 76 Expect.throws( |
| 77 () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError); | 77 () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError); |
| 78 | 78 |
| 79 // Test startIndex too large | 79 // Test startIndex too large |
| 80 Expect.throws( | 80 Expect.throws( |
| 81 () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError); | 81 () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError); |
| 82 | 82 |
| 83 // Test null startIndex | 83 // Test null startIndex |
| 84 Expect.throws( | 84 Expect.throws( |
| 85 () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError); | 85 () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError); |
| 86 | 86 |
| 87 // Test object startIndex | 87 // Test object startIndex |
| 88 Expect.throws( | 88 Expect.throws( |
| 89 () => "hello".replaceFirst("h", "X", new Object())); | 89 () => "hello".replaceFirst("h", "X", new Object())); |
| 90 } | 90 |
| 91 |
| 92 // Test replaceFirstMapped. |
| 93 |
| 94 Expect.equals( |
| 95 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to")); |
| 96 |
| 97 // Test with the replaced string at the begining. |
| 98 Expect.equals( |
| 99 "toABtoCDtoE", "fromABtoCDtoE".replaceFirstMapped("from", (_) => "to")); |
| 100 |
| 101 // Test with the replaced string at the end. |
| 102 Expect.equals( |
| 103 "toABtoCDtoEto", |
| 104 "fromABtoCDtoEto".replaceFirstMapped("from", (_) => "to")); |
| 105 |
| 106 // Test when there are no occurence of the string to replace. |
| 107 Expect.equals("ABC", "ABC".replaceFirstMapped("from", (_) => "to")); |
| 108 |
| 109 // Test when the string to change is the empty string. |
| 110 Expect.equals("", "".replaceFirstMapped("from", (_) => "to")); |
| 111 |
| 112 // Test when the string to change is a substring of the string to |
| 113 // replace. |
| 114 Expect.equals("fro", "fro".replaceFirstMapped("from", (_) => "to")); |
| 115 |
| 116 // Test when the string to change is the replaced string. |
| 117 Expect.equals("to", "from".replaceFirstMapped("from", (_) => "to")); |
| 118 |
| 119 // Test when the string to change is the replacement string. |
| 120 Expect.equals("to", "to".replaceFirstMapped("from", (_) => "to")); |
| 121 |
| 122 // Test replacing by the empty string. |
| 123 Expect.equals("", "from".replaceFirstMapped("from", (_) => "")); |
| 124 Expect.equals("AB", "AfromB".replaceFirstMapped("from", (_) => "")); |
| 125 |
| 126 // Test changing the empty string. |
| 127 Expect.equals("to", "".replaceFirstMapped("", (_) => "to")); |
| 128 |
| 129 // Test replacing the empty string. |
| 130 Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirstMapped("", (_) => "to")); |
| 131 |
| 132 // Test startIndex. |
| 133 Expect.equals( |
| 134 "foo-AAA-foo-bar", |
| 135 "foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 4)); |
| 136 |
| 137 // Test startIndex skipping one case at the begining. |
| 138 Expect.equals( |
| 139 "foo-bar-AAA-bar", |
| 140 "foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "AAA", 1)); |
| 141 |
| 142 // Test startIndex skipping one case at the begining. |
| 143 Expect.equals( |
| 144 "foo-bar-foo-AAA", |
| 145 "foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 5)); |
| 146 |
| 147 // Test startIndex replacing with the empty string. |
| 148 Expect.equals( |
| 149 "foo-bar--bar", "foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "", 1))
; |
| 150 |
| 151 // Test startIndex with a RegExp with carat |
| 152 Expect.equals( |
| 153 "foo-bar-foo-bar", |
| 154 "foo-bar-foo-bar".replaceFirstMapped(new RegExp(r"^foo"), (_) => "", 8)); |
| 155 |
| 156 // Test startIndex with a RegExp |
| 157 Expect.equals( |
| 158 "aaa{3}X{3}", |
| 159 "aaa{3}aaa{3}".replaceFirstMapped(new RegExp(r"a{3}"), (_) => "X", 1)); |
| 160 |
| 161 // Test startIndex with regexp-looking String |
| 162 Expect.equals( |
| 163 "aaa{3}aaX", "aaa{3}aaa{3}".replaceFirstMapped("a{3}", (_) => "X", 3)); |
| 164 |
| 165 // Test negative startIndex |
| 166 Expect.throws( |
| 167 () => "hello".replaceFirstMapped("h", (_) => "X", -1), |
| 168 (e) => e is RangeError); |
| 169 |
| 170 // Test startIndex too large |
| 171 Expect.throws( |
| 172 () => "hello".replaceFirstMapped("h", (_) => "X", 6), |
| 173 (e) => e is RangeError); |
| 174 |
| 175 // Test null startIndex |
| 176 Expect.throws( |
| 177 () => "hello".replaceFirstMapped("h", (_) => "X", null), |
| 178 (e) => e is ArgumentError); |
| 179 |
| 180 // Test object startIndex |
| 181 Expect.throws( |
| 182 () => "hello".replaceFirstMapped("h", (_) => "X", new Object())); |
| 183 |
| 184 // Test replacement depending on argument. |
| 185 Expect.equals( |
| 186 "foo-BAR-foo-bar", |
| 187 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0].toUpperCase())); |
| 188 |
| 189 Expect.equals( |
| 190 "foo-[bar]-foo-bar", |
| 191 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => "[${v[0]}]")); |
| 192 |
| 193 Expect.equals("foo-foo-bar-foo-bar-foo-bar", |
| 194 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v.input)); |
| 195 |
| 196 // Test replacement throwing. |
| 197 Expect.throws( |
| 198 () => "foo-bar".replaceFirstMapped("bar", (v) => throw 42), |
| 199 (e) => e == 42); |
| 200 |
| 201 // Test replacement returning non-String. |
| 202 var o = new Object(); |
| 203 Expect.equals("foo-$o", "foo-bar".replaceFirstMapped("bar", |
| 204 (v) { return o; })); |
| 205 |
| 206 Expect.equals("foo-42", "foo-bar".replaceFirstMapped("bar", |
| 207 (v) { return 42; })); |
| 208 |
| 209 // Test replacement returning object throwing on string-conversion. |
| 210 var n = new Naughty(); |
| 211 Expect.throws( |
| 212 () => "foo-bar".replaceFirstMapped("bar", (v) { return n; })); |
| 91 } | 213 } |
| 92 | 214 |
| 93 main() { | 215 // Fails to return a String on toString, throws if converted by "$naughty". |
| 94 StringReplaceTest.testMain(); | 216 class Naughty { |
| 217 toString() => this; |
| 95 } | 218 } |
| OLD | NEW |