Debian Codegraph @

.cargo/vendor/regex/tests/searcher.rs

raw
1 macro_rules! searcher {
2 ($name:ident, $re:expr, $haystack:expr) => (
3 searcher!($name, $re, $haystack, vec vec![]);
4 );
5 ($name:ident, $re:expr, $haystack:expr, $($steps:expr,)*) => (
6 searcher!($name, $re, $haystack, vec vec![$($steps),*]);
7 );
8 ($name:ident, $re:expr, $haystack:expr, $($steps:expr),*) => (
9 searcher!($name, $re, $haystack, vec vec![$($steps),*]);
10 );
11 ($name:ident, $re:expr, $haystack:expr, vec $expect_steps:expr) => (
12 #[test]
13 #[allow(unused_imports)]
14 fn $name() {
15 use std::str::pattern::{Pattern, Searcher};
16 use std::str::pattern::SearchStep::{Match, Reject, Done};
17 let re = regex::Regex::new($re).unwrap();
18 let mut se = re.into_searcher($haystack);
19 let mut got_steps = vec![];
20 loop {
21 match se.next() {
22 Done => break,
23 step => { got_steps.push(step); }
24 }
25 }
26 assert_eq!(got_steps, $expect_steps);
27 }
28 );
29 }
30
31 searcher!(searcher_empty_regex_empty_haystack, r"", "", Match(0, 0));
32 searcher!(
33 searcher_empty_regex,
34 r"",
35 "ab",
36 Match(0, 0),
37 Reject(0, 1),
38 Match(1, 1),
39 Reject(1, 2),
40 Match(2, 2)
41 );
42 searcher!(searcher_empty_haystack, r"\d", "");
43 searcher!(searcher_one_match, r"\d", "5", Match(0, 1));
44 searcher!(searcher_no_match, r"\d", "a", Reject(0, 1));
45 searcher!(
46 searcher_two_adjacent_matches,
47 r"\d",
48 "56",
49 Match(0, 1),
50 Match(1, 2)
51 );
52 searcher!(
53 searcher_two_non_adjacent_matches,
54 r"\d",
55 "5a6",
56 Match(0, 1),
57 Reject(1, 2),
58 Match(2, 3)
59 );
60 searcher!(searcher_reject_first, r"\d", "a6", Reject(0, 1), Match(1, 2));
61 searcher!(
62 searcher_one_zero_length_matches,
63 r"\d*",
64 "a1b2",
65 Match(0, 0), // ^
66 Reject(0, 1), // a
67 Match(1, 2), // a1
68 Reject(2, 3), // a1b
69 Match(3, 4), // a1b2
70 );
71 searcher!(
72 searcher_many_zero_length_matches,
73 r"\d*",
74 "a1bbb2",
75 Match(0, 0), // ^
76 Reject(0, 1), // a
77 Match(1, 2), // a1
78 Reject(2, 3), // a1b
79 Match(3, 3), // a1bb
80 Reject(3, 4), // a1bb
81 Match(4, 4), // a1bbb
82 Reject(4, 5), // a1bbb
83 Match(5, 6), // a1bbba
84 );
85 searcher!(
86 searcher_unicode,
87 r".+?",
88 "Ⅰ1Ⅱ2",
89 Match(0, 3),
90 Match(3, 4),
91 Match(4, 7),
92 Match(7, 8)
93 );