use hjkl_engine::{Editor, Input, Key};
fn editor_with(content: &str) -> Editor {
let opts = hjkl_engine::Options::default();
let mut e = hjkl_vim::vim_editor(
hjkl_buffer::View::new(),
hjkl_engine::DefaultHost::new(),
opts,
);
e.set_content(content);
e
}
fn inp(key: Key) -> Input {
Input {
key,
ctrl: false,
alt: false,
shift: false,
}
}
fn dispatch_keys(e: &mut Editor, keys: &str) {
for c in keys.chars() {
hjkl_vim::dispatch_input(e, inp(Key::Char(c)));
}
}
fn buffer_text(e: &Editor) -> String {
let mut c = e.content();
if c.ends_with('\n') {
c.pop();
}
c
}
fn unnamed(e: &Editor) -> String {
e.with_registers(|r| r.unnamed.text.clone())
}
#[test]
fn dap_last_paragraph_takes_leading_blank() {
let mut e = editor_with("foo\n\nbar\n");
dispatch_keys(&mut e, "jjdap");
assert_eq!(buffer_text(&e), "foo\n");
assert_eq!(e.cursor(), (0, 0));
assert_eq!(unnamed(&e), "\nbar\n");
}
#[test]
fn dap_last_paragraph_absorbs_all_leading_blanks() {
let mut e = editor_with("foo\n\n\nbar\n");
dispatch_keys(&mut e, "3jdap");
assert_eq!(buffer_text(&e), "foo\n");
assert_eq!(e.cursor(), (0, 0));
assert_eq!(unnamed(&e), "\n\nbar\n");
}
#[test]
fn dap_middle_paragraph_still_takes_trailing_blank() {
let mut e = editor_with("foo\n\nbar\n\nbaz\n");
dispatch_keys(&mut e, "jjdap");
assert_eq!(buffer_text(&e), "foo\n\nbaz\n");
assert_eq!(e.cursor(), (2, 0));
assert_eq!(unnamed(&e), "bar\n\n");
}
#[test]
fn dip_last_paragraph_is_inner_only() {
let mut e = editor_with("foo\n\nbar\n");
dispatch_keys(&mut e, "jjdip");
assert_eq!(buffer_text(&e), "foo\n\n");
assert_eq!(e.cursor(), (1, 0));
assert_eq!(unnamed(&e), "bar\n");
}
#[test]
fn dap_single_paragraph_empties_to_empty_string() {
let mut e = editor_with("bar\n");
dispatch_keys(&mut e, "dap");
assert_eq!(buffer_text(&e), "");
assert_eq!(e.buffer().rope().to_string(), "");
assert_eq!(e.cursor(), (0, 0));
assert_eq!(unnamed(&e), "bar\n");
}
#[test]
fn vgd_captures_register_without_phantom_newline() {
let mut e = editor_with("one\ntwo\nthree\nfour\n");
dispatch_keys(&mut e, "VGd");
assert_eq!(unnamed(&e), "one\ntwo\nthree\nfour\n");
assert_eq!(buffer_text(&e), "");
assert_eq!(e.buffer().rope().to_string(), "");
assert_eq!(e.cursor(), (0, 0));
}
#[test]
fn visual_line_delete_through_eof_clamps_off_phantom_row() {
let mut e = editor_with("one\ntwo\nthree\nfour\n");
dispatch_keys(&mut e, "jVjjd");
assert_eq!(buffer_text(&e), "one\n");
assert_eq!(e.cursor(), (0, 0));
assert_eq!(unnamed(&e), "two\nthree\nfour\n");
}
#[test]
fn visual_line_delete_last_line_clamps_off_phantom_row() {
let mut e = editor_with("one\ntwo\nthree\nfour\n");
dispatch_keys(&mut e, "GVd");
assert_eq!(buffer_text(&e), "one\ntwo\nthree\n");
assert_eq!(e.cursor(), (2, 0));
assert_eq!(unnamed(&e), "four\n");
}
#[test]
fn visual_line_delete_non_eof_cursor_unchanged() {
let mut e = editor_with("one\ntwo\nthree\nfour\n");
dispatch_keys(&mut e, "jVjd");
assert_eq!(buffer_text(&e), "one\nfour\n");
assert_eq!(e.cursor(), (1, 0));
assert_eq!(unnamed(&e), "two\nthree\n");
}
#[test]
fn dg_empties_to_empty_string_not_newline() {
let mut e = editor_with("one\ntwo\nthree\nfour\n");
dispatch_keys(&mut e, "dG");
assert_eq!(e.buffer().rope().to_string(), "");
assert_eq!(buffer_text(&e), "");
assert_eq!(e.cursor(), (0, 0));
}