Skip to content

Commit

Permalink
Append generated test macro so next test macros are aware of it
Browse files Browse the repository at this point in the history
This way test macros following `#[rstest]` can decide whether or not to
generate test macro to avoid duplicate test runs.

It is an attempt to improve capabilities among test macros. Currently,
following test from [googletest](https://github.com/google/googletest-rust/blob/21f2948684847922a416252b8118e3eada8e29d6/integration_tests/src/google_test_with_rstest.rs#L52-L57)(`main` branch at 2025-01-16) will run twice.

```rust
#[rstest]
#[case(1)]
#[gtest]
fn paramterised_test_should_work_with_rstest_first(#[case] value: u32) -> Result<()> {
    verify_that!(value, eq(value))
}
```

See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, kezhuw/stuck#53.
Refs: rust-lang/rust#67839, rust-lang/rust#82419.
  • Loading branch information
kezhuw committed Jan 16, 2025
1 parent 154d0b0 commit e3045e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion rstest_macros/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ fn single_test_case(
let lifetimes = generics.lifetimes();

quote! {
#test_attr
#(#attrs)*
#test_attr
#asyncness fn #name<#(#lifetimes,)*>(#(#ignored_args,)*) #output {
#test_impl
#inject
Expand Down
35 changes: 19 additions & 16 deletions rstest_macros/src/render/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,9 @@ mod single_test_should {

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

assert_eq!(result.attrs[0], test_attribute);
assert_eq!(&result.attrs[1..], attributes.as_slice());
let (generated_attribute, old_attributes) = result.attrs.split_last().unwrap();
assert_eq!(old_attributes, attributes.as_slice());
assert_eq!(generated_attribute, &test_attribute);
}

#[rstest]
Expand Down Expand Up @@ -648,7 +649,7 @@ mod cases_should {
assert!(tests.len() > 0);

for t in tests {
assert_eq!(item_fn.attrs, &t.attrs[1..]);
assert_eq!(item_fn.attrs, &t.attrs[..t.attrs.len() - 1]);
}
}

Expand All @@ -669,7 +670,8 @@ mod cases_should {

let tokens = parametrize(item_fn, info);

let test_attrs = &TestsGroup::from(tokens).get_all_tests()[0].attrs[1..];
let tests = TestsGroup::from(tokens).get_all_tests();
let test_attrs = tests[0].attrs.split_last().unwrap().1;

let l = given_attrs.len();

Expand Down Expand Up @@ -886,9 +888,10 @@ mod cases_should {
let tokens = parametrize(item_fn, info);

let tests = TestsGroup::from(tokens).get_all_tests();
let (generated_attribute, old_attributes) = tests[0].attrs.split_last().unwrap();

assert_eq!(tests[0].attrs[0], test_attribute);
assert_eq!(&tests[0].attrs[1..], attributes.as_slice());
assert_eq!(old_attributes, attributes.as_slice());
assert_eq!(generated_attribute, &test_attribute);
}

#[test]
Expand Down Expand Up @@ -1213,8 +1216,8 @@ mod matrix_cases_should {
assert!(tests.len() > 0);

for t in tests {
let end = t.attrs.len() - 1;
assert_eq!(item_fn.attrs, &t.attrs[1..end]);
let end = t.attrs.len() - 2;
assert_eq!(item_fn.attrs, &t.attrs[0..end]);
}
}

Expand Down Expand Up @@ -1370,8 +1373,8 @@ mod matrix_cases_should {
assert!(tests.len() > 0);

for test in tests {
assert_eq!(test.attrs[0], test_attribute);
assert_eq!(&test.attrs[1..test.attrs.len() - 1], attributes.as_slice());
assert_eq!(&test.attrs[..test.attrs.len() - 2], attributes.as_slice());
assert_eq!(test.attrs[test.attrs.len() - 1], test_attribute);
}
}

Expand Down Expand Up @@ -1434,8 +1437,8 @@ mod matrix_cases_should {
assert!(tests.len() > 0);

for test in tests {
assert_eq!(test.attrs.last().unwrap(), non_snake_case);
assert_eq!(&test.attrs[1..test.attrs.len() - 1], attributes.as_slice());
assert_eq!(&test.attrs[test.attrs.len() - 2], non_snake_case);
assert_eq!(&test.attrs[..test.attrs.len() - 2], attributes.as_slice());
}
}

Expand Down Expand Up @@ -1926,11 +1929,11 @@ mod complete_should {
let attrs = attrs("#[first]#[second(arg)]");

for f in modules[0].get_all_tests() {
let end = f.attrs.len() - 1;
assert_eq!(attrs, &f.attrs[1..end]);
let end = f.attrs.len() - 2;
assert_eq!(attrs, &f.attrs[..end]);
}
for f in modules[1].get_all_tests() {
assert_eq!(attrs, &f.attrs[1..3]);
assert_eq!(attrs, &f.attrs[..2]);
}
}
#[test]
Expand All @@ -1939,7 +1942,7 @@ mod complete_should {
let attrs = attrs("#[third]#[forth(other)]");

for f in modules[1].get_all_tests() {
assert_eq!(attrs, &f.attrs[3..5]);
assert_eq!(attrs, &f.attrs[2..4]);
}
}
}

0 comments on commit e3045e4

Please sign in to comment.