add test for write-quit happy path

imgbot
Skyler Hawthorne 2 years ago
parent 7c0bca186c
commit fac36bc5ea

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

@ -37,41 +37,56 @@ pub async fn test_key_sequence(
app: &mut Application,
in_keys: Option<&str>,
test_fn: Option<&dyn Fn(&Application)>,
should_exit: bool,
) -> 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)]
pub async fn test_key_sequences(
app: &mut Application,
inputs: Vec<(Option<&str>, Option<&dyn Fn(&Application)>)>,
should_exit: bool,
) -> anyhow::Result<()> {
const TIMEOUT: Duration = Duration::from_millis(500);
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
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 {
for key_event in parse_macro(in_keys)?.into_iter() {
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");
}
// 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 {
test(app);
};
}
for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
if !should_exit {
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?;
Ok(())
@ -81,6 +96,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
app: Option<Application>,
test_case: T,
test_fn: &dyn Fn(&Application),
should_exit: bool,
) -> anyhow::Result<()> {
let test_case = test_case.into();
let mut app = match app {
@ -100,7 +116,13 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
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
@ -114,16 +136,21 @@ pub async fn test_with_config<T: Into<TestCase>>(
let test_case = test_case.into();
let app = Application::new(args, config)?;
test_key_sequence_with_input_text(Some(app), test_case.clone(), &|app| {
let doc = doc!(app.editor);
assert_eq!(&test_case.out_text, doc.text());
let mut selections: Vec<_> = doc.selections().values().cloned().collect();
assert_eq!(1, selections.len());
let sel = selections.pop().unwrap();
assert_eq!(test_case.out_selection, sel);
})
test_key_sequence_with_input_text(
Some(app),
test_case.clone(),
&|app| {
let doc = doc!(app.editor);
assert_eq!(&test_case.out_text, doc.text());
let mut selections: Vec<_> = doc.selections().values().cloned().collect();
assert_eq!(1, selections.len());
let sel = selections.pop().unwrap();
assert_eq!(test_case.out_selection, sel);
},
false,
)
.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>"),
None,
false,
)
.await?;
@ -31,6 +32,39 @@ async fn test_write() -> anyhow::Result<()> {
let mut file_content = String::new();
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!(
helpers::platform_line("i can eat glass, it will not hurt me"),
file_content
@ -61,6 +95,7 @@ async fn test_write_concurrent() -> anyhow::Result<()> {
)?,
Some(&command),
None,
false,
)
.await?;
@ -112,6 +147,7 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
}),
),
],
false,
)
.await?;
@ -149,6 +185,7 @@ async fn test_write_fail_new_path() -> anyhow::Result<()> {
}),
),
],
false,
)
.await?;

Loading…
Cancel
Save