Skip to main content

lex_core/lex/testing/
matchers.rs

1//! Text matching utilities for AST assertions
2
3/// Text matching strategies for assertions
4#[derive(Debug, Clone)]
5pub enum TextMatch {
6    /// Exact text match
7    Exact(String),
8    /// Text starts with prefix
9    StartsWith(String),
10    /// Text contains substring
11    Contains(String),
12}
13
14impl TextMatch {
15    /// Check if the actual text matches this pattern (returns bool)
16    pub fn matches(&self, actual: &str) -> bool {
17        match self {
18            TextMatch::Exact(expected) => actual == expected,
19            TextMatch::StartsWith(prefix) => actual.starts_with(prefix),
20            TextMatch::Contains(substring) => actual.contains(substring),
21        }
22    }
23
24    /// Assert that the actual text matches this pattern
25    pub fn assert(&self, actual: &str, context: &str) {
26        match self {
27            TextMatch::Exact(expected) => {
28                assert_eq!(
29                    actual, expected,
30                    "{context}: Expected text to be '{expected}', but got '{actual}'"
31                );
32            }
33            TextMatch::StartsWith(prefix) => {
34                assert!(
35                    actual.starts_with(prefix),
36                    "{context}: Expected text to start with '{prefix}', but got '{actual}'"
37                );
38            }
39            TextMatch::Contains(substring) => {
40                assert!(
41                    actual.contains(substring),
42                    "{context}: Expected text to contain '{substring}', but got '{actual}'"
43                );
44            }
45        }
46    }
47}