factor new Application with file arg to function

This commit is contained in:
Skyler Hawthorne 2022-06-10 23:35:34 -04:00
parent 65bf6836b7
commit 665286c199
5 changed files with 21 additions and 56 deletions

View file

@ -239,11 +239,9 @@ impl Application {
self.last_render = Instant::now();
loop {
if self.editor.should_close() {
if !self.event_loop_until_idle(input_stream).await {
break;
}
self.event_loop_until_idle(input_stream).await;
}
}

View file

@ -13,13 +13,7 @@ async fn test_write_quit_fail() -> anyhow::Result<()> {
let file = helpers::new_readonly_tempfile()?;
test_key_sequence(
&mut Application::new(
Args {
files: vec![(file.path().to_path_buf(), Position::default())],
..Default::default()
},
Config::default(),
)?,
&mut helpers::app_with_file(file.path())?,
Some("ihello<esc>:wq<ret>"),
Some(&|app| {
assert_eq!(&Severity::Error, app.editor.get_status().unwrap().1);
@ -76,13 +70,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
command.push_str(":buffer<minus>close<ret>");
test_key_sequence(
&mut Application::new(
Args {
files: vec![(file.path().to_path_buf(), Position::default())],
..Default::default()
},
Config::default(),
)?,
&mut helpers::app_with_file(file.path())?,
Some(&command),
Some(&|app| {
assert!(!app.editor.is_err(), "error: {:?}", app.editor.get_status());

View file

@ -1,4 +1,4 @@
use std::{io::Write, time::Duration};
use std::{io::Write, path::PathBuf, time::Duration};
use anyhow::bail;
use crossterm::event::{Event, KeyEvent};
@ -199,3 +199,15 @@ pub fn new_readonly_tempfile() -> anyhow::Result<NamedTempFile> {
file.as_file_mut().set_permissions(perms)?;
Ok(file)
}
/// Creates a new Application with default config that opens the given file
/// path
pub fn app_with_file<P: Into<PathBuf>>(path: P) -> anyhow::Result<Application> {
Application::new(
Args {
files: vec![(path.into(), helix_core::Position::default())],
..Default::default()
},
Config::default(),
)
}

View file

@ -1,5 +1,3 @@
use helix_term::application::Application;
use super::*;
#[tokio::test]
@ -72,14 +70,7 @@ async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> {
async fn cursor_position_newly_opened_file() -> anyhow::Result<()> {
let test = |content: &str, expected_sel: Selection| -> anyhow::Result<()> {
let file = helpers::temp_file_with_contents(content)?;
let mut app = Application::new(
Args {
files: vec![(file.path().to_path_buf(), Position::default())],
..Default::default()
},
Config::default(),
)?;
let mut app = helpers::app_with_file(file.path())?;
let (view, doc) = helix_view::current!(app.editor);
let sel = doc.selection(view.id).clone();

View file

@ -14,13 +14,7 @@ async fn test_write() -> 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(),
)?,
&mut helpers::app_with_file(file.path())?,
Some("ii can eat glass, it will not hurt me<ret><esc>:w<ret>"),
None,
false,
@ -46,13 +40,7 @@ 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(),
)?,
&mut helpers::app_with_file(file.path())?,
Some("ii can eat glass, it will not hurt me<ret><esc>:wq<ret>"),
None,
true,
@ -86,13 +74,7 @@ async fn test_write_concurrent() -> anyhow::Result<()> {
}
test_key_sequence(
&mut Application::new(
Args {
files: vec![(file.path().to_path_buf(), Position::default())],
..Default::default()
},
Config::default(),
)?,
&mut helpers::app_with_file(file.path())?,
Some(&command),
None,
false,
@ -115,13 +97,7 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
let file = helpers::new_readonly_tempfile()?;
test_key_sequences(
&mut Application::new(
Args {
files: vec![(file.path().to_path_buf(), Position::default())],
..Default::default()
},
Config::default(),
)?,
&mut helpers::app_with_file(file.path())?,
vec![
(
None,