Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: append #[::core::prelude::v1::test] only if it does not exist #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ tracing = {version = "0.1.20"}
#
# Probably fixed by https://github.com/rust-lang-nursery/lazy-static.rs/pull/107.
_lazy_static_unused = { package = "lazy_static", version = "1.0.2" }

[patch.crates-io]
test-case = { git = "https://github.com/kezhuw/test-case.git", branch = "test-proc-macros-cooperation" }
tokio = { git = "https://github.com/kezhuw/tokio.git", branch = "test-proc-macros-cooperation" }
41 changes: 35 additions & 6 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,28 @@ fn parse_attrs(attrs: Vec<Attribute>) -> syn::Result<(AttributeArgs, Vec<Attribu
}
}

fn try_test(attr: TokenStream, input: ItemFn) -> syn::Result<Tokens> {
let inner_test = if attr.is_empty() {
quote! { ::core::prelude::v1::test }
} else {
attr.into()
// Check whether given attribute is `#[test]` or `#[::core::prelude::v1::test]`.
fn is_test_attribute(attr: &Attribute) -> bool {
let path = match &attr.meta {
syn::Meta::Path(path) => path,
_ => return false,
};
let segments = ["core", "prelude", "v1", "test"];
if path.leading_colon.is_none() {
return path.segments.len() == 1
&& path.segments[0].arguments.is_none()
&& path.segments[0].ident == "test";
} else if path.segments.len() != segments.len() {
return false;
}
path
.segments
.iter()
.zip(segments)
.all(|(segment, path)| segment.arguments.is_none() && segment.ident == path)
}

fn try_test(attr: TokenStream, input: ItemFn) -> syn::Result<Tokens> {
let ItemFn {
attrs,
vis,
Expand All @@ -61,9 +76,23 @@ fn try_test(attr: TokenStream, input: ItemFn) -> syn::Result<Tokens> {
let logging_init = expand_logging_init(&attribute_args);
let tracing_init = expand_tracing_init(&attribute_args);

let (inner_test, generated_test) = if attr.is_empty() {
let has_test = ignored_attrs.iter().any(is_test_attribute);
let generated_test = if has_test {
quote! {}
} else {
quote! { #[::core::prelude::v1::test]}
};
(quote! {}, generated_test)
} else {
let attr = Tokens::from(attr);
(quote! { #[#attr] }, quote! {})
};

let result = quote! {
#[#inner_test]
#inner_test
#(#ignored_attrs)*
#generated_test
#vis #sig {
// We put all initialization code into a separate module here in
// order to prevent potential ambiguities that could result in
Expand Down
38 changes: 38 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,44 @@ fn with_inner_test_attribute_and_test_args_and_panic(x: i8, _y: i8) {
assert_eq!(x, 0);
}

#[test_log::test]
#[test]
fn with_existing_test_attribute() {}

#[test_log::test]
#[::core::prelude::v1::test]
fn with_existing_generated_test_attribute() {}

#[tokio::test]
#[test_log::test]
async fn with_append_test_attribute_and_async() {
assert_eq!(async { 42 }.await, 42)
}

#[test_case::test_case(-2, -4)]
#[test_case::test_case(-2, -5)]
#[test_log::test]
fn with_append_test_attribute_and_test_args(x: i8, _y: i8) {
assert_eq!(x, -2);
}

#[should_panic]
#[test_case::test_case(-2, -4)]
#[test_case::test_case(-3, -4)]
#[test_log::test]
fn with_append_test_attribute_and_test_args_and_panic(x: i8, _y: i8) {
assert_eq!(x, 0);
}

#[should_panic]
#[test_case::test_case(-2, -4)]
#[test_case::test_case(-3, -4)]
#[tokio::test]
#[test_log::test]
async fn with_append_test_attribute_and_test_args_and_panic_async(x: i8, _y: i8) {
assert_eq!(x, 0);
}

#[instrument]
async fn instrumented(input: usize) -> usize {
info!("input = {}", input);
Expand Down