@@ -37,41 +37,56 @@ pub async fn test_key_sequence(
37 app: &mut Application,
37 app: &mut Application,
38 in_keys: Option<&str>,
38 in_keys: Option<&str>,
39 test_fn: Option<&dyn Fn(&Application)>,
39 test_fn: Option<&dyn Fn(&Application)>,
40) -> anyhow::Result<()> {
41) -> 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
44#[allow(clippy::type_complexity)]
45#[allow(clippy::type_complexity)]
45pub async fn test_key_sequences(
46pub async fn test_key_sequences(
46 app: &mut Application,
47 app: &mut Application,
47 inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
48 inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
48) -> anyhow::Result<()> {
50) -> anyhow::Result<()> {
49 const TIMEOUT: Duration = Duration::from_millis(500);
51 const TIMEOUT: Duration = Duration::from_millis(500);
50 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
52 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
51 let mut rx_stream = UnboundedReceiverStream::new(rx);
53 let mut rx_stream = UnboundedReceiverStream::new(rx);
54+ let num_inputs = inputs.len();
53− for (in_keys, test_fn) in inputs {
56+ for (i, (in_keys, test_fn)) in inputs.into_iter().enumerate() {
54 if let Some(in_keys) = in_keys {
57 if let Some(in_keys) = in_keys {
55 for key_event in parse_macro(in_keys)?.into_iter() {
58 for key_event in parse_macro(in_keys)?.into_iter() {
56 tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
59 tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
60− if !app.event_loop_until_idle(&mut rx_stream).await {
63+ let app_exited = !app.event_loop_until_idle(&mut rx_stream).await;
65+ // the app should not exit from any test until the last one
66+ if i < num_inputs - 1 && app_exited {
61 bail!("application exited before test function could run");
67 bail!("application exited before test function could run");
70+ // verify if it exited on the last iteration if it should have and
72+ if i == num_inputs - 1 && app_exited != should_exit {
73+ bail!("expected app to exit: {} != {}", app_exited, should_exit);
64 if let Some(test) = test_fn {
76 if let Some(test) = test_fn {
65 test(app);
77 test(app);
69− for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
82+ for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
70− tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
83+ tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
86+ let event_loop = app.event_loop(&mut rx_stream);
87+ tokio::time::timeout(TIMEOUT, event_loop).await?;
73− let event_loop = app.event_loop(&mut rx_stream);
74− tokio::time::timeout(TIMEOUT, event_loop).await?;
75 app.close().await?;
90 app.close().await?;
@@ -81,6 +96,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
81 app: Option<Application>,
96 app: Option<Application>,
82 test_case: T,
97 test_case: T,
83 test_fn: &dyn Fn(&Application),
98 test_fn: &dyn Fn(&Application),
84) -> anyhow::Result<()> {
100) -> anyhow::Result<()> {
85 let test_case = test_case.into();
101 let test_case = test_case.into();
86 let mut app = match app {
102 let mut app = match app {
@@ -100,7 +116,13 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
103− test_key_sequence(&mut app, Some(&test_case.in_keys), Some(test_fn)).await
121+ Some(&test_case.in_keys),
106/// Use this for very simple test cases where there is one input
128/// 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>>(
114 let test_case = test_case.into();
136 let test_case = test_case.into();
115 let app = Application::new(args, config)?;
137 let app = Application::new(args, config)?;
117− test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
139+ test_key_sequence_with_input_text(
118− let doc = doc!(app.editor);
143+ let doc = doc!(app.editor);
119− assert_eq!(&test_case.out_text, doc.text());
144+ assert_eq!(&test_case.out_text, doc.text());
121− let mut selections: Vec<_> = doc.selections().values().cloned().collect();
146+ let mut selections: Vec<_> = doc.selections().values().cloned().collect();
122− assert_eq!(1, selections.len());
147+ assert_eq!(1, selections.len());
124− let sel = selections.pop().unwrap();
149+ let sel = selections.pop().unwrap();
125− assert_eq!(test_case.out_selection, sel);
150+ assert_eq!(test_case.out_selection, sel);