|
|
@ -5,7 +5,6 @@ use crossterm::event::{Event, KeyEvent};
|
|
|
|
use helix_core::{test, Selection, Transaction};
|
|
|
|
use helix_core::{test, Selection, Transaction};
|
|
|
|
use helix_term::{application::Application, args::Args, config::Config};
|
|
|
|
use helix_term::{application::Application, args::Args, config::Config};
|
|
|
|
use helix_view::{doc, input::parse_macro};
|
|
|
|
use helix_view::{doc, input::parse_macro};
|
|
|
|
use tokio::time::timeout;
|
|
|
|
|
|
|
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
|
|
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
@ -32,35 +31,48 @@ impl<S: Into<String>> From<(S, S, S)> for TestCase {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub async fn test_key_sequence(
|
|
|
|
pub async fn test_key_sequence(
|
|
|
|
app: &mut Application,
|
|
|
|
app: &mut Application,
|
|
|
|
in_keys: &str,
|
|
|
|
in_keys: &str,
|
|
|
|
test_fn: Option<&dyn Fn(&Application)>,
|
|
|
|
test_fn: Option<&dyn Fn(&Application)>,
|
|
|
|
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
|
|
|
|
test_key_sequences(app, vec![(in_keys, test_fn)], timeout).await
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn test_key_sequences(
|
|
|
|
|
|
|
|
app: &mut Application,
|
|
|
|
|
|
|
|
inputs: Vec<(&str, Option<&dyn Fn(&Application)>)>,
|
|
|
|
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
|
|
|
|
let timeout = timeout.unwrap_or(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);
|
|
|
|
|
|
|
|
|
|
|
|
for key_event in parse_macro(&in_keys)?.into_iter() {
|
|
|
|
for (in_keys, test_fn) in inputs {
|
|
|
|
tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
|
|
|
|
for key_event in parse_macro(&in_keys)?.into_iter() {
|
|
|
|
}
|
|
|
|
tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let mut rx_stream = UnboundedReceiverStream::new(rx);
|
|
|
|
let event_loop = app.event_loop(&mut rx_stream);
|
|
|
|
let event_loop = app.event_loop(&mut rx_stream);
|
|
|
|
let result = tokio::time::timeout(timeout, event_loop).await;
|
|
|
|
let result = timeout(Duration::from_millis(500), event_loop).await;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if result.is_ok() {
|
|
|
|
if result.is_ok() {
|
|
|
|
bail!("application exited before test function could run");
|
|
|
|
bail!("application exited before test function could run");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
for key_event in parse_macro("<esc>:q!<ret>")?.into_iter() {
|
|
|
|
tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
|
|
|
|
tx.send(Ok(Event::Key(KeyEvent::from(key_event))))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let event_loop = app.event_loop(&mut rx_stream);
|
|
|
|
let event_loop = app.event_loop(&mut rx_stream);
|
|
|
|
timeout(Duration::from_millis(5000), event_loop).await?;
|
|
|
|
tokio::time::timeout(timeout, event_loop).await?;
|
|
|
|
app.close().await?;
|
|
|
|
app.close().await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
@ -70,6 +82,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),
|
|
|
|
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let test_case = test_case.into();
|
|
|
|
let test_case = test_case.into();
|
|
|
|
let mut app =
|
|
|
|
let mut app =
|
|
|
@ -87,7 +100,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
|
|
|
|
view.id,
|
|
|
|
view.id,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
test_key_sequence(&mut app, &test_case.in_keys, Some(test_fn)).await
|
|
|
|
test_key_sequence(&mut app, &test_case.in_keys, Some(test_fn), timeout).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
|
|
|
@ -97,20 +110,26 @@ pub async fn test_key_sequence_text_result<T: Into<TestCase>>(
|
|
|
|
args: Args,
|
|
|
|
args: Args,
|
|
|
|
config: Config,
|
|
|
|
config: Config,
|
|
|
|
test_case: T,
|
|
|
|
test_case: T,
|
|
|
|
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let test_case = test_case.into();
|
|
|
|
let test_case = test_case.into();
|
|
|
|
let app = Application::new(args, config).unwrap();
|
|
|
|
let app = Application::new(args, config).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
timeout,
|
|
|
|
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|