Skip to content

Commit e3045e4

Browse files
committed
Append generated test macro so next test macros are aware of it
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.
1 parent 154d0b0 commit e3045e4

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

rstest_macros/src/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ fn single_test_case(
374374
let lifetimes = generics.lifetimes();
375375

376376
quote! {
377-
#test_attr
378377
#(#attrs)*
378+
#test_attr
379379
#asyncness fn #name<#(#lifetimes,)*>(#(#ignored_args,)*) #output {
380380
#test_impl
381381
#inject

rstest_macros/src/render/test.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ mod single_test_should {
287287

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

290-
assert_eq!(result.attrs[0], test_attribute);
291-
assert_eq!(&result.attrs[1..], attributes.as_slice());
290+
let (generated_attribute, old_attributes) = result.attrs.split_last().unwrap();
291+
assert_eq!(old_attributes, attributes.as_slice());
292+
assert_eq!(generated_attribute, &test_attribute);
292293
}
293294

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

650651
for t in tests {
651-
assert_eq!(item_fn.attrs, &t.attrs[1..]);
652+
assert_eq!(item_fn.attrs, &t.attrs[..t.attrs.len() - 1]);
652653
}
653654
}
654655

@@ -669,7 +670,8 @@ mod cases_should {
669670

670671
let tokens = parametrize(item_fn, info);
671672

672-
let test_attrs = &TestsGroup::from(tokens).get_all_tests()[0].attrs[1..];
673+
let tests = TestsGroup::from(tokens).get_all_tests();
674+
let test_attrs = tests[0].attrs.split_last().unwrap().1;
673675

674676
let l = given_attrs.len();
675677

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

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

890-
assert_eq!(tests[0].attrs[0], test_attribute);
891-
assert_eq!(&tests[0].attrs[1..], attributes.as_slice());
893+
assert_eq!(old_attributes, attributes.as_slice());
894+
assert_eq!(generated_attribute, &test_attribute);
892895
}
893896

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

12151218
for t in tests {
1216-
let end = t.attrs.len() - 1;
1217-
assert_eq!(item_fn.attrs, &t.attrs[1..end]);
1219+
let end = t.attrs.len() - 2;
1220+
assert_eq!(item_fn.attrs, &t.attrs[0..end]);
12181221
}
12191222
}
12201223

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

13721375
for test in tests {
1373-
assert_eq!(test.attrs[0], test_attribute);
1374-
assert_eq!(&test.attrs[1..test.attrs.len() - 1], attributes.as_slice());
1376+
assert_eq!(&test.attrs[..test.attrs.len() - 2], attributes.as_slice());
1377+
assert_eq!(test.attrs[test.attrs.len() - 1], test_attribute);
13751378
}
13761379
}
13771380

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

14361439
for test in tests {
1437-
assert_eq!(test.attrs.last().unwrap(), non_snake_case);
1438-
assert_eq!(&test.attrs[1..test.attrs.len() - 1], attributes.as_slice());
1440+
assert_eq!(&test.attrs[test.attrs.len() - 2], non_snake_case);
1441+
assert_eq!(&test.attrs[..test.attrs.len() - 2], attributes.as_slice());
14391442
}
14401443
}
14411444

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

19281931
for f in modules[0].get_all_tests() {
1929-
let end = f.attrs.len() - 1;
1930-
assert_eq!(attrs, &f.attrs[1..end]);
1932+
let end = f.attrs.len() - 2;
1933+
assert_eq!(attrs, &f.attrs[..end]);
19311934
}
19321935
for f in modules[1].get_all_tests() {
1933-
assert_eq!(attrs, &f.attrs[1..3]);
1936+
assert_eq!(attrs, &f.attrs[..2]);
19341937
}
19351938
}
19361939
#[test]
@@ -1939,7 +1942,7 @@ mod complete_should {
19391942
let attrs = attrs("#[third]#[forth(other)]");
19401943

19411944
for f in modules[1].get_all_tests() {
1942-
assert_eq!(attrs, &f.attrs[3..5]);
1945+
assert_eq!(attrs, &f.attrs[2..4]);
19431946
}
19441947
}
19451948
}

0 commit comments

Comments
 (0)