lex_core/lex/testing/
matchers.rs1#[derive(Debug, Clone)]
5pub enum TextMatch {
6 Exact(String),
8 StartsWith(String),
10 Contains(String),
12}
13
14impl TextMatch {
15 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 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}