blob: 3d6c8c3ac88e5a7182210e908c217fe6917f4d38 [file] [log] [blame]
Chih-Hung Hsiehe42c5052020-04-16 10:44:21 -07001// Macros for use in writing tests generic over &str/&[u8].
2macro_rules! text { ($text:expr) => { $text.as_bytes() } }
3macro_rules! t { ($re:expr) => { text!($re) } }
4macro_rules! match_text { ($text:expr) => { $text.as_bytes() } }
5macro_rules! use_ { ($($path: tt)*) => { use regex::bytes::$($path)*; } }
6macro_rules! empty_vec { () => { <Vec<&[u8]>>::new() } }
Chih-Hung Hsiehe42c5052020-04-16 10:44:21 -07007macro_rules! bytes { ($text:expr) => { $text } }
8
9macro_rules! no_expand {
10 ($text:expr) => {{
11 use regex::bytes::NoExpand;
12 NoExpand(text!($text))
13 }}
14}
15
16macro_rules! show {
17 ($text:expr) => {{
18 use std::ascii::escape_default;
19 let mut s = vec![];
20 for &b in bytes!($text) {
21 s.extend(escape_default(b));
22 }
23 String::from_utf8(s).unwrap()
24 }}
25}
26
27macro_rules! expand {
28 ($name:ident, $re:expr, $text:expr, $expand:expr, $expected:expr) => {
29 #[test]
30 fn $name() {
31 let re = regex!($re);
32 let cap = re.captures(t!($text)).unwrap();
33
34 let mut got = vec![];
35 cap.expand(t!($expand), &mut got);
36 assert_eq!(show!(t!($expected)), show!(&*got));
37 }
38 }
39}