• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #[test]
empty_regex_empty_match()2 fn empty_regex_empty_match() {
3     let re = regex!("");
4     assert_eq!(vec![(0, 0)], findall!(re, ""));
5 }
6 
7 #[test]
empty_regex_nonempty_match()8 fn empty_regex_nonempty_match() {
9     let re = regex!("");
10     assert_eq!(vec![(0, 0), (1, 1), (2, 2), (3, 3)], findall!(re, "abc"));
11 }
12 
13 #[test]
one_zero_length_match()14 fn one_zero_length_match() {
15     let re = regex!(r"[0-9]*");
16     assert_eq!(vec![(0, 0), (1, 2), (3, 4)], findall!(re, "a1b2"));
17 }
18 
19 #[test]
many_zero_length_match()20 fn many_zero_length_match() {
21     let re = regex!(r"[0-9]*");
22     assert_eq!(
23         vec![(0, 0), (1, 2), (3, 3), (4, 4), (5, 6)],
24         findall!(re, "a1bbb2")
25     );
26 }
27 
28 #[test]
many_sequential_zero_length_match()29 fn many_sequential_zero_length_match() {
30     let re = regex!(r"[0-9]?");
31     assert_eq!(
32         vec![(0, 0), (1, 2), (2, 3), (4, 5), (6, 6)],
33         findall!(re, "a12b3c")
34     );
35 }
36 
37 #[test]
quoted_bracket_set()38 fn quoted_bracket_set() {
39     let re = regex!(r"([\x{5b}\x{5d}])");
40     assert_eq!(vec![(0, 1), (1, 2)], findall!(re, "[]"));
41     let re = regex!(r"([\[\]])");
42     assert_eq!(vec![(0, 1), (1, 2)], findall!(re, "[]"));
43 }
44 
45 #[test]
first_range_starts_with_left_bracket()46 fn first_range_starts_with_left_bracket() {
47     let re = regex!(r"([\[-z])");
48     assert_eq!(vec![(0, 1), (1, 2)], findall!(re, "[]"));
49 }
50 
51 #[test]
range_ends_with_escape()52 fn range_ends_with_escape() {
53     let re = regex!(r"([\[-\x{5d}])");
54     assert_eq!(vec![(0, 1), (1, 2)], findall!(re, "[]"));
55 }
56 
57 #[test]
empty_match_find_iter()58 fn empty_match_find_iter() {
59     let re = regex!(r".*?");
60     assert_eq!(vec![(0, 0), (1, 1), (2, 2), (3, 3)], findall!(re, "abc"));
61 }
62 
63 #[test]
empty_match_captures_iter()64 fn empty_match_captures_iter() {
65     let re = regex!(r".*?");
66     let ms: Vec<_> = re
67         .captures_iter(text!("abc"))
68         .map(|c| c.get(0).unwrap())
69         .map(|m| (m.start(), m.end()))
70         .collect();
71     assert_eq!(ms, vec![(0, 0), (1, 1), (2, 2), (3, 3)]);
72 }
73 
74 #[test]
capture_names()75 fn capture_names() {
76     let re = regex!(r"(.)(?P<a>.)");
77     assert_eq!(3, re.captures_len());
78     assert_eq!((3, Some(3)), re.capture_names().size_hint());
79     assert_eq!(
80         vec![None, None, Some("a")],
81         re.capture_names().collect::<Vec<_>>()
82     );
83 }
84 
85 #[test]
regex_string()86 fn regex_string() {
87     assert_eq!(r"[a-zA-Z0-9]+", regex!(r"[a-zA-Z0-9]+").as_str());
88     assert_eq!(r"[a-zA-Z0-9]+", &format!("{}", regex!(r"[a-zA-Z0-9]+")));
89     assert_eq!(r"[a-zA-Z0-9]+", &format!("{:?}", regex!(r"[a-zA-Z0-9]+")));
90 }
91 
92 #[test]
capture_index()93 fn capture_index() {
94     let re = regex!(r"^(?P<name>.+)$");
95     let cap = re.captures(t!("abc")).unwrap();
96     assert_eq!(&cap[0], t!("abc"));
97     assert_eq!(&cap[1], t!("abc"));
98     assert_eq!(&cap["name"], t!("abc"));
99 }
100 
101 #[test]
102 #[should_panic]
103 #[cfg_attr(all(target_env = "msvc", target_pointer_width = "32"), ignore)]
capture_index_panic_usize()104 fn capture_index_panic_usize() {
105     let re = regex!(r"^(?P<name>.+)$");
106     let cap = re.captures(t!("abc")).unwrap();
107     let _ = cap[2];
108 }
109 
110 #[test]
111 #[should_panic]
112 #[cfg_attr(all(target_env = "msvc", target_pointer_width = "32"), ignore)]
capture_index_panic_name()113 fn capture_index_panic_name() {
114     let re = regex!(r"^(?P<name>.+)$");
115     let cap = re.captures(t!("abc")).unwrap();
116     let _ = cap["bad name"];
117 }
118 
119 #[test]
capture_index_lifetime()120 fn capture_index_lifetime() {
121     // This is a test of whether the types on `caps["..."]` are general
122     // enough. If not, this will fail to typecheck.
123     fn inner(s: &str) -> usize {
124         let re = regex!(r"(?P<number>[0-9]+)");
125         let caps = re.captures(t!(s)).unwrap();
126         caps["number"].len()
127     }
128     assert_eq!(3, inner("123"));
129 }
130 
131 #[test]
capture_misc()132 fn capture_misc() {
133     let re = regex!(r"(.)(?P<a>a)?(.)(?P<b>.)");
134     let cap = re.captures(t!("abc")).unwrap();
135 
136     assert_eq!(5, cap.len());
137 
138     assert_eq!((0, 3), {
139         let m = cap.get(0).unwrap();
140         (m.start(), m.end())
141     });
142     assert_eq!(None, cap.get(2));
143     assert_eq!((2, 3), {
144         let m = cap.get(4).unwrap();
145         (m.start(), m.end())
146     });
147 
148     assert_eq!(t!("abc"), match_text!(cap.get(0).unwrap()));
149     assert_eq!(None, cap.get(2));
150     assert_eq!(t!("c"), match_text!(cap.get(4).unwrap()));
151 
152     assert_eq!(None, cap.name("a"));
153     assert_eq!(t!("c"), match_text!(cap.name("b").unwrap()));
154 }
155 
156 #[test]
sub_capture_matches()157 fn sub_capture_matches() {
158     let re = regex!(r"([a-z])(([a-z])|([0-9]))");
159     let cap = re.captures(t!("a5")).unwrap();
160     let subs: Vec<_> = cap.iter().collect();
161 
162     assert_eq!(5, subs.len());
163     assert!(subs[0].is_some());
164     assert!(subs[1].is_some());
165     assert!(subs[2].is_some());
166     assert!(subs[3].is_none());
167     assert!(subs[4].is_some());
168 
169     assert_eq!(t!("a5"), match_text!(subs[0].unwrap()));
170     assert_eq!(t!("a"), match_text!(subs[1].unwrap()));
171     assert_eq!(t!("5"), match_text!(subs[2].unwrap()));
172     assert_eq!(t!("5"), match_text!(subs[4].unwrap()));
173 }
174 
175 expand!(expand1, r"(?-u)(?P<foo>\w+)", "abc", "$foo", "abc");
176 expand!(expand2, r"(?-u)(?P<foo>\w+)", "abc", "$0", "abc");
177 expand!(expand3, r"(?-u)(?P<foo>\w+)", "abc", "$1", "abc");
178 expand!(expand4, r"(?-u)(?P<foo>\w+)", "abc", "$$1", "$1");
179 expand!(expand5, r"(?-u)(?P<foo>\w+)", "abc", "$$foo", "$foo");
180 expand!(expand6, r"(?-u)(?P<a>\w+)\s+(?P<b>\d+)", "abc 123", "$b$a", "123abc");
181 expand!(expand7, r"(?-u)(?P<a>\w+)\s+(?P<b>\d+)", "abc 123", "z$bz$az", "z");
182 expand!(
183     expand8,
184     r"(?-u)(?P<a>\w+)\s+(?P<b>\d+)",
185     "abc 123",
186     ".$b.$a.",
187     ".123.abc."
188 );
189 expand!(
190     expand9,
191     r"(?-u)(?P<a>\w+)\s+(?P<b>\d+)",
192     "abc 123",
193     " $b $a ",
194     " 123 abc "
195 );
196 expand!(expand10, r"(?-u)(?P<a>\w+)\s+(?P<b>\d+)", "abc 123", "$bz$az", "");
197 
198 expand!(expand_name1, r"%(?P<Z>[a-z]+)", "%abc", "$Z%", "abc%");
199 expand!(expand_name2, r"\[(?P<Z>[a-z]+)", "[abc", "$Z[", "abc[");
200 expand!(expand_name3, r"\{(?P<Z>[a-z]+)", "{abc", "$Z{", "abc{");
201 expand!(expand_name4, r"\}(?P<Z>[a-z]+)", "}abc", "$Z}", "abc}");
202 expand!(expand_name5, r"%([a-z]+)", "%abc", "$1a%", "%");
203 expand!(expand_name6, r"%([a-z]+)", "%abc", "${1}a%", "abca%");
204 expand!(expand_name7, r"\[(?P<Z[>[a-z]+)", "[abc", "${Z[}[", "abc[");
205 expand!(expand_name8, r"\[(?P<Z[>[a-z]+)", "[abc", "${foo}[", "[");
206 expand!(expand_name9, r"\[(?P<Z[>[a-z]+)", "[abc", "${1a}[", "[");
207 expand!(expand_name10, r"\[(?P<Z[>[a-z]+)", "[abc", "${#}[", "[");
208 expand!(expand_name11, r"\[(?P<Z[>[a-z]+)", "[abc", "${$$}[", "[");
209 
210 split!(
211     split1,
212     r"(?-u)\s+",
213     "a b\nc\td\n\t e",
214     &[t!("a"), t!("b"), t!("c"), t!("d"), t!("e")]
215 );
216 split!(
217     split2,
218     r"(?-u)\b",
219     "a b c",
220     &[t!(""), t!("a"), t!(" "), t!("b"), t!(" "), t!("c"), t!("")]
221 );
222 split!(split3, r"a$", "a", &[t!(""), t!("")]);
223 split!(split_none, r"-", r"a", &[t!("a")]);
224 split!(split_trailing_blank, r"-", r"a-", &[t!("a"), t!("")]);
225 split!(split_trailing_blanks, r"-", r"a--", &[t!("a"), t!(""), t!("")]);
226 split!(split_empty, r"-", r"", &[t!("")]);
227 
228 splitn!(splitn_below_limit, r"-", r"a", 2, &[t!("a")]);
229 splitn!(splitn_at_limit, r"-", r"a-b", 2, &[t!("a"), t!("b")]);
230 splitn!(splitn_above_limit, r"-", r"a-b-c", 2, &[t!("a"), t!("b-c")]);
231 splitn!(splitn_zero_limit, r"-", r"a-b", 0, empty_vec!());
232 splitn!(splitn_trailing_blank, r"-", r"a-", 2, &[t!("a"), t!("")]);
233 splitn!(splitn_trailing_separator, r"-", r"a--", 2, &[t!("a"), t!("-")]);
234 splitn!(splitn_empty, r"-", r"", 1, &[t!("")]);
235