assert_rs/assertion/
unary.rs1use super::{AssertInfo, Assertion, Debug, Display};
2
3#[must_use = "assertions do not fire unless returned from a test"]
5pub struct UnaryAssertion<T> {
6 this: T,
7 test_result: bool,
8 test_repr: &'static str,
9 info: AssertInfo,
10}
11
12impl<T> Display for UnaryAssertion<T>
13where
14 T: Debug,
15{
16 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17 let Self {
18 this,
19 test_result: _,
20 test_repr,
21 info: AssertInfo { file, line, column },
22 } = self;
23
24 write!(
25 f,
26 "assertion starting at {file}:{line}:{column} failed: `{test_repr}`\nthis: {this:?}"
27 )
28 }
29}
30
31impl<T> Debug for UnaryAssertion<T>
32where
33 T: Debug,
34{
35 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36 Display::fmt(self, f)
37 }
38}
39
40impl<T> UnaryAssertion<T> {
41 pub(crate) const fn new(
42 this: T,
43 test_result: bool,
44 test_repr: &'static str,
45 info: AssertInfo,
46 ) -> Self {
47 Self {
48 this,
49 test_result,
50 test_repr,
51 info,
52 }
53 }
54}
55
56#[cfg(feature = "std")]
57impl<T> std::process::Termination for UnaryAssertion<T>
58where
59 T: Debug,
60{
61 fn report(self) -> std::process::ExitCode {
62 if self.test() {
63 std::process::ExitCode::SUCCESS
64 } else {
65 println!("{self}");
66 std::process::ExitCode::FAILURE
67 }
68 }
69}
70
71impl<T> Assertion for UnaryAssertion<T>
72where
73 T: Debug,
74{
75 fn test(&self) -> bool {
76 self.test_result
77 }
78}