Use gpui::test for tests in gpui

Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-05-12 18:21:32 +02:00
parent ab04d71508
commit 2326ac3dbf
2 changed files with 423 additions and 451 deletions

File diff suppressed because it is too large Load diff

View file

@ -2,38 +2,56 @@ use std::mem;
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{parse_macro_input, ItemFn};
use syn::{parse_macro_input, parse_quote, AttributeArgs, ItemFn, Meta, NestedMeta};
#[proc_macro_attribute]
pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
assert!(args.is_empty());
let mut namespace = format_ident!("gpui");
let args = syn::parse_macro_input!(args as AttributeArgs);
for arg in args {
match arg {
NestedMeta::Meta(Meta::Path(name))
if name.get_ident().map_or(false, |n| n == "self") =>
{
namespace = format_ident!("crate");
}
other => {
return TokenStream::from(
syn::Error::new_spanned(other, "invalid argument").into_compile_error(),
)
}
}
}
let mut inner_fn = parse_macro_input!(function as ItemFn);
let inner_fn_attributes = mem::take(&mut inner_fn.attrs);
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! {
let mut outer_fn: ItemFn = if inner_fn.sig.asyncness.is_some() {
parse_quote! {
#[test]
fn #outer_fn_name() {
#inner_fn
gpui::App::test_async((), move |ctx| async {
#namespace::App::test_async((), move |ctx| async {
#inner_fn_name(ctx).await;
});
}
}
} else {
quote! {
parse_quote! {
#[test]
fn #outer_fn_name() {
#inner_fn
gpui::App::test((), |ctx| {
#namespace::App::test((), |ctx| {
#inner_fn_name(ctx);
});
}
}
};
outer_fn.attrs.extend(inner_fn_attributes);
TokenStream::from(outer_fn)
TokenStream::from(quote!(#outer_fn))
}