assert-rs 0.1.1

An assertion library that uses types and data to fail tests instead of panicking.
Documentation
use core::fmt::{Debug, Display};

use crate::AssertInfo;

mod binary;
mod collected;
mod fail;
mod multi;
mod should_fail;
mod succeed;
mod unary;

#[cfg(feature = "alloc")]
mod multi_dyn;

pub use binary::BinaryAssertion;
pub use collected::{AssertAll, CollectedAssertion};
pub use fail::Fail;
pub use multi::MultiAssertion;
pub use should_fail::ShouldFail;
pub use succeed::Succeed;
pub use unary::UnaryAssertion;

#[cfg(feature = "alloc")]
pub use multi_dyn::MultiDynAssertion;

#[cfg(feature = "std")]
#[must_use = "assertions do not fire unless returned from a test"]
/// An assertion that has been made about some object.
pub trait Assertion: Debug + Display + std::process::Termination {
    /// `true` if the assertion was true.
    fn test(&self) -> bool;

    /// Wrap the assertion in [`ShouldFail`], inverting its condition.
    fn should_fail(self) -> ShouldFail<Self>
    where
        Self: Sized,
    {
        ShouldFail(self)
    }

    /// Panic if the assertion was false.
    ///
    /// # Panics
    ///
    /// See above.
    #[allow(
        clippy::panic,
        reason = "I agree with you clippy, but some people don't care about panics."
    )]
    fn unwrap(&self) {
        assert!(self.test(), "failed assertion was unwrapped:\n{self}");
    }
}

#[cfg(not(feature = "std"))]
#[must_use = "assertions do not fire unless returned from a test"]
/// An assertion that has been made about some object.
pub trait Assertion: Debug + Display {
    /// `true` if the assertion was true.
    fn test(&self) -> bool;

    /// Wrap the assertion in [`ShouldFail`], inverting its condition.
    fn should_fail(self) -> ShouldFail<Self>
    where
        Self: Sized,
    {
        ShouldFail(self)
    }

    /// Panic if the assertion was false.
    ///
    /// # Panics
    ///
    /// See above.
    #[allow(
        clippy::panic,
        reason = "I agree with you clippy, but some people don't care about panics."
    )]
    fn unwrap(&self) {
        assert!(self.test(), "failed assertion was unwrapped:\n{self}");
    }
}