1use std::ops::{Deref, DerefMut};
2
3use gpui::ContextHandle;
4
5use crate::state::Mode;
6
7use super::{ExemptionFeatures, NeovimBackedTestContext, SUPPORTED_FEATURES};
8
9pub struct NeovimBackedBindingTestContext<'a, const COUNT: usize> {
10 cx: NeovimBackedTestContext<'a>,
11 keystrokes_under_test: [&'static str; COUNT],
12}
13
14impl<'a, const COUNT: usize> NeovimBackedBindingTestContext<'a, COUNT> {
15 pub fn new(
16 keystrokes_under_test: [&'static str; COUNT],
17 cx: NeovimBackedTestContext<'a>,
18 ) -> Self {
19 Self {
20 cx,
21 keystrokes_under_test,
22 }
23 }
24
25 pub fn consume(self) -> NeovimBackedTestContext<'a> {
26 self.cx
27 }
28
29 pub fn binding<const NEW_COUNT: usize>(
30 self,
31 keystrokes: [&'static str; NEW_COUNT],
32 ) -> NeovimBackedBindingTestContext<'a, NEW_COUNT> {
33 self.consume().binding(keystrokes)
34 }
35
36 pub async fn assert(
37 &mut self,
38 marked_positions: &str,
39 ) -> Option<(ContextHandle, ContextHandle)> {
40 self.cx
41 .assert_binding_matches(self.keystrokes_under_test, marked_positions)
42 .await
43 }
44
45 pub async fn assert_exempted(
46 &mut self,
47 marked_positions: &str,
48 feature: ExemptionFeatures,
49 ) -> Option<(ContextHandle, ContextHandle)> {
50 if SUPPORTED_FEATURES.contains(&feature) {
51 self.cx
52 .assert_binding_matches(self.keystrokes_under_test, marked_positions)
53 .await
54 } else {
55 None
56 }
57 }
58
59 pub fn assert_manual(
60 &mut self,
61 initial_state: &str,
62 mode_before: Mode,
63 state_after: &str,
64 mode_after: Mode,
65 ) {
66 self.cx.assert_binding(
67 self.keystrokes_under_test,
68 initial_state,
69 mode_before,
70 state_after,
71 mode_after,
72 );
73 }
74
75 pub async fn assert_all(&mut self, marked_positions: &str) {
76 self.cx
77 .assert_binding_matches_all(self.keystrokes_under_test, marked_positions)
78 .await
79 }
80
81 pub async fn assert_all_exempted(
82 &mut self,
83 marked_positions: &str,
84 feature: ExemptionFeatures,
85 ) {
86 if SUPPORTED_FEATURES.contains(&feature) {
87 self.cx
88 .assert_binding_matches_all(self.keystrokes_under_test, marked_positions)
89 .await
90 }
91 }
92}
93
94impl<'a, const COUNT: usize> Deref for NeovimBackedBindingTestContext<'a, COUNT> {
95 type Target = NeovimBackedTestContext<'a>;
96
97 fn deref(&self) -> &Self::Target {
98 &self.cx
99 }
100}
101
102impl<'a, const COUNT: usize> DerefMut for NeovimBackedBindingTestContext<'a, COUNT> {
103 fn deref_mut(&mut self) -> &mut Self::Target {
104 &mut self.cx
105 }
106}