add test for write-quit happy path

pull/2359/head
Skyler Hawthorne 3 years ago
parent 7c0bca186c
commit fac36bc5ea

@ -25,6 +25,7 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
Some(&|app| { Some(&|app| {
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1); assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
}), }),
false,
) )
.await?; .await?;
@ -59,6 +60,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
}), }),
), ),
], ],
false,
) )
.await?; .await?;
@ -89,6 +91,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
let doc = app.editor.document_by_path(file.path()); let doc = app.editor.document_by_path(file.path());
assert!(doc.is_none(), "found doc: {:?}", doc); assert!(doc.is_none(), "found doc: {:?}", doc);
}), }),
false,
) )
.await?; .await?;

@ -37,41 +37,56 @@ pub async fn test_key_sequence(
app: &mut Application, app: &mut Application,
in_keys: Option<&str>, in_keys: Option<&str>,
test_fn: Option<&dyn Fn(&Application)>, test_fn: Option<&dyn Fn(&Application)>,
should_exit: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
test_key_sequences(app, vec![(in_keys, test_fn)]).await test_key_sequences(app, vec![(in_keys, test_fn)], should_exit).await
} }
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub async fn test_key_sequences( pub async fn test_key_sequences(
app: &mut Application, app: &mut Application,
inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>, inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
should_exit: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
const TIMEOUT: Duration = Duration::from_millis(500); const TIMEOUT: Duration = Duration::from_millis(500);
let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let mut rx_stream = UnboundedReceiverStream::new(rx); let mut rx_stream = UnboundedReceiverStream::new(rx);
let num_inputs = inputs.len();
for (in_keys, test_fn) in inputs { for (i, (in_keys, test_fn)) in inputs.into_iter().enumerate() {
if let Some(in_keys) = in_keys { if let Some(in_keys) = in_keys {
for key_event in parse_macro(in_keys)?.into_iter() { for key_event in parse_macro(in_keys)?.into_iter() {
tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?; tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
} }
} }
if !app.event_loop_until_idle(&mut rx_stream).await { let app_exited = !app.event_loop_until_idle(&mut rx_stream).await;
// the app should not exit from any test until the last one
if i < num_inputs - 1 && app_exited {
bail!("application exited before test function could run"); bail!("application exited before test function could run");
} }
// verify if it exited on the last iteration if it should have and
// the inverse
if i == num_inputs - 1 && app_exited != should_exit {
bail!("expected app to exit: {} != {}", app_exited, should_exit);
}
if let Some(test) = test_fn { if let Some(test) = test_fn {
test(app); test(app);
}; };
} }
for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() { if !should_exit {
tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?; for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
}
let event_loop = app.event_loop(&mut rx_stream);
tokio::time::timeout(TIMEOUT, event_loop).await?;
} }
let event_loop = app.event_loop(&mut rx_stream);
tokio::time::timeout(TIMEOUT, event_loop).await?;
app.close().await?; app.close().await?;
Ok(()) Ok(())
@ -81,6 +96,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
app: Option<Application>, app: Option<Application>,
test_case: T, test_case: T,
test_fn: &dyn Fn(&Application), test_fn: &dyn Fn(&Application),
should_exit: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let test_case = test_case.into(); let test_case = test_case.into();
let mut app = match app { let mut app = match app {
@ -100,7 +116,13 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
view.id, view.id,
); );
test_key_sequence(&mut app, Some(&test_case.in_keys), Some(test_fn)).await test_key_sequence(
&mut app,
Some(&test_case.in_keys),
Some(test_fn),
should_exit,
)
.await
} }
/// Use this for very simple test cases where there is one input /// 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>>(
let test_case = test_case.into(); let test_case = test_case.into();
let app = Application::new(args, config)?; let app = Application::new(args, config)?;
test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| { test_key_sequence_with_input_text(
let doc = doc!(app.editor); Some(app),
assert_eq!(&test_case.out_text, doc.text()); test_case.clone(),
&|app| {
let mut selections: Vec<_> = doc.selections().values().cloned().collect(); let doc = doc!(app.editor);
assert_eq!(1, selections.len()); assert_eq!(&test_case.out_text, doc.text());
let sel = selections.pop().unwrap(); let mut selections: Vec<_> = doc.selections().values().cloned().collect();
assert_eq!(test_case.out_selection, sel); assert_eq!(1, selections.len());
})
let sel = selections.pop().unwrap();
assert_eq!(test_case.out_selection, sel);
},
false,
)
.await .await
} }

@ -23,6 +23,7 @@ async fn test_write() -> anyhow::Result<()> {
)?, )?,
Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"), Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"),
None, None,
false,
) )
.await?; .await?;
@ -31,6 +32,39 @@ async fn test_write() -> anyhow::Result<()> {
let mut file_content = String::new(); let mut file_content = String::new();
file.as_file_mut().read_to_string(&mut file_content)?; file.as_file_mut().read_to_string(&mut file_content)?;
assert_eq!(
helpers::platform_line("i can eat glass, it will not hurt me"),
file_content
);
Ok(())
}
#[tokio::test]
async fn test_write_quit() -> anyhow::Result<()> {
let mut file = tempfile::NamedTempFile::new()?;
test_key_sequence(
&mut Application::new(
Args {
files: vec![(file.path().to_path_buf(), Position::default())],
..Default::default()
},
Config::default(),
)?,
Some("ii can eat glass, it will not hurt me<ret><esc>:wq<ret>"),
None,
true,
)
.await?;
file.as_file_mut().flush()?;
file.as_file_mut().sync_all()?;
let mut file_content = String::new();
file.as_file_mut().read_to_string(&mut file_content)?;
assert_eq!( assert_eq!(
helpers::platform_line("i can eat glass, it will not hurt me"), helpers::platform_line("i can eat glass, it will not hurt me"),
file_content file_content
@ -61,6 +95,7 @@ async fn test_write_concurrent() -> anyhow::Result<()> {
)?, )?,
Some(&command), Some(&command),
None, None,
false,
) )
.await?; .await?;
@ -112,6 +147,7 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
}), }),
), ),
], ],
false,
) )
.await?; .await?;
@ -149,6 +185,7 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
}), }),
), ),
], ],
false,
) )
.await?; .await?;

Loading…
Cancel
Save