@@ -1,9 +1,12 @@
1−use std::io::Write;
1+use std::{io::Write, time::Duration};
3use crossterm::event::{Event, KeyEvent};
4use crossterm::event::{Event, KeyEvent};
4use helix_core::{test, Selection, Transaction};
5use helix_core::{test, Selection, Transaction};
5use helix_term::{application::Application, args::Args, config::Config};
6use helix_term::{application::Application, args::Args, config::Config};
6use helix_view::{doc, input::parse_macro};
7use helix_view::{doc, input::parse_macro};
8+use tokio::time::timeout;
9+use tokio_stream::wrappers::UnboundedReceiverStream;
8#[derive(Clone, Debug)]
11#[derive(Clone, Debug)]
9pub struct TestCase {
12pub struct TestCase {
@@ -29,10 +32,44 @@ impl<S: Into<String>> From<(S, S, S)> for TestCase {
32−pub fn test_key_sequence<T: Into<TestCase>>(
35+pub async fn test_key_sequence(
36+ app: &mut Application,
38+ test_fn: Option<&dyn Fn(&Application)>,
39+) -> anyhow::Result<()> {
40+ let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
42+ for key_event in parse_macro(&in_keys)?.into_iter() {
43+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
46+ let mut rx_stream = UnboundedReceiverStream::new(rx);
47+ let event_loop = app.event_loop(&mut rx_stream);
48+ let result = timeout(Duration::from_millis(500), event_loop).await;
51+ bail!("application exited before test function could run");
54+ if let Some(test) = test_fn {
58+ for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
59+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
62+ let event_loop = app.event_loop(&mut rx_stream);
63+ timeout(Duration::from_millis(5000), event_loop).await?;
69+pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
33 app: Option<Application>,
70 app: Option<Application>,
34 test_case: T,
71 test_case: T,
35− test_fn: &dyn Fn(&mut Application),
72+ test_fn: &dyn Fn(&Application),
36) -> anyhow::Result<()> {
73) -> anyhow::Result<()> {
37 let test_case = test_case.into();
74 let test_case = test_case.into();
38 let mut app =
75 let mut app =
@@ -50,23 +87,13 @@ pub fn test_key_sequence<T: Into<TestCase>>(
53− let input_keys = parse_macro(&test_case.in_keys)?
90+ test_key_sequence(&mut app, &test_case.in_keys, Some(test_fn)).await
55− .map(|key_event| Event::Key(KeyEvent::from(key_event)));
57− for key in input_keys {
58− app.handle_terminal_events(Ok(key));
66/// Use this for very simple test cases where there is one input
93/// Use this for very simple test cases where there is one input
67/// document, selection, and sequence of key presses, and you just
94/// document, selection, and sequence of key presses, and you just
68/// want to verify the resulting document and selection.
95/// want to verify the resulting document and selection.
69−pub fn test_key_sequence_text_result<T: Into<TestCase>>(
96+pub async fn test_key_sequence_text_result<T: Into<TestCase>>(
70 args: Args,
97 args: Args,
71 config: Config,
98 config: Config,
72 test_case: T,
99 test_case: T,
@@ -74,7 +101,7 @@ pub fn test_key_sequence_text_result<T: Into<TestCase>>(
74 let test_case = test_case.into();
101 let test_case = test_case.into();
75 let app = Application::new(args, config).unwrap();
102 let app = Application::new(args, config).unwrap();
77− test_key_sequence(Some(app), test_case.clone(), &|app| {
104+ test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
78 let doc = doc!(app.editor);
105 let doc = doc!(app.editor);
79 assert_eq!(&test_case.out_text, doc.text());
106 assert_eq!(&test_case.out_text, doc.text());
@@ -83,9 +110,8 @@ pub fn test_key_sequence_text_result<T: Into<TestCase>>(
84 let sel = selections.pop().unwrap();
111 let sel = selections.pop().unwrap();
85 assert_eq!(test_case.out_selection, sel);
112 assert_eq!(test_case.out_selection, sel);
91pub fn temp_file_with_contents<S: AsRef<str>>(content: S) -> tempfile::NamedTempFile {
117pub fn temp_file_with_contents<S: AsRef<str>>(content: S) -> tempfile::NamedTempFile {