aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/compositor.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-term/src/compositor.rs')
-rw-r--r--helix-term/src/compositor.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs
index 3dcb5f2b..6a357401 100644
--- a/helix-term/src/compositor.rs
+++ b/helix-term/src/compositor.rs
@@ -35,6 +35,50 @@ impl<'a> Context<'a> {
tokio::task::block_in_place(|| helix_lsp::block_on(self.editor.flush_writes()))?;
Ok(())
}
+
+ /// Purpose: to test `handle_event` without escalating the test case to integration test
+ /// Usage:
+ /// ```
+ /// let mut editor = Context::dummy_editor();
+ /// let mut jobs = Context::dummy_jobs();
+ /// let mut cx = Context::dummy(&mut jobs, &mut editor);
+ /// ```
+ #[cfg(test)]
+ pub fn dummy(jobs: &'a mut Jobs, editor: &'a mut helix_view::Editor) -> Context<'a> {
+ Context {
+ jobs,
+ scroll: None,
+ editor,
+ }
+ }
+
+ #[cfg(test)]
+ pub fn dummy_jobs() -> Jobs {
+ Jobs::new()
+ }
+
+ #[cfg(test)]
+ pub fn dummy_editor() -> Editor {
+ use crate::config::Config;
+ use arc_swap::{access::Map, ArcSwap};
+ use helix_core::syntax::{self, Configuration};
+ use helix_view::theme;
+ use std::{collections::HashMap, sync::Arc};
+
+ let config = Arc::new(ArcSwap::from_pointee(Config::default()));
+ Editor::new(
+ Rect::new(0, 0, 60, 120),
+ Arc::new(theme::Loader::new(&[])),
+ Arc::new(syntax::Loader::new(Configuration {
+ language: vec![],
+ language_server: HashMap::new(),
+ })),
+ Arc::new(Arc::new(Map::new(
+ Arc::clone(&config),
+ |config: &Config| &config.editor,
+ ))),
+ )
+ }
}
pub trait Component: Any + AnyComponent {
@@ -73,6 +117,21 @@ pub trait Component: Any + AnyComponent {
fn id(&self) -> Option<&'static str> {
None
}
+
+ #[cfg(test)]
+ /// Utility method for testing `handle_event` without using integration test.
+ /// Especially useful for testing helper components such as `Prompt`, `TreeView` etc
+ fn handle_events(&mut self, events: &str) -> anyhow::Result<()> {
+ use helix_view::input::parse_macro;
+
+ let mut editor = Context::dummy_editor();
+ let mut jobs = Context::dummy_jobs();
+ let mut cx = Context::dummy(&mut jobs, &mut editor);
+ for event in parse_macro(events)? {
+ self.handle_event(&Event::Key(event), &mut cx);
+ }
+ Ok(())
+ }
}
pub struct Compositor {