mirror of https://github.com/helix-editor/helix
refactor helpers, use new test helpers
parent
0f3c10a021
commit
84bbe6b8f3
|
@ -1,92 +1,22 @@
|
||||||
#[cfg(feature = "integration")]
|
#[cfg(feature = "integration")]
|
||||||
mod integration {
|
mod integration {
|
||||||
|
mod helpers;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use helix_core::{syntax::AutoPairConfig, Position, Selection, Transaction};
|
use helix_core::{syntax::AutoPairConfig, Position, Selection};
|
||||||
use helix_term::{application::Application, args::Args, config::Config};
|
use helix_term::{args::Args, config::Config};
|
||||||
use helix_view::{doc, input::parse_macro};
|
|
||||||
|
|
||||||
use crossterm::event::{Event, KeyEvent};
|
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
|
||||||
pub struct TestCase {
|
use self::helpers::*;
|
||||||
pub in_text: String,
|
|
||||||
pub in_selection: Selection,
|
|
||||||
pub in_keys: String,
|
|
||||||
pub out_text: String,
|
|
||||||
pub out_selection: Selection,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_key_sequence(
|
|
||||||
app: Option<Application>,
|
|
||||||
test_case: &TestCase,
|
|
||||||
test_fn: &dyn Fn(&mut Application),
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let mut app =
|
|
||||||
app.unwrap_or_else(|| Application::new(Args::default(), Config::default()).unwrap());
|
|
||||||
|
|
||||||
let (view, doc) = helix_view::current!(app.editor);
|
|
||||||
let sel = doc.selection(view.id).clone();
|
|
||||||
|
|
||||||
// replace the initial text with the input text
|
|
||||||
doc.apply(
|
|
||||||
&Transaction::change_by_selection(&doc.text(), &sel, |_| {
|
|
||||||
(0, doc.text().len_chars(), Some((&test_case.in_text).into()))
|
|
||||||
})
|
|
||||||
.with_selection(test_case.in_selection.clone()),
|
|
||||||
view.id,
|
|
||||||
);
|
|
||||||
|
|
||||||
let input_keys = parse_macro(&test_case.in_keys)?
|
|
||||||
.into_iter()
|
|
||||||
.map(|key_event| Event::Key(KeyEvent::from(key_event)));
|
|
||||||
|
|
||||||
for key in input_keys {
|
|
||||||
app.handle_terminal_events(Ok(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
test_fn(&mut app);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Use this for very simple test cases where there is one input
|
|
||||||
/// document, selection, and sequence of key presses, and you just
|
|
||||||
/// want to verify the resulting document and selection.
|
|
||||||
fn test_key_sequence_text_result(
|
|
||||||
args: Args,
|
|
||||||
config: Config,
|
|
||||||
test_case: TestCase,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let app = Application::new(args, config).unwrap();
|
|
||||||
|
|
||||||
test_key_sequence(Some(app), &test_case, &|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);
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn hello_world() -> anyhow::Result<()> {
|
async fn hello_world() -> anyhow::Result<()> {
|
||||||
test_key_sequence_text_result(
|
test_key_sequence_text_result(
|
||||||
Args::default(),
|
Args::default(),
|
||||||
Config::default(),
|
Config::default(),
|
||||||
TestCase {
|
("#[\n|]#", "ihello world<esc>", "hello world#[|\n]#"),
|
||||||
in_text: "\n".into(),
|
|
||||||
in_selection: Selection::single(0, 1),
|
|
||||||
// TODO: fix incorrect selection on new doc
|
|
||||||
in_keys: "ihello world<esc>".into(),
|
|
||||||
out_text: "hello world\n".into(),
|
|
||||||
out_selection: Selection::single(12, 11),
|
|
||||||
},
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -109,42 +39,79 @@ mod integration {
|
||||||
test_key_sequence_text_result(
|
test_key_sequence_text_result(
|
||||||
Args::default(),
|
Args::default(),
|
||||||
Config::default(),
|
Config::default(),
|
||||||
TestCase {
|
("#[\n|]#", "i", "#[|\n]#"),
|
||||||
in_text: "\n".into(),
|
|
||||||
in_selection: Selection::single(0, 1),
|
|
||||||
in_keys: "i".into(),
|
|
||||||
out_text: "\n".into(),
|
|
||||||
out_selection: Selection::single(1, 0),
|
|
||||||
},
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
test_key_sequence_text_result(
|
test_key_sequence_text_result(
|
||||||
Args::default(),
|
Args::default(),
|
||||||
Config::default(),
|
Config::default(),
|
||||||
TestCase {
|
("#[\n|]#", "i<esc>", "#[|\n]#"),
|
||||||
in_text: "\n".into(),
|
)?;
|
||||||
in_selection: Selection::single(0, 1),
|
|
||||||
in_keys: "i<esc>i".into(),
|
test_key_sequence_text_result(
|
||||||
out_text: "\n".into(),
|
Args::default(),
|
||||||
out_selection: Selection::single(1, 0),
|
Config::default(),
|
||||||
},
|
("#[\n|]#", "i<esc>i", "#[|\n]#"),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Range direction is preserved when escaping insert mode to normal
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> {
|
async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> {
|
||||||
test_key_sequence_text_result(
|
test_key_sequence_text_result(
|
||||||
Args::default(),
|
Args::default(),
|
||||||
Config::default(),
|
Config::default(),
|
||||||
TestCase {
|
("#[f|]#oo\n", "vll<A-;><esc>", "#[|foo]#\n"),
|
||||||
in_text: "\n".into(),
|
)?;
|
||||||
in_selection: Selection::single(0, 1),
|
|
||||||
in_keys: "i".into(),
|
test_key_sequence_text_result(
|
||||||
out_text: "\n".into(),
|
Args::default(),
|
||||||
out_selection: Selection::single(1, 0),
|
Config::default(),
|
||||||
},
|
(
|
||||||
|
indoc! {"\
|
||||||
|
#[f|]#oo
|
||||||
|
#(b|)#ar"
|
||||||
|
},
|
||||||
|
"vll<A-;><esc>",
|
||||||
|
indoc! {"\
|
||||||
|
#[|foo]#
|
||||||
|
#(|bar)#"
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
test_key_sequence_text_result(
|
||||||
|
Args::default(),
|
||||||
|
Config::default(),
|
||||||
|
(
|
||||||
|
indoc! {"\
|
||||||
|
#[f|]#oo
|
||||||
|
#(b|)#ar"
|
||||||
|
},
|
||||||
|
"a",
|
||||||
|
indoc! {"\
|
||||||
|
#[fo|]#o
|
||||||
|
#(ba|)#r"
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
test_key_sequence_text_result(
|
||||||
|
Args::default(),
|
||||||
|
Config::default(),
|
||||||
|
(
|
||||||
|
indoc! {"\
|
||||||
|
#[f|]#oo
|
||||||
|
#(b|)#ar"
|
||||||
|
},
|
||||||
|
"a<esc>",
|
||||||
|
indoc! {"\
|
||||||
|
#[f|]#oo
|
||||||
|
#(b|)#ar"
|
||||||
|
},
|
||||||
|
),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -155,13 +122,7 @@ mod integration {
|
||||||
test_key_sequence_text_result(
|
test_key_sequence_text_result(
|
||||||
Args::default(),
|
Args::default(),
|
||||||
Config::default(),
|
Config::default(),
|
||||||
TestCase {
|
("#[\n|]#", "i(<esc>", "(#[|)]#\n"),
|
||||||
in_text: "\n".into(),
|
|
||||||
in_selection: Selection::single(0, 1),
|
|
||||||
in_keys: "i(<esc>".into(),
|
|
||||||
out_text: "()\n".into(),
|
|
||||||
out_selection: Selection::single(2, 1),
|
|
||||||
},
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
test_key_sequence_text_result(
|
test_key_sequence_text_result(
|
||||||
|
@ -173,39 +134,30 @@ mod integration {
|
||||||
},
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
TestCase {
|
("#[\n|]#", "i(<esc>", "(#[|\n]#"),
|
||||||
in_text: "\n".into(),
|
|
||||||
in_selection: Selection::single(0, 1),
|
|
||||||
in_keys: "i(<esc>".into(),
|
|
||||||
out_text: "(\n".into(),
|
|
||||||
out_selection: Selection::single(2, 1),
|
|
||||||
},
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn auto_indent_rs() -> anyhow::Result<()> {
|
async fn auto_indent_c() -> anyhow::Result<()> {
|
||||||
test_key_sequence_text_result(
|
test_key_sequence_text_result(
|
||||||
Args {
|
Args {
|
||||||
files: vec![(PathBuf::from("foo.c"), Position::default())],
|
files: vec![(PathBuf::from("foo.c"), Position::default())],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
Config::default(),
|
Config::default(),
|
||||||
TestCase {
|
// switches to append mode?
|
||||||
in_text: "void foo() {}\n".into(),
|
(
|
||||||
in_selection: Selection::single(13, 12),
|
"void foo() {#[|}]#\n",
|
||||||
in_keys: "i<ret><esc>".into(),
|
"i<ret><esc>",
|
||||||
out_text: indoc! {r#"
|
indoc! {"\
|
||||||
void foo() {
|
void foo() {
|
||||||
|
#[|\n]#\
|
||||||
}
|
}
|
||||||
"#}
|
"},
|
||||||
.trim_start()
|
),
|
||||||
.into(),
|
|
||||||
out_selection: Selection::single(16, 15),
|
|
||||||
},
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
use crossterm::event::{Event, KeyEvent};
|
||||||
|
use helix_core::{test, Selection, Transaction};
|
||||||
|
use helix_term::{application::Application, args::Args, config::Config};
|
||||||
|
use helix_view::{doc, input::parse_macro};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct TestCase {
|
||||||
|
pub in_text: String,
|
||||||
|
pub in_selection: Selection,
|
||||||
|
pub in_keys: String,
|
||||||
|
pub out_text: String,
|
||||||
|
pub out_selection: Selection,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: Into<String>> From<(S, S, S)> for TestCase {
|
||||||
|
fn from((input, keys, output): (S, S, S)) -> Self {
|
||||||
|
let (in_text, in_selection) = test::print(&input.into());
|
||||||
|
let (out_text, out_selection) = test::print(&output.into());
|
||||||
|
|
||||||
|
TestCase {
|
||||||
|
in_text,
|
||||||
|
in_selection,
|
||||||
|
in_keys: keys.into(),
|
||||||
|
out_text,
|
||||||
|
out_selection,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_key_sequence<T: Into<TestCase>>(
|
||||||
|
app: Option<Application>,
|
||||||
|
test_case: T,
|
||||||
|
test_fn: &dyn Fn(&mut Application),
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let test_case = test_case.into();
|
||||||
|
let mut app =
|
||||||
|
app.unwrap_or_else(|| Application::new(Args::default(), Config::default()).unwrap());
|
||||||
|
|
||||||
|
let (view, doc) = helix_view::current!(app.editor);
|
||||||
|
let sel = doc.selection(view.id).clone();
|
||||||
|
|
||||||
|
// replace the initial text with the input text
|
||||||
|
doc.apply(
|
||||||
|
&Transaction::change_by_selection(&doc.text(), &sel, |_| {
|
||||||
|
(0, doc.text().len_chars(), Some((&test_case.in_text).into()))
|
||||||
|
})
|
||||||
|
.with_selection(test_case.in_selection.clone()),
|
||||||
|
view.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
let input_keys = parse_macro(&test_case.in_keys)?
|
||||||
|
.into_iter()
|
||||||
|
.map(|key_event| Event::Key(KeyEvent::from(key_event)));
|
||||||
|
|
||||||
|
for key in input_keys {
|
||||||
|
app.handle_terminal_events(Ok(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
test_fn(&mut app);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Use this for very simple test cases where there is one input
|
||||||
|
/// document, selection, and sequence of key presses, and you just
|
||||||
|
/// want to verify the resulting document and selection.
|
||||||
|
pub fn test_key_sequence_text_result<T: Into<TestCase>>(
|
||||||
|
args: Args,
|
||||||
|
config: Config,
|
||||||
|
test_case: T,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let test_case = test_case.into();
|
||||||
|
let app = Application::new(args, config).unwrap();
|
||||||
|
|
||||||
|
test_key_sequence(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);
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue