Skip to main content

assert_rs/assertion/
should_fail.rs

1use super::{Assertion, Debug, Display};
2
3/// An assertion that will fail if the given assertion was true
4/// and succeed if the given assertion was false.
5pub struct ShouldFail<A: Assertion>(pub A);
6
7impl<A: Assertion> Display for ShouldFail<A> {
8    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
9        let inner = &self.0;
10        write!(f, "assertion expected to fail succeeded:\n{inner}")
11    }
12}
13
14#[cfg(feature = "std")]
15impl<A: Assertion> std::process::Termination for ShouldFail<A> {
16    fn report(self) -> std::process::ExitCode {
17        if self.0.test() {
18            println!("{self}");
19            std::process::ExitCode::FAILURE
20        } else {
21            std::process::ExitCode::SUCCESS
22        }
23    }
24}
25
26impl<A: Assertion> Assertion for ShouldFail<A> {
27    fn test(&self) -> bool {
28        !self.0.test()
29    }
30}
31
32impl<A: Assertion> Debug for ShouldFail<A> {
33    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34        Display::fmt(self, f)
35    }
36}