fix rt_test macros

This commit is contained in:
Nikolay Kim 2021-02-24 14:07:58 +06:00
parent 055bd730d4
commit a707020171
3 changed files with 60 additions and 2 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "ntex-macros"
version = "0.1.2"
version = "0.1.3"
description = "ntex proc macros"
readme = "README.md"
authors = ["ntex contributors <team@ntex.rs>"]

View file

@ -279,3 +279,61 @@ pub fn rt_test(_: TokenStream, item: TokenStream) -> TokenStream {
result.into()
}
/// Marks async test function to be executed by ntex runtime.
///
/// ## Usage
///
/// ```no_run
/// #[ntex::test]
/// async fn my_test() {
/// assert!(true);
/// }
/// ```
#[doc(hidden)]
#[proc_macro_attribute]
pub fn rt_test2(_: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);
let ret = &input.sig.output;
let name = &input.sig.ident;
let body = &input.block;
let attrs = &input.attrs;
let mut has_test_attr = false;
for attr in attrs {
if attr.path.is_ident("test") {
has_test_attr = true;
}
}
if input.sig.asyncness.is_none() {
return syn::Error::new_spanned(
input.sig.fn_token,
format!("only async fn is supported, {}", input.sig.ident),
)
.to_compile_error()
.into();
}
let result = if has_test_attr {
quote! {
#(#attrs)*
fn #name() #ret {
ntex_rt::System::new("test")
.block_on(async { #body })
}
}
} else {
quote! {
#[test]
#(#attrs)*
fn #name() #ret {
ntex_rt::System::new("test")
.block_on(async { #body })
}
}
};
result.into()
}

View file

@ -31,7 +31,7 @@ extern crate derive_more;
pub use ntex_macros::{rt_main as main, rt_test as test};
#[cfg(test)]
pub(crate) use ntex_macros::rt_test;
pub(crate) use ntex_macros::rt_test2 as rt_test;
pub mod channel;
pub mod connect;