Trait test_casing::decorators::DecorateTest

source ·
pub trait DecorateTest<R>: RefUnwindSafe + Send + Sync + 'static {
    // Required method
    fn decorate_and_test<F: TestFn<R>>(&'static self, test_fn: F) -> R;
}
Expand description

Test decorator.

See module docs for the extended description.

§Examples

The following decorator implements a #[should_panic] analogue for errors.

use test_casing::decorators::{DecorateTest, TestFn};

#[derive(Debug, Clone, Copy)]
pub struct ShouldError(pub &'static str);

impl<E: ToString> DecorateTest<Result<(), E>> for ShouldError {
    fn decorate_and_test<F: TestFn<Result<(), E>>>(
        &self,
        test_fn: F,
    ) -> Result<(), E> {
        let Err(err) = test_fn() else {
            panic!("Expected test to error, but it completed successfully");
        };
        let err = err.to_string();
        if err.contains(self.0) {
            Ok(())
        } else {
            panic!(
                "Expected error message to contain `{}`, but it was: {err}",
                self.0
            );
        }
    }
}

// Usage:
#[test]
#[decorate(ShouldError("oops"))]
fn test_with_an_error() -> Result<(), Box<dyn Error>> {
    Err("oops, this test failed".into())
}

Required Methods§

source

fn decorate_and_test<F: TestFn<R>>(&'static self, test_fn: F) -> R

Decorates the provided test function and runs the test.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<R, A> DecorateTest<R> for (A,)
where A: DecorateTest<R>,

source§

fn decorate_and_test<Fn: TestFn<R>>(&'static self, test_fn: Fn) -> R

source§

impl<R, A, B> DecorateTest<R> for (A, B)
where A: DecorateTest<R>, B: DecorateTest<R>,

source§

fn decorate_and_test<Fn: TestFn<R>>(&'static self, test_fn: Fn) -> R

source§

impl<R, A, B, C> DecorateTest<R> for (A, B, C)
where A: DecorateTest<R>, B: DecorateTest<R>, C: DecorateTest<R>,

source§

fn decorate_and_test<Fn: TestFn<R>>(&'static self, test_fn: Fn) -> R

source§

impl<R, A, B, C, D> DecorateTest<R> for (A, B, C, D)
where A: DecorateTest<R>, B: DecorateTest<R>, C: DecorateTest<R>, D: DecorateTest<R>,

source§

fn decorate_and_test<Fn: TestFn<R>>(&'static self, test_fn: Fn) -> R

source§

impl<R, A, B, C, D, E> DecorateTest<R> for (A, B, C, D, E)
where A: DecorateTest<R>, B: DecorateTest<R>, C: DecorateTest<R>, D: DecorateTest<R>, E: DecorateTest<R>,

source§

fn decorate_and_test<Fn: TestFn<R>>(&'static self, test_fn: Fn) -> R

source§

impl<R, A, B, C, D, E, F> DecorateTest<R> for (A, B, C, D, E, F)
where A: DecorateTest<R>, B: DecorateTest<R>, C: DecorateTest<R>, D: DecorateTest<R>, E: DecorateTest<R>, F: DecorateTest<R>,

source§

fn decorate_and_test<Fn: TestFn<R>>(&'static self, test_fn: Fn) -> R

source§

impl<R, A, B, C, D, E, F, G> DecorateTest<R> for (A, B, C, D, E, F, G)
where A: DecorateTest<R>, B: DecorateTest<R>, C: DecorateTest<R>, D: DecorateTest<R>, E: DecorateTest<R>, F: DecorateTest<R>, G: DecorateTest<R>,

source§

fn decorate_and_test<Fn: TestFn<R>>(&'static self, test_fn: Fn) -> R

source§

impl<R, A, B, C, D, E, F, G, H> DecorateTest<R> for (A, B, C, D, E, F, G, H)
where A: DecorateTest<R>, B: DecorateTest<R>, C: DecorateTest<R>, D: DecorateTest<R>, E: DecorateTest<R>, F: DecorateTest<R>, G: DecorateTest<R>, H: DecorateTest<R>,

source§

fn decorate_and_test<Fn: TestFn<R>>(&'static self, test_fn: Fn) -> R

source§

impl<R, T: DecorateTest<R>> DecorateTest<R> for &'static T

source§

fn decorate_and_test<F: TestFn<R>>(&'static self, test_fn: F) -> R

Implementors§