add basic impl TestContext for AsyncTestContext

This commit is contained in:
Michael Bryant 2021-02-21 17:30:31 -08:00
parent c55eed5464
commit 8d349b6f0c
No known key found for this signature in database
GPG key ID: D2614C09FD76338B
2 changed files with 21 additions and 0 deletions

View file

@ -84,3 +84,18 @@ where
/// normal "drop" semantics.
async fn teardown(self) {}
}
// Automatically impl TestContext for anything Send that impls AsyncTestContext.
//
// A future improvement may be to use feature flags to enable using a specific runtime
// to run the future synchronously. This is the easiest way to implement it, though, and
// introduces no new dependencies.
impl<T> TestContext for T where T: AsyncTestContext + Send {
fn setup() -> Self {
futures::executor::block_on(<T as AsyncTestContext>::setup())
}
fn teardown(self) {
futures::executor::block_on(<T as AsyncTestContext>::teardown(self))
}
}

View file

@ -94,3 +94,9 @@ async fn async_return_value_func(ctx: &mut AsyncContext) -> u32 {
async fn async_includes_return_value() {
assert_eq!(async_return_value_func().await, 1);
}
#[test_context(AsyncContext)]
#[test]
fn async_auto_impls_sync(ctx: &mut AsyncContext) {
assert_eq!(ctx.n, 1);
}