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§
sourcefn decorate_and_test<F: TestFn<R>>(&'static self, test_fn: F) -> R
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.