clearcheck 0.0.2

Elegant and extensible assertions in rust.
Documentation
use regex::Regex;

use crate::matchers::{Should, ShouldNot};
use crate::matchers::string::regex::match_with;

/// RegularExpressionAssertion enables assertions about whether a string (or str) matches a regular expression.
pub trait RegularExpressionAssertion {
    /// - Asserts that the string matches the given regular expression.
    /// - Returns a reference to self for fluent chaining.
    /// - Panics if the assertion fails.
    /// # Example
    /// ```
    /// use regex::Regex;
    /// use clearcheck::assertions::string::regex::RegularExpressionAssertion;
    ///
    /// let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
    /// let phrase = "Started clearcheck on 2024-01-02.";
    /// phrase.should_match(regex);
    /// ```
    fn should_match(&self, regex: Regex) -> &Self;

    /// - Asserts that the string does not match the given regular expression.
    /// - Returns a reference to self for fluent chaining.
    /// - Panics if the assertion fails.
    /// # Example
    /// ```
    /// use regex::Regex;
    /// use clearcheck::assertions::string::regex::RegularExpressionAssertion;
    ///
    /// let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
    /// let phrase = String::from("Started clearcheck on 02nd January 2024");
    /// phrase.should_not_match(regex);
    /// ```
    fn should_not_match(&self, regex: Regex) -> &Self;
}

impl<T> RegularExpressionAssertion for T
    where T: AsRef<str> {
    fn should_match(&self, regex: Regex) -> &Self {
        self.should(&match_with(regex));
        self
    }

    fn should_not_match(&self, regex: Regex) -> &Self {
        self.should_not(&match_with(regex));
        self
    }
}

#[cfg(all(test, feature = "regex"))]
mod tests {
    use regex::Regex;

    use crate::assertions::string::regex::RegularExpressionAssertion;

    #[test]
    fn should_match_regular_expression() {
        let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
        let str = "Started clearcheck on On 2024-01-02.";
        str.should_match(regex);
    }

    #[test]
    #[should_panic]
    fn should_match_regular_expression_but_it_did_not() {
        let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
        let str = "Started clearcheck on On 02nd January 2024";
        str.should_match(regex);
    }

    #[test]
    fn should_not_match_regular_expression() {
        let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
        let str = "Started clearcheck on On 02nd January 2024";
        str.should_not_match(regex);
    }

    #[test]
    #[should_panic]
    fn should_not_match_regular_expression_but_it_did() {
        let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
        let str = "Started clearcheck on On 2024-01-02.";
        str.should_not_match(regex);
    }
}

#[cfg(all(test, feature = "regex"))]
mod string_tests {
    use regex::Regex;

    use crate::assertions::string::regex::RegularExpressionAssertion;

    #[test]
    fn should_match_regular_expression() {
        let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
        let str = String::from("Started clearcheck on 2024-01-02.");
        str.should_match(regex);
    }

    #[test]
    #[should_panic]
    fn should_match_regular_expression_but_it_did_not() {
        let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
        let str = String::from("Started clearcheck on 02nd January 2024");
        str.should_match(regex);
    }

    #[test]
    fn should_not_match_regular_expression() {
        let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
        let str = String::from("Started clearcheck on 02nd January 2024");
        str.should_not_match(regex);
    }

    #[test]
    #[should_panic]
    fn should_not_match_regular_expression_but_it_did() {
        let regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
        let str = String::from("Started clearcheck on 2024-01-02.");
        str.should_not_match(regex);
    }
}