1 macro_rules! replace(
2     ($name:ident, $which:ident, $re:expr,
3      $search:expr, $replace:expr, $result:expr) => (
4         #[test]
5         fn $name() {
6             let re = regex!($re);
7             assert_eq!(re.$which(text!($search), $replace), text!($result));
8         }
9     );
10 );
11 
12 replace!(first, replace, r"[0-9]", "age: 26", t!("Z"), "age: Z6");
13 replace!(plus, replace, r"[0-9]+", "age: 26", t!("Z"), "age: Z");
14 replace!(all, replace_all, r"[0-9]", "age: 26", t!("Z"), "age: ZZ");
15 replace!(
16     groups,
17     replace,
18     r"(?-u)(\S+)\s+(\S+)",
19     "w1 w2",
20     t!("$2 $1"),
21     "w2 w1"
22 );
23 replace!(
24     double_dollar,
25     replace,
26     r"(?-u)(\S+)\s+(\S+)",
27     "w1 w2",
28     t!("$2 $$1"),
29     "w2 $1"
30 );
31 // replace!(adjacent_index, replace,
32 // r"([^aeiouy])ies$", "skies", t!("$1y"), "sky");
33 replace!(
34     named,
35     replace_all,
36     r"(?-u)(?P<first>\S+)\s+(?P<last>\S+)(?P<space>\s*)",
37     "w1 w2 w3 w4",
38     t!("$last $first$space"),
39     "w2 w1 w4 w3"
40 );
41 replace!(
42     trim,
43     replace_all,
44     "^[ \t]+|[ \t]+$",
45     " \t  trim me\t   \t",
46     t!(""),
47     "trim me"
48 );
49 replace!(number_hypen, replace, r"(.)(.)", "ab", t!("$1-$2"), "a-b");
50 // replace!(number_underscore, replace, r"(.)(.)", "ab", t!("$1_$2"), "a_b");
51 replace!(
52     simple_expand,
53     replace_all,
54     r"(?-u)(\w) (\w)",
55     "a b",
56     t!("$2 $1"),
57     "b a"
58 );
59 replace!(
60     literal_dollar1,
61     replace_all,
62     r"(?-u)(\w+) (\w+)",
63     "a b",
64     t!("$$1"),
65     "$1"
66 );
67 replace!(
68     literal_dollar2,
69     replace_all,
70     r"(?-u)(\w+) (\w+)",
71     "a b",
72     t!("$2 $$c $1"),
73     "b $c a"
74 );
75 replace!(
76     no_expand1,
77     replace,
78     r"(?-u)(\S+)\s+(\S+)",
79     "w1 w2",
80     no_expand!("$2 $1"),
81     "$2 $1"
82 );
83 replace!(
84     no_expand2,
85     replace,
86     r"(?-u)(\S+)\s+(\S+)",
87     "w1 w2",
88     no_expand!("$$1"),
89     "$$1"
90 );
91 use_!(Captures);
92 replace!(
93     closure_returning_reference,
94     replace,
95     r"([0-9]+)",
96     "age: 26",
97     |captures: &Captures<'_>| {
98         match_text!(captures.get(1).unwrap())[0..1].to_owned()
99     },
100     "age: 2"
101 );
102 replace!(
103     closure_returning_value,
104     replace,
105     r"[0-9]+",
106     "age: 26",
107     |_captures: &Captures<'_>| t!("Z").to_owned(),
108     "age: Z"
109 );
110 
111 // See https://github.com/rust-lang/regex/issues/314
112 replace!(
113     match_at_start_replace_with_empty,
114     replace_all,
115     r"foo",
116     "foobar",
117     t!(""),
118     "bar"
119 );
120 
121 // See https://github.com/rust-lang/regex/issues/393
122 replace!(single_empty_match, replace, r"^", "bar", t!("foo"), "foobar");
123 
124 // See https://github.com/rust-lang/regex/issues/399
125 replace!(
126     capture_longest_possible_name,
127     replace_all,
128     r"(.)",
129     "b",
130     t!("${1}a $1a"),
131     "ba "
132 );
133 
134 replace!(
135     impl_string,
136     replace,
137     r"[0-9]",
138     "age: 26",
139     t!("Z".to_string()),
140     "age: Z6"
141 );
142 replace!(
143     impl_string_ref,
144     replace,
145     r"[0-9]",
146     "age: 26",
147     t!(&"Z".to_string()),
148     "age: Z6"
149 );
150 replace!(
151     impl_cow_str_borrowed,
152     replace,
153     r"[0-9]",
154     "age: 26",
155     t!(std::borrow::Cow::<'_, str>::Borrowed("Z")),
156     "age: Z6"
157 );
158 replace!(
159     impl_cow_str_borrowed_ref,
160     replace,
161     r"[0-9]",
162     "age: 26",
163     t!(&std::borrow::Cow::<'_, str>::Borrowed("Z")),
164     "age: Z6"
165 );
166 replace!(
167     impl_cow_str_owned,
168     replace,
169     r"[0-9]",
170     "age: 26",
171     t!(std::borrow::Cow::<'_, str>::Owned("Z".to_string())),
172     "age: Z6"
173 );
174 replace!(
175     impl_cow_str_owned_ref,
176     replace,
177     r"[0-9]",
178     "age: 26",
179     t!(&std::borrow::Cow::<'_, str>::Owned("Z".to_string())),
180     "age: Z6"
181 );
182 
183 replace!(
184     impl_vec_u8,
185     replace,
186     r"[0-9]",
187     "age: 26",
188     bytes!(vec![b'Z']),
189     "age: Z6"
190 );
191 replace!(
192     impl_vec_u8_ref,
193     replace,
194     r"[0-9]",
195     "age: 26",
196     bytes!(&vec![b'Z']),
197     "age: Z6"
198 );
199 replace!(
200     impl_cow_slice_borrowed,
201     replace,
202     r"[0-9]",
203     "age: 26",
204     bytes!(std::borrow::Cow::<'_, [u8]>::Borrowed(&[b'Z'])),
205     "age: Z6"
206 );
207 replace!(
208     impl_cow_slice_borrowed_ref,
209     replace,
210     r"[0-9]",
211     "age: 26",
212     bytes!(&std::borrow::Cow::<'_, [u8]>::Borrowed(&[b'Z'])),
213     "age: Z6"
214 );
215 replace!(
216     impl_cow_slice_owned,
217     replace,
218     r"[0-9]",
219     "age: 26",
220     bytes!(std::borrow::Cow::<'_, [u8]>::Owned(vec![b'Z'])),
221     "age: Z6"
222 );
223 replace!(
224     impl_cow_slice_owned_ref,
225     replace,
226     r"[0-9]",
227     "age: 26",
228     bytes!(&std::borrow::Cow::<'_, [u8]>::Owned(vec![b'Z'])),
229     "age: Z6"
230 );
231