1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use crate::description::Description;
16 use crate::matcher::{Matcher, MatcherResult};
17 use std::fmt::{Debug, Display};
18 use std::marker::PhantomData;
19 
20 /// Matches the string representation of types that implement `Display`.
21 ///
22 /// ```ignore
23 /// let result: impl Display = ...;
24 /// verify_that!(result, displays_as(eq(format!("{}", result))))?;
25 /// ```
displays_as<T: Debug + Display, InnerMatcher: Matcher<ActualT = String>>( inner: InnerMatcher, ) -> impl Matcher<ActualT = T>26 pub fn displays_as<T: Debug + Display, InnerMatcher: Matcher<ActualT = String>>(
27     inner: InnerMatcher,
28 ) -> impl Matcher<ActualT = T> {
29     DisplayMatcher::<T, _> { inner, phantom: Default::default() }
30 }
31 
32 struct DisplayMatcher<T, InnerMatcher: Matcher> {
33     inner: InnerMatcher,
34     phantom: PhantomData<T>,
35 }
36 
37 impl<T: Debug + Display, InnerMatcher: Matcher<ActualT = String>> Matcher
38     for DisplayMatcher<T, InnerMatcher>
39 {
40     type ActualT = T;
41 
matches(&self, actual: &T) -> MatcherResult42     fn matches(&self, actual: &T) -> MatcherResult {
43         self.inner.matches(&format!("{actual}"))
44     }
45 
explain_match(&self, actual: &T) -> Description46     fn explain_match(&self, actual: &T) -> Description {
47         format!("which displays as a string {}", self.inner.explain_match(&format!("{actual}")))
48             .into()
49     }
50 
describe(&self, matcher_result: MatcherResult) -> Description51     fn describe(&self, matcher_result: MatcherResult) -> Description {
52         match matcher_result {
53             MatcherResult::Match => {
54                 format!("displays as a string which {}", self.inner.describe(MatcherResult::Match))
55                     .into()
56             }
57             MatcherResult::NoMatch => format!(
58                 "doesn't display as a string which {}",
59                 self.inner.describe(MatcherResult::Match)
60             )
61             .into(),
62         }
63     }
64 }
65 
66 #[cfg(test)]
67 mod tests {
68     use super::displays_as;
69     use crate::prelude::*;
70     use indoc::indoc;
71     use std::fmt::{Debug, Display, Error, Formatter};
72 
73     #[test]
display_matches_i32() -> Result<()>74     fn display_matches_i32() -> Result<()> {
75         let value = 32;
76         verify_that!(value, displays_as(eq("32")))?;
77         Ok(())
78     }
79 
80     #[test]
display_matches_str() -> Result<()>81     fn display_matches_str() -> Result<()> {
82         let value = "32";
83         verify_that!(value, displays_as(eq("32")))?;
84         Ok(())
85     }
86 
87     #[test]
display_matches_struct() -> Result<()>88     fn display_matches_struct() -> Result<()> {
89         #[allow(dead_code)]
90         #[derive(Debug)]
91         struct Struct {
92             a: i32,
93             b: i64,
94         }
95         impl Display for Struct {
96             fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
97                 write!(f, "{:?}", self)
98             }
99         }
100         verify_that!(Struct { a: 123, b: 321 }, displays_as(eq("Struct { a: 123, b: 321 }")))?;
101         Ok(())
102     }
103 
104     #[test]
display_displays_error_message_with_explanation_from_inner_matcher() -> Result<()>105     fn display_displays_error_message_with_explanation_from_inner_matcher() -> Result<()> {
106         let result = verify_that!("123\n234", displays_as(eq("123\n345")));
107 
108         verify_that!(
109             result,
110             err(displays_as(contains_substring(indoc!(
111                 "
112                   Actual: \"123\\n234\",
113                     which displays as a string which isn't equal to \"123\\n345\"
114                     Difference(-actual / +expected):
115                      123
116                     -234
117                     +345
118                 "
119             ))))
120         )
121     }
122 }
123