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 macro_rules! bytes { ($text:expr) => { $text } } 8 9 macro_rules! no_expand { 10 ($text:expr) => {{ 11 use regex::bytes::NoExpand; 12 NoExpand(text!($text)) 13 }} 14 } 15 16 macro_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 27 macro_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 } 40