#![cfg(feature = "regex")]
use std::fmt;
use crate::core::Matcher;
use crate::matchers::strings::RegexMatcher;
use super::MismatchFormat;
pub fn match_regex<'a, Actual>(regex: impl AsRef<str>) -> Matcher<'a, Actual, Actual>
where
Actual: fmt::Debug + AsRef<str> + 'a,
{
Matcher::new(
RegexMatcher::new(regex),
MismatchFormat::new("to match the regex", "to not match the regex"),
)
}
#[cfg(test)]
mod tests {
use super::match_regex;
use crate::expect;
#[test]
fn succeeds_when_matches_regex() {
expect!("foobar").to(match_regex("^foo"));
}
#[test]
fn succeeds_when_not_matches_regex() {
expect!("foobar").to_not(match_regex("^a regex that does not match$"));
}
#[test]
#[should_panic]
fn fails_when_matches_regex() {
expect!("foobar").to_not(match_regex("^foo"));
}
#[test]
#[should_panic]
fn fails_when_not_matches_regex() {
expect!("foobar").to(match_regex("^a regex that does not match$"));
}
#[test]
#[should_panic]
fn fails_when_regex_is_invalid() {
expect!("foobar").to(match_regex("[an invalid regex"));
}
}