Skip to content

Commit f6c2d83

Browse files
committed
f macro
1 parent 975f018 commit f6c2d83

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

ext-test-macro/src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
4646
let expanded = match input {
4747
Item::Mod(item_mod) => {
4848
let cfg = if attrs.is_empty() {
49-
quote! { #[cfg(test)] }
49+
quote! { #[cfg_attr(test, test)] }
5050
} else {
51-
quote! { #[cfg(any(test, #attrs))] }
51+
quote! { #[cfg_attr(test, test)] #[cfg(any(test, #attrs))] }
5252
};
5353
quote! {
5454
#cfg
@@ -57,10 +57,23 @@ pub fn xtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
5757
},
5858
Item::Fn(item_fn) => {
5959
let cfg_attr = if attrs.is_empty() {
60-
quote! { #[cfg(test)] }
60+
// export the test if not actually testing
61+
quote! { #[cfg_attr(test, test)] }
6162
} else {
62-
quote! { #[cfg(any(test, #attrs))] }
63+
// export the test if the feature is enabled
64+
quote! { #[cfg_attr(test, test)] #[cfg(any(test, #attrs))] }
6365
};
66+
67+
// Check that the function doesn't take args and returns nothing
68+
if !item_fn.sig.inputs.is_empty() || !matches!(item_fn.sig.output, syn::ReturnType::Default) {
69+
return syn::Error::new_spanned(
70+
item_fn.sig,
71+
"xtest functions must not take arguments and must return nothing",
72+
)
73+
.to_compile_error()
74+
.into();
75+
}
76+
6477
quote! {
6578
#cfg_attr
6679
#item_fn

0 commit comments

Comments
 (0)