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