Skip to content

Commit

Permalink
refactor: remove support for async-trait crate
Browse files Browse the repository at this point in the history
  • Loading branch information
JasterV committed Feb 26, 2024
1 parent d932f85 commit cb4043f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ struct MyAsyncContext {
value: String
}

#[async_trait::async_trait]
impl AsyncTestContext for MyAsyncContext {
async fn setup() -> MyAsyncContext {
MyAsyncContext { value: "Hello, World!".to_string() }
Expand Down
2 changes: 0 additions & 2 deletions test-context-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use quote::{format_ident, quote};
/// ```ignore
/// #[test_context(MyContext)]
/// #[test]
/// #[ignore]
/// fn my_test() {
/// }
/// ```
Expand All @@ -18,7 +17,6 @@ use quote::{format_ident, quote};
///
/// ```ignore
/// #[test]
/// #[ignore]
/// #[test_context(MyContext)]
/// fn my_test() {
/// }
Expand Down
2 changes: 1 addition & 1 deletion test-context/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "test-context"
rust-version = "1.75.0"
description = "A library for providing custom setup/teardown for Rust tests without needing a test harness"
readme = "../README.md"
keywords = ["test", "setup", "teardown"]
Expand All @@ -13,7 +14,6 @@ license.workspace = true

[dependencies]
test-context-macros = { version = "0.1.6", path = "../test-context-macros/" }
async-trait = "0.1.42"
futures = "0.3"

[dev-dependencies]
Expand Down
41 changes: 22 additions & 19 deletions test-context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
//! Alternatively, you can use `async` functions in your test context by using the
//! `AsyncTestContext`.
//!
//! ```
//! ```no_run
//! use test_context::{test_context, AsyncTestContext};
//!
//! struct MyAsyncContext {
//! value: String
//! }
//!
//! #[async_trait::async_trait]
//! impl AsyncTestContext for MyAsyncContext {
//! async fn setup() -> MyAsyncContext {
//! MyAsyncContext { value: "Hello, world!".to_string() }
Expand All @@ -46,6 +45,7 @@
//! }
//!
//! #[test_context(MyAsyncContext)]
//! #[test]
//! fn test_works(ctx: &mut MyAsyncContext) {
//! assert_eq!(ctx.value, "Hello, World!");
//! }
Expand All @@ -55,20 +55,22 @@
//! [`actix_rt::test`](https://docs.rs/actix-rt/1.1.1/actix_rt/attr.test.html) or
//! [`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html).
//!
//! ```
//! # use test_context::{test_context, AsyncTestContext};
//! # struct MyAsyncContext {
//! # value: String
//! # }
//! # #[async_trait::async_trait]
//! # impl AsyncTestContext for MyAsyncContext {
//! # async fn setup() -> MyAsyncContext {
//! # MyAsyncContext { value: "Hello, world!".to_string() }
//! # }
//! # async fn teardown(self) {
//! # // Perform any teardown you wish.
//! # }
//! # }
//! ```no_run
//! use test_context::{test_context, AsyncTestContext};
//!
//! struct MyAsyncContext {
//! value: String
//! }
//!
//! impl AsyncTestContext for MyAsyncContext {
//! async fn setup() -> MyAsyncContext {
//! MyAsyncContext { value: "Hello, world!".to_string() }
//! }
//! async fn teardown(self) {
//! // Perform any teardown you wish.
//! }
//! }
//!
//! #[test_context(MyAsyncContext)]
//! #[tokio::test]
//! async fn test_async_works(ctx: &mut MyAsyncContext) {
Expand All @@ -95,17 +97,18 @@ where
}

/// The trait to implement to get setup/teardown functionality for async tests.
#[async_trait::async_trait]
pub trait AsyncTestContext
where
Self: Sized,
{
/// Create the context. This is run once before each test that uses the context.
async fn setup() -> Self;
fn setup() -> impl std::future::Future<Output = Self> + Send;

/// Perform any additional cleanup of the context besides that already provided by
/// normal "drop" semantics.
async fn teardown(self) {}
fn teardown(self) -> impl std::future::Future<Output = ()> + Send {
async {}
}
}

// Automatically impl TestContext for anything Send that impls AsyncTestContext.
Expand Down
1 change: 0 additions & 1 deletion test-context/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ struct AsyncContext {
n: u32,
}

#[async_trait::async_trait]
impl AsyncTestContext for AsyncContext {
async fn setup() -> Self {
Self { n: 1 }
Expand Down

0 comments on commit cb4043f

Please sign in to comment.