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