From cb4043fae619022e80365915abfdea81313f9701 Mon Sep 17 00:00:00 2001 From: Victor Martinez <49537445+JasterV@users.noreply.github.com> Date: Mon, 26 Feb 2024 12:05:07 +0100 Subject: [PATCH] refactor: remove support for async-trait crate --- README.md | 1 - test-context-macros/src/lib.rs | 2 -- test-context/Cargo.toml | 2 +- test-context/src/lib.rs | 41 ++++++++++++++++++---------------- test-context/tests/test.rs | 1 - 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 734fd00..42bc36e 100644 --- a/README.md +++ b/README.md @@ -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() } diff --git a/test-context-macros/src/lib.rs b/test-context-macros/src/lib.rs index 58aa45f..36492df 100644 --- a/test-context-macros/src/lib.rs +++ b/test-context-macros/src/lib.rs @@ -9,7 +9,6 @@ use quote::{format_ident, quote}; /// ```ignore /// #[test_context(MyContext)] /// #[test] -/// #[ignore] /// fn my_test() { /// } /// ``` @@ -18,7 +17,6 @@ use quote::{format_ident, quote}; /// /// ```ignore /// #[test] -/// #[ignore] /// #[test_context(MyContext)] /// fn my_test() { /// } diff --git a/test-context/Cargo.toml b/test-context/Cargo.toml index 08f84e7..5be27e1 100644 --- a/test-context/Cargo.toml +++ b/test-context/Cargo.toml @@ -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"] @@ -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] diff --git a/test-context/src/lib.rs b/test-context/src/lib.rs index 649e8a5..3ea5692 100644 --- a/test-context/src/lib.rs +++ b/test-context/src/lib.rs @@ -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() } @@ -46,6 +45,7 @@ //! } //! //! #[test_context(MyAsyncContext)] +//! #[test] //! fn test_works(ctx: &mut MyAsyncContext) { //! assert_eq!(ctx.value, "Hello, World!"); //! } @@ -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) { @@ -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 + 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 + Send { + async {} + } } // Automatically impl TestContext for anything Send that impls AsyncTestContext. diff --git a/test-context/tests/test.rs b/test-context/tests/test.rs index 71defda..5319a2f 100644 --- a/test-context/tests/test.rs +++ b/test-context/tests/test.rs @@ -51,7 +51,6 @@ struct AsyncContext { n: u32, } -#[async_trait::async_trait] impl AsyncTestContext for AsyncContext { async fn setup() -> Self { Self { n: 1 }