Skip to content

Commit 87b2fc2

Browse files
committed
Append generated test macro so next test macros are aware of it
`#[gtest]` will benefit from la10736/rstest#291, we could also benefit other test macros. It is an attempt to improve capabilities among test macros to avoid duplicated test runs which is rare to be aware of. The rationale is simple: procedure of attribute macro see only attributes following it. Macros next to processing macro will not see generated macro if it is generated in-place. So, instead of generating test macro in-place, appending generated test macro will let macros next to processing macro have chance to react, for example, not generate test macro if there is already one. We could deprecate `#[googletest::test]` oneday after rust-lang/rust#82419 released. See: tokio-rs/tokio#6497, d-e-s-o/test-log#46, frondeus/test-case#143, la10736/rstest#291, kezhuw/stuck#53. Refs: rust-lang/rust#67839, rust-lang/rust#82419.
1 parent 21f2948 commit 87b2fc2

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

googletest_macro/src/lib.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
use quote::quote;
1616
use syn::{
17-
parse_macro_input, punctuated::Punctuated, spanned::Spanned, Attribute, DeriveInput, FnArg,
18-
ItemFn, PatType, ReturnType, Signature, Type,
17+
parse_macro_input, parse_quote, punctuated::Punctuated, spanned::Spanned, Attribute,
18+
DeriveInput, FnArg, ItemFn, PatType, ReturnType, Signature, Type,
1919
};
2020

2121
/// Marks a test to be run by the Google Rust test runner.
@@ -75,7 +75,7 @@ pub fn gtest(
7575
_args: proc_macro::TokenStream,
7676
input: proc_macro::TokenStream,
7777
) -> proc_macro::TokenStream {
78-
let ItemFn { attrs, sig, block, .. } = parse_macro_input!(input as ItemFn);
78+
let ItemFn { mut attrs, sig, block, .. } = parse_macro_input!(input as ItemFn);
7979
let (outer_return_type, trailer) = if attrs
8080
.iter()
8181
.any(|attr| attr.path().is_ident("should_panic"))
@@ -164,6 +164,13 @@ pub fn gtest(
164164
)
165165
}
166166
};
167+
168+
if !attrs.iter().any(is_test_attribute) && !is_rstest_enabled {
169+
let test_attr: Attribute = parse_quote! {
170+
#[::core::prelude::v1::test]
171+
};
172+
attrs.push(test_attr);
173+
};
167174
let function = quote! {
168175
#(#attrs)*
169176
#outer_sig -> #outer_return_type {
@@ -175,16 +182,7 @@ pub fn gtest(
175182
#trailer
176183
}
177184
};
178-
179-
let output = if attrs.iter().any(is_test_attribute) || is_rstest_enabled {
180-
function
181-
} else {
182-
quote! {
183-
#[::core::prelude::v1::test]
184-
#function
185-
}
186-
};
187-
output.into()
185+
function.into()
188186
}
189187

190188
/// Alias for [`googletest::gtest`].

0 commit comments

Comments
 (0)