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
`#[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 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.
  • Loading branch information
kezhuw committed Jan 20, 2025
1 parent 21f2948 commit 61d7917
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions googletest_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use quote::quote;
use syn::{
parse_macro_input, punctuated::Punctuated, spanned::Spanned, Attribute, DeriveInput, FnArg,
ItemFn, PatType, ReturnType, Signature, Type,
parse_macro_input, parse_quote, punctuated::Punctuated, spanned::Spanned, Attribute,
DeriveInput, FnArg, ItemFn, PatType, ReturnType, Signature, Type,
};

/// Marks a test to be run by the Google Rust test runner.
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn gtest(
_args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let ItemFn { attrs, sig, block, .. } = parse_macro_input!(input as ItemFn);
let ItemFn { mut attrs, sig, block, .. } = parse_macro_input!(input as ItemFn);
let (outer_return_type, trailer) = if attrs
.iter()
.any(|attr| attr.path().is_ident("should_panic"))
Expand Down Expand Up @@ -164,6 +164,13 @@ pub fn gtest(
)
}
};

if !attrs.iter().any(is_test_attribute) && !is_rstest_enabled {
let test_attr: Attribute = parse_quote! {
#[::core::prelude::v1::test]
};
attrs.push(test_attr);
};
let function = quote! {
#(#attrs)*
#outer_sig -> #outer_return_type {
Expand All @@ -175,16 +182,7 @@ pub fn gtest(
#trailer
}
};

let output = if attrs.iter().any(is_test_attribute) || is_rstest_enabled {
function
} else {
quote! {
#[::core::prelude::v1::test]
#function
}
};
output.into()
function.into()
}

/// Alias for [`googletest::gtest`].
Expand Down

0 comments on commit 61d7917

Please sign in to comment.