use std::{
any::{Any, TypeId},
collections::HashMap,
ops::{Deref, DerefMut},
sync::{Mutex, OnceLock},
};
pub trait Fixture: Sized {
fn set_up() -> crate::Result<Self>;
fn tear_down(self) -> crate::Result<()>;
}
pub trait ConsumableFixture: Sized {
fn set_up() -> crate::Result<Self>;
}
pub struct FixtureOf<T>(T);
impl<T: Default> ConsumableFixture for FixtureOf<T> {
fn set_up() -> crate::Result<Self> {
Ok(Self(T::default()))
}
}
impl<T> Deref for FixtureOf<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for FixtureOf<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub trait StaticFixture: Sized + Sync + Send {
fn set_up_once() -> crate::Result<Self>;
}
impl<F: StaticFixture + 'static> Fixture for &'static F {
fn set_up() -> crate::Result<Self> {
static ONCE_FIXTURE_REPO: OnceLock<
Mutex<HashMap<TypeId, &'static (dyn Any + Sync + Send)>>,
> = OnceLock::new();
let mut map = ONCE_FIXTURE_REPO.get_or_init(|| Mutex::new(HashMap::new())).lock()?;
let any =
map.entry(TypeId::of::<F>()).or_insert_with(|| Box::leak(Box::new(F::set_up_once())));
match any.downcast_ref::<crate::Result<F>>() {
Some(Ok(ref fixture)) => Ok(fixture),
Some(Err(e)) => Err(e.clone()),
None => panic!("Downcast failed. This is a bug in GoogleTest Rust"),
}
}
fn tear_down(self) -> crate::Result<()> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::sync::Once;
use super::FixtureOf;
use super::StaticFixture;
use crate as googletest;
use crate::prelude::*;
use crate::test;
use crate::Result;
#[test]
fn fixture_no_fixture() -> Result<()> {
Ok(())
}
struct AlwaysSucceed;
impl Fixture for AlwaysSucceed {
fn set_up() -> crate::Result<Self> {
Ok(Self)
}
fn tear_down(self) -> crate::Result<()> {
Ok(())
}
}
#[test]
fn fixture_one_fixture(_: &AlwaysSucceed) -> Result<()> {
Ok(())
}
#[test]
fn fixture_three_fixtures(
_: &AlwaysSucceed,
_: &AlwaysSucceed,
_: &AlwaysSucceed,
) -> Result<()> {
Ok(())
}
struct NotAFixture {
a_field: i32,
}
impl Default for NotAFixture {
fn default() -> Self {
Self { a_field: 33 }
}
}
#[test]
fn fixture_of_non_fixture(not_a_fixture: FixtureOf<NotAFixture>) -> Result<()> {
verify_that!(not_a_fixture.a_field, eq(33))
}
#[test]
fn fixture_of_non_fixture_mut(mut not_a_fixture: FixtureOf<NotAFixture>) -> Result<()> {
not_a_fixture.a_field += 10;
verify_that!(not_a_fixture.a_field, eq(43))
}
struct PanickyFixture;
impl Fixture for PanickyFixture {
fn set_up() -> crate::Result<Self> {
Ok(Self)
}
fn tear_down(self) -> crate::Result<()> {
panic!("Whoooops");
}
}
#[test]
#[should_panic(expected = "Whoooops")]
fn fixture_teardown_called_even_if_test_fail(_: &PanickyFixture) {
panic!("Test failed");
}
struct FailingTearDown;
impl Fixture for FailingTearDown {
fn set_up() -> crate::Result<Self> {
Ok(Self)
}
fn tear_down(self) -> crate::Result<()> {
Err(googletest::TestAssertionFailure::create("It must fail!".into()))
}
}
struct OnlyOnce;
impl StaticFixture for OnlyOnce {
fn set_up_once() -> crate::Result<Self> {
static ONCE: Once = Once::new();
assert!(!ONCE.is_completed());
ONCE.call_once(|| {});
Ok(Self)
}
}
#[test]
fn static_fixture_works(_: &&OnlyOnce) {}
#[test]
fn static_fixture_same_static_fixture_twice(once: &&OnlyOnce, twice: &&OnlyOnce) {
let once: *const OnlyOnce = *once;
let twice: *const OnlyOnce = *twice;
expect_eq!(once, twice);
}
struct AnotherStaticFixture;
impl StaticFixture for AnotherStaticFixture {
fn set_up_once() -> crate::Result<Self> {
Ok(Self)
}
}
#[test]
fn static_fixture_two_different_static_fixtures(_: &&OnlyOnce, _: &&AnotherStaticFixture) {}
}