Skip to content

Commit 63cb463

Browse files
authored
chore: internal refactor & update docs (#64)
1 parent b37885d commit 63cb463

File tree

6 files changed

+27
-144
lines changed

6 files changed

+27
-144
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,3 @@ jobs:
5656
with:
5757
command: clippy
5858
args: --all-targets -- -D warnings
59-

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A library for providing custom setup/teardown for Rust tests without needing a test harness.
99

10-
```rust
10+
```rust ignore
1111
use test_context::{test_context, TestContext};
1212

1313
struct MyContext {
@@ -77,7 +77,7 @@ The `AsyncTestContext` works well with async test wrappers like
7777
[`actix_rt::test`](https://docs.rs/actix-rt/1.1.1/actix_rt/attr.test.html) or
7878
[`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html).
7979

80-
```rust
80+
```rust ignore
8181
#[test_context(MyAsyncContext)]
8282
#[tokio::test]
8383
async fn test_works(ctx: &mut MyAsyncContext) {
@@ -95,15 +95,15 @@ that runs setup/teardown.
9595

9696
Valid:
9797

98-
```rust
98+
```rust ignore
9999
#[test_context(MyAsyncContext)]
100100
#[tokio::test]
101101
async fn my_test(ctx: &mut MyAsyncContext) {}
102102
```
103103

104104
Invalid:
105105

106-
```rust
106+
```rust ignore
107107
#[tokio::test]
108108
#[test_context(MyAsyncContext)]
109109
async fn my_test(ctx: &mut MyAsyncContext) {}
@@ -130,7 +130,7 @@ tests annotated with `#[tokio::test]` continue to work as usual without the feat
130130
Also, if you don't care about the teardown execution for a specific test,
131131
you can use the `skip_teardown` keyword on the macro like this:
132132

133-
```rust
133+
```rust ignore
134134
use test_context::{test_context, TestContext};
135135

136136
struct MyContext {}
@@ -150,7 +150,7 @@ fn test_without_teardown(ctx: &MyContext) {}
150150

151151
If the teardown is ON (default behavior), you can only take a reference to the context, either mutable or immutable, as follows:
152152

153-
```rust
153+
```rust ignore
154154
#[test_context(MyContext)]
155155
#[test]
156156
fn test_with_teardown_using_immutable_ref(ctx: &MyContext) {}
@@ -162,15 +162,15 @@ fn test_with_teardown_using_mutable_ref(ctx: &mut MyContext) {}
162162

163163
❌The following is invalid:
164164

165-
```rust
165+
```rust ignore
166166
#[test_context(MyContext)]
167167
#[test]
168168
fn test_with_teardown_taking_ownership(ctx: MyContext) {}
169169
```
170170

171171
If the teardown is skipped (as specified in the section above), you can take an immutable ref, mutable ref or full ownership of the context:
172172

173-
```rust
173+
```rust ignore
174174
#[test_context(MyContext, skip_teardown)]
175175
#[test]
176176
fn test_without_teardown(ctx: MyContext) {
@@ -186,14 +186,13 @@ fn test_without_teardown_taking_a_ref(ctx: &MyContext) {}
186186
fn test_without_teardown_taking_a_mut_ref(ctx: &mut MyContext) {}
187187
```
188188

189-
190189
## ⚠️ Ensure that the context type specified in the macro matches the test function argument type exactly
191190

192191
The error occurs when a context type with an absolute path is mixed with an it's alias.
193192

194193
For example:
195194

196-
```
195+
```rust ignore
197196
mod database {
198197
use test_context::TestContext;
199198

@@ -207,7 +206,8 @@ mod database {
207206
```
208207

209208
✅The following code will work:
210-
```
209+
210+
```rust ignore
211211
use database::Connection as DbConn;
212212

213213
#[test_context(DbConn)]
@@ -228,7 +228,8 @@ fn test1(ctx: &mut database::Connection) {
228228
```
229229

230230
The following code will not work:
231-
```
231+
232+
```rust ignore
232233
use database::Connection as DbConn;
233234

234235
#[test_context(database::Connection)]

test-context-macros/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod macro_args;
22
mod test_args;
33

44
use crate::test_args::{ContextArg, ContextArgMode, TestArg};
5-
use macro_args::TestContextArgs;
5+
use macro_args::MacroArgs;
66
use proc_macro::TokenStream;
77
use quote::{format_ident, quote};
88
use syn::ItemFn;
@@ -29,7 +29,7 @@ use syn::ItemFn;
2929
/// ```
3030
#[proc_macro_attribute]
3131
pub fn test_context(attr: TokenStream, item: TokenStream) -> TokenStream {
32-
let args = syn::parse_macro_input!(attr as TestContextArgs);
32+
let args = syn::parse_macro_input!(attr as MacroArgs);
3333
let input = syn::parse_macro_input!(item as syn::ItemFn);
3434

3535
let (input, context_args) = remove_context_args(input, args.context_type.clone());
@@ -86,7 +86,7 @@ fn remove_context_args(
8686

8787
fn refactor_input_body(
8888
input: syn::ItemFn,
89-
args: &TestContextArgs,
89+
args: &MacroArgs,
9090
context_arg: ContextArg,
9191
) -> syn::ItemFn {
9292
let context_type = &args.context_type;

test-context-macros/src/macro_args.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use syn::{Token, Type, parse::Parse};
22

3-
pub(crate) struct TestContextArgs {
3+
/// Contains the parsed arguments passed to the macro `#[test_context(..)]`
4+
pub(crate) struct MacroArgs {
5+
/// The context type passed in the macro arguments.
6+
/// It must implement `TestContext` or `AsyncTestContext`
47
pub(crate) context_type: Type,
8+
59
pub(crate) skip_teardown: bool,
610
}
711

8-
impl Parse for TestContextArgs {
12+
impl Parse for MacroArgs {
913
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
1014
let mut skip_teardown = false;
1115
let mut context_type: Option<Type> = None;
@@ -28,7 +32,7 @@ impl Parse for TestContextArgs {
2832
}
2933
}
3034

31-
Ok(TestContextArgs {
35+
Ok(MacroArgs {
3236
context_type: context_type
3337
.ok_or(input.error("expected at least one type identifier"))?,
3438
skip_teardown,

test-context/src/lib.rs

Lines changed: 1 addition & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,4 @@
1-
//! A library for providing custom setup/teardown for Rust tests without needing a test harness.
2-
//!
3-
//! ```no_run
4-
//! use test_context::{test_context, TestContext};
5-
//!
6-
//! struct MyContext {
7-
//! value: String
8-
//! }
9-
//!
10-
//! impl TestContext for MyContext {
11-
//! fn setup() -> MyContext {
12-
//! MyContext { value: "Hello, world!".to_string() }
13-
//! }
14-
//!
15-
//! fn teardown(self) {
16-
//! // Perform any teardown you wish.
17-
//! }
18-
//! }
19-
//!
20-
//! #[test_context(MyContext)]
21-
//! #[test]
22-
//! fn test_works(ctx: &mut MyContext) {
23-
//! assert_eq!(ctx.value, "Hello, world!");
24-
//! }
25-
//! ```
26-
//!
27-
//! Alternatively, you can use `async` functions in your test context by using the
28-
//! `AsyncTestContext`.
29-
//!
30-
//! ```no_run
31-
//! use test_context::{test_context, AsyncTestContext};
32-
//!
33-
//! struct MyAsyncContext {
34-
//! value: String
35-
//! }
36-
//!
37-
//! impl AsyncTestContext for MyAsyncContext {
38-
//! async fn setup() -> MyAsyncContext {
39-
//! MyAsyncContext { value: "Hello, world!".to_string() }
40-
//! }
41-
//!
42-
//! async fn teardown(self) {
43-
//! // Perform any teardown you wish.
44-
//! }
45-
//! }
46-
//!
47-
//! #[test_context(MyAsyncContext)]
48-
//! #[test]
49-
//! fn test_works(ctx: &mut MyAsyncContext) {
50-
//! assert_eq!(ctx.value, "Hello, World!");
51-
//! }
52-
//! ```
53-
//!
54-
//! The `AsyncTestContext` works well with async test wrappers like
55-
//! [`actix_rt::test`](https://docs.rs/actix-rt/1.1.1/actix_rt/attr.test.html) or
56-
//! [`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html).
57-
//!
58-
//! ```no_run
59-
//! use test_context::{test_context, AsyncTestContext};
60-
//!
61-
//! struct MyAsyncContext {
62-
//! value: String
63-
//! }
64-
//!
65-
//! impl AsyncTestContext for MyAsyncContext {
66-
//! async fn setup() -> MyAsyncContext {
67-
//! MyAsyncContext { value: "Hello, world!".to_string() }
68-
//! }
69-
//! async fn teardown(self) {
70-
//! // Perform any teardown you wish.
71-
//! }
72-
//! }
73-
//!
74-
//! #[test_context(MyAsyncContext)]
75-
//! #[tokio::test]
76-
//! async fn test_async_works(ctx: &mut MyAsyncContext) {
77-
//! assert_eq!(ctx.value, "Hello, World!");
78-
//! }
79-
//! ```
80-
//!
81-
//! # Attribute order
82-
//!
83-
//! Attribute order matters. Always place `#[test_context(...)]` before other test attributes
84-
//! like `#[tokio::test]` or `#[test]`.
85-
//!
86-
//! Why: Rust expands attributes in source order. `#[test_context]` wraps your function and
87-
//! re-attaches the remaining attributes to the wrapper; it must run first so the test attributes
88-
//! apply to the wrapper that performs setup/teardown.
89-
//!
90-
//! Valid:
91-
//! ```ignore
92-
//! #[test_context(MyAsyncContext)]
93-
//! #[tokio::test]
94-
//! async fn my_test(ctx: &mut MyAsyncContext) {}
95-
//! ```
96-
//!
97-
//! Invalid:
98-
//! ```ignore
99-
//! #[tokio::test]
100-
//! #[test_context(MyAsyncContext)]
101-
//! async fn my_test(ctx: &mut MyAsyncContext) {}
102-
//! ```
103-
//!
104-
//! # Skipping the teardown execution
105-
//!
106-
//! Also, if you don't care about the teardown execution for a specific test,
107-
//! you can use the `skip_teardown` keyword on the macro like this:
108-
//!
109-
//! ```no_run
110-
//! use test_context::{test_context, TestContext};
111-
//!
112-
//! struct MyContext {}
113-
//!
114-
//! impl TestContext for MyContext {
115-
//! fn setup() -> MyContext {
116-
//! MyContext {}
117-
//! }
118-
//! }
119-
//!
120-
//! #[test_context(MyContext, skip_teardown)]
121-
//! #[test]
122-
//! fn test_without_teardown(ctx: &mut MyContext) {
123-
//! // Perform any operations that require full ownership of your context
124-
//! }
125-
//! ```
1+
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../README.md"))]
1262

1273
// Reimported to allow for use in the macro.
1284
pub use futures;

yamlfmt.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
formatter:
2+
type: basic
3+
retain_line_breaks_single: true

0 commit comments

Comments
 (0)