@@ -1,9 +1,12 @@
1−use std::io::Write;
1+use std::{io::Write, time::Duration};
22
3+use anyhow::bail;
34use crossterm::event::{Event, KeyEvent};
45use helix_core::{test, Selection, Transaction};
56use helix_term::{application::Application, args::Args, config::Config};
67use helix_view::{doc, input::parse_macro};
8+use tokio::time::timeout;
9+use tokio_stream::wrappers::UnboundedReceiverStream;
710
811#[derive(Clone, Debug)]
912pub struct TestCase {
@@ -29,10 +32,44 @@ impl<S: Into<String>> From<(S, S, S)> for TestCase {
2932 }
3033}
3134
32−pub fn test_key_sequence<T: Into<TestCase>>(
35+pub async fn test_key_sequence(
36+ app: &mut Application,
37+ in_keys: &str,
38+ test_fn: Option<&dyn Fn(&Application)>,
39+) -> anyhow::Result<()> {
40+ let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
41+
42+ for key_event in parse_macro(&in_keys)?.into_iter() {
43+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
44+ }
45+
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;
49+
50+ if result.is_ok() {
51+ bail!("application exited before test function could run");
52+ }
53+
54+ if let Some(test) = test_fn {
55+ test(app);
56+ };
57+
58+ for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
59+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
60+ }
61+
62+ let event_loop = app.event_loop(&mut rx_stream);
63+ timeout(Duration::from_millis(5000), event_loop).await?;
64+ app.close().await?;
65+
66+ Ok(())
67+}
68+
69+pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
3370 app: Option<Application>,
3471 test_case: T,
35− test_fn: &dyn Fn(&mut Application),
72+ test_fn: &dyn Fn(&Application),
3673) -> anyhow::Result<()> {
3774 let test_case = test_case.into();
3875 let mut app =
@@ -50,23 +87,13 @@ pub fn test_key_sequence<T: Into<TestCase>>(
5087 view.id,
5188 );
5289
53− let input_keys = parse_macro(&test_case.in_keys)?
54− .into_iter()
55− .map(|key_event| Event::Key(KeyEvent::from(key_event)));
56−
57− for key in input_keys {
58− app.handle_terminal_events(Ok(key));
59− }
60−
61− test_fn(&mut app);
62−
63− Ok(())
90+ test_key_sequence(&mut app, &test_case.in_keys, Some(test_fn)).await
6491}
6592
6693/// Use this for very simple test cases where there is one input
6794/// document, selection, and sequence of key presses, and you just
6895/// 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>>(
7097 args: Args,
7198 config: Config,
7299 test_case: T,
@@ -74,7 +101,7 @@ pub fn test_key_sequence_text_result<T: Into<TestCase>>(
74101 let test_case = test_case.into();
75102 let app = Application::new(args, config).unwrap();
76103
77− test_key_sequence(Some(app), test_case.clone(), &|app| {
104+ test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
78105 let doc = doc!(app.editor);
79106 assert_eq!(&test_case.out_text, doc.text());
80107
@@ -83,9 +110,8 @@ pub fn test_key_sequence_text_result<T: Into<TestCase>>(
83110
84111 let sel = selections.pop().unwrap();
85112 assert_eq!(test_case.out_selection, sel);
86− })?;
87−
88− Ok(())
113+ })
114+ .await
89115}
90116
91117pub fn temp_file_with_contents<S: AsRef<str>>(content: S) -> tempfile::NamedTempFile {