From c7d97adf2382c0af0d0fa892f50206ea34e1fb1b Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 12 May 2021 17:05:03 +0200 Subject: [PATCH] Introduce a `gpui::test` macro --- Cargo.lock | 9 +++++++++ Cargo.toml | 2 +- gpui/Cargo.toml | 1 + gpui/src/lib.rs | 1 + gpui_macros/Cargo.toml | 11 +++++++++++ gpui_macros/src/lib.rs | 39 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 gpui_macros/Cargo.toml create mode 100644 gpui_macros/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 1fba36fd1210a8ddb0427046923b468f11463463..ba63f85d85ce299443b63148bfca7af796a033cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1180,6 +1180,7 @@ dependencies = [ "etagere", "font-kit", "foreign-types", + "gpui_macros", "log", "metal", "num_cpus", @@ -1205,6 +1206,14 @@ dependencies = [ "usvg", ] +[[package]] +name = "gpui_macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "hashbrown" version = "0.9.1" diff --git a/Cargo.toml b/Cargo.toml index bf2d5c814372d3fa4184b285cc78e0ad3dfa42d5..8109db121bbf0926b9348bec198a2cd009fa9cd2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["zed", "gpui", "fsevent", "scoped_pool"] +members = ["zed", "gpui", "gpui_macros", "fsevent", "scoped_pool"] [patch.crates-io] async-task = {git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e"} diff --git a/gpui/Cargo.toml b/gpui/Cargo.toml index 8c7c3bf4cbfc4770e8d208557fb4bcf76052387a..d421a5a2f3fd2e59bcc8fd436705a4ea08dec98d 100644 --- a/gpui/Cargo.toml +++ b/gpui/Cargo.toml @@ -8,6 +8,7 @@ version = "0.1.0" async-task = "4.0.3" ctor = "0.1" etagere = "0.2" +gpui_macros = {path = "../gpui_macros"} log = "0.4" num_cpus = "1.13" ordered-float = "2.1.1" diff --git a/gpui/src/lib.rs b/gpui/src/lib.rs index edf14bc65c8ae4bc1332ee7016685d5bd3e389d7..19aac02c4037f9a57c920277dac54f2d2180bb56 100644 --- a/gpui/src/lib.rs +++ b/gpui/src/lib.rs @@ -24,6 +24,7 @@ pub mod color; pub mod json; pub mod keymap; mod platform; +pub use gpui_macros::test; pub use platform::{Event, PathPromptOptions}; pub use presenter::{ AfterLayoutContext, Axis, DebugContext, EventContext, LayoutContext, PaintContext, diff --git a/gpui_macros/Cargo.toml b/gpui_macros/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..19a8e11eb61656746be77361e88317452fbd39bf --- /dev/null +++ b/gpui_macros/Cargo.toml @@ -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" diff --git a/gpui_macros/src/lib.rs b/gpui_macros/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..10458687774a6b9b27f1c8da7204705bdcfcae7a --- /dev/null +++ b/gpui_macros/src/lib.rs @@ -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) +}