Improve support for gpui tests that need multiple contexts

If a test function takes multiple contexts, pass it however many
distinct contexts are needed. Construct each one with a different
starting entity id so that they do not share any entity ids.
This commit is contained in:
Max Brunsfeld
2021-07-07 17:18:42 -07:00
parent 7335e70eb7
commit f46c0a790e
5 changed files with 26 additions and 24 deletions
+14 -6
View File
@@ -38,6 +38,16 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
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());
// Pass to the test function the number of app contexts that it needs,
// based on its parameter list.
let inner_fn_args = (0..inner_fn.sig.inputs.len())
.map(|i| {
let first_entity_id = i * 100_000;
quote!(#namespace::TestAppContext::new(foreground.clone(), #first_entity_id),)
})
.collect::<proc_macro2::TokenStream>();
let mut outer_fn: ItemFn = if inner_fn.sig.asyncness.is_some() {
parse_quote! {
#[test]
@@ -48,9 +58,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
let mut retries = 0;
loop {
let result = std::panic::catch_unwind(|| {
#namespace::App::test_async(move |cx| async {
#inner_fn_name(cx).await;
});
let foreground = ::std::rc::Rc::new(#namespace::executor::Foreground::test());
#namespace::block_on(foreground.run(#inner_fn_name(#inner_fn_args)));
});
match result {
@@ -66,9 +75,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
}
}
} else {
#namespace::App::test_async(move |cx| async {
#inner_fn_name(cx).await;
});
let foreground = ::std::rc::Rc::new(#namespace::executor::Foreground::test());
#namespace::block_on(foreground.run(#inner_fn_name(#inner_fn_args)));
}
}
}