Introduce a gpui::test
macro
This commit is contained in:
parent
de6376ca80
commit
c7d97adf23
6 changed files with 62 additions and 1 deletions
39
gpui_macros/src/lib.rs
Normal file
39
gpui_macros/src/lib.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use std::mem;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_macro_input, ItemFn};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
assert!(args.is_empty());
|
||||
|
||||
let mut inner_fn = parse_macro_input!(function as ItemFn);
|
||||
let inner_fn_name = format_ident!("_{}", inner_fn.sig.ident);
|
||||
let outer_fn_name = mem::replace(&mut inner_fn.sig.ident, inner_fn_name.clone());
|
||||
let outer_fn = if inner_fn.sig.asyncness.is_some() {
|
||||
quote! {
|
||||
#[test]
|
||||
fn #outer_fn_name() {
|
||||
#inner_fn
|
||||
|
||||
gpui::App::test_async((), move |ctx| async {
|
||||
#inner_fn_name(ctx).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#[test]
|
||||
fn #outer_fn_name() {
|
||||
#inner_fn
|
||||
|
||||
gpui::App::test((), |ctx| {
|
||||
#inner_fn_name(ctx);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(outer_fn)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue