Add a public gpui::TestAppContext::new method

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-06-23 16:34:36 -07:00
parent f1587fbb6e
commit a57cb2b189
2 changed files with 22 additions and 21 deletions

View file

@ -111,10 +111,7 @@ pub struct TestAppContext {
} }
impl App { impl App {
pub fn test<T, A: AssetSource, F: FnOnce(&mut MutableAppContext) -> T>( pub fn test<T, F: FnOnce(&mut MutableAppContext) -> T>(f: F) -> T {
asset_source: A,
f: F,
) -> T {
let foreground_platform = platform::test::foreground_platform(); let foreground_platform = platform::test::foreground_platform();
let platform = platform::test::platform(); let platform = platform::test::platform();
let foreground = Rc::new(executor::Foreground::test()); let foreground = Rc::new(executor::Foreground::test());
@ -122,32 +119,20 @@ impl App {
foreground, foreground,
Arc::new(platform), Arc::new(platform),
Rc::new(foreground_platform), Rc::new(foreground_platform),
asset_source, (),
))); )));
cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx)); cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx));
let mut cx = cx.borrow_mut(); let mut cx = cx.borrow_mut();
f(&mut *cx) f(&mut *cx)
} }
pub fn test_async<T, F, A: AssetSource, Fn>(asset_source: A, f: Fn) -> T pub fn test_async<T, F, Fn>(f: Fn) -> T
where where
Fn: FnOnce(TestAppContext) -> F, Fn: FnOnce(TestAppContext) -> F,
F: Future<Output = T>, F: Future<Output = T>,
{ {
let platform = Arc::new(platform::test::platform());
let foreground_platform = Rc::new(platform::test::foreground_platform());
let foreground = Rc::new(executor::Foreground::test()); let foreground = Rc::new(executor::Foreground::test());
let cx = TestAppContext { let cx = TestAppContext::new(foreground.clone());
cx: Rc::new(RefCell::new(MutableAppContext::new(
foreground.clone(),
platform,
foreground_platform.clone(),
asset_source,
))),
foreground_platform,
};
cx.cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx.cx));
let future = f(cx); let future = f(cx);
smol::block_on(foreground.run(future)) smol::block_on(foreground.run(future))
} }
@ -261,6 +246,22 @@ impl App {
} }
impl TestAppContext { impl TestAppContext {
pub fn new(foreground: Rc<executor::Foreground>) -> Self {
let platform = Arc::new(platform::test::platform());
let foreground_platform = Rc::new(platform::test::foreground_platform());
let cx = TestAppContext {
cx: Rc::new(RefCell::new(MutableAppContext::new(
foreground.clone(),
platform,
foreground_platform.clone(),
(),
))),
foreground_platform,
};
cx.cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx.cx));
cx
}
pub fn dispatch_action<T: 'static + Any>( pub fn dispatch_action<T: 'static + Any>(
&self, &self,
window_id: usize, window_id: usize,

View file

@ -34,7 +34,7 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
fn #outer_fn_name() { fn #outer_fn_name() {
#inner_fn #inner_fn
#namespace::App::test_async((), move |cx| async { #namespace::App::test_async(move |cx| async {
#inner_fn_name(cx).await; #inner_fn_name(cx).await;
}); });
} }
@ -45,7 +45,7 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
fn #outer_fn_name() { fn #outer_fn_name() {
#inner_fn #inner_fn
#namespace::App::test((), |cx| { #namespace::App::test(|cx| {
#inner_fn_name(cx); #inner_fn_name(cx);
}); });
} }