Add Comprehensive Tests for Backreference Behavior #15

Closed
opened 2026-03-11 18:10:43 +03:00 by NiXTheDev · 0 comments
NiXTheDev commented 2026-03-11 18:10:43 +03:00 (Migrated from github.com)

Goal: Ensure backreferences (numbered, named, relative) work correctly in all edge cases.

Current tests cover basic backreferences, but need to include:

  • Relative backreferences with no numbered groups (should fail to match? Or treat as unmatchable).
  • Relative backreferences within named groups (group numbering exclusions).
  • Backreferences to groups that haven't been captured yet (e.g., forward reference) – should not match.
  • Multiple backreferences to the same group.
  • Backreferences inside quantified groups.
  • Nested backreferences.

Implementation Plan:
Create a new test module tests/backreference_edge_cases.rs (or add to existing) with specific test cases for each scenario. Use Regex::new and is_match/find to verify expected behavior.

Expected Behavior (to be defined):

  • If a backreference refers to a group that never captured (e.g., because it was in an unmatched alternative), it should not match.
  • Relative backreferences that go beyond the number of captured groups should cause the match to fail.

Example:

#[test]
fn relative_backref_without_enough_groups() {
    let re = Regex::new(r"(a)\g{-2}").unwrap(); // only one group, but -2 requested
    assert!(!re.is_match("aa"));
}
**Goal**: Ensure backreferences (numbered, named, relative) work correctly in all edge cases. Current tests cover basic backreferences, but need to include: - Relative backreferences with no numbered groups (should fail to match? Or treat as unmatchable). - Relative backreferences within named groups (group numbering exclusions). - Backreferences to groups that haven't been captured yet (e.g., forward reference) – should not match. - Multiple backreferences to the same group. - Backreferences inside quantified groups. - Nested backreferences. **Implementation Plan**: Create a new test module `tests/backreference_edge_cases.rs` (or add to existing) with specific test cases for each scenario. Use `Regex::new` and `is_match`/`find` to verify expected behavior. **Expected Behavior** (to be defined): - If a backreference refers to a group that never captured (e.g., because it was in an unmatched alternative), it should not match. - Relative backreferences that go beyond the number of captured groups should cause the match to fail. **Example**: ```rust #[test] fn relative_backref_without_enough_groups() { let re = Regex::new(r"(a)\g{-2}").unwrap(); // only one group, but -2 requested assert!(!re.is_match("aa")); } ```
Sign in to join this conversation.
No description provided.