Debian Codegraph @

debian/rust-vendor/fancy-regex/tests/regex_options.rs

raw
1 use fancy_regex::{CompileError, Error, ParseError, Regex, RegexBuilder, RegexInput};
2
3 mod common;
4
5 fn build_regex(builder: &RegexBuilder) -> Regex {
6 let result = builder.build();
7 assert!(
8 result.is_ok(),
9 "Expected regex to build successfully, got {:?}",
10 result.err()
11 );
12 result.unwrap()
13 }
14
15 #[test]
16 fn check_casing_option() {
17 let regex = build_regex(RegexBuilder::new(r"TEST foo").case_insensitive(false));
18
19 assert!(regex.is_match(r"TEST foo").unwrap_or_default());
20 assert!(!regex.is_match(r"test foo").unwrap_or_default());
21 }
22
23 #[test]
24 fn check_override_casing_option() {
25 let regex = build_regex(RegexBuilder::new(r"FOO(?i:bar)quux").case_insensitive(false));
26
27 assert!(!regex.is_match("FoObarQuUx").unwrap_or_default());
28 assert!(!regex.is_match("fooBARquux").unwrap_or_default());
29 assert!(regex.is_match("FOObarquux").unwrap_or_default());
30
31 let regex = build_regex(RegexBuilder::new(r"FOO(?-i:bar)quux").case_insensitive(true));
32
33 assert!(regex.is_match("FoObarQuUx").unwrap_or_default());
34 assert!(!regex.is_match("fooBARquux").unwrap_or_default());
35 assert!(regex.is_match("FOObarquuX").unwrap_or_default());
36 }
37
38 #[test]
39 fn check_casing_insensitive_option() {
40 let regex = build_regex(RegexBuilder::new(r"TEST FOO").case_insensitive(true));
41
42 assert!(regex.is_match(r"test foo").unwrap_or_default());
43 }
44
45 #[test]
46 fn check_multi_line_option() {
47 let test_text = "test\nhugo\ntest";
48
49 let regex = build_regex(RegexBuilder::new(r"^test$").multi_line(true));
50 assert!(regex.is_match(test_text).unwrap_or_default());
51
52 let test_text = "foo\nbar\ntest";
53
54 let regex = build_regex(RegexBuilder::new(r"^test\z").multi_line(true));
55 assert!(regex.is_match(test_text).unwrap_or_default());
56
57 let test_text = "foo\nbar\ntest";
58
59 let regex = build_regex(RegexBuilder::new(r"^foo\z").multi_line(true));
60 assert!(!regex.is_match(test_text).unwrap_or_default());
61
62 let regex = build_regex(RegexBuilder::new(r"^foo\z").multi_line(false));
63 assert!(!regex.is_match(test_text).unwrap_or_default());
64 let regex = build_regex(RegexBuilder::new(r"^foo$").multi_line(false));
65 assert!(!regex.is_match(test_text).unwrap_or_default());
66 }
67
68 #[test]
69 fn check_ignore_whitespace_option() {
70 let regex = build_regex(RegexBuilder::new(r"test foo").ignore_whitespace(true));
71
72 let test_text = r"testfoo";
73 assert!(regex.is_match(test_text).unwrap_or_default());
74 }
75
76 #[test]
77 fn check_dot_matches_new_line_option() {
78 let regex = build_regex(RegexBuilder::new(r"<div>(.*?)<\/div>").dot_matches_new_line(true));
79
80 let test_text = r"<div>
81 hello</div>";
82
83 assert!(regex.is_match(test_text).unwrap_or_default());
84 }
85
86 #[test]
87 fn check_casing_insensitive_option_hard() {
88 let regex = build_regex(RegexBuilder::new(r"[a-z](?<=[^f])").case_insensitive(true));
89
90 assert!(regex.is_match(r"J").unwrap_or_default());
91 assert!(!regex.is_match(r"F").unwrap_or_default());
92 assert!(regex.is_match(r"j").unwrap_or_default());
93 }
94
95 #[test]
96 fn check_ignore_whitespace_option_fancy() {
97 let regex = build_regex(RegexBuilder::new(r"(?=test foo)").ignore_whitespace(true));
98
99 let test_text = r"testfoo";
100
101 assert!(regex.is_match(test_text).unwrap_or_default());
102 }
103
104 #[test]
105 fn check_ignore_whitespace_with_lookahead_matches() {
106 let regex = build_regex(RegexBuilder::new(r"(?=test foo)").ignore_whitespace(true));
107
108 let test_text = r"test foo";
109
110 assert!(!regex.is_match(test_text).unwrap_or_default());
111 }
112
113 #[test]
114 fn check_verbose_mode_option() {
115 let pattern = "
116 test foo #hugo
117 ";
118 let regex = build_regex(RegexBuilder::new(pattern).verbose_mode(true));
119
120 let test_text = r"test foo";
121
122 assert!(!regex.is_match(test_text).unwrap_or_default());
123 }
124
125 #[test]
126 fn issue_163_fancy_email_test() {
127 let pattern =
128 r"^(?!\.)(?!.*\.\.)([a-z0-9_'+\-\.]*)[a-z0-9_'+\-]@([a-z0-9][a-z0-9\-]*\.)+[a-z]{2,}$";
129
130 let regex = build_regex(RegexBuilder::new(pattern).case_insensitive(true));
131
132 let test_text = "[email protected]";
133 assert!(regex.is_match(test_text).unwrap());
134 }
135
136 #[test]
137 fn check_oniguruma_mode_changes_wordbounds() {
138 fn find_all_matches(regex: &Regex, text: &'static str) -> Vec<&'static str> {
139 regex.find_iter(text).map(|m| m.unwrap().as_str()).collect()
140 }
141
142 let pattern = r"\<prefix_\w*\>";
143 let test_text = "not_prefix_oops prefix_with_suffix <prefix_>";
144
145 // By default the pattern is interpretted as a left word-bound followed by the literal "prefix_"
146 // followed by any number of word characters ending on a right word-bound
147 let default_mode = Regex::new(pattern).unwrap();
148 let default_matches = find_all_matches(&default_mode, test_text);
149 assert_eq!(default_matches, ["prefix_with_suffix", "prefix_"]);
150
151 // When Oniguruma-mode is used the pattern is instead a literal "<prefix_" followed by any
152 // number of word characters followed by a literal ">"
153 let oniguruma_mode = build_regex(RegexBuilder::new(pattern).oniguruma_mode(true));
154 let oniguruma_matches = find_all_matches(&oniguruma_mode, test_text);
155 assert_eq!(oniguruma_matches, ["<prefix_>"]);
156 }
157
158 #[test]
159 fn empty_noncapturing_group_with_quantifier_oniguruma_mode() {
160 // Oniguruma silently accepts quantifiers on empty non-capturing groups.
161 // These patterns should compile and match any input (they are zero-width).
162 let patterns = ["(?:)*", "(?:)+", "(?:)?", "(?:){3}", "(?:)*?", "(?:)++"];
163 for pat in patterns {
164 let regex = build_regex(RegexBuilder::new(pat).oniguruma_mode(true));
165 assert!(
166 regex.is_match("anything").unwrap(),
167 "Expected '{}' to match",
168 pat
169 );
170 assert!(
171 regex.is_match("").unwrap(),
172 "Expected '{}' to match empty",
173 pat
174 );
175 }
176 }
177
178 #[test]
179 fn empty_noncapturing_group_with_quantifier_default_mode_errors() {
180 // In default mode, quantifiers on empty non-capturing groups should fail.
181 let result = RegexBuilder::new("(?:)*").build();
182 assert!(result.is_err(), "Expected (?:)* to fail in default mode");
183 }
184
185 #[test]
186 fn markdown_fenced_code_block_empty_group_oniguruma_mode() {
187 // Repro of a real-world pattern that syntect produces for markdown
188 // fenced-code-block closing, where a backreference substitution
189 // expands to the empty string.
190 let pattern =
191 "^(?x:\n [ \\t]*\n (\n ```\n (?:(?:`)*|(?:)*)\n )\n (\\s*(?m:$)\\n?)\n)";
192 let regex = build_regex(RegexBuilder::new(pattern).oniguruma_mode(true));
193 assert!(regex.is_match(" ```\n").unwrap());
194 }
195
196 #[test]
197 fn check_crlf_option() {
198 // With CRLF mode and multi-line enabled, ^ and $ should treat \r\n as a single line ending
199 let regex = build_regex(RegexBuilder::new(r"^test$").multi_line(true).crlf(true));
200 assert!(regex.is_match("test").unwrap_or_default());
201 assert!(regex.is_match("\r\ntest\r\n").unwrap_or_default());
202 assert!(regex.is_match("test\r\n").unwrap_or_default());
203 assert!(regex.is_match("\r\ntest").unwrap_or_default());
204 }
205
206 #[test]
207 fn check_crlf_flag_in_pattern() {
208 // The (?mR) flag combination in the pattern itself should enable CRLF mode
209 let regex = build_regex(&RegexBuilder::new(r"(?mR)^test$"));
210 assert!(regex.is_match("test").unwrap_or_default());
211 assert!(regex.is_match("\r\ntest\r\n").unwrap_or_default());
212 assert!(regex.is_match("test\r\n").unwrap_or_default());
213 assert!(regex.is_match("\r\ntest").unwrap_or_default());
214 }
215
216 #[test]
217 fn can_build_multiple_regexes_with_same_options() {
218 let mut builder = RegexBuilder::new(r"^[a-z@.]+$");
219 builder.case_insensitive(true);
220 let regex1 = build_regex(&builder);
221
222 let test_text = "[email protected]";
223 assert!(regex1.is_match(test_text).unwrap());
224
225 let regex2 = build_regex(&builder.pattern(r"AB.$".to_string()));
226
227 let test_text = "abc";
228 assert!(regex2.is_match(test_text).unwrap());
229 }
230
231 #[test]
232 fn changing_builder_options_has_no_effect_on_already_built_regexes() {
233 let pattern1 = r"^[a-z@.]+$";
234 let pattern2 = r"AB.$";
235
236 let mut builder = RegexBuilder::new(pattern1);
237 builder.case_insensitive(true);
238 let regex1 = build_regex(&builder);
239 let regex2 = build_regex(&builder.case_insensitive(false));
240 let regex3 = build_regex(&builder.pattern(pattern2.to_string()));
241 let regex4 = build_regex(&builder.case_insensitive(true));
242
243 let test_text1 = "[email protected]";
244 let test_text2 = "abc";
245
246 assert!(regex1.is_match(test_text1).unwrap());
247 assert!(!regex2.is_match(test_text1).unwrap());
248 assert!(!regex3.is_match(test_text2).unwrap());
249 assert!(regex4.is_match(test_text2).unwrap());
250 }
251
252 #[test]
253 fn check_find_not_empty_option() {
254 // \d* can match empty, so without find_not_empty it matches at position 0 with zero length.
255 // With find_not_empty enabled, that zero-length match is rejected and find returns None.
256 let pattern = r"\d*";
257 let text = "hello";
258
259 let normal = build_regex(RegexBuilder::new(pattern).find_not_empty(false));
260 assert_eq!(
261 normal.find(text).unwrap().map(|m| (m.start(), m.end())),
262 Some((0, 0))
263 );
264
265 let not_empty = build_regex(RegexBuilder::new(pattern).find_not_empty(true));
266 assert_eq!(
267 not_empty.find(text).unwrap().map(|m| (m.start(), m.end())),
268 None
269 );
270 }
271
272 #[test]
273 fn check_find_not_empty_rejects_zero_width_only_patterns() {
274 // Patterns that can only ever produce zero-length matches should fail to compile
275 // when find_not_empty is set, since they can never return a result.
276 let zero_width_patterns = vec![
277 r"^", r"$", r"\b", r"(?!bar)", r"(?<=x)", r"(?<!y)", r"(?=.)", r"\G",
278 ];
279
280 for pattern in zero_width_patterns {
281 let result = RegexBuilder::new(pattern).find_not_empty(true).build();
282 assert!(
283 matches!(
284 result,
285 Err(Error::CompileError(ref e)) if matches!(**e, CompileError::PatternCanNeverMatch)
286 ),
287 "Expected PatternCanNeverMatch for pattern {:?}, got {:?}",
288 pattern,
289 result,
290 );
291 }
292 }
293
294 #[test]
295 fn check_find_not_empty_allows_patterns_that_can_match_non_empty() {
296 // Patterns that can produce non-empty matches should compile fine even with find_not_empty
297 let patterns = vec![
298 r"\d*",
299 r"a?",
300 r"\d+",
301 r"[a-z]+",
302 r"(?:foo)?",
303 r"\d*(?=x)",
304 r"a(?=b)",
305 ];
306
307 for pattern in patterns {
308 let result = RegexBuilder::new(pattern).find_not_empty(true).build();
309 assert!(
310 result.is_ok(),
311 "Expected pattern {:?} to compile with find_not_empty, got {:?}",
312 pattern,
313 result.err(),
314 );
315 }
316 }
317
318 #[test]
319 fn check_find_not_empty_is_match() {
320 // is_match should respect find_not_empty: \d* normally matches empty at position 0
321 // in "hello", but with find_not_empty it should return false.
322 let pattern = r"\d*";
323 let text = "hello";
324
325 let normal = build_regex(RegexBuilder::new(pattern).find_not_empty(false));
326 assert!(normal.is_match(text).unwrap());
327
328 let not_empty = build_regex(RegexBuilder::new(pattern).find_not_empty(true));
329 assert!(!not_empty.is_match(text).unwrap());
330 }
331
332 #[test]
333 fn check_find_not_empty_allows_trailing_lookahead_with_content() {
334 // a?(?=b) optimizes to (a?)b — group 0 is "a?" which is not always empty, so it should compile
335 let regex = build_regex(RegexBuilder::new(r"a(?=b)").find_not_empty(true));
336 assert!(regex.is_match("ab").unwrap());
337 assert!(!regex.is_match("ac").unwrap());
338 }
339
340 // --- unicode_mode tests ---
341 //
342 // These tests document the behavior of `unicode_mode(false)` when matching
343 // against `str` input (the default, which corresponds to UTF-8 encoded text).
344 //
345 // Summary:
346 // - Character classes `\w`, `\d`, `\s` are honoured: they become ASCII-only.
347 // - Negated classes `\W`, `\D`, `\S`, bare `.`, and Unicode properties
348 // `\p{...}` fail to compile, because they could match individual bytes that
349 // violate UTF-8 boundaries (the underlying engine requires valid UTF-8 when
350 // operating on `str`).
351 // - The inline `(?-u)` flag is rejected when it would change the unicode mode
352 // (i.e. when unicode_mode(true) is active, the default), but accepted as a
353 // no-op when it matches the current builder setting (i.e. unicode_mode(false)).
354 // - The unicode flag is also honoured in "hard" (backtracking-VM) patterns
355 // such as those containing lookarounds.
356 //
357 // If you need to use `unicode_mode(false)` with `.`, `\W`, `\D`, `\S`, or
358 // Unicode properties, use the bytes API (`BytesMode::Ascii`) instead.
359
360 /// `\w` with `unicode_mode(false)` on `str` input matches only ASCII word chars.
361 #[test]
362 fn unicode_mode_false_w_is_ascii_only_on_str() {
363 // With unicode enabled, \w matches Unicode letters such as 'é'.
364 let re_unicode = build_regex(RegexBuilder::new(r"^\w+$").unicode_mode(true));
365 assert!(re_unicode.is_match("café").unwrap());
366
367 // With unicode disabled, \w matches only [a-zA-Z0-9_], so 'café' (which
368 // contains the non-ASCII 'é') no longer matches when anchored.
369 let re_ascii = build_regex(RegexBuilder::new(r"^\w+$").unicode_mode(false));
370 assert!(!re_ascii.is_match("café").unwrap());
371 // Pure ASCII still matches.
372 assert!(re_ascii.is_match("cafe").unwrap());
373 }
374
375 /// `\d` with `unicode_mode(false)` on `str` input matches only ASCII digits.
376 #[test]
377 fn unicode_mode_false_d_is_ascii_only_on_str() {
378 // Arabic-Indic digit THREE (U+0663) is matched by \d in Unicode mode.
379 let arabic_digit = "\u{0663}";
380
381 let re_unicode = build_regex(RegexBuilder::new(r"^\d+$").unicode_mode(true));
382 assert!(re_unicode.is_match(arabic_digit).unwrap());
383
384 let re_ascii = build_regex(RegexBuilder::new(r"^\d+$").unicode_mode(false));
385 assert!(!re_ascii.is_match(arabic_digit).unwrap());
386 // Plain ASCII digit still matches.
387 assert!(re_ascii.is_match("3").unwrap());
388 }
389
390 /// `\s` with `unicode_mode(false)` on `str` input matches only ASCII whitespace.
391 #[test]
392 fn unicode_mode_false_s_is_ascii_only_on_str() {
393 // EM SPACE (U+2003) is a Unicode space character but not ASCII whitespace.
394 let em_space = "\u{2003}";
395
396 let re_unicode = build_regex(RegexBuilder::new(r"^\s$").unicode_mode(true));
397 assert!(re_unicode.is_match(em_space).unwrap());
398
399 let re_ascii = build_regex(RegexBuilder::new(r"^\s$").unicode_mode(false));
400 assert!(!re_ascii.is_match(em_space).unwrap());
401 // Plain ASCII space still matches.
402 assert!(re_ascii.is_match(" ").unwrap());
403 }
404
405 /// `\W`, `\D`, and `\S` fail to compile with `unicode_mode(false)` on `str`
406 /// input because their negation could match byte sequences that are not valid
407 /// UTF-8 codepoint boundaries.
408 #[test]
409 fn unicode_mode_false_negated_classes_fail_to_compile_on_str() {
410 for pattern in [r"\W", r"\D", r"\S"] {
411 let result = RegexBuilder::new(pattern).unicode_mode(false).build();
412 assert!(
413 result.is_err(),
414 "Expected '{}' to fail to compile with unicode_mode(false) on str input",
415 pattern
416 );
417 }
418 }
419
420 /// `.` (bare dot) fails to compile with `unicode_mode(false)` on `str` input
421 /// for the same reason: it could match arbitrary bytes.
422 #[test]
423 fn unicode_mode_false_dot_fails_to_compile_on_str() {
424 let result = RegexBuilder::new(r".").unicode_mode(false).build();
425 assert!(
426 result.is_err(),
427 "Expected '.' to fail to compile with unicode_mode(false) on str input"
428 );
429 }
430
431 /// `\p{...}` Unicode properties fail to compile with `unicode_mode(false)`.
432 #[test]
433 fn unicode_mode_false_unicode_properties_fail_to_compile() {
434 let result = RegexBuilder::new(r"\p{L}").unicode_mode(false).build();
435 assert!(
436 result.is_err(),
437 "{}",
438 "Expected \\p{{L}} to fail to compile with unicode_mode(false)"
439 );
440 }
441
442 /// The inline `(?-u)` flag is rejected when unicode mode is **enabled**
443 /// (the default), because it would attempt to change the mode. It is accepted
444 /// as a no-op when unicode mode is **already disabled** via the builder.
445 /// Conversely, inline `(?u)` is accepted when unicode mode is already enabled,
446 /// and rejected when it is disabled.
447 #[test]
448 fn unicode_mode_inline_flag_only_allowed_when_matching_builder_setting() {
449 // (?-u) while unicode is ON (default) → error: would change the mode
450 let result = Regex::new(r"(?-u)\w+");
451 assert!(
452 matches!(
453 result,
454 Err(Error::ParseError(
455 _,
456 ParseError::ChangingUnicodeModeUnsupported
457 ))
458 ),
459 "Expected ChangingUnicodeModeUnsupported for (?-u) with unicode enabled, got {:?}",
460 result
461 );
462
463 // (?-u) while unicode is OFF → accepted (no-op)
464 let result = RegexBuilder::new(r"(?-u)\w+").unicode_mode(false).build();
465 assert!(
466 result.is_ok(),
467 "Expected (?-u) to be accepted when unicode_mode(false), got {:?}",
468 result
469 );
470
471 // (?u) while unicode is ON (default) → accepted (no-op)
472 let result = Regex::new(r"(?u)\w+");
473 assert!(
474 result.is_ok(),
475 "Expected (?u) to be accepted when unicode is enabled (default), got {:?}",
476 result
477 );
478
479 // (?u) while unicode is OFF → error: would change the mode
480 let result = RegexBuilder::new(r"(?u)\w+").unicode_mode(false).build();
481 assert!(
482 matches!(
483 result,
484 Err(Error::ParseError(
485 _,
486 ParseError::ChangingUnicodeModeUnsupported
487 ))
488 ),
489 "Expected ChangingUnicodeModeUnsupported for (?u) with unicode disabled, got {:?}",
490 result
491 );
492 }
493
494 /// `unicode_mode(false)` is also honoured in "hard" (backtracking-VM) patterns
495 /// such as those containing lookaheads.
496 #[test]
497 fn unicode_mode_false_hard_pattern_w_is_ascii_only_on_str() {
498 // The lookahead makes this a "hard" pattern processed by the VM.
499 let re_unicode = build_regex(RegexBuilder::new(r"^\w+(?=!)").unicode_mode(true));
500 let re_ascii = build_regex(RegexBuilder::new(r"^\w+(?=!)").unicode_mode(false));
501
502 // With unicode enabled, \w matches 'é' so the whole pattern matches.
503 assert!(re_unicode.is_match("café!").unwrap());
504 // With unicode disabled, \w is ASCII-only, so the pattern does not match.
505 assert!(!re_ascii.is_match("café!").unwrap());
506 // Pure-ASCII input still matches with unicode disabled.
507 assert!(re_ascii.is_match("cafe!").unwrap());
508 }
509
510 /// `unicode_mode(false)` is honoured for hard-path backrefs: `(\w+)\1` should
511 /// only match ASCII word characters when unicode is disabled.
512 #[test]
513 fn unicode_mode_false_w_is_ascii_only_in_hard_path_backref() {
514 // The backref makes this a "hard" pattern processed by the VM.
515 let re_unicode = build_regex(RegexBuilder::new(r"(\w+)\1").unicode_mode(true));
516 let re_ascii = build_regex(RegexBuilder::new(r"(\w+)\1").unicode_mode(false));
517
518 // With unicode enabled, \w matches Unicode letters like 'δ'.
519 assert!(re_unicode.is_match("δδ").unwrap());
520 // With unicode disabled, \w is ASCII-only, so δδ doesn't match.
521 assert!(!re_ascii.is_match("δδ").unwrap());
522 // Pure-ASCII input still matches with unicode disabled.
523 assert!(re_ascii.is_match("abcabc").unwrap());
524 }
525
526 /// Case-insensitive backrefs with `unicode_mode(false)` should NOT apply
527 /// Unicode case folding (e.g. δ ↔ Δ).
528 #[test]
529 fn unicode_mode_false_backref_casei_no_unicode_folding() {
530 // The backref + case-insensitive makes this a "hard" pattern.
531 let re_unicode = build_regex(
532 RegexBuilder::new(r"(?i)(.)\1")
533 .unicode_mode(true)
534 .case_insensitive(true),
535 );
536 let re_ascii = build_regex(
537 RegexBuilder::new(r"(?i)(.)\1")
538 .unicode_mode(false)
539 .case_insensitive(true),
540 );
541
542 // With unicode enabled, case-insensitive backref equates δ with Δ.
543 assert!(re_unicode.is_match("δΔ").unwrap());
544 // With unicode disabled, only ASCII case folding applies, so δ ≠ Δ.
545 assert!(!re_ascii.is_match("δΔ").unwrap());
546 // ASCII case folding still works.
547 assert!(re_ascii.is_match("aA").unwrap());
548 }
549
550 #[test]
551 fn disallow_empty_match_at_eof_after_newline_does_as_it_says() {
552 fn find_all_matches(regex: &Regex, text: &'static str) -> Vec<usize> {
553 regex.find_iter(text).map(|m| m.unwrap().start()).collect()
554 }
555
556 fn create_regex(pattern: &str, disallow: bool) -> Regex {
557 let regex = build_regex(
558 RegexBuilder::new(pattern)
559 .multi_line(true)
560 .disallow_empty_match_at_eof_after_newline(disallow),
561 );
562 println!("{}", common::DebugRegex(&regex));
563 regex
564 }
565
566 let haystack = "a\nb\n";
567 assert_eq!(
568 find_all_matches(&create_regex(r"^", true), haystack),
569 [0, 2]
570 );
571 assert_eq!(
572 find_all_matches(&create_regex(r"$", true), haystack),
573 [1, 3]
574 );
575 assert_eq!(
576 find_all_matches(&create_regex(r"(?=)", true), haystack),
577 [0, 1, 2, 3]
578 );
579
580 // to demonstrate the difference, here is how it looks without this option
581 assert_eq!(
582 find_all_matches(&create_regex(r"^", false), haystack),
583 [0, 2, 4]
584 );
585 assert_eq!(
586 find_all_matches(&create_regex(r"$", false), haystack),
587 [1, 3, 4]
588 );
589 assert_eq!(
590 find_all_matches(&create_regex(r"(?=)", false), haystack),
591 [0, 1, 2, 3, 4]
592 );
593 }
594
595 #[test]
596 fn disallow_empty_match_at_eof_after_newline_still_allows_slash_z() {
597 fn find_all_matches(regex: &Regex, text: &'static str) -> Vec<usize> {
598 // only collect the start of the match because they are empty anyway
599 regex.find_iter(text).map(|m| m.unwrap().start()).collect()
600 }
601
602 fn create_regex(pattern: &str, disallow: bool) -> Regex {
603 let regex = build_regex(
604 RegexBuilder::new(pattern)
605 .multi_line(true)
606 .disallow_empty_match_at_eof_after_newline(disallow),
607 );
608 println!("{}", common::DebugRegex(&regex));
609 regex
610 }
611
612 let haystack = "a\nb\n";
613 assert_eq!(find_all_matches(&create_regex(r"\z", true), haystack), [4]);
614 assert_eq!(find_all_matches(&create_regex(r"$\z", true), haystack), [4]);
615
616 assert_eq!(find_all_matches(&create_regex(r"\z", false), haystack), [4]);
617 assert_eq!(
618 find_all_matches(&create_regex(r"$\z", false), haystack),
619 [4]
620 );
621 }
622
623 #[test]
624 fn input_assertion_overrides_are_ignored_by_default() {
625 let re = build_regex(&RegexBuilder::new(r"\Afoo\z"));
626 assert_eq!(
627 re.find_input(RegexInput::new("foo").start_text(false).end_text(false))
628 .unwrap()
629 .map(|m| (m.start(), m.end())),
630 Some((0, 3))
631 );
632 }
633
634 #[test]
635 fn input_assertion_overrides_require_builder_opt_in() {
636 let re = build_regex(RegexBuilder::new(r"\Afoo\z").allow_input_assertion_overrides(true));
637 assert_eq!(
638 re.find_input(RegexInput::new("foo").start_text(false).end_text(false))
639 .unwrap()
640 .map(|m| (m.start(), m.end())),
641 None
642 );
643 }
644
645 #[test]
646 fn continue_from_previous_match_end_override_is_suppression_only() {
647 let re = build_regex(RegexBuilder::new(r"\G").allow_input_assertion_overrides(true));
648 let text = "ab";
649
650 let default_spans: Vec<_> = re
651 .find_iter_input(RegexInput::new(text))
652 .map(|m| {
653 let m = m.unwrap();
654 (m.start(), m.end())
655 })
656 .collect();
657 assert_eq!(default_spans, vec![(0, 0)]);
658
659 let suppressed_spans: Vec<_> = re
660 .find_iter_input(RegexInput::new(text).continue_from_previous_match_end(false))
661 .map(|m| {
662 let m = m.unwrap();
663 (m.start(), m.end())
664 })
665 .collect();
666 assert!(suppressed_spans.is_empty());
667
668 let cleared_override_spans: Vec<_> = re
669 .find_iter_input(RegexInput::new(text).continue_from_previous_match_end(true))
670 .map(|m| {
671 let m = m.unwrap();
672 (m.start(), m.end())
673 })
674 .collect();
675 assert_eq!(cleared_override_spans, default_spans);
676 }