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