use alloc::string::String;
use core::fmt::{Debug, Display, Error, Formatter};
#[doc(hidden)]
pub enum TestOutcome {
Success,
Failure,
}
#[cfg(feature = "std")]
std::thread_local! {
static CURRENT_TEST_OUTCOME: core::cell::RefCell<Option<TestOutcome>> = const { core::cell::RefCell::new(None) };
}
impl TestOutcome {
#[doc(hidden)]
#[cfg(feature = "std")]
pub fn init_current_test_outcome() {
Self::with_current_test_outcome(|mut current_test_outcome| {
*current_test_outcome = Some(TestOutcome::Success);
})
}
#[doc(hidden)]
#[cfg(feature = "std")]
pub fn close_current_test_outcome<E: Display>(
inner_result: Result<(), E>,
) -> Result<(), TestFailure> {
TestOutcome::with_current_test_outcome(|mut outcome| {
let outer_result = match &*outcome {
Some(TestOutcome::Success) => match inner_result {
Ok(()) => Ok(()),
Err(_) => Err(TestFailure),
},
Some(TestOutcome::Failure) => Err(TestFailure),
None => {
panic!("No test context found. This indicates a bug in Test That!.")
}
};
if let Err(fatal_assertion_failure) = inner_result {
std::println!("{fatal_assertion_failure}");
}
*outcome = None;
outer_result
})
}
#[cfg(feature = "std")]
pub(crate) fn get_current_test_outcome() -> Result<(), TestAssertionFailure> {
TestOutcome::with_current_test_outcome(|mut outcome| {
let outcome = outcome
.as_mut()
.expect("No test context found. This indicates a bug in Test That!.");
match outcome {
TestOutcome::Success => Ok(()),
TestOutcome::Failure => Err(TestAssertionFailure::create("Test failed".into())),
}
})
}
#[cfg(feature = "std")]
fn fail_current_test() {
TestOutcome::with_current_test_outcome(|mut outcome| {
let outcome = outcome
.as_mut()
.expect("No test context found. This indicates a bug in Test That!.");
*outcome = TestOutcome::Failure;
})
}
#[cfg(feature = "std")]
fn with_current_test_outcome<T>(
action: impl FnOnce(core::cell::RefMut<Option<TestOutcome>>) -> T,
) -> T {
CURRENT_TEST_OUTCOME.with(|current_test_outcome| action(current_test_outcome.borrow_mut()))
}
pub(crate) fn ensure_text_context_present() {
#[cfg(feature = "std")]
TestOutcome::with_current_test_outcome(|outcome| {
outcome.as_ref().expect(
"
No test context found.
* Did you annotate the test with test_that::test?
* Is the assertion running in the original test thread?
",
);
})
}
}
pub struct TestFailure;
#[cfg(feature = "std")]
impl std::error::Error for TestFailure {}
impl core::fmt::Debug for TestFailure {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
writeln!(f, "See failure output above")?;
Ok(())
}
}
impl core::fmt::Display for TestFailure {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
writeln!(f, "See failure output above")?;
Ok(())
}
}
#[doc(hidden)]
#[derive(Clone)]
pub struct TestAssertionFailure {
pub description: String,
pub custom_message: Option<String>,
}
impl TestAssertionFailure {
pub fn create(description: String) -> Self {
Self { description, custom_message: None }
}
pub(crate) fn log(&self) {
#[cfg(feature = "std")]
TestOutcome::fail_current_test();
#[cfg(feature = "std")]
std::println!("{}", self);
}
}
impl Display for TestAssertionFailure {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
writeln!(f, "{}", self.description)?;
if let Some(custom_message) = &self.custom_message {
writeln!(f, "{}", custom_message)?;
}
Ok(())
}
}
impl Debug for TestAssertionFailure {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
Display::fmt(self, f)
}
}
impl<T: core::error::Error> From<T> for TestAssertionFailure {
fn from(value: T) -> Self {
TestAssertionFailure::create(alloc::format!("{value}"))
}
}
#[cfg(feature = "proptest")]
impl From<TestAssertionFailure> for proptest::test_runner::TestCaseError {
fn from(value: TestAssertionFailure) -> Self {
proptest::test_runner::TestCaseError::Fail(alloc::format!("{value}").into())
}
}