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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 168 |
169 // Test replacement throwing. | 169 // Test replacement throwing. |
170 Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) => throw 42), | 170 Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) => throw 42), |
171 (e) => e == 42); | 171 (e) => e == 42); |
172 | 172 |
173 // Test replacement returning non-String. | 173 // Test replacement returning non-String. |
174 var o = new Object(); | 174 var o = new Object(); |
175 Expect.equals( | 175 Expect.equals( |
176 "foo-$o", | 176 "foo-$o", |
177 "foo-bar".replaceFirstMapped("bar", (v) { | 177 "foo-bar".replaceFirstMapped("bar", (v) { |
178 return o; | 178 // TODO(jmesserly): in strong mode, this function must return a string. |
| 179 // If we want to allow any Object, we'll have to fix the API signature. |
| 180 // See https://github.com/dart-lang/sdk/issues/30248. |
| 181 return '$o'; |
179 })); | 182 })); |
180 | 183 |
181 for (var string in ["", "x", "foo", "x\u2000z"]) { | 184 for (var string in ["", "x", "foo", "x\u2000z"]) { |
182 for (var replacement in ["", "foo", string]) { | 185 for (var replacement in ["", "foo", string]) { |
183 for (int start = 0; start <= string.length; start++) { | 186 for (int start = 0; start <= string.length; start++) { |
184 var expect; | 187 var expect; |
185 for (int end = start; end <= string.length; end++) { | 188 for (int end = start; end <= string.length; end++) { |
186 expect = | 189 expect = |
187 string.substring(0, start) + replacement + string.substring(end); | 190 string.substring(0, start) + replacement + string.substring(end); |
188 Expect.equals(expect, string.replaceRange(start, end, replacement), | 191 Expect.equals(expect, string.replaceRange(start, end, replacement), |
189 '"$string"[$start:$end]="$replacement"'); | 192 '"$string"[$start:$end]="$replacement"'); |
190 } | 193 } |
191 // Reuse expect from "end == string.length" case when omitting end. | 194 // Reuse expect from "end == string.length" case when omitting end. |
192 Expect.equals(expect, string.replaceRange(start, null, replacement), | 195 Expect.equals(expect, string.replaceRange(start, null, replacement), |
193 '"$string"[$start:]="$replacement"'); | 196 '"$string"[$start:]="$replacement"'); |
194 } | 197 } |
195 } | 198 } |
196 Expect.throws(() => string.replaceRange(0, 0, null)); | 199 Expect.throws(() => string.replaceRange(0, 0, null)); |
197 Expect.throws(() => string.replaceRange(-1, 0, "x")); | 200 Expect.throws(() => string.replaceRange(-1, 0, "x")); |
198 Expect.throws(() => string.replaceRange(0, string.length + 1, "x")); | 201 Expect.throws(() => string.replaceRange(0, string.length + 1, "x")); |
199 } | 202 } |
200 } | 203 } |
OLD | NEW |