Skip to main content

assert_rs/
assertion.rs

1use core::fmt::{Debug, Display};
2
3use crate::AssertInfo;
4
5mod binary;
6mod collected;
7mod fail;
8mod multi;
9mod should_fail;
10mod succeed;
11mod unary;
12
13#[cfg(feature = "alloc")]
14mod multi_dyn;
15
16pub use binary::BinaryAssertion;
17pub use collected::{AssertAll, CollectedAssertion};
18pub use fail::Fail;
19pub use multi::MultiAssertion;
20pub use should_fail::ShouldFail;
21pub use succeed::Succeed;
22pub use unary::UnaryAssertion;
23
24#[cfg(feature = "alloc")]
25pub use multi_dyn::MultiDynAssertion;
26
27#[cfg(feature = "std")]
28#[must_use = "assertions do not fire unless returned from a test"]
29/// An assertion that has been made about some object.
30pub trait Assertion: Debug + Display + std::process::Termination {
31    /// `true` if the assertion was true.
32    fn test(&self) -> bool;
33
34    /// Wrap the assertion in [`ShouldFail`], inverting its condition.
35    fn should_fail(self) -> ShouldFail<Self>
36    where
37        Self: Sized,
38    {
39        ShouldFail(self)
40    }
41
42    /// Panic if the assertion was false.
43    ///
44    /// # Panics
45    ///
46    /// See above.
47    #[allow(
48        clippy::panic,
49        reason = "I agree with you clippy, but some people don't care about panics."
50    )]
51    fn unwrap(&self) {
52        assert!(self.test(), "failed assertion was unwrapped:\n{self}");
53    }
54}
55
56#[cfg(not(feature = "std"))]
57#[must_use = "assertions do not fire unless returned from a test"]
58/// An assertion that has been made about some object.
59pub trait Assertion: Debug + Display {
60    /// `true` if the assertion was true.
61    fn test(&self) -> bool;
62
63    /// Wrap the assertion in [`ShouldFail`], inverting its condition.
64    fn should_fail(self) -> ShouldFail<Self>
65    where
66        Self: Sized,
67    {
68        ShouldFail(self)
69    }
70
71    /// Panic if the assertion was false.
72    ///
73    /// # Panics
74    ///
75    /// See above.
76    #[allow(
77        clippy::panic,
78        reason = "I agree with you clippy, but some people don't care about panics."
79    )]
80    fn unwrap(&self) {
81        assert!(self.test(), "failed assertion was unwrapped:\n{self}");
82    }
83}