| 1 |
#[allow(unused_imports)] |
| 2 |
pub use fancy_regex::DebugRegex; |
| 3 |
use fancy_regex::{BytesMode, Captures, Regex, RegexBuilder, RegexInput}; |
| 4 |
use std::ops::Range; |
| 5 |
|
| 6 |
#[allow(dead_code)] |
| 7 |
pub fn regex(re: &str) -> Regex { |
| 8 |
let parse_result = Regex::new(re); |
| 9 |
assert!( |
| 10 |
parse_result.is_ok(), |
| 11 |
"Expected regex '{}' to be compiled successfully, got {:?}", |
| 12 |
re, |
| 13 |
parse_result.err() |
| 14 |
); |
| 15 |
parse_result.unwrap() |
| 16 |
} |
| 17 |
|
| 18 |
/// Build a regex in ASCII bytes mode. Useful for bytes-mode tests that need |
| 19 |
/// to configure options beyond what the simple dual-mode helpers provide. |
| 20 |
#[allow(dead_code)] |
| 21 |
pub fn ascii_bytes_regex(re: &str) -> Regex { |
| 22 |
let parse_result = RegexBuilder::new(re).bytes_mode(BytesMode::Ascii).build(); |
| 23 |
assert!( |
| 24 |
parse_result.is_ok(), |
| 25 |
"Expected bytes regex '{}' to be compiled successfully, got {:?}", |
| 26 |
re, |
| 27 |
parse_result.err() |
| 28 |
); |
| 29 |
parse_result.unwrap() |
| 30 |
} |
| 31 |
|
| 32 |
/// Assert that `re` matches `text` in **both** str mode and ASCII bytes mode. |
| 33 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 34 |
#[allow(dead_code)] |
| 35 |
pub fn assert_is_match(re: &str, text: &str) { |
| 36 |
let str_result = regex(re).is_match(text); |
| 37 |
assert_eq!( |
| 38 |
str_result.unwrap(), |
| 39 |
true, |
| 40 |
"Expected regex '{}' to match '{}' (str mode)", |
| 41 |
re, |
| 42 |
text |
| 43 |
); |
| 44 |
let bytes_result = ascii_bytes_regex(re).is_match(text.as_bytes()); |
| 45 |
assert_eq!( |
| 46 |
bytes_result.unwrap(), |
| 47 |
true, |
| 48 |
"Expected regex '{}' to match {:?} (bytes mode)", |
| 49 |
re, |
| 50 |
text.as_bytes() |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/// Assert that `re` does **not** match `text` in either str mode or ASCII bytes |
| 55 |
/// mode. |
| 56 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 57 |
#[allow(dead_code)] |
| 58 |
pub fn assert_no_match(re: &str, text: &str) { |
| 59 |
let str_result = regex(re).is_match(text); |
| 60 |
assert_eq!( |
| 61 |
str_result.unwrap(), |
| 62 |
false, |
| 63 |
"Expected regex '{}' to not match '{}' (str mode)", |
| 64 |
re, |
| 65 |
text |
| 66 |
); |
| 67 |
let bytes_result = ascii_bytes_regex(re).is_match(text.as_bytes()); |
| 68 |
assert_eq!( |
| 69 |
bytes_result.unwrap(), |
| 70 |
false, |
| 71 |
"Expected regex '{}' to not match {:?} (bytes mode)", |
| 72 |
re, |
| 73 |
text.as_bytes() |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/// Run `find` against `text` in both str mode and ASCII bytes mode, assert |
| 78 |
/// that both agree, and return the common result. |
| 79 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 80 |
#[allow(dead_code)] |
| 81 |
pub fn assert_find(re: &str, text: &str) -> Option<(usize, usize)> { |
| 82 |
let str_result = regex(re).find(text).unwrap().map(|m| (m.start(), m.end())); |
| 83 |
let bytes_result = ascii_bytes_regex(re) |
| 84 |
.find(text.as_bytes()) |
| 85 |
.unwrap() |
| 86 |
.map(|m| (m.start(), m.end())); |
| 87 |
assert_eq!( |
| 88 |
str_result, bytes_result, |
| 89 |
"Expected regex '{}' find results to agree between str and bytes mode for text '{}'", |
| 90 |
re, text |
| 91 |
); |
| 92 |
str_result |
| 93 |
} |
| 94 |
|
| 95 |
/// Run `find_input` against `text` in both str mode and ASCII bytes mode, assert |
| 96 |
/// that both agree, and return the common result. |
| 97 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 98 |
#[allow(dead_code)] |
| 99 |
pub fn assert_find_input( |
| 100 |
re: &str, |
| 101 |
text: &str, |
| 102 |
start: usize, |
| 103 |
range: Range<usize>, |
| 104 |
) -> Option<(usize, usize)> { |
| 105 |
let str_result = regex(re) |
| 106 |
.find_input(RegexInput::new(text).from_pos(start).range(range.clone())) |
| 107 |
.unwrap() |
| 108 |
.map(|m| (m.start(), m.end())); |
| 109 |
let bytes_result = ascii_bytes_regex(re) |
| 110 |
.find_input( |
| 111 |
RegexInput::new(text.as_bytes()) |
| 112 |
.from_pos(start) |
| 113 |
.range(range), |
| 114 |
) |
| 115 |
.unwrap() |
| 116 |
.map(|m| (m.start(), m.end())); |
| 117 |
assert_eq!( |
| 118 |
str_result, bytes_result, |
| 119 |
"Expected regex '{}' find_input results to agree between str and bytes mode for text '{}'", |
| 120 |
re, text |
| 121 |
); |
| 122 |
str_result |
| 123 |
} |
| 124 |
|
| 125 |
/// Run `captures_iter` against `text` in both str mode and ASCII bytes mode, assert |
| 126 |
/// that both agree on every match's group spans, and return the str-mode results. |
| 127 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 128 |
#[allow(dead_code)] |
| 129 |
pub fn assert_captures_iter<'t>(re: &str, text: &'t str) -> Vec<Captures<'t, str>> { |
| 130 |
let str_results: Vec<_> = regex(re) |
| 131 |
.captures_iter(text) |
| 132 |
.map(|c| c.expect("captures_iter succeeded (str mode)")) |
| 133 |
.collect(); |
| 134 |
let bytes_results: Vec<_> = ascii_bytes_regex(re) |
| 135 |
.captures_iter(text.as_bytes()) |
| 136 |
.map(|c| c.expect("captures_iter succeeded (bytes mode)")) |
| 137 |
.collect(); |
| 138 |
assert_eq!( |
| 139 |
str_results.len(), |
| 140 |
bytes_results.len(), |
| 141 |
"Expected same number of captures_iter results for regex '{}' on '{}' between str and bytes modes", |
| 142 |
re, |
| 143 |
text |
| 144 |
); |
| 145 |
for (i, (s, b)) in str_results.iter().zip(bytes_results.iter()).enumerate() { |
| 146 |
assert_eq!( |
| 147 |
s.len(), |
| 148 |
b.len(), |
| 149 |
"Expected capture group count to agree for regex '{}' on '{}' at match {}", |
| 150 |
re, |
| 151 |
text, |
| 152 |
i |
| 153 |
); |
| 154 |
for j in 0..s.len() { |
| 155 |
let str_span = s.get(j).map(|m| (m.start(), m.end())); |
| 156 |
let bytes_span = b.get(j).map(|m| (m.start(), m.end())); |
| 157 |
assert_eq!( |
| 158 |
str_span, |
| 159 |
bytes_span, |
| 160 |
"Expected capture group {} to agree between str and bytes modes for regex '{}' on '{}' at match {}", |
| 161 |
j, |
| 162 |
re, |
| 163 |
text, |
| 164 |
i |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
str_results |
| 169 |
} |
| 170 |
|
| 171 |
/// Run `captures_from_pos` against `text` starting at `pos` in both str mode and ASCII bytes |
| 172 |
/// mode, assert that both agree on every group span, and return the str-mode result. |
| 173 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 174 |
#[allow(dead_code)] |
| 175 |
pub fn assert_captures_from_pos<'t>( |
| 176 |
re: &str, |
| 177 |
text: &'t str, |
| 178 |
pos: usize, |
| 179 |
) -> Option<Captures<'t, str>> { |
| 180 |
let str_result = regex(re) |
| 181 |
.captures_from_pos(text, pos) |
| 182 |
.expect("expected captures_from_pos to succeed (str mode)"); |
| 183 |
let bytes_result = ascii_bytes_regex(re) |
| 184 |
.captures_from_pos(text.as_bytes(), pos) |
| 185 |
.expect("expected captures_from_pos to succeed (bytes mode)"); |
| 186 |
assert_eq!( |
| 187 |
str_result.is_some(), |
| 188 |
bytes_result.is_some(), |
| 189 |
"Expected regex '{}' captures_from_pos({}) to agree between str and bytes modes for '{}'", |
| 190 |
re, |
| 191 |
pos, |
| 192 |
text |
| 193 |
); |
| 194 |
if let (Some(ref s), Some(ref b)) = (&str_result, &bytes_result) { |
| 195 |
assert_eq!( |
| 196 |
s.len(), |
| 197 |
b.len(), |
| 198 |
"Expected capture group count to agree for regex '{}' on '{}' at pos {}", |
| 199 |
re, |
| 200 |
text, |
| 201 |
pos |
| 202 |
); |
| 203 |
for i in 0..s.len() { |
| 204 |
let str_span = s.get(i).map(|m| (m.start(), m.end())); |
| 205 |
let bytes_span = b.get(i).map(|m| (m.start(), m.end())); |
| 206 |
assert_eq!( |
| 207 |
str_span, |
| 208 |
bytes_span, |
| 209 |
"Expected capture group {} to agree between str and bytes modes for regex '{}' on '{}' at pos {}", |
| 210 |
i, |
| 211 |
re, |
| 212 |
text, |
| 213 |
pos |
| 214 |
); |
| 215 |
} |
| 216 |
} |
| 217 |
str_result |
| 218 |
} |
| 219 |
|
| 220 |
/// Run `captures_input` against `text` in both str mode and ASCII bytes mode, |
| 221 |
/// assert that both agree on every group span, and return the str-mode result. |
| 222 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 223 |
#[allow(dead_code)] |
| 224 |
pub fn assert_captures_input<'t>( |
| 225 |
re: &str, |
| 226 |
text: &'t str, |
| 227 |
start: usize, |
| 228 |
range: Range<usize>, |
| 229 |
) -> Option<Captures<'t, str>> { |
| 230 |
let str_result = regex(re) |
| 231 |
.captures_input(RegexInput::new(text).from_pos(start).range(range.clone())) |
| 232 |
.expect("expected captures_input to succeed (str mode)"); |
| 233 |
let bytes_result = ascii_bytes_regex(re) |
| 234 |
.captures_input( |
| 235 |
RegexInput::new(text.as_bytes()) |
| 236 |
.from_pos(start) |
| 237 |
.range(range), |
| 238 |
) |
| 239 |
.expect("expected captures_input to succeed (bytes mode)"); |
| 240 |
assert_eq!( |
| 241 |
str_result.is_some(), |
| 242 |
bytes_result.is_some(), |
| 243 |
"Expected regex '{}' captures_input to agree between str and bytes modes for '{}'", |
| 244 |
re, |
| 245 |
text |
| 246 |
); |
| 247 |
if let (Some(ref s), Some(ref b)) = (&str_result, &bytes_result) { |
| 248 |
assert_eq!( |
| 249 |
s.len(), |
| 250 |
b.len(), |
| 251 |
"Expected capture group count to agree for regex '{}' on '{}'", |
| 252 |
re, |
| 253 |
text |
| 254 |
); |
| 255 |
for i in 0..s.len() { |
| 256 |
let str_span = s.get(i).map(|m| (m.start(), m.end())); |
| 257 |
let bytes_span = b.get(i).map(|m| (m.start(), m.end())); |
| 258 |
assert_eq!( |
| 259 |
str_span, |
| 260 |
bytes_span, |
| 261 |
"Expected capture group {} to agree between str and bytes modes for regex '{}' on '{}'", |
| 262 |
i, |
| 263 |
re, |
| 264 |
text |
| 265 |
); |
| 266 |
} |
| 267 |
} |
| 268 |
str_result |
| 269 |
} |
| 270 |
|
| 271 |
/// Run `captures` against `text` in both str mode and ASCII bytes mode, assert |
| 272 |
/// that both engines agree on the spans of every capture group, and return the |
| 273 |
/// str-mode `Captures` (or `None` if neither engine matched). |
| 274 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 275 |
#[allow(dead_code)] |
| 276 |
pub fn assert_captures<'t>(re: &str, text: &'t str) -> Option<Captures<'t, str>> { |
| 277 |
let str_result = regex(re) |
| 278 |
.captures(text) |
| 279 |
.expect("expected captures to succeed (str mode)"); |
| 280 |
let bytes_result = ascii_bytes_regex(re) |
| 281 |
.captures(text.as_bytes()) |
| 282 |
.expect("expected captures to succeed (bytes mode)"); |
| 283 |
assert_eq!( |
| 284 |
str_result.is_some(), |
| 285 |
bytes_result.is_some(), |
| 286 |
"Expected regex '{}' captures to agree between str and bytes mode for '{}'", |
| 287 |
re, |
| 288 |
text |
| 289 |
); |
| 290 |
if let (Some(ref s), Some(ref b)) = (&str_result, &bytes_result) { |
| 291 |
assert_eq!( |
| 292 |
s.len(), |
| 293 |
b.len(), |
| 294 |
"Expected capture group count to agree for regex '{}' on '{}'", |
| 295 |
re, |
| 296 |
text |
| 297 |
); |
| 298 |
for i in 0..s.len() { |
| 299 |
let str_span = s.get(i).map(|m| (m.start(), m.end())); |
| 300 |
let bytes_span = b.get(i).map(|m| (m.start(), m.end())); |
| 301 |
assert_eq!( |
| 302 |
str_span, |
| 303 |
bytes_span, |
| 304 |
"Expected capture group {} to agree between str and bytes modes for regex '{}' on '{}'", |
| 305 |
i, |
| 306 |
re, |
| 307 |
text |
| 308 |
); |
| 309 |
} |
| 310 |
} |
| 311 |
str_result |
| 312 |
} |
| 313 |
|
| 314 |
#[cfg_attr(feature = "track_caller", track_caller)] |
| 315 |
#[allow(dead_code)] |
| 316 |
/// Assert that `seek(true)` produces the same first match as without seeking. |
| 317 |
pub fn assert_seek_same(re: &str, text: &str) -> Option<(usize, usize)> { |
| 318 |
let str_result = regex(re).find(text).unwrap(); |
| 319 |
let seek_result = RegexBuilder::new(re) |
| 320 |
.seek(true) |
| 321 |
.build() |
| 322 |
.expect("regex should compile with seek enabled") |
| 323 |
.find(text) |
| 324 |
.unwrap(); |
| 325 |
assert_eq!( |
| 326 |
str_result.map(|m| (m.start(), m.end())), |
| 327 |
seek_result.map(|m| (m.start(), m.end())), |
| 328 |
"seek=true gave different result for /{re}/ on '{text}'" |
| 329 |
); |
| 330 |
str_result.map(|m| (m.start(), m.end())) |
| 331 |
} |