Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async fixtures #90

Merged
merged 4 commits into from Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions resources/fixture/async_fixture.rs
@@ -0,0 +1,34 @@
use std::future::Future;
use std::io::prelude::*;

use rstest::*;

#[fixture]
async fn async_u32() -> u32 {
42
}

#[fixture]
async fn nest_fixture(async_u32: impl Future<Output=u32>) -> u32 {
async_u32.await
}

#[rstest]
async fn default_is_async() {
assert_eq!(42, async_u32::default().await);
}

#[rstest]
async fn use_async_fixture(async_u32: impl Future<Output=u32>) {
assert_eq!(42, async_u32.await);
}

#[fixture]
async fn async_impl_output() -> impl Read {
std::io::Cursor::new(vec![1, 2, 3, 4, 5])
}

#[rstest]
async fn use_async_impl_output<T: Read>(async_impl_output: impl Future<Output = T>) {
let reader = async_impl_output.await;
}
41 changes: 35 additions & 6 deletions src/render/fixture.rs
Expand Up @@ -10,6 +10,7 @@ use crate::utils::{fn_args, fn_args_idents};

pub(crate) fn render<'a>(fixture: ItemFn, info: FixtureInfo) -> TokenStream {
let name = &fixture.sig.ident;
let asyncness = &fixture.sig.asyncness.clone();
let vargs = fn_args_idents(&fixture).cloned().collect::<Vec<_>>();
let args = &vargs;
let orig_args = &fixture.sig.inputs;
Expand All @@ -29,20 +30,37 @@ pub(crate) fn render<'a>(fixture: ItemFn, info: FixtureInfo) -> TokenStream {
let inject = resolve_args(fn_args_idents(&fixture), &resolver);
let partials =
(1..=orig_args.len()).map(|n| render_partial_impl(&fixture, n, &resolver, &info));

let (self_get_default, self_get) = if asyncness.is_none() {
(
quote! {
Self::get(#(#args),*)
},
quote! {#name(#(#args),*)},
)
} else {
(
quote! {
Self::get(#(#args),*).await
},
quote! {#name(#(#args),*).await},
)
};

quote! {
#[allow(non_camel_case_types)]
#visibility struct #name {}

impl #name {
#(#orig_attrs)*
#[allow(unused_mut)]
pub fn get #generics (#orig_args) #output #where_clause {
#name(#(#args),*)
pub #asyncness fn get #generics (#orig_args) #output #where_clause {
#self_get
}

pub fn default #default_generics () #default_output #default_where_clause {
pub #asyncness fn default #default_generics () #default_output #default_where_clause {
#inject
Self::get(#(#args),*)
#self_get_default
}

#(#partials)*
Expand All @@ -66,18 +84,29 @@ fn render_partial_impl(

let generics = generics_clean_up(&fixture.sig.generics, fn_args(fixture).take(n), &output);
let where_clause = &generics.where_clause;
let asyncness = &fixture.sig.asyncness;

let inject = resolve_args(fn_args_idents(fixture).skip(n), resolver);

let sign_args = fn_args(fixture).take(n);
let fixture_args = fn_args_idents(fixture);
let name = Ident::new(&format!("partial_{}", n), Span::call_site());

let self_get = if asyncness.is_none() {
quote! {
Self::get(#(#fixture_args),*)
}
} else {
quote! {
Self::get(#(#fixture_args),*).await
}
};

quote! {
#[allow(unused_mut)]
pub fn #name #generics (#(#sign_args),*) #output #where_clause {
pub #asyncness fn #name #generics (#(#sign_args),*) #output #where_clause {
#inject
Self::get(#(#fixture_args),*)
#self_get
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/fixture/mod.rs
Expand Up @@ -83,6 +83,20 @@ mod should {
}
}

#[test]
fn resolve_async_fixture() {
let prj = prj("async_fixture.rs");
prj.add_dependency("async-std", r#"{version="*", features=["attributes"]}"#);

let output = prj.run_tests().unwrap();

TestResults::new()
.ok("default_is_async")
.ok("use_async_fixture")
.ok("use_async_impl_output")
.assert(output);
}

#[test]
fn resolve_fixture_generics_by_fixture_input() {
let (output, _) = run_test("resolve.rs");
Expand Down