-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
glib-macros: add test macro for async-glib-context tests
- Loading branch information
1 parent
8052e13
commit 4579c85
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use proc_macro::TokenStream; | ||
use quote::ToTokens; | ||
|
||
pub(crate) fn async_test(_args: TokenStream, mut item: TokenStream) -> TokenStream { | ||
let mut item_fn: syn::ItemFn = match syn::parse(item.clone()) { | ||
Ok(it) => it, | ||
Err(e) => { | ||
item.extend(TokenStream::from(e.into_compile_error())); | ||
return item; | ||
} | ||
}; | ||
|
||
if item_fn.sig.asyncness.is_none() { | ||
item.extend(TokenStream::from( | ||
syn::Error::new_spanned( | ||
item_fn.sig.ident, | ||
"The 'async' keyword is missing from the test function declaration", | ||
) | ||
.into_compile_error(), | ||
)); | ||
return item; | ||
} | ||
|
||
item_fn.sig.asyncness = None; | ||
|
||
let gen_attr = quote::quote! { | ||
#[::core::prelude::v1::test] | ||
}; | ||
|
||
let body = &item_fn.block; | ||
|
||
item_fn.block = syn::parse2(quote::quote! { | ||
{ | ||
let main_ctx = glib::MainContext::new(); | ||
main_ctx.block_on(async #body) | ||
} | ||
}) | ||
.expect("Body parsing failure"); | ||
|
||
let mut tokens = TokenStream::new(); | ||
tokens.extend(TokenStream::from(gen_attr.to_token_stream())); | ||
tokens.extend(TokenStream::from(item_fn.into_token_stream())); | ||
|
||
tokens | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters