#[cfg(test)]
pub mod tests {
use crate::util::time::tests::create_time;
use chrono::{DateTime, Utc};
use googletest::description::Description;
use googletest::matcher::{Matcher, MatcherBase, MatcherResult};
use std::fmt::Debug;
use termcolor::{Color, ColorSpec};
#[derive(MatcherBase)]
pub struct GenericMatcher<T> {
expected: T,
}
impl<T: Debug + PartialEq> Matcher<&T> for GenericMatcher<T> {
fn matches(&self, actual: &T) -> MatcherResult {
(self.expected == *actual).into()
}
fn describe(&self, result: MatcherResult) -> Description {
describe(&self.expected, result, None)
}
}
impl<T: Copy + Debug + PartialEq> Matcher<T> for GenericMatcher<T> {
fn matches(&self, actual: T) -> MatcherResult {
(self.expected == actual).into()
}
fn describe(&self, result: MatcherResult) -> Description {
describe(&self.expected, result, None)
}
}
pub fn time_eq(
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
) -> GenericMatcher<DateTime<Utc>> {
let expected = create_time(year, month, day, hour, minute, second).unwrap();
GenericMatcher { expected }
}
pub fn color_eq(
fg_color: Option<Color>,
bg_color: Option<Color>,
bold: bool,
under: bool,
) -> GenericMatcher<ColorSpec> {
let mut expected = ColorSpec::new();
expected.set_fg(fg_color);
expected.set_bg(bg_color);
expected.set_bold(bold);
expected.set_underline(under);
GenericMatcher { expected }
}
fn describe<T: Debug>(
expected: &T,
result: MatcherResult,
field: Option<&str>,
) -> Description {
let text = match (field, result) {
(None, MatcherResult::Match) => format!("is equal to {:?}", expected),
(None, MatcherResult::NoMatch) => format!("isn't equal to {:?}", expected),
(Some(field), MatcherResult::Match) => format!("[{}] is equal to {:?}", field, expected),
(Some(field), MatcherResult::NoMatch) => format!("[{}] isn't equal to {:?}", field, expected),
};
Description::new().text(text)
}
}