Chromium Code Reviews| 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 class StringReplaceTest { |
| 8 static testMain() { | 8 static testMain() { |
| 9 Expect.equals( | 9 Expect.equals( |
| 10 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to")); | 10 "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to")); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 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 | |
| 46 // Test startIndex. | |
| 47 Expect.equals( | |
| 48 "foo-AAA-foo-bar", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 4)); | |
| 49 | |
| 50 // Test startIndex skipping one case at the begining. | |
| 51 Expect.equals( | |
| 52 "foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1)); | |
| 53 | |
| 54 // Test startIndex skipping one case at the begining. | |
| 55 Expect.equals( | |
| 56 "foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5)); | |
| 57 | |
| 58 // Test startIndex replacing with the empty string. | |
| 59 Expect.equals( | |
| 60 "foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1)); | |
| 61 | |
| 62 // Test startIndex with a RegExp with carat | |
| 63 Expect.equals( | |
| 64 "foo-bar-foo-bar", | |
| 65 "foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8)); | |
| 66 | |
| 67 // Test startIndex with a RegExp | |
| 68 Expect.equals( | |
| 69 "aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1)); | |
| 70 | |
| 71 // Test startIndex with regexp-looking String | |
| 72 Expect.equals( | |
| 73 "aaa{3}aaX", "aaa{3}aaa{3}".replaceFirst("a{3}", "X", 3)); | |
| 74 | |
| 75 // Test negative startIndex | |
| 76 Expect.throws( | |
| 77 () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError); | |
| 78 | |
| 79 // Test startIndex too large | |
| 80 Expect.throws( | |
| 81 () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError); | |
| 82 | |
| 83 // Test null startIndex | |
| 84 Expect.throws( | |
| 85 () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError); | |
| 86 | |
| 87 // Test object startIndex | |
| 88 Expect.throws( | |
| 89 () => "hello".replaceFirst("h", "X", new Object()), | |
| 90 (e) => e is TypeError); | |
|
Lasse Reichstein Nielsen
2014/08/20 08:58:53
This is only correct in checked mode on the VM. In
srawlins
2014/08/22 14:46:40
Done.
| |
| 45 } | 91 } |
| 46 } | 92 } |
| 47 | 93 |
| 48 main() { | 94 main() { |
| 49 StringReplaceTest.testMain(); | 95 StringReplaceTest.testMain(); |
| 50 } | 96 } |
| OLD | NEW |