use std::fmt;
use std::future::Future;
use crate::harness::{Harness, TestProgram};
use super::environment::Environment;
use super::support::Context;
#[diagnostic::on_unimplemented(
message = "`{Self}` is not an icedwright fixture",
label = "this parameter must implement `icedwright::Fixture`",
note = "built-in fixtures: `&mut Harness<impl TestProgram>`, `TempDir`, `Environment`, \
`Locale`, `Seed`, `fake::Calls<Req, Resp>`"
)]
pub trait Fixture: Sized + 'static {
type Deps: FixtureSet;
fn setup(
deps: <Self::Deps as FixtureSet>::Refs<'_>,
env: &Environment,
) -> Result<Self, FixtureError>;
fn started(&mut self, runtime: &mut RuntimeRef<'_>) -> Result<(), FixtureError> {
let _ = runtime;
Ok(())
}
fn teardown(
self,
outcome: &TestOutcome,
runtime: &mut RuntimeRef<'_>,
) -> Result<(), FixtureError> {
let _ = (outcome, runtime);
Ok(())
}
}
mod sealed {
pub trait Sealed {}
}
pub trait FixtureSet: sealed::Sealed + 'static {
type Refs<'a>;
#[doc(hidden)]
fn prepare(context: &mut Context) -> Result<(), FixtureError>;
#[doc(hidden)]
fn refs(context: &Context) -> Result<Self::Refs<'_>, FixtureError>;
}
impl sealed::Sealed for () {}
impl FixtureSet for () {
type Refs<'a> = ();
fn prepare(_: &mut Context) -> Result<(), FixtureError> {
Ok(())
}
fn refs(_: &Context) -> Result<(), FixtureError> {
Ok(())
}
}
macro_rules! fixture_sets {
($($name:ident),+) => {
impl<$($name: Fixture),+> sealed::Sealed for ($($name,)+) {}
impl<$($name: Fixture),+> FixtureSet for ($($name,)+) {
type Refs<'a> = ($(&'a $name,)+);
fn prepare(context: &mut Context) -> Result<(), FixtureError> {
$(context.prepare::<$name>()?;)+
Ok(())
}
fn refs(context: &Context) -> Result<Self::Refs<'_>, FixtureError> {
Ok(($(context.get::<$name>()?,)+))
}
}
};
}
fixture_sets!(A);
fixture_sets!(A, B);
fixture_sets!(A, B, C);
fixture_sets!(A, B, C, D);
fixture_sets!(A, B, C, D, E);
fixture_sets!(A, B, C, D, E, F);
fixture_sets!(A, B, C, D, E, F, G);
fixture_sets!(A, B, C, D, E, F, G, H);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TestOutcome {
Passed,
Failed(String),
}
impl TestOutcome {
#[must_use]
pub fn passed(&self) -> bool {
matches!(self, TestOutcome::Passed)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stage {
Setup,
Started,
Teardown,
}
impl fmt::Display for Stage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Stage::Setup => "setup",
Stage::Started => "started",
Stage::Teardown => "teardown",
})
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum FixtureError {
#[error("fixtures {a} and {b} depend on each other through their Deps")]
#[non_exhaustive]
Cycle {
a: &'static str,
b: &'static str,
},
#[error("fixture {fixture} failed in {stage}: {source}")]
#[non_exhaustive]
Failed {
fixture: &'static str,
stage: Stage,
source: Box<FixtureError>,
},
#[error("fixture {0} was read before it was built")]
NotBuilt(&'static str),
#[error(
"ICEDWRIGHT_SEED={value:?} is not valid; expected a hex seed such as 0x9f3a51c2e07b4d18, or random"
)]
#[non_exhaustive]
BadSeed {
value: String,
},
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Harness(Box<crate::Error>),
#[error("{0}")]
Other(Box<dyn std::error::Error + Send + Sync>),
}
impl FixtureError {
pub fn other(error: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Other(Box::new(error))
}
}
impl From<crate::Error> for FixtureError {
fn from(error: crate::Error) -> Self {
Self::Harness(Box::new(error))
}
}
impl From<FixtureError> for crate::Error {
fn from(error: FixtureError) -> Self {
crate::Error::other(error)
}
}
#[derive(Debug)]
pub struct RuntimeRef<'a> {
runtime: Option<&'a tokio::runtime::Runtime>,
}
impl<'a> RuntimeRef<'a> {
pub(crate) fn new(runtime: Option<&'a tokio::runtime::Runtime>) -> Self {
Self { runtime }
}
pub fn block_on<T>(&mut self, future: impl Future<Output = T>) -> crate::Result<T> {
let runtime = self.runtime.ok_or(crate::HarnessError::Exited)?;
Ok(runtime.block_on(future))
}
}
impl<P: TestProgram> Harness<P> {
pub fn runtime(&mut self) -> RuntimeRef<'_> {
RuntimeRef::new(self.tokio.as_ref())
}
}