assertr/assertions/core/
display.rs

1use alloc::format;
2use core::fmt::Display;
3
4use crate::assertions::core::strip_quotation_marks;
5use crate::{AssertThat, Mode, tracking::AssertionTracking};
6
7pub trait DisplayAssertions {
8    fn has_display_value(self, expected: impl Display) -> Self;
9}
10
11impl<T: Display, M: Mode> DisplayAssertions for AssertThat<'_, T, M> {
12    #[track_caller]
13    fn has_display_value(self, expected: impl Display) -> Self {
14        self.track_assertion();
15
16        let actual_string = format!("{}", self.actual());
17        let expected_string = format!("{}", expected);
18
19        let actual_str = strip_quotation_marks(actual_string.as_str());
20        let expected_str = strip_quotation_marks(expected_string.as_str());
21
22        if actual_str != expected_str {
23            self.fail(format_args!(
24                "Expected: {expected_str:?}\n\n  Actual: {actual_str:?}\n"
25            ));
26        }
27        self
28    }
29}
30
31#[cfg(test)]
32mod tests {
33
34    mod has_display_value {
35
36        mod with_number {
37            use crate::prelude::*;
38            use indoc::formatdoc;
39
40            #[test]
41            fn succeeds_when_equal_using_same_value() {
42                assert_that(42).has_display_value(42);
43            }
44
45            #[test]
46            fn succeeds_when_equal_using_string_representation() {
47                assert_that(42).has_display_value("42");
48            }
49
50            #[test]
51            fn panics_when_not_equal() {
52                assert_that_panic_by(|| {
53                    assert_that(42)
54                        .with_location(false)
55                        .has_display_value("foo")
56                })
57                .has_type::<String>()
58                .is_equal_to(formatdoc! {r#"
59                    -------- assertr --------
60                    Expected: "foo"
61
62                      Actual: "42"
63                    -------- assertr --------
64                "#});
65            }
66        }
67
68        mod with_string {
69            use crate::prelude::*;
70            use indoc::formatdoc;
71
72            #[test]
73            fn succeeds_when_equal_using_string_representation() {
74                assert_that("foo:bar").has_display_value("foo:bar");
75            }
76
77            #[test]
78            fn panics_when_not_equal() {
79                assert_that_panic_by(|| {
80                    assert_that("foo:bar")
81                        .with_location(false)
82                        .has_display_value("foo:baz")
83                })
84                .has_type::<String>()
85                .is_equal_to(formatdoc! {r#"
86                    -------- assertr --------
87                    Expected: "foo:baz"
88
89                      Actual: "foo:bar"
90                    -------- assertr --------
91                "#});
92            }
93        }
94
95        mod with_custom_struct {
96            use std::fmt::Display;
97
98            use crate::prelude::*;
99            use indoc::formatdoc;
100
101            #[allow(dead_code)] // Allow fields to never be read.
102            struct Person {
103                age: u32,
104                alive: bool,
105            }
106
107            impl Display for Person {
108                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109                    f.write_fmt(format_args!(
110                        "PERSON<AGE={},ALIVE={}>",
111                        self.age, self.alive
112                    ))
113                }
114            }
115
116            #[test]
117            fn succeeds_when_equal_using_string_representation() {
118                assert_that(Person {
119                    age: 42,
120                    alive: true,
121                })
122                .has_display_value("PERSON<AGE=42,ALIVE=true>");
123            }
124
125            #[test]
126            fn panics_when_not_equal() {
127                assert_that_panic_by(|| {
128                    assert_that(Person {
129                        age: 42,
130                        alive: true,
131                    })
132                    .with_location(false)
133                    .has_display_value("foo")
134                })
135                .has_type::<String>()
136                .is_equal_to(formatdoc! {r#"
137                    -------- assertr --------
138                    Expected: "foo"
139
140                      Actual: "PERSON<AGE=42,ALIVE=true>"
141                    -------- assertr --------
142                "#});
143            }
144        }
145    }
146}