Skip to main content

assert_rs/assertion/
succeed.rs

1use super::{AssertInfo, Assertion, Debug, Display};
2
3/// An assertion that will unconditionally succeed.
4#[must_use = "assertions do not fire unless returned from a test"]
5pub struct Succeed {
6    info: AssertInfo,
7}
8
9impl Display for Succeed {
10    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11        let AssertInfo { file, line, column } = self.info;
12
13        write!(
14            f,
15            "assertion starting at {file}:{line}:{column} was forced success"
16        )
17    }
18}
19
20impl Debug for Succeed {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        Display::fmt(self, f)
23    }
24}
25
26impl Succeed {
27    #[doc(hidden)]
28    /// Don't use this constructor. Use [`succeed!`] instead.
29    pub const fn manual_constructor(file: &'static str, line: u32, column: u32) -> Self {
30        Self {
31            info: AssertInfo { file, line, column },
32        }
33    }
34}
35
36#[cfg(feature = "std")]
37impl std::process::Termination for Succeed {
38    fn report(self) -> std::process::ExitCode {
39        std::process::ExitCode::SUCCESS
40    }
41}
42
43impl Assertion for Succeed {
44    fn test(&self) -> bool {
45        true
46    }
47}
48
49/// Construct an [`Assertion`] that will always succeed.
50///
51/// ```no_run
52/// #[test]
53/// fn succeed() -> impl Assertion {
54///     succeed!()
55/// }
56/// ```
57#[macro_export]
58macro_rules! succeed {
59    () => {
60        $crate::assertion::Succeed::manual_constructor(
61            ::core::file!(),
62            ::core::line!(),
63            ::core::column!(),
64        )
65    };
66}