use std::cell::{RefCell, RefMut};
use std::fmt::{Debug, Display, Error, Formatter};
use std::thread_local;
#[doc(hidden)]
pub enum TestOutcome {
Success,
Failure,
}
thread_local! {
static CURRENT_TEST_OUTCOME: RefCell<Option<TestOutcome>> = const { RefCell::new(None) };
}
impl TestOutcome {
#[doc(hidden)]
pub fn init_current_test_outcome() {
Self::with_current_test_outcome(|mut current_test_outcome| {
*current_test_outcome = Some(TestOutcome::Success);
})
}
#[doc(hidden)]
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 GoogleTest.")
}
};
if let Err(fatal_assertion_failure) = inner_result {
println!("{fatal_assertion_failure}");
}
*outcome = None;
outer_result
})
}
#[track_caller]
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 GoogleTest.");
match outcome {
TestOutcome::Success => Ok(()),
TestOutcome::Failure => Err(TestAssertionFailure::create("Test failed".into())),
}
})
}
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 GoogleTest.");
*outcome = TestOutcome::Failure;
})
}
fn with_current_test_outcome<T>(action: impl FnOnce(RefMut<Option<TestOutcome>>) -> T) -> T {
CURRENT_TEST_OUTCOME.with(|current_test_outcome| action(current_test_outcome.borrow_mut()))
}
pub(crate) fn ensure_test_context_present() {
TestOutcome::with_current_test_outcome(|outcome| {
outcome.as_ref().expect(
"
No test context found.
* Did you annotate the test with gtest?
* Is the assertion running in the original test thread?
",
);
})
}
}
pub struct TestFailure;
impl std::error::Error for TestFailure {}
impl std::fmt::Debug for TestFailure {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
writeln!(f, "See failure output above")?;
Ok(())
}
}
impl std::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>,
location: Location,
}
#[doc(hidden)]
#[derive(Clone)]
enum Location {
Real(&'static std::panic::Location<'static>),
Fake { file: &'static str, line: u32, column: u32 },
}
impl Display for Location {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Location::Real(l) => write!(f, "{l}"),
Location::Fake { file, line, column } => write!(f, "{file}:{line}:{column}"),
}
}
}
impl TestAssertionFailure {
#[track_caller]
pub fn create(description: String) -> Self {
Self {
description,
custom_message: None,
location: Location::Real(std::panic::Location::caller()),
}
}
pub fn with_fake_location(mut self, file: &'static str, line: u32, column: u32) -> Self {
self.location = Location::Fake { file, line, column };
self
}
pub(crate) fn log(&self) {
TestOutcome::fail_current_test();
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}")?;
}
writeln!(f, " at {}", self.location)
}
}
impl Debug for TestAssertionFailure {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
Display::fmt(self, f)
}
}
impl<T: std::error::Error> From<T> for TestAssertionFailure {
#[track_caller]
fn from(value: T) -> Self {
TestAssertionFailure::create(format!("{value}"))
}
}
#[cfg(feature = "proptest")]
impl From<TestAssertionFailure> for proptest::test_runner::TestCaseError {
fn from(value: TestAssertionFailure) -> Self {
proptest::test_runner::TestCaseError::Fail(format!("{value}").into())
}
}