Skip to content

Commit 0475168

Browse files
ibraheemdevtaiki-e
authored andcommitted
async test function attr
1 parent 5691066 commit 0475168

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

futures-macro/src/executor.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use proc_macro::TokenStream;
2+
use quote::quote;
3+
4+
pub(crate) fn test(_: TokenStream, item: TokenStream) -> TokenStream {
5+
let mut input = syn::parse_macro_input!(item as syn::ItemFn);
6+
let attrs = &input.attrs;
7+
let vis = &input.vis;
8+
let sig = &mut input.sig;
9+
let body = &input.block;
10+
11+
if sig.asyncness.is_none() {
12+
return syn::Error::new_spanned(sig.fn_token, "Only async functions are supported")
13+
.to_compile_error()
14+
.into();
15+
}
16+
17+
sig.asyncness = None;
18+
19+
let gen = quote! {
20+
#(#attrs)*
21+
#vis #sig {
22+
::futures_test::__private::block_on(async move { #body })
23+
}
24+
};
25+
26+
gen.into()
27+
}

futures-macro/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern crate proc_macro;
1414

1515
use proc_macro::TokenStream;
1616

17+
mod executor;
1718
mod join;
1819
mod select;
1920

@@ -44,3 +45,9 @@ pub fn select_internal(input: TokenStream) -> TokenStream {
4445
pub fn select_biased_internal(input: TokenStream) -> TokenStream {
4546
crate::select::select_biased(input)
4647
}
48+
49+
/// The `test` attribute.
50+
#[proc_macro_attribute]
51+
pub fn test_internal(input: TokenStream, item: TokenStream) -> TokenStream {
52+
crate::executor::test(input, item)
53+
}

futures-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ futures-io = { version = "0.3.14", path = "../futures-io", default-features = fa
1818
futures-util = { version = "=0.4.0-alpha.0", path = "../futures-util", default-features = false }
1919
futures-executor = { version = "=0.4.0-alpha.0", path = "../futures-executor", default-features = false }
2020
futures-sink = { version = "=0.4.0-alpha.0", path = "../futures-sink", default-features = false }
21+
futures-macro = { version = "=0.4.0-alpha.0", path = "../futures-macro", default-features = false }
2122
pin-utils = { version = "0.1.0", default-features = false }
2223
pin-project = "1.0.1"
2324

futures-test/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ compile_error!(
1616
#[cfg(feature = "std")]
1717
pub mod __private {
1818
pub use futures_core::{future, stream, task};
19+
pub use futures_executor::block_on;
1920
pub use std::{
2021
option::Option::{None, Some},
2122
pin::Pin,
@@ -49,3 +50,16 @@ pub mod io;
4950
mod assert_unmoved;
5051
mod interleave_pending;
5152
mod track_closed;
53+
54+
/// Enables an `async` test function. The generated future will be run to completion with
55+
/// [`futures_executor::block_on`](futures_executor::block_on).
56+
///
57+
/// ```no_run
58+
/// #[futures_test::test]
59+
/// async fn my_test() {
60+
/// let fut = async { true };
61+
/// assert!(fut.await);
62+
/// }
63+
/// ```
64+
#[cfg(feature = "std")]
65+
pub use futures_macro::test_internal as test;

0 commit comments

Comments
 (0)