neovim_backed_binding_test_context.rs

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