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

Dont_remove_lifetimes #243

Merged
merged 4 commits into from
Apr 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

### Fixed

- Don't remove Lifetimes from test function if any. See [#230](https://github.com/la10736/rstest/issues/230)
[#241](https://github.com/la10736/rstest/issues/241) for more details.

## [0.19.0] 2024/4/9

### Changed

- Defined `rust-version` for each crate (see [#227](https://github.com/la10736/rstest/issues/235))
- Defined `rust-version` for each crate (see [#227](https://github.com/la10736/rstest/issues/227))

### Fixed

Expand Down
21 changes: 21 additions & 0 deletions rstest/tests/resources/rstest/lifetimes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use rstest::*;

enum E<'a> {
A(bool),
B(&'a std::cell::Cell<E<'a>>),
}

#[rstest]
#[case(E::A(true))]
fn case<'a>(#[case] e: E<'a>) {}

#[rstest]
fn values<'a>(#[values(E::A(true))] e: E<'a>) {}

#[fixture]
fn e<'a>() -> E<'a> {
E::A(true)
}

#[rstest]
fn fixture<'a>(e: E<'a>) {}
12 changes: 12 additions & 0 deletions rstest/tests/rstest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ fn use_mutable_fixture_in_parametric_argumnts() {
.assert(output);
}

#[test]
fn should_not_remove_lifetimes() {
let (output, _) = run_test("lifetimes.rs");

TestResults::new()
.with_contains(true)
.ok("case")
.ok("values")
.ok("fixture")
.assert(output);
}

#[test]
fn should_reject_no_item_function() {
let (output, name) = run_test("reject_no_item_function.rs");
Expand Down
33 changes: 13 additions & 20 deletions rstest_macros/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ pub(crate) fn single(mut test: ItemFn, info: RsTestInfo) -> TokenStream {
let args = test.sig.inputs.iter().cloned().collect::<Vec<_>>();
let attrs = std::mem::take(&mut test.attrs);
let asyncness = test.sig.asyncness;
let generic_types = test
.sig
.generics
.type_params()
.map(|tp| &tp.ident)
.cloned()
.collect::<Vec<_>>();

single_test_case(
&test.sig.ident,
Expand All @@ -56,7 +49,7 @@ pub(crate) fn single(mut test: ItemFn, info: RsTestInfo) -> TokenStream {
Some(&test),
resolver,
&info.attributes,
&generic_types,
&test.sig.generics,
)
}

Expand Down Expand Up @@ -207,7 +200,8 @@ fn render_test_call(
) -> TokenStream {
let timeout = timeout.map(|x| quote! {#x}).or_else(|| {
std::env::var("RSTEST_TIMEOUT")
.ok().map(|to| quote! { std::time::Duration::from_secs( (#to).parse().unwrap()) })
.ok()
.map(|to| quote! { std::time::Duration::from_secs( (#to).parse().unwrap()) })
});
match (timeout, is_async) {
(Some(to_expr), true) => quote! {
Expand All @@ -222,6 +216,10 @@ fn render_test_call(
}
}

fn generics_types_ident<'a>(generics: &'a syn::Generics) -> impl Iterator<Item = &'a Ident> {
generics.type_params().map(|tp| &tp.ident)
}

/// Render a single test case:
///
/// * `name` - Test case name
Expand All @@ -247,15 +245,16 @@ fn single_test_case(
test_impl: Option<&ItemFn>,
resolver: impl Resolver,
attributes: &RsTestAttributes,
generic_types: &[Ident],
generics: &syn::Generics,
) -> TokenStream {
let (attrs, trace_me): (Vec<_>, Vec<_>) =
attrs.iter().cloned().partition(|a| !attr_is(a, "trace"));
let mut attributes = attributes.clone();
if !trace_me.is_empty() {
attributes.add_trace(format_ident!("trace"));
}
let inject = inject::resolve_aruments(args.iter(), &resolver, generic_types);
let generics_types = generics_types_ident(generics).cloned().collect::<Vec<_>>();
let inject = inject::resolve_aruments(args.iter(), &resolver, &generics_types);
let args = args
.iter()
.filter_map(MaybeIdent::maybe_ident)
Expand All @@ -282,11 +281,12 @@ fn single_test_case(
Some(resolve_default_test_attr(is_async))
};
let execute = render_test_call(testfn_name.clone().into(), &args, timeout, is_async);
let lifetimes = generics.lifetimes();

quote! {
#test_attr
#(#attrs)*
#asyncness fn #name() #output {
#asyncness fn #name<#(#lifetimes,)*>() #output {
#test_impl
#inject
#trace_args
Expand Down Expand Up @@ -339,13 +339,6 @@ impl<'a> TestCaseRender<'a> {
let mut attrs = testfn.attrs.clone();
attrs.extend(self.attrs.iter().cloned());
let asyncness = testfn.sig.asyncness;
let generic_types = testfn
.sig
.generics
.type_params()
.map(|tp| &tp.ident)
.cloned()
.collect::<Vec<_>>();

single_test_case(
&self.name,
Expand All @@ -357,7 +350,7 @@ impl<'a> TestCaseRender<'a> {
None,
self.resolver,
attributes,
&generic_types,
&testfn.sig.generics,
)
}
}
Expand Down
14 changes: 14 additions & 0 deletions rstest_macros/src/render/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ mod single_test_should {
assert_eq!(inner_fn_impl.display_code(), input_fn.block.display_code());
}

#[test]
fn not_remove_lifetimes() {
let input_fn: ItemFn = r#"
pub fn test<'a, 'b, 'c: 'a + 'b>(a: A<'a>, b: A<'b>, c: A<'c>) -> A<'c>
{
}
"#
.ast();

let result: ItemFn = single(input_fn.clone(), Default::default()).ast();

assert_eq!(3, result.sig.generics.lifetimes().count());
}

#[rstest]
fn not_copy_any_attributes(
#[values(
Expand Down
Loading