refactor: remove support for async-trait crate

This commit is contained in:
Victor Martinez 2024-02-26 12:05:07 +01:00
parent d932f8529f
commit cb4043fae6
5 changed files with 23 additions and 24 deletions

View file

@ -41,7 +41,6 @@ struct MyAsyncContext {
value: String value: String
} }
#[async_trait::async_trait]
impl AsyncTestContext for MyAsyncContext { impl AsyncTestContext for MyAsyncContext {
async fn setup() -> MyAsyncContext { async fn setup() -> MyAsyncContext {
MyAsyncContext { value: "Hello, World!".to_string() } MyAsyncContext { value: "Hello, World!".to_string() }

View file

@ -9,7 +9,6 @@ use quote::{format_ident, quote};
/// ```ignore /// ```ignore
/// #[test_context(MyContext)] /// #[test_context(MyContext)]
/// #[test] /// #[test]
/// #[ignore]
/// fn my_test() { /// fn my_test() {
/// } /// }
/// ``` /// ```
@ -18,7 +17,6 @@ use quote::{format_ident, quote};
/// ///
/// ```ignore /// ```ignore
/// #[test] /// #[test]
/// #[ignore]
/// #[test_context(MyContext)] /// #[test_context(MyContext)]
/// fn my_test() { /// fn my_test() {
/// } /// }

View file

@ -1,5 +1,6 @@
[package] [package]
name = "test-context" name = "test-context"
rust-version = "1.75.0"
description = "A library for providing custom setup/teardown for Rust tests without needing a test harness" description = "A library for providing custom setup/teardown for Rust tests without needing a test harness"
readme = "../README.md" readme = "../README.md"
keywords = ["test", "setup", "teardown"] keywords = ["test", "setup", "teardown"]
@ -13,7 +14,6 @@ license.workspace = true
[dependencies] [dependencies]
test-context-macros = { version = "0.1.6", path = "../test-context-macros/" } test-context-macros = { version = "0.1.6", path = "../test-context-macros/" }
async-trait = "0.1.42"
futures = "0.3" futures = "0.3"
[dev-dependencies] [dev-dependencies]

View file

@ -27,14 +27,13 @@
//! Alternatively, you can use `async` functions in your test context by using the //! Alternatively, you can use `async` functions in your test context by using the
//! `AsyncTestContext`. //! `AsyncTestContext`.
//! //!
//! ``` //! ```no_run
//! use test_context::{test_context, AsyncTestContext}; //! use test_context::{test_context, AsyncTestContext};
//! //!
//! struct MyAsyncContext { //! struct MyAsyncContext {
//! value: String //! value: String
//! } //! }
//! //!
//! #[async_trait::async_trait]
//! impl AsyncTestContext for MyAsyncContext { //! impl AsyncTestContext for MyAsyncContext {
//! async fn setup() -> MyAsyncContext { //! async fn setup() -> MyAsyncContext {
//! MyAsyncContext { value: "Hello, world!".to_string() } //! MyAsyncContext { value: "Hello, world!".to_string() }
@ -46,6 +45,7 @@
//! } //! }
//! //!
//! #[test_context(MyAsyncContext)] //! #[test_context(MyAsyncContext)]
//! #[test]
//! fn test_works(ctx: &mut MyAsyncContext) { //! fn test_works(ctx: &mut MyAsyncContext) {
//! assert_eq!(ctx.value, "Hello, World!"); //! 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 //! [`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). //! [`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html).
//! //!
//! ``` //! ```no_run
//! # use test_context::{test_context, AsyncTestContext}; //! use test_context::{test_context, AsyncTestContext};
//! # struct MyAsyncContext { //!
//! # value: String //! struct MyAsyncContext {
//! # } //! value: String
//! # #[async_trait::async_trait] //! }
//! # impl AsyncTestContext for MyAsyncContext { //!
//! # async fn setup() -> MyAsyncContext { //! impl AsyncTestContext for MyAsyncContext {
//! # MyAsyncContext { value: "Hello, world!".to_string() } //! async fn setup() -> MyAsyncContext {
//! # } //! MyAsyncContext { value: "Hello, world!".to_string() }
//! # async fn teardown(self) { //! }
//! # // Perform any teardown you wish. //! async fn teardown(self) {
//! # } //! // Perform any teardown you wish.
//! # } //! }
//! }
//!
//! #[test_context(MyAsyncContext)] //! #[test_context(MyAsyncContext)]
//! #[tokio::test] //! #[tokio::test]
//! async fn test_async_works(ctx: &mut MyAsyncContext) { //! 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. /// The trait to implement to get setup/teardown functionality for async tests.
#[async_trait::async_trait]
pub trait AsyncTestContext pub trait AsyncTestContext
where where
Self: Sized, Self: Sized,
{ {
/// Create the context. This is run once before each test that uses the context. /// 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 /// Perform any additional cleanup of the context besides that already provided by
/// normal "drop" semantics. /// 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. // Automatically impl TestContext for anything Send that impls AsyncTestContext.

View file

@ -51,7 +51,6 @@ struct AsyncContext {
n: u32, n: u32,
} }
#[async_trait::async_trait]
impl AsyncTestContext for AsyncContext { impl AsyncTestContext for AsyncContext {
async fn setup() -> Self { async fn setup() -> Self {
Self { n: 1 } Self { n: 1 }