Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame] | 1 | // Macros for use in writing tests generic over &str/&[u8]. |
| 2 | macro_rules! text { ($text:expr) => { $text.as_bytes() } } |
| 3 | macro_rules! t { ($re:expr) => { text!($re) } } |
| 4 | macro_rules! match_text { ($text:expr) => { $text.as_bytes() } } |
| 5 | macro_rules! use_ { ($($path: tt)*) => { use regex::bytes::$($path)*; } } |
| 6 | macro_rules! empty_vec { () => { <Vec<&[u8]>>::new() } } |
| 7 | |
| 8 | macro_rules! bytes { ($text:expr) => { $text } } |
| 9 | |
| 10 | macro_rules! no_expand { |
| 11 | ($text:expr) => {{ |
| 12 | use regex::bytes::NoExpand; |
| 13 | NoExpand(text!($text)) |
| 14 | }} |
| 15 | } |
| 16 | |
| 17 | macro_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 | |
| 28 | macro_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 | } |