Introduce a gpui::test macro

This commit is contained in:
Antonio Scandurra 2021-05-12 17:05:03 +02:00
parent de6376ca80
commit c7d97adf23
6 changed files with 62 additions and 1 deletions

9
Cargo.lock generated
View File

@ -1180,6 +1180,7 @@ dependencies = [
"etagere", "etagere",
"font-kit", "font-kit",
"foreign-types", "foreign-types",
"gpui_macros",
"log", "log",
"metal", "metal",
"num_cpus", "num_cpus",
@ -1205,6 +1206,14 @@ dependencies = [
"usvg", "usvg",
] ]
[[package]]
name = "gpui_macros"
version = "0.1.0"
dependencies = [
"quote",
"syn",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.9.1" version = "0.9.1"

View File

@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["zed", "gpui", "fsevent", "scoped_pool"] members = ["zed", "gpui", "gpui_macros", "fsevent", "scoped_pool"]
[patch.crates-io] [patch.crates-io]
async-task = {git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e"} async-task = {git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e"}

View File

@ -8,6 +8,7 @@ version = "0.1.0"
async-task = "4.0.3" async-task = "4.0.3"
ctor = "0.1" ctor = "0.1"
etagere = "0.2" etagere = "0.2"
gpui_macros = {path = "../gpui_macros"}
log = "0.4" log = "0.4"
num_cpus = "1.13" num_cpus = "1.13"
ordered-float = "2.1.1" ordered-float = "2.1.1"

View File

@ -24,6 +24,7 @@ pub mod color;
pub mod json; pub mod json;
pub mod keymap; pub mod keymap;
mod platform; mod platform;
pub use gpui_macros::test;
pub use platform::{Event, PathPromptOptions}; pub use platform::{Event, PathPromptOptions};
pub use presenter::{ pub use presenter::{
AfterLayoutContext, Axis, DebugContext, EventContext, LayoutContext, PaintContext, AfterLayoutContext, Axis, DebugContext, EventContext, LayoutContext, PaintContext,

11
gpui_macros/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "gpui_macros"
version = "0.1.0"
edition = "2018"
[lib]
proc-macro = true
[dependencies]
syn = "1.0"
quote = "1.0"

39
gpui_macros/src/lib.rs Normal file
View 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)
}