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("AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to")); | 9 Expect.equals("AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to")); |
10 | 10 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError); | 71 () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError); |
72 | 72 |
73 // Test startIndex too large | 73 // Test startIndex too large |
74 Expect.throws( | 74 Expect.throws( |
75 () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError); | 75 () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError); |
76 | 76 |
77 // Test null startIndex | 77 // Test null startIndex |
78 Expect.throws( | 78 Expect.throws( |
79 () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError); | 79 () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError); |
80 | 80 |
81 // Test object startIndex | |
82 Expect.throws(() => "hello".replaceFirst("h", "X", new Object())); | |
83 | |
84 // Test replaceFirstMapped. | 81 // Test replaceFirstMapped. |
85 | |
86 Expect.equals( | 82 Expect.equals( |
87 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to")); | 83 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to")); |
88 | 84 |
89 // Test with the replaced string at the beginning. | 85 // Test with the replaced string at the beginning. |
90 Expect.equals( | 86 Expect.equals( |
91 "toABtoCDtoE", "fromABtoCDtoE".replaceFirstMapped("from", (_) => "to")); | 87 "toABtoCDtoE", "fromABtoCDtoE".replaceFirstMapped("from", (_) => "to")); |
92 | 88 |
93 // Test with the replaced string at the end. | 89 // Test with the replaced string at the end. |
94 Expect.equals("toABtoCDtoEto", | 90 Expect.equals("toABtoCDtoEto", |
95 "fromABtoCDtoEto".replaceFirstMapped("from", (_) => "to")); | 91 "fromABtoCDtoEto".replaceFirstMapped("from", (_) => "to")); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 (e) => e is RangeError); | 149 (e) => e is RangeError); |
154 | 150 |
155 // Test startIndex too large | 151 // Test startIndex too large |
156 Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", 6), | 152 Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", 6), |
157 (e) => e is RangeError); | 153 (e) => e is RangeError); |
158 | 154 |
159 // Test null startIndex | 155 // Test null startIndex |
160 Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", null), | 156 Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", null), |
161 (e) => e is ArgumentError); | 157 (e) => e is ArgumentError); |
162 | 158 |
163 // Test object startIndex | |
164 Expect | |
165 .throws(() => "hello".replaceFirstMapped("h", (_) => "X", new Object())); | |
166 | |
167 // Test replacement depending on argument. | 159 // Test replacement depending on argument. |
168 Expect.equals("foo-BAR-foo-bar", | 160 Expect.equals("foo-BAR-foo-bar", |
169 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0].toUpperCase())); | 161 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0].toUpperCase())); |
170 | 162 |
171 Expect.equals("foo-[bar]-foo-bar", | 163 Expect.equals("foo-[bar]-foo-bar", |
172 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => "[${v[0]}]")); | 164 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => "[${v[0]}]")); |
173 | 165 |
174 Expect.equals("foo-foo-bar-foo-bar-foo-bar", | 166 Expect.equals("foo-foo-bar-foo-bar-foo-bar", |
175 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v.input)); | 167 "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v.input)); |
176 | 168 |
177 // Test replacement throwing. | 169 // Test replacement throwing. |
178 Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) => throw 42), | 170 Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) => throw 42), |
179 (e) => e == 42); | 171 (e) => e == 42); |
180 | 172 |
181 // Test replacement returning non-String. | 173 // Test replacement returning non-String. |
182 var o = new Object(); | 174 var o = new Object(); |
183 Expect.equals( | 175 Expect.equals( |
184 "foo-$o", | 176 "foo-$o", |
185 "foo-bar".replaceFirstMapped("bar", (v) { | 177 "foo-bar".replaceFirstMapped("bar", (v) { |
186 return o; | 178 return o; |
187 })); | 179 })); |
188 | 180 |
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"]) { | 181 for (var string in ["", "x", "foo", "x\u2000z"]) { |
202 for (var replacement in ["", "foo", string]) { | 182 for (var replacement in ["", "foo", string]) { |
203 for (int start = 0; start <= string.length; start++) { | 183 for (int start = 0; start <= string.length; start++) { |
204 var expect; | 184 var expect; |
205 for (int end = start; end <= string.length; end++) { | 185 for (int end = start; end <= string.length; end++) { |
206 expect = | 186 expect = |
207 string.substring(0, start) + replacement + string.substring(end); | 187 string.substring(0, start) + replacement + string.substring(end); |
208 Expect.equals(expect, string.replaceRange(start, end, replacement), | 188 Expect.equals(expect, string.replaceRange(start, end, replacement), |
209 '"$string"[$start:$end]="$replacement"'); | 189 '"$string"[$start:$end]="$replacement"'); |
210 } | 190 } |
211 // Reuse expect from "end == string.length" case when omitting end. | 191 // Reuse expect from "end == string.length" case when omitting end. |
212 Expect.equals(expect, string.replaceRange(start, null, replacement), | 192 Expect.equals(expect, string.replaceRange(start, null, replacement), |
213 '"$string"[$start:]="$replacement"'); | 193 '"$string"[$start:]="$replacement"'); |
214 } | 194 } |
215 } | 195 } |
216 Expect.throws(() => string.replaceRange(0, 0, null)); | 196 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")); | 197 Expect.throws(() => string.replaceRange(-1, 0, "x")); |
220 Expect.throws(() => string.replaceRange(0, string.length + 1, "x")); | 198 Expect.throws(() => string.replaceRange(0, string.length + 1, "x")); |
221 } | 199 } |
222 } | 200 } |
223 | |
224 // Fails to return a String on toString, throws if converted by "$naughty". | |
225 class Naughty { | |
226 toString() => this; | |
227 } | |
OLD | NEW |