summaryrefslogtreecommitdiffstats
path: root/vendor/regex/tests/macros.rs
blob: e70e9489fd01e4721bec6efef89e5b52b35111ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Convenience macros.

macro_rules! findall {
    ($re:expr, $text:expr) => {{
        $re.find_iter(text!($text))
           .map(|m| (m.start(), m.end())).collect::<Vec<_>>()
    }}
}

// Macros for automatically producing tests.

macro_rules! ismatch {
    ($name:ident, $re:expr, $text:expr, $ismatch:expr) => {
        #[test]
        fn $name() {
            let re = regex!($re);
            assert_eq!($ismatch, re.is_match(text!($text)));
        }
    };
}

macro_rules! mat(
    ($name:ident, $re:expr, $text:expr, $($loc:tt)+) => (
        #[test]
        fn $name() {
            let text = text!($text);
            let expected: Vec<Option<_>> = vec![$($loc)+];
            let r = regex!($re);
            let got: Vec<Option<_>> = match r.captures(text) {
                Some(c) => {
                    assert!(r.is_match(text));
                    assert!(r.shortest_match(text).is_some());
                    r.capture_names()
                     .enumerate()
                     .map(|(i, _)| c.get(i).map(|m| (m.start(), m.end())))
                     .collect()
                }
                None => vec![None],
            };
            // The test set sometimes leave out capture groups, so truncate
            // actual capture groups to match test set.
            let mut sgot = &got[..];
            if sgot.len() > expected.len() {
                sgot = &sgot[0..expected.len()]
            }
            if expected != sgot {
                panic!("For RE '{}' against '{:?}', \
                        expected '{:?}' but got '{:?}'",
                       $re, text, expected, sgot);
            }
        }
    );
);

macro_rules! matiter(
    ($name:ident, $re:expr, $text:expr) => (
        #[test]
        fn $name() {
            let text = text!($text);
            let expected: Vec<(usize, usize)> = vec![];
            let r = regex!($re);
            let got: Vec<_> =
                r.find_iter(text).map(|m| (m.start(), m.end())).collect();
            if expected != got {
                panic!("For RE '{}' against '{:?}', \
                        expected '{:?}' but got '{:?}'",
                       $re, text, expected, got);
            }
            let captures_got: Vec<_> =
                r.captures_iter(text)
                 .map(|c| c.get(0).unwrap())
                 .map(|m| (m.start(), m.end()))
                 .collect();
            if captures_got != got {
                panic!("For RE '{}' against '{:?}', \
                        got '{:?}' using find_iter but got '{:?}' \
                        using captures_iter",
                       $re, text, got, captures_got);
            }
        }
    );
    ($name:ident, $re:expr, $text:expr, $($loc:tt)+) => (
        #[test]
        fn $name() {
            let text = text!($text);
            let expected: Vec<_> = vec![$($loc)+];
            let r = regex!($re);
            let got: Vec<_> =
                r.find_iter(text).map(|m| (m.start(), m.end())).collect();
            if expected != got {
                panic!("For RE '{}' against '{:?}', \
                        expected '{:?}' but got '{:?}'",
                       $re, text, expected, got);
            }
            let captures_got: Vec<_> =
                r.captures_iter(text)
                 .map(|c| c.get(0).unwrap())
                 .map(|m| (m.start(), m.end()))
                 .collect();
            if captures_got != got {
                panic!("For RE '{}' against '{:?}', \
                        got '{:?}' using find_iter but got '{:?}' \
                        using captures_iter",
                       $re, text, got, captures_got);
            }
        }
    );
);

macro_rules! matset {
    ($name:ident, $res:expr, $text:expr, $($match_index:expr),*) => {
        #[test]
        fn $name() {
            let text = text!($text);
            let set = regex_set!($res);
            assert!(set.is_match(text));
            let expected = vec![$($match_index),*];
            let matches = set.matches(text);
            assert!(matches.matched_any());
            let got: Vec<_> = matches.into_iter().collect();
            assert_eq!(expected, got);
        }
    }
}

macro_rules! nomatset {
    ($name:ident, $res:expr, $text:expr) => {
        #[test]
        fn $name() {
            let text = text!($text);
            let set = regex_set!($res);
            assert!(!set.is_match(text));
            let matches = set.matches(text);
            assert!(!matches.matched_any());
            assert_eq!(0, matches.into_iter().count());
        }
    }
}

macro_rules! split {
    ($name:ident, $re:expr, $text:expr, $expected:expr) => {
        #[test]
        fn $name() {
            let re = regex!($re);
            let splitted: Vec<_> = re.split(t!($text)).collect();
            assert_eq!($expected, &*splitted);
        }
    }
}

macro_rules! splitn {
    ($name:ident, $re:expr, $text:expr, $limit:expr, $expected:expr) => {
        #[test]
        fn $name() {
            let re = regex!($re);
            let splitted: Vec<_> = re.splitn(t!($text), $limit).collect();
            assert_eq!($expected, &*splitted);
        }
    }
}