Skip to content

Commit

Permalink
#98: Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed May 2, 2021
1 parent fa0f21e commit 0146a6e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -164,6 +164,22 @@ features list in your `Cargo.toml`:
async-std = { version = "1.5", features = ["attributes"] }
```

If your test input is an async value (fixture or test parameter) you can use `#[future]`
attribute to remove `impl Future<Output = T>` boilerplate and just use `T`:

```rust
use rstest::*;
#[fixture]
async fn base() -> u32 { 42 }

#[rstest]
#[case(21, async { 2 })]
#[case(6, async { 7 })]
async fn my_async_test(#[future] base: u32, #[case] expected: u32, #[future] #[case] div: u32) {
assert_eq!(expected, base.await / div.await);
}
```

### Inject Test Attribute

If you would like to use another `test` attribute for your test you can simply
Expand All @@ -179,7 +195,7 @@ use std::future::Future;
#[case(2, async { 4 })]
#[case(21, async { 42 })]
#[actix_rt::test]
async fn my_async_test(#[case] a: u32, result: #[case] impl Future<Output=u32>) {
async fn my_async_test(#[case] a: u32, result: #[case] #[future] u32) {
assert_eq!(2 * a, result.await);
}
```
Expand Down
35 changes: 32 additions & 3 deletions src/lib.rs
Expand Up @@ -322,18 +322,31 @@ use quote::ToTokens;
///
/// ```
/// use rstest::*;
/// # use std::future::Future;
///
/// #[fixture]
/// async fn async_fixture() -> i32 { 42 }
///
///
/// #[rstest]
/// async fn the_test(async_fixture: impl Future<Output = i32>) {
/// async fn the_test(#[future] async_fixture: i32) {
/// assert_eq!(42, async_fixture.await)
/// }
/// ```
/// The `#[future]` argument attribute helps to remove the `impl Future<Output = T>` boilerplate.
/// In this case the macro expands it in:
///
/// ```
/// # use rstest::*;
/// # use std::future::Future;
/// # #[fixture]
/// # async fn async_fixture() -> i32 { 42 }
/// #[rstest]
/// async fn the_test(async_fixture: impl std::future::Future<Output = i32>) {
/// assert_eq!(42, async_fixture.await)
/// }
/// ```
/// If you need, you can use `#[future]` attribute also with an inplicit lifetime reference
/// because the macro will replace the implicit lifetime with an explicit one.
///
/// # Partial Injection
///
Expand Down Expand Up @@ -843,6 +856,22 @@ pub fn fixture(
/// async-std = { version = "1.5", features = ["attributes"] }
/// ```
///
/// If your test input is an async value (fixture or test parameter) you can use `#[future]`
/// attribute to remove `impl Future<Output = T>` boilerplate and just use `T`:
///
/// ```
/// use rstest::*;
/// #[fixture]
/// async fn base() -> u32 { 42 }
///
/// #[rstest]
/// #[case(21, async { 2 })]
/// #[case(6, async { 7 })]
/// async fn my_async_test(#[future] base: u32, #[case] expected: u32, #[future] #[case] div: u32) {
/// assert_eq!(expected, base.await / div.await);
/// }
/// ```
///
/// ## Inject Test Attribute
///
/// If you would like to use another `test` attribute for your test you can simply
Expand All @@ -858,7 +887,7 @@ pub fn fixture(
/// #[case(2, async { 4 })]
/// #[case(21, async { 42 })]
/// #[actix_rt::test]
/// async fn my_async_test(#[case] a: u32, #[case] result: impl Future<Output=u32>) {
/// async fn my_async_test(#[case] a: u32, #[case] #[future] result: u32) {
/// assert_eq!(2 * a, result.await);
/// }
/// ```
Expand Down

0 comments on commit 0146a6e

Please sign in to comment.