1 // Convenience macros. 2 3 macro_rules! findall { 4 ($re:expr, $text:expr) => {{ 5 $re.find_iter(text!($text)) 6 .map(|m| (m.start(), m.end())).collect::<Vec<_>>() 7 }} 8 } 9 10 // Macros for automatically producing tests. 11 12 macro_rules! ismatch { 13 ($name:ident, $re:expr, $text:expr, $ismatch:expr) => { 14 #[test] 15 fn $name() { 16 let re = regex!($re); 17 assert_eq!($ismatch, re.is_match(text!($text))); 18 } 19 }; 20 } 21 22 macro_rules! mat( 23 ($name:ident, $re:expr, $text:expr, $($loc:tt)+) => ( 24 #[test] 25 fn $name() { 26 let text = text!($text); 27 let expected: Vec<Option<_>> = vec![$($loc)+]; 28 let r = regex!($re); 29 let got: Vec<Option<_>> = match r.captures(text) { 30 Some(c) => { 31 assert!(r.is_match(text)); 32 assert!(r.shortest_match(text).is_some()); 33 r.capture_names() 34 .enumerate() 35 .map(|(i, _)| c.get(i).map(|m| (m.start(), m.end()))) 36 .collect() 37 } 38 None => vec![None], 39 }; 40 // The test set sometimes leave out capture groups, so truncate 41 // actual capture groups to match test set. 42 let mut sgot = &got[..]; 43 if sgot.len() > expected.len() { 44 sgot = &sgot[0..expected.len()] 45 } 46 if expected != sgot { 47 panic!("For RE '{}' against '{:?}', \ 48 expected '{:?}' but got '{:?}'", 49 $re, text, expected, sgot); 50 } 51 } 52 ); 53 ); 54 55 macro_rules! matiter( 56 ($name:ident, $re:expr, $text:expr) => ( 57 #[test] 58 fn $name() { 59 let text = text!($text); 60 let expected: Vec<(usize, usize)> = vec![]; 61 let r = regex!($re); 62 let got: Vec<_> = 63 r.find_iter(text).map(|m| (m.start(), m.end())).collect(); 64 if expected != got { 65 panic!("For RE '{}' against '{:?}', \ 66 expected '{:?}' but got '{:?}'", 67 $re, text, expected, got); 68 } 69 let captures_got: Vec<_> = 70 r.captures_iter(text) 71 .map(|c| c.get(0).unwrap()) 72 .map(|m| (m.start(), m.end())) 73 .collect(); 74 if captures_got != got { 75 panic!("For RE '{}' against '{:?}', \ 76 got '{:?}' using find_iter but got '{:?}' \ 77 using captures_iter", 78 $re, text, got, captures_got); 79 } 80 } 81 ); 82 ($name:ident, $re:expr, $text:expr, $($loc:tt)+) => ( 83 #[test] 84 fn $name() { 85 let text = text!($text); 86 let expected: Vec<_> = vec![$($loc)+]; 87 let r = regex!($re); 88 let got: Vec<_> = 89 r.find_iter(text).map(|m| (m.start(), m.end())).collect(); 90 if expected != got { 91 panic!("For RE '{}' against '{:?}', \ 92 expected '{:?}' but got '{:?}'", 93 $re, text, expected, got); 94 } 95 let captures_got: Vec<_> = 96 r.captures_iter(text) 97 .map(|c| c.get(0).unwrap()) 98 .map(|m| (m.start(), m.end())) 99 .collect(); 100 if captures_got != got { 101 panic!("For RE '{}' against '{:?}', \ 102 got '{:?}' using find_iter but got '{:?}' \ 103 using captures_iter", 104 $re, text, got, captures_got); 105 } 106 } 107 ); 108 ); 109 110 macro_rules! matset { 111 ($name:ident, $res:expr, $text:expr, $($match_index:expr),*) => { 112 #[test] 113 fn $name() { 114 let text = text!($text); 115 let set = regex_set!($res); 116 assert!(set.is_match(text)); 117 let expected = vec![$($match_index),*]; 118 let matches = set.matches(text); 119 assert!(matches.matched_any()); 120 let got: Vec<_> = matches.into_iter().collect(); 121 assert_eq!(expected, got); 122 } 123 } 124 } 125 126 macro_rules! nomatset { 127 ($name:ident, $res:expr, $text:expr) => { 128 #[test] 129 fn $name() { 130 let text = text!($text); 131 let set = regex_set!($res); 132 assert!(!set.is_match(text)); 133 let matches = set.matches(text); 134 assert!(!matches.matched_any()); 135 assert_eq!(0, matches.into_iter().count()); 136 } 137 } 138 } 139 140 macro_rules! split { 141 ($name:ident, $re:expr, $text:expr, $expected:expr) => { 142 #[test] 143 fn $name() { 144 let re = regex!($re); 145 let splitted: Vec<_> = re.split(t!($text)).collect(); 146 assert_eq!($expected, &*splitted); 147 } 148 } 149 } 150 151 macro_rules! splitn { 152 ($name:ident, $re:expr, $text:expr, $limit:expr, $expected:expr) => { 153 #[test] 154 fn $name() { 155 let re = regex!($re); 156 let splitted: Vec<_> = re.splitn(t!($text), $limit).collect(); 157 assert_eq!($expected, &*splitted); 158 } 159 } 160 } 161