1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use proc_macro::TokenStream; |
4 | use quote::ToTokens; |
5 | |
6 | pub(crate) fn async_test(_args: TokenStream, mut item: TokenStream) -> TokenStream { |
7 | let mut item_fn: syn::ItemFn = match syn::parse(item.clone()) { |
8 | Ok(it) => it, |
9 | Err(e) => { |
10 | item.extend(TokenStream::from(e.into_compile_error())); |
11 | return item; |
12 | } |
13 | }; |
14 | |
15 | if item_fn.sig.asyncness.is_none() { |
16 | item.extend(TokenStream::from( |
17 | syn::Error::new_spanned( |
18 | item_fn.sig.ident, |
19 | "The 'async' keyword is missing from the test function declaration" , |
20 | ) |
21 | .into_compile_error(), |
22 | )); |
23 | return item; |
24 | } |
25 | |
26 | item_fn.sig.asyncness = None; |
27 | |
28 | let gen_attr = quote::quote! { |
29 | #[::core::prelude::v1::test] |
30 | }; |
31 | |
32 | let body = &item_fn.block; |
33 | |
34 | item_fn.block = syn::parse2(quote::quote! { |
35 | { |
36 | let main_ctx = glib::MainContext::new(); |
37 | main_ctx.block_on(async #body) |
38 | } |
39 | }) |
40 | .expect("Body parsing failure" ); |
41 | |
42 | let mut tokens = TokenStream::new(); |
43 | tokens.extend(TokenStream::from(gen_attr.to_token_stream())); |
44 | tokens.extend(TokenStream::from(item_fn.into_token_stream())); |
45 | |
46 | tokens |
47 | } |
48 | |