Skip to content

Commit ee4996d

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. This 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 la10736/rstest#291 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 40f0b33 commit ee4996d

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

googletest_macro/src/lib.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
use quote::quote;
1616
use syn::{
17-
parse_macro_input, punctuated::Punctuated, spanned::Spanned, Attribute, DeriveInput, Expr,
18-
ExprLit, FnArg, ItemFn, Lit, MetaNameValue, PatType, ReturnType, Signature, Type,
17+
parse_macro_input, parse_quote, punctuated::Punctuated, spanned::Spanned, Attribute,
18+
DeriveInput, Expr, ExprLit, FnArg, ItemFn, Lit, MetaNameValue, PatType, ReturnType, Signature,
19+
Type,
1920
};
2021

2122
/// Marks a test to be run by the Google Rust test runner.
@@ -75,7 +76,7 @@ pub fn gtest(
7576
_args: proc_macro::TokenStream,
7677
input: proc_macro::TokenStream,
7778
) -> proc_macro::TokenStream {
78-
let ItemFn { attrs, sig, block, .. } = parse_macro_input!(input as ItemFn);
79+
let ItemFn { mut attrs, sig, block, .. } = parse_macro_input!(input as ItemFn);
7980
let test_case_hash: u64 = {
8081
use std::collections::hash_map::DefaultHasher;
8182
use std::hash::{Hash, Hasher};
@@ -185,6 +186,13 @@ pub fn gtest(
185186
)
186187
}
187188
};
189+
190+
if !attrs.iter().any(is_test_attribute) && !is_rstest_enabled {
191+
let test_attr: Attribute = parse_quote! {
192+
#[::core::prelude::v1::test]
193+
};
194+
attrs.push(test_attr);
195+
};
188196
let function = quote! {
189197
#(#attrs)*
190198
#outer_sig -> #outer_return_type {
@@ -200,17 +208,7 @@ pub fn gtest(
200208
#trailer
201209
}
202210
};
203-
204-
let output = if attrs.iter().any(is_test_attribute) || is_rstest_enabled {
205-
function
206-
} else {
207-
quote! {
208-
#[::core::prelude::v1::test]
209-
#function
210-
}
211-
};
212-
213-
output.into()
211+
function.into()
214212
}
215213

216214
/// Extract the optional "expected" string literal from a `should_panic`

0 commit comments

Comments
 (0)