Fix errors in passing TestAppContext in async gpui2 tests

This commit is contained in:
Antonio Scandurra 2023-10-25 21:07:13 +02:00
parent 43d230cb2d
commit c5763cdb99
3 changed files with 41 additions and 38 deletions

View file

@ -48,6 +48,14 @@ impl TestAppContext {
}
}
pub fn remove_all_windows(&self) {
// todo!("use app quit instead")
}
pub fn clear_globals(&self) {
// todo!("use app quit instead")
}
pub fn refresh(&mut self) -> Result<()> {
let mut lock = self.app.lock();
lock.refresh();

View file

@ -3,8 +3,8 @@ use proc_macro2::Ident;
use quote::{format_ident, quote};
use std::mem;
use syn::{
parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, DeriveInput, FnArg,
GenericParam, Generics, ItemFn, Lit, Meta, NestedMeta, Type, WhereClause,
parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, FnArg,
ItemFn, Lit, Meta, NestedMeta, Type,
};
pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
@ -106,13 +106,13 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
let cx_varname = format_ident!("cx_{}", ix);
cx_vars.extend(quote!(
let mut #cx_varname = gpui2::TestAppContext::new(
std::sync::Arc::new(dispatcher.clone())
dispatcher.clone()
);
));
cx_teardowns.extend(quote!(
#cx_varname.remove_all_windows();
dispatcher.run_until_parked();
#cx_varname.update(|cx| cx.clear_globals());
#cx_varname.clear_globals();
dispatcher.run_until_parked();
));
inner_fn_args.extend(quote!(&mut #cx_varname,));
@ -170,7 +170,7 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
let cx_varname_lock = format_ident!("cx_{}_lock", ix);
cx_vars.extend(quote!(
let mut #cx_varname = gpui2::TestAppContext::new(
std::sync::Arc::new(dispatcher.clone())
dispatcher.clone()
);
let mut #cx_varname_lock = cx_varname.app.lock();
));
@ -178,7 +178,7 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
cx_teardowns.extend(quote!(
#cx_varname.remove_all_windows();
dispatcher.run_until_parked();
#cx_varname.update(|cx| cx.clear_globals());
#cx_varname.clear_globals();
dispatcher.run_until_parked();
));
continue;
@ -187,13 +187,13 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
let cx_varname = format_ident!("cx_{}", ix);
cx_vars.extend(quote!(
let mut #cx_varname = gpui2::TestAppContext::new(
std::sync::Arc::new(dispatcher.clone())
dispatcher.clone()
);
));
cx_teardowns.extend(quote!(
#cx_varname.remove_all_windows();
dispatcher.run_until_parked();
#cx_varname.update(|cx| cx.clear_globals());
#cx_varname.clear_globals();
dispatcher.run_until_parked();
));
inner_fn_args.extend(quote!(&mut #cx_varname,));
@ -243,13 +243,3 @@ fn parse_int(literal: &Lit) -> Result<usize, TokenStream> {
result.map_err(|err| TokenStream::from(err.into_compile_error()))
}
fn parse_bool(literal: &Lit) -> Result<bool, TokenStream> {
let result = if let Lit::Bool(result) = &literal {
Ok(result.value)
} else {
Err(syn::Error::new(literal.span(), "must be a boolean"))
};
result.map_err(|err| TokenStream::from(err.into_compile_error()))
}

View file

@ -1,3 +1,7 @@
use crate::Buffer;
use gpui2::{Context, TestAppContext};
use text::{Point, ToPoint};
// use crate::language_settings::{
// AllLanguageSettings, AllLanguageSettingsContent, LanguageSettingsContent,
// };
@ -219,28 +223,29 @@
// );
// }
// #[gpui::test]
// async fn test_apply_diff(cx: &mut gpui::TestAppContext) {
// let text = "a\nbb\nccc\ndddd\neeeee\nffffff\n";
// let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, text));
// let anchor = buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(3, 3)));
// #[gpui::test] todo!()
#[gpui2::test]
async fn test_apply_diff(cx: &mut TestAppContext) {
let text = "a\nbb\nccc\ndddd\neeeee\nffffff\n";
let buffer = cx.entity(|cx| Buffer::new(0, cx.entity_id().as_u64(), text));
let anchor = buffer.update(cx, |buffer, _| buffer.anchor_before(Point::new(3, 3)));
// let text = "a\nccc\ndddd\nffffff\n";
// let diff = buffer.read_with(cx, |b, cx| b.diff(text.into(), cx)).await;
// buffer.update(cx, |buffer, cx| {
// buffer.apply_diff(diff, cx).unwrap();
// assert_eq!(buffer.text(), text);
// assert_eq!(anchor.to_point(buffer), Point::new(2, 3));
// });
let text = "a\nccc\ndddd\nffffff\n";
let diff = buffer.update(cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx).unwrap();
assert_eq!(buffer.text(), text);
assert_eq!(anchor.to_point(buffer), Point::new(2, 3));
});
// let text = "a\n1\n\nccc\ndd2dd\nffffff\n";
// let diff = buffer.read_with(cx, |b, cx| b.diff(text.into(), cx)).await;
// buffer.update(cx, |buffer, cx| {
// buffer.apply_diff(diff, cx).unwrap();
// assert_eq!(buffer.text(), text);
// assert_eq!(anchor.to_point(buffer), Point::new(4, 4));
// });
// }
let text = "a\n1\n\nccc\ndd2dd\nffffff\n";
let diff = buffer.update(cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx).unwrap();
assert_eq!(buffer.text(), text);
assert_eq!(anchor.to_point(buffer), Point::new(4, 4));
});
}
// #[gpui::test(iterations = 10)]
// async fn test_normalize_whitespace(cx: &mut gpui::TestAppContext) {