Tidy up TestContext lifecycle
Co-Authored-By: Max <max@zed.dev>
This commit is contained in:
parent
c7568a7d37
commit
709682e8bc
10 changed files with 82 additions and 78 deletions
|
@ -4,30 +4,27 @@ use crate::state::Mode;
|
|||
|
||||
use super::{ExemptionFeatures, NeovimBackedTestContext, SUPPORTED_FEATURES};
|
||||
|
||||
pub struct NeovimBackedBindingTestContext<'a, const COUNT: usize> {
|
||||
cx: NeovimBackedTestContext<'a>,
|
||||
pub struct NeovimBackedBindingTestContext<const COUNT: usize> {
|
||||
cx: NeovimBackedTestContext,
|
||||
keystrokes_under_test: [&'static str; COUNT],
|
||||
}
|
||||
|
||||
impl<'a, const COUNT: usize> NeovimBackedBindingTestContext<'a, COUNT> {
|
||||
pub fn new(
|
||||
keystrokes_under_test: [&'static str; COUNT],
|
||||
cx: NeovimBackedTestContext<'a>,
|
||||
) -> Self {
|
||||
impl<const COUNT: usize> NeovimBackedBindingTestContext<COUNT> {
|
||||
pub fn new(keystrokes_under_test: [&'static str; COUNT], cx: NeovimBackedTestContext) -> Self {
|
||||
Self {
|
||||
cx,
|
||||
keystrokes_under_test,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn consume(self) -> NeovimBackedTestContext<'a> {
|
||||
pub fn consume(self) -> NeovimBackedTestContext {
|
||||
self.cx
|
||||
}
|
||||
|
||||
pub fn binding<const NEW_COUNT: usize>(
|
||||
self,
|
||||
keystrokes: [&'static str; NEW_COUNT],
|
||||
) -> NeovimBackedBindingTestContext<'a, NEW_COUNT> {
|
||||
) -> NeovimBackedBindingTestContext<NEW_COUNT> {
|
||||
self.consume().binding(keystrokes)
|
||||
}
|
||||
|
||||
|
@ -80,15 +77,15 @@ impl<'a, const COUNT: usize> NeovimBackedBindingTestContext<'a, COUNT> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, const COUNT: usize> Deref for NeovimBackedBindingTestContext<'a, COUNT> {
|
||||
type Target = NeovimBackedTestContext<'a>;
|
||||
impl<const COUNT: usize> Deref for NeovimBackedBindingTestContext<COUNT> {
|
||||
type Target = NeovimBackedTestContext;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.cx
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, const COUNT: usize> DerefMut for NeovimBackedBindingTestContext<'a, COUNT> {
|
||||
impl<const COUNT: usize> DerefMut for NeovimBackedBindingTestContext<COUNT> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.cx
|
||||
}
|
||||
|
|
|
@ -47,8 +47,8 @@ impl ExemptionFeatures {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct NeovimBackedTestContext<'a> {
|
||||
cx: VimTestContext<'a>,
|
||||
pub struct NeovimBackedTestContext {
|
||||
cx: VimTestContext,
|
||||
// Lookup for exempted assertions. Keyed by the insertion text, and with a value indicating which
|
||||
// bindings are exempted. If None, all bindings are ignored for that insertion text.
|
||||
exemptions: HashMap<String, Option<HashSet<String>>>,
|
||||
|
@ -60,8 +60,8 @@ pub struct NeovimBackedTestContext<'a> {
|
|||
is_dirty: bool,
|
||||
}
|
||||
|
||||
impl<'a> NeovimBackedTestContext<'a> {
|
||||
pub async fn new(cx: &'a mut gpui::TestAppContext) -> NeovimBackedTestContext<'a> {
|
||||
impl NeovimBackedTestContext {
|
||||
pub async fn new(cx: &mut gpui::TestAppContext) -> NeovimBackedTestContext {
|
||||
// rust stores the name of the test on the current thread.
|
||||
// We use this to automatically name a file that will store
|
||||
// the neovim connection's requests/responses so that we can
|
||||
|
@ -393,20 +393,20 @@ impl<'a> NeovimBackedTestContext<'a> {
|
|||
pub fn binding<const COUNT: usize>(
|
||||
self,
|
||||
keystrokes: [&'static str; COUNT],
|
||||
) -> NeovimBackedBindingTestContext<'a, COUNT> {
|
||||
) -> NeovimBackedBindingTestContext<COUNT> {
|
||||
NeovimBackedBindingTestContext::new(keystrokes, self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for NeovimBackedTestContext<'a> {
|
||||
type Target = VimTestContext<'a>;
|
||||
impl Deref for NeovimBackedTestContext {
|
||||
type Target = VimTestContext;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.cx
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DerefMut for NeovimBackedTestContext<'a> {
|
||||
impl DerefMut for NeovimBackedTestContext {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.cx
|
||||
}
|
||||
|
@ -415,7 +415,7 @@ impl<'a> DerefMut for NeovimBackedTestContext<'a> {
|
|||
// a common mistake in tests is to call set_shared_state when
|
||||
// you mean asswert_shared_state. This notices that and lets
|
||||
// you know.
|
||||
impl<'a> Drop for NeovimBackedTestContext<'a> {
|
||||
impl Drop for NeovimBackedTestContext {
|
||||
fn drop(&mut self) {
|
||||
if self.is_dirty {
|
||||
panic!("Test context was dropped after set_shared_state before assert_shared_state")
|
||||
|
@ -425,9 +425,8 @@ impl<'a> Drop for NeovimBackedTestContext<'a> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use gpui::TestAppContext;
|
||||
|
||||
use crate::test::NeovimBackedTestContext;
|
||||
use gpui::TestAppContext;
|
||||
|
||||
#[gpui::test]
|
||||
async fn neovim_backed_test_context_works(cx: &mut TestAppContext) {
|
||||
|
|
|
@ -10,11 +10,11 @@ use search::BufferSearchBar;
|
|||
|
||||
use crate::{state::Operator, *};
|
||||
|
||||
pub struct VimTestContext<'a> {
|
||||
cx: EditorLspTestContext<'a>,
|
||||
pub struct VimTestContext {
|
||||
cx: EditorLspTestContext,
|
||||
}
|
||||
|
||||
impl<'a> VimTestContext<'a> {
|
||||
impl VimTestContext {
|
||||
pub fn init(cx: &mut gpui::TestAppContext) {
|
||||
if cx.has_global::<Vim>() {
|
||||
dbg!("OOPS");
|
||||
|
@ -29,13 +29,13 @@ impl<'a> VimTestContext<'a> {
|
|||
});
|
||||
}
|
||||
|
||||
pub async fn new(cx: &'a mut gpui::TestAppContext, enabled: bool) -> VimTestContext<'a> {
|
||||
pub async fn new(cx: &mut gpui::TestAppContext, enabled: bool) -> VimTestContext {
|
||||
Self::init(cx);
|
||||
let lsp = EditorLspTestContext::new_rust(Default::default(), cx).await;
|
||||
Self::new_with_lsp(lsp, enabled)
|
||||
}
|
||||
|
||||
pub async fn new_typescript(cx: &'a mut gpui::TestAppContext) -> VimTestContext<'a> {
|
||||
pub async fn new_typescript(cx: &mut gpui::TestAppContext) -> VimTestContext {
|
||||
Self::init(cx);
|
||||
Self::new_with_lsp(
|
||||
EditorLspTestContext::new_typescript(Default::default(), cx).await,
|
||||
|
@ -43,7 +43,7 @@ impl<'a> VimTestContext<'a> {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn new_with_lsp(mut cx: EditorLspTestContext<'a>, enabled: bool) -> VimTestContext<'a> {
|
||||
pub fn new_with_lsp(mut cx: EditorLspTestContext, enabled: bool) -> VimTestContext {
|
||||
cx.update(|cx| {
|
||||
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||
store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
|
||||
|
@ -162,15 +162,15 @@ impl<'a> VimTestContext<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for VimTestContext<'a> {
|
||||
type Target = EditorTestContext<'a>;
|
||||
impl Deref for VimTestContext {
|
||||
type Target = EditorTestContext;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.cx
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DerefMut for VimTestContext<'a> {
|
||||
impl DerefMut for VimTestContext {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.cx
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue