OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, 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 testReplaceAll() { |
| 6 Expect.equals( |
| 7 "aXXcaXXdae", "abcabdae".replaceAll("b", "XX")); |
| 8 |
| 9 // Test with the replaced string at the begining. |
| 10 Expect.equals( |
| 11 "XXbcXXbdXXe", "abcabdae".replaceAll("a", "XX")); |
| 12 |
| 13 // Test with the replaced string at the end. |
| 14 Expect.equals( |
| 15 "abcabdaXX", "abcabdae".replaceAll("e", "XX")); |
| 16 |
| 17 // Test when there are no occurence of the string to replace. |
| 18 Expect.equals( |
| 19 "abcabdae", "abcabdae".replaceAll("f", "XX")); |
| 20 |
| 21 // Test when the string to change is the empty string. |
| 22 Expect.equals("", "".replaceAll("from", "to")); |
| 23 |
| 24 // Test when the string to change is a substring of the string to |
| 25 // replace. |
| 26 Expect.equals("fro", "fro".replaceAll("from", "to")); |
| 27 |
| 28 // Test when the string to change is the replaced string. |
| 29 Expect.equals("to", "from".replaceAll("from", "to")); |
| 30 |
| 31 // Test when matches are adjacent |
| 32 Expect.equals("toto", "fromfrom".replaceAll("from", "to")); |
| 33 |
| 34 // Test when the string to change is the replacement string. |
| 35 Expect.equals("to", "to".replaceAll("from", "to")); |
| 36 |
| 37 // Test replacing by the empty string. |
| 38 Expect.equals( |
| 39 "bcbde", "abcabdae".replaceAll("a", "")); |
| 40 Expect.equals("AB", "AfromB".replaceAll("from", "")); |
| 41 |
| 42 // Test changing the empty string. |
| 43 Expect.equals("to", "".replaceAll("", "to")); |
| 44 |
| 45 // Test replacing the empty string. |
| 46 Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to")); |
| 47 } |
| 48 |
| 49 testReplaceAllMapped() { |
| 50 String mark(Match m) => "[${m[0]}]"; |
| 51 Expect.equals( |
| 52 "a[b]ca[b]dae", "abcabdae".replaceAllMapped("b", mark)); |
| 53 |
| 54 // Test with the replaced string at the begining. |
| 55 Expect.equals( |
| 56 "[a]bc[a]bd[a]e", "abcabdae".replaceAllMapped("a", mark)); |
| 57 |
| 58 // Test with the replaced string at the end. |
| 59 Expect.equals( |
| 60 "abcabda[e]", "abcabdae".replaceAllMapped("e", mark)); |
| 61 |
| 62 // Test when there are no occurence of the string to replace. |
| 63 Expect.equals( |
| 64 "abcabdae", "abcabdae".replaceAllMapped("f", mark)); |
| 65 |
| 66 // Test when the string to change is the empty string. |
| 67 Expect.equals("", "".replaceAllMapped("from", mark)); |
| 68 |
| 69 // Test when the string to change is a substring of the string to |
| 70 // replace. |
| 71 Expect.equals("fro", "fro".replaceAllMapped("from", mark)); |
| 72 |
| 73 // Test when matches are adjacent |
| 74 Expect.equals("[from][from]", "fromfrom".replaceAllMapped("from", mark)); |
| 75 |
| 76 // Test replacing by the empty string. |
| 77 Expect.equals( |
| 78 "bcbde", "abcabdae".replaceAllMapped("a", (m) => "")); |
| 79 Expect.equals("AB", "AfromB".replaceAllMapped("from", (m) => "")); |
| 80 |
| 81 // Test changing the empty string. |
| 82 Expect.equals("[]", "".replaceAllMapped("", mark)); |
| 83 |
| 84 // Test replacing the empty string. |
| 85 Expect.equals("[]A[]B[]C[]", "ABC".replaceAllMapped("", mark)); |
| 86 } |
| 87 |
| 88 testSplitMapJoin() { |
| 89 String mark(Match m) => "[${m[0]}]"; |
| 90 String wrap(String s) => "<${s}>"; |
| 91 |
| 92 Expect.equals( |
| 93 "<a>[b]<ca>[b]<dae>", |
| 94 "abcabdae".splitMapJoin("b", onMatch: mark, onNonMatch: wrap)); |
| 95 |
| 96 // Test with the replaced string at the begining. |
| 97 Expect.equals( |
| 98 "<>[a]<bc>[a]<bd>[a]<e>", |
| 99 "abcabdae".splitMapJoin("a", onMatch: mark, onNonMatch: wrap)); |
| 100 |
| 101 // Test with the replaced string at the end. |
| 102 Expect.equals( |
| 103 "<abcabda>[e]<>", |
| 104 "abcabdae".splitMapJoin("e", onMatch: mark, onNonMatch: wrap)); |
| 105 |
| 106 // Test when there are no occurence of the string to replace. |
| 107 Expect.equals( |
| 108 "<abcabdae>", |
| 109 "abcabdae".splitMapJoin("f", onMatch: mark, onNonMatch: wrap)); |
| 110 |
| 111 // Test when the string to change is the empty string. |
| 112 Expect.equals("<>", "".splitMapJoin("from", onMatch: mark, onNonMatch: wrap)); |
| 113 |
| 114 // Test when the string to change is a substring of the string to |
| 115 // replace. |
| 116 Expect.equals("<fro>", |
| 117 "fro".splitMapJoin("from", onMatch: mark, onNonMatch: wrap)); |
| 118 |
| 119 // Test when matches are adjacent |
| 120 Expect.equals("<>[from]<>[from]<>", |
| 121 "fromfrom".splitMapJoin("from", onMatch: mark, onNonMatch: wrap)); |
| 122 |
| 123 // Test changing the empty string. |
| 124 Expect.equals("<>[]<>", "".splitMapJoin("", onMatch: mark, onNonMatch: wrap)); |
| 125 |
| 126 // Test replacing the empty string. |
| 127 Expect.equals("<>[]<A>[]<B>[]<C>[]<>", "ABC".splitMapJoin("", onMatch: mark, |
| 128 onNonMatch: wrap)); |
| 129 |
| 130 // Test with only onMatch. |
| 131 Expect.equals( |
| 132 "[a]bc[a]bd[a]e", |
| 133 "abcabdae".splitMapJoin("a", onMatch: mark)); |
| 134 |
| 135 |
| 136 // Test with only onNonMatch |
| 137 Expect.equals( |
| 138 "<>a<bc>a<bd>a<e>", |
| 139 "abcabdae".splitMapJoin("a", onNonMatch: wrap)); |
| 140 |
| 141 } |
| 142 |
| 143 main() { |
| 144 testReplaceAll(); |
| 145 testReplaceAllMapped(); |
| 146 testSplitMapJoin(); |
| 147 } |
OLD | NEW |