use super::*;
fn line(marked: &str) -> LineEdit {
let mut edit = LineEdit::new();
let (mut caret, mut anchor) = (None, None);
let mut count = 0;
for c in marked.chars() {
match c {
'|' => caret = Some(count),
'[' => anchor = Some(count),
_ => {
edit.text.push(c);
count += 1;
}
}
}
edit.caret = caret.expect("a marked line has a caret");
edit.anchor = anchor.unwrap_or(edit.caret);
edit
}
fn marked(edit: &LineEdit) -> String {
let mut out = String::new();
for (index, c) in edit.text.chars().chain(['\0']).enumerate() {
if index == edit.anchor && edit.anchor != edit.caret {
out.push('[');
}
if index == edit.caret {
out.push('|');
}
if c != '\0' {
out.push(c);
}
}
out
}
const fn mv(motion: Motion) -> Edit {
Edit::Move {
motion,
select: false,
}
}
const fn sel(motion: Motion) -> Edit {
Edit::Move {
motion,
select: true,
}
}
type Row = (Edit, [&'static str; 3], [Option<ClipboardOp>; 3]);
#[test]
fn every_edit_against_every_selection_state() {
const STATES: [&str; 3] = ["one t|wo", "o[ne t|wo", "o|ne t]wo"];
let offer = |text: &str| Some(ClipboardOp::Offer(text.to_owned()));
let table: Vec<Row> = vec![
(
Edit::Insert("X".to_owned()),
["one tX|wo", "oX|wo", "oX|wo"],
[None, None, None],
),
(
mv(Motion::Left),
["one |two", "o|ne two", "o|ne two"],
[None, None, None],
),
(
mv(Motion::Right),
["one tw|o", "one t|wo", "one t|wo"],
[None, None, None],
),
(
mv(Motion::WordLeft),
["one |two", "|one two", "|one two"],
[None, None, None],
),
(
mv(Motion::WordRight),
["one two|", "one two|", "one two|"],
[None, None, None],
),
(
mv(Motion::Home),
["|one two", "|one two", "|one two"],
[None, None, None],
),
(
mv(Motion::End),
["one two|", "one two|", "one two|"],
[None, None, None],
),
(
sel(Motion::Left),
["one |t[wo", "o[ne |two", "|one t[wo"],
[None, None, None],
),
(
sel(Motion::Right),
["one t[w|o", "o[ne tw|o", "on|e t[wo"],
[None, None, None],
),
(
sel(Motion::WordLeft),
["one |t[wo", "o[ne |two", "|one t[wo"],
[None, None, None],
),
(
sel(Motion::WordRight),
["one t[wo|", "o[ne two|", "one| t[wo"],
[None, None, None],
),
(
sel(Motion::Home),
["|one t[wo", "|o[ne two", "|one t[wo"],
[None, None, None],
),
(
sel(Motion::End),
["one t[wo|", "o[ne two|", "one t[wo|"],
[None, None, None],
),
(
Edit::Backspace,
["one |wo", "o|wo", "o|wo"],
[None, None, None],
),
(
Edit::Delete,
["one t|o", "o|wo", "o|wo"],
[None, None, None],
),
(
Edit::SelectAll,
["[one two|", "[one two|", "[one two|"],
[None, None, None],
),
(Edit::Copy, STATES, [None, offer("ne t"), offer("ne t")]),
(
Edit::Cut,
["one t|wo", "o|wo", "o|wo"],
[None, offer("ne t"), offer("ne t")],
),
(
Edit::Paste,
STATES,
[
Some(ClipboardOp::Read),
Some(ClipboardOp::Read),
Some(ClipboardOp::Read),
],
),
];
let read = |state: &str| line(&state.replace(']', "["));
for (edit, after, clipboard) in table {
for ((state, want), want_clipboard) in STATES.iter().zip(after).zip(clipboard) {
let mut model = read(state);
let before = model.clone();
let applied = model.apply(&edit);
assert_eq!(marked(&model), marked(&read(want)), "{edit:?} on `{state}`");
assert_eq!(
applied.clipboard, want_clipboard,
"{edit:?} on `{state}` asked the clipboard for the wrong thing"
);
assert_eq!(
applied.text,
model.text != before.text,
"{edit:?} on `{state}` misreported a text change"
);
assert_eq!(
applied.caret,
(model.caret, model.anchor) != (before.caret, before.anchor),
"{edit:?} on `{state}` misreported a caret move"
);
}
}
}
#[test]
fn moves_and_deletions_stop_at_the_ends_of_the_line() {
let mut start = line("|ab");
for edit in [
mv(Motion::Left),
mv(Motion::WordLeft),
mv(Motion::Home),
Edit::Backspace,
] {
let applied = start.apply(&edit);
assert_eq!(marked(&start), "|ab", "{edit:?} at the start");
assert_eq!(applied, Applied::default(), "{edit:?} reported work");
}
let mut end = line("ab|");
for edit in [
mv(Motion::Right),
mv(Motion::WordRight),
mv(Motion::End),
Edit::Delete,
] {
let applied = end.apply(&edit);
assert_eq!(marked(&end), "ab|", "{edit:?} at the end");
assert_eq!(applied, Applied::default(), "{edit:?} reported work");
}
}
#[test]
fn word_moves_stop_at_the_edges_of_runs_of_one_class() {
let text: Vec<char> = " foo_1.bar -- baz".chars().collect();
let mut stops = Vec::new();
let mut at = 0;
loop {
let next = word_right(&text, at);
if next == at {
break;
}
stops.push(next);
at = next;
}
assert_eq!(stops, [7, 8, 11, 15, 19]);
let mut stops = Vec::new();
let mut at = text.len();
loop {
let next = word_left(&text, at);
if next == at {
break;
}
stops.push(next);
at = next;
}
assert_eq!(stops, [16, 13, 8, 7, 2, 0]);
assert_eq!(run_at(&text, 3), 2..7, "inside `foo_1`");
assert_eq!(run_at(&text, 7), 7..8, "the `.`");
assert_eq!(run_at(&text, 12), 11..13, "the spaces after `bar`");
assert_eq!(run_at(&text, 99), 16..19, "past the end is the last word");
assert_eq!(run_at(&[], 0), 0..0);
}
#[test]
fn multi_byte_and_combining_text_is_edited_by_char() {
let mut precomposed = line("caf\u{e9}|");
assert!(precomposed.apply(&Edit::Backspace).text);
assert_eq!(precomposed.text(), "caf");
let mut combining = line("cafe\u{301}|");
assert_eq!(combining.len(), 5, "the accent is a char of its own");
assert!(combining.apply(&mv(Motion::Left)).caret);
assert_eq!(
combining.caret(),
4,
"the caret stopped between e and its accent"
);
assert!(combining.apply(&Edit::Delete).text);
assert_eq!(
combining.text(),
"cafe",
"Delete took the accent and nothing else"
);
let mut cjk = line("|\u{65e5}\u{672c}\u{8a9e} \u{1f600}x");
cjk.apply(&sel(Motion::WordRight));
assert_eq!(cjk.selected_text(), "\u{65e5}\u{672c}\u{8a9e}");
cjk.apply(&Edit::Insert("\u{fc}".to_owned()));
assert_eq!(cjk.text(), "\u{fc} \u{1f600}x");
cjk.apply(&mv(Motion::WordRight));
assert_eq!(cjk.caret(), 3, "the emoji is a run of its own");
cjk.apply(&sel(Motion::Left));
assert_eq!(cjk.selected_text(), "\u{1f600}");
let applied = cjk.apply(&Edit::Cut);
assert_eq!(
applied.clipboard,
Some(ClipboardOp::Offer("\u{1f600}".to_owned()))
);
assert_eq!(marked(&cjk), "\u{fc} |x");
}
#[test]
fn a_paste_replaces_the_selection_and_drops_line_breaks() {
let mut edit = line("say [hello| world");
assert!(edit.insert("good\r\nbye"));
assert_eq!(marked(&edit), "say goodbye| world");
let mut edit = line("[ab|");
assert!(!edit.insert("\n\t"));
assert_eq!(marked(&edit), "[ab|");
}
#[test]
fn sync_takes_an_outside_edit_and_holds_the_caret_inside_it() {
let mut edit = line("abc[def|");
assert!(!edit.sync("abcdef"));
assert_eq!(marked(&edit), "abc[def|");
assert!(edit.sync("ab"));
assert_eq!(marked(&edit), "ab|");
assert!(edit.sync("abcd"));
assert_eq!(marked(&edit), "ab|cd", "the caret moved with a longer line");
}
#[test]
fn keys_become_edits() {
let none = Modifiers::empty();
let shift = Modifiers::SHIFT;
assert_eq!(
Edit::for_key(KeyCode::ArrowLeft, none),
Some(mv(Motion::Left))
);
assert_eq!(
Edit::for_key(KeyCode::ArrowRight, shift),
Some(sel(Motion::Right))
);
for word in [Modifiers::CTRL, Modifiers::ALT] {
assert_eq!(
Edit::for_key(KeyCode::ArrowLeft, word | shift),
Some(sel(Motion::WordLeft))
);
assert_eq!(
Edit::for_key(KeyCode::ArrowRight, word),
Some(mv(Motion::WordRight))
);
}
assert_eq!(
Edit::for_key(KeyCode::ArrowLeft, Modifiers::SUPER),
Some(mv(Motion::Home))
);
assert_eq!(Edit::for_key(KeyCode::End, shift), Some(sel(Motion::End)));
assert_eq!(Edit::for_key(KeyCode::Home, none), Some(mv(Motion::Home)));
assert_eq!(
Edit::for_key(KeyCode::Backspace, none),
Some(Edit::Backspace)
);
assert_eq!(Edit::for_key(KeyCode::Delete, none), Some(Edit::Delete));
for shortcut in [Modifiers::CTRL, Modifiers::SUPER] {
assert_eq!(
Edit::for_key(KeyCode::KeyA, shortcut),
Some(Edit::SelectAll)
);
assert_eq!(Edit::for_key(KeyCode::KeyC, shortcut), Some(Edit::Copy));
assert_eq!(Edit::for_key(KeyCode::KeyX, shortcut), Some(Edit::Cut));
assert_eq!(Edit::for_key(KeyCode::KeyV, shortcut), Some(Edit::Paste));
}
assert_eq!(Edit::for_key(KeyCode::KeyV, none), None, "a bare V types");
assert_eq!(
Edit::for_key(KeyCode::KeyC, Modifiers::CTRL | Modifiers::ALT),
None,
"AltGr+C types a character on many layouts"
);
for key in [
KeyCode::Enter,
KeyCode::Escape,
KeyCode::Tab,
KeyCode::Space,
] {
assert_eq!(Edit::for_key(key, none), None, "{key} is not an edit");
}
}