From c2573f87e3d9fc331d51ae07ef13de27afcf9168 Mon Sep 17 00:00:00 2001 From: Kezhu Wang Date: Wed, 17 Apr 2024 20:19:39 +0800 Subject: [PATCH] fix: improve compatibility among test proc macros This pr proposes a generic mechanism among different test proc macros to avoid to generate multiple `[::core::prelude::v1::test]` on test method. `proc_macro_attribute` function is fed with tokens after its attribute and no tokens before it. Give the above, this pr proposes test proc macros to append newly generated macros after existing ones. This way, proc macros processed later can read all macros including generated and handwritten and make further decisions. Specifically, proc macros can append `#[::core::prelude::v1::test]` only if it does not exist. Closes #101. --- crates/test-case-core/src/test_case.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/test-case-core/src/test_case.rs b/crates/test-case-core/src/test_case.rs index 376b14e..d2d5ec9 100644 --- a/crates/test-case-core/src/test_case.rs +++ b/crates/test-case-core/src/test_case.rs @@ -99,7 +99,10 @@ impl TestCase { quote! { let _result = super::#item_name(#(#arg_values),*).await; }, ) } else { - attrs.insert(0, parse_quote! { #[::core::prelude::v1::test] }); + let test_attr = parse_quote! { #[::core::prelude::v1::test] }; + if !attrs.iter().any(|attr| *attr == test_attr) { + attrs.push(test_attr); + } ( TokenStream2::new(), quote! { let _result = super::#item_name(#(#arg_values),*); },