1#[cfg(test)]
2pub mod tests {
3 use crate::util::time::tests::create_time;
4 use chrono::{DateTime, Utc};
5 use googletest::description::Description;
6 use googletest::matcher::{Matcher, MatcherBase, MatcherResult};
7 use std::fmt::Debug;
8 use termcolor::{Color, ColorSpec};
9
10 #[derive(MatcherBase)]
11 pub struct GenericMatcher<T> {
12 expected: T,
13 }
14
15 impl<T: Debug + PartialEq> Matcher<&T> for GenericMatcher<T> {
16 fn matches(&self, actual: &T) -> MatcherResult {
17 (self.expected == *actual).into()
18 }
19
20 fn describe(&self, result: MatcherResult) -> Description {
21 describe(&self.expected, result, None)
22 }
23 }
24
25 impl<T: Copy + Debug + PartialEq> Matcher<T> for GenericMatcher<T> {
26 fn matches(&self, actual: T) -> MatcherResult {
27 (self.expected == actual).into()
28 }
29
30 fn describe(&self, result: MatcherResult) -> Description {
31 describe(&self.expected, result, None)
32 }
33 }
34
35 pub fn time_eq(
36 year: i32,
37 month: u32,
38 day: u32,
39 hour: u32,
40 minute: u32,
41 second: u32,
42 ) -> GenericMatcher<DateTime<Utc>> {
43 let expected = create_time(year, month, day, hour, minute, second).unwrap();
44 GenericMatcher { expected }
45 }
46
47 pub fn color_eq(
48 fg_color: Option<Color>,
49 bg_color: Option<Color>,
50 bold: bool,
51 under: bool,
52 ) -> GenericMatcher<ColorSpec> {
53 let mut expected = ColorSpec::new();
54 expected.set_fg(fg_color);
55 expected.set_bg(bg_color);
56 expected.set_bold(bold);
57 expected.set_underline(under);
58 GenericMatcher { expected }
59 }
60
61 fn describe<T: Debug>(
62 expected: &T,
63 result: MatcherResult,
64 field: Option<&str>,
65 ) -> Description {
66 let text = match (field, result) {
67 (None, MatcherResult::Match) => format!("is equal to {:?}", expected),
68 (None, MatcherResult::NoMatch) => format!("isn't equal to {:?}", expected),
69 (Some(field), MatcherResult::Match) => format!("[{}] is equal to {:?}", field, expected),
70 (Some(field), MatcherResult::NoMatch) => format!("[{}] isn't equal to {:?}", field, expected),
71 };
72 Description::new().text(text)
73 }
74}