blob: 03c370d698b0635000ee9efc8e0218f5eae3cdcd [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() } }
7
8macro_rules! bytes { ($text:expr) => { $text } }
9
10macro_rules! no_expand {
11 ($text:expr) => {{
12 use regex::bytes::NoExpand;
13 NoExpand(text!($text))
14 }}
15}
16
17macro_rules! show {
18 ($text:expr) => {{
19 use std::ascii::escape_default;
20 let mut s = vec![];
21 for &b in bytes!($text) {
22 s.extend(escape_default(b));
23 }
24 String::from_utf8(s).unwrap()
25 }}
26}
27
28macro_rules! expand {
29 ($name:ident, $re:expr, $text:expr, $expand:expr, $expected:expr) => {
30 #[test]
31 fn $name() {
32 let re = regex!($re);
33 let cap = re.captures(t!($text)).unwrap();
34
35 let mut got = vec![];
36 cap.expand(t!($expand), &mut got);
37 assert_eq!(show!(t!($expected)), show!(&*got));
38 }
39 }
40}