@@ -37,41 +37,56 @@ pub async fn test_key_sequence(
3737 app: &mut Application,
3838 in_keys: Option<&str>,
3939 test_fn: Option<&dyn Fn(&Application)>,
40+ should_exit: bool,
4041) -> anyhow::Result<()> {
41− test_key_sequences(app, vec![(in_keys, test_fn)]).await
42+ test_key_sequences(app, vec![(in_keys, test_fn)], should_exit).await
4243}
4344
4445#[allow(clippy::type_complexity)]
4546pub async fn test_key_sequences(
4647 app: &mut Application,
4748 inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
49+ should_exit: bool,
4850) -> anyhow::Result<()> {
4951 const TIMEOUT: Duration = Duration::from_millis(500);
5052 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
5153 let mut rx_stream = UnboundedReceiverStream::new(rx);
54+ let num_inputs = inputs.len();
5255
53− for (in_keys, test_fn) in inputs {
56+ for (i, (in_keys, test_fn)) in inputs.into_iter().enumerate() {
5457 if let Some(in_keys) = in_keys {
5558 for key_event in parse_macro(in_keys)?.into_iter() {
5659 tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
5760 }
5861 }
5962
60− if !app.event_loop_until_idle(&mut rx_stream).await {
63+ let app_exited = !app.event_loop_until_idle(&mut rx_stream).await;
64+
65+ // the app should not exit from any test until the last one
66+ if i < num_inputs - 1 && app_exited {
6167 bail!("application exited before test function could run");
6268 }
6369
70+ // verify if it exited on the last iteration if it should have and
71+ // the inverse
72+ if i == num_inputs - 1 && app_exited != should_exit {
73+ bail!("expected app to exit: {} != {}", app_exited, should_exit);
74+ }
75+
6476 if let Some(test) = test_fn {
6577 test(app);
6678 };
6779 }
6880
69− for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
70− tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
81+ if !should_exit {
82+ for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
83+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
84+ }
85+
86+ let event_loop = app.event_loop(&mut rx_stream);
87+ tokio::time::timeout(TIMEOUT, event_loop).await?;
7188 }
7289
73− let event_loop = app.event_loop(&mut rx_stream);
74− tokio::time::timeout(TIMEOUT, event_loop).await?;
7590 app.close().await?;
7691
7792 Ok(())
@@ -81,6 +96,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
8196 app: Option<Application>,
8297 test_case: T,
8398 test_fn: &dyn Fn(&Application),
99+ should_exit: bool,
84100) -> anyhow::Result<()> {
85101 let test_case = test_case.into();
86102 let mut app = match app {
@@ -100,7 +116,13 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
100116 view.id,
101117 );
102118
103− test_key_sequence(&mut app, Some(&test_case.in_keys), Some(test_fn)).await
119+ test_key_sequence(
120+ &mut app,
121+ Some(&test_case.in_keys),
122+ Some(test_fn),
123+ should_exit,
124+ )
125+ .await
104126}
105127
106128/// Use this for very simple test cases where there is one input
@@ -114,16 +136,21 @@ pub async fn test_with_config<T: Into<TestCase>>(
114136 let test_case = test_case.into();
115137 let app = Application::new(args, config)?;
116138
117− test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
118− let doc = doc!(app.editor);
119− assert_eq!(&test_case.out_text, doc.text());
120−
121− let mut selections: Vec<_> = doc.selections().values().cloned().collect();
122− assert_eq!(1, selections.len());
123−
124− let sel = selections.pop().unwrap();
125− assert_eq!(test_case.out_selection, sel);
126− })
139+ test_key_sequence_with_input_text(
140+ Some(app),
141+ test_case.clone(),
142+ &|app| {
143+ let doc = doc!(app.editor);
144+ assert_eq!(&test_case.out_text, doc.text());
145+
146+ let mut selections: Vec<_> = doc.selections().values().cloned().collect();
147+ assert_eq!(1, selections.len());
148+
149+ let sel = selections.pop().unwrap();
150+ assert_eq!(test_case.out_selection, sel);
151+ },
152+ false,
153+ )
127154 .await
128155}
129156