use super::*;
#[test]
fn test_insert_paste_enter_cases() {
use super::EditorAction::*;
let cases = [
(
"insert_character",
Insert('l'),
"helo",
(0, 2),
"hello",
None,
),
(
"insert_at_end",
Insert('!'),
"hello",
(0, 5),
"hello!",
None,
),
("paste", Paste("ll".into()), "heo", (0, 2), "hello", None),
(
"enter",
Enter,
"hello world",
(0, 5),
"hello\n world",
Some((1, 0)),
),
(
"multi_byte_character",
Insert('é'),
"héllo",
(0, 1),
"hééllo",
Some((0, 2)),
),
];
for &(name, ref action, input, (line, col), expected, expected_cursor) in &cases {
let buf = EditorBuffer::with_text(input, None);
buf.move_to(line, col);
buf.perform_action(action.clone());
assert_eq!(buf.text(), expected, "case {name}: text");
if let Some((line, col)) = expected_cursor {
let cursor = buf.cursor();
assert_eq!(cursor.line, line, "case {name}: line");
assert_eq!(cursor.column, col, "case {name}: col");
}
}
let cases = [
(
"insert_with_selection_replaces",
Insert('X'),
"hello world",
(0, 0),
None,
true,
"X",
),
(
"paste_with_selection_replaces",
Paste("there".into()),
"hello world",
(0, 6),
Some((0, 11)),
false,
"hello there",
),
(
"enter_with_selection_replaces",
Enter,
"hello",
(0, 3),
Some((0, 5)),
false,
"hel\n",
),
];
for &(name, ref action, input, (line, col), select_to, select_all, expected) in &cases {
let buf = EditorBuffer::with_text(input, None);
buf.move_to(line, col);
if select_all {
buf.select_all();
}
if let Some((line, col)) = select_to {
buf.perform_action(SelectTo { line, col });
}
buf.perform_action(action.clone());
assert_eq!(buf.text(), expected, "case {name}: text");
}
}
#[test]
fn test_enter_at_end_of_highlighted_file_with_trailing_newline() {
let buf = EditorBuffer::with_text("fn main() {}\n", Some(HighlightLanguage::Rust));
assert_eq!(buf.line_count(), 2);
let cursor_before = buf.cursor();
assert_eq!(cursor_before.line, 0);
assert_eq!(cursor_before.column, 0);
let content_len = "fn main() {}".chars().count();
buf.move_to(0, content_len);
buf.perform_action(EditorAction::Enter);
assert_eq!(buf.text(), "fn main() {}\n\n");
let cursor = buf.cursor();
assert_eq!(cursor.line, 1);
assert_eq!(cursor.column, 0);
}
#[test]
fn test_enter_at_end_of_highlighted_file_no_trailing_newline() {
let buf = EditorBuffer::with_text("fn main() {}", Some(HighlightLanguage::Rust));
assert_eq!(buf.line_count(), 1);
let content_len = "fn main() {}".chars().count();
buf.move_to(0, content_len);
buf.perform_action(EditorAction::Enter);
assert_eq!(buf.text(), "fn main() {}\n");
let cursor = buf.cursor();
assert_eq!(cursor.line, 1);
assert_eq!(cursor.column, 0);
}
#[test]
fn test_backspace_delete_cases() {
let cases = [
("backspace", "hello", Some((0, 5)), "hell", Some((0, 4))),
("backspace_at_start", "hello", None, "hello", None),
(
"backspace_newline",
"hello\nworld",
Some((1, 0)),
"helloworld",
Some((0, 5)),
),
];
for &(name, input, cursor_pos, expected, expected_cursor) in &cases {
let buf = EditorBuffer::with_text(input, None);
if let Some((line, col)) = cursor_pos {
buf.move_to(line, col);
}
buf.perform_action(EditorAction::Backspace);
assert_eq!(buf.text(), expected, "case {name}: text");
if let Some((line, col)) = expected_cursor {
let cursor = buf.cursor();
assert_eq!(cursor.line, line, "case {name}: line");
assert_eq!(cursor.column, col, "case {name}: col");
}
}
let cases = [
("delete", "hello", None, "ello"),
("delete_at_end", "hello", Some((0, 5)), "hello"),
("delete_cafe", "café", Some((0, 3)), "caf"),
("delete_cyrillic", "привет", Some((0, 0)), "ривет"),
("delete_emoji", "a🎉b", Some((0, 1)), "ab"),
];
for &(name, input, cursor_pos, expected) in &cases {
let buf = EditorBuffer::with_text(input, None);
if let Some((line, col)) = cursor_pos {
buf.move_to(line, col);
}
buf.perform_action(EditorAction::Delete);
assert_eq!(buf.text(), expected, "case {name}: text");
}
}
#[test]
fn test_char_col_to_byte_offset_multibyte() {
let line = "héllo";
assert_eq!(char_col_to_byte_offset_in_line(line, 0), 0);
assert_eq!(char_col_to_byte_offset_in_line(line, 1), 1);
assert_eq!(char_col_to_byte_offset_in_line(line, 2), 3);
assert_eq!(char_col_to_byte_range_in_line(line, 1), (1, 3));
}
#[test]
fn test_multiline_indent_preserves_selection() {
let buf = EditorBuffer::with_text("- item one\n- item two\n- item three", None);
buf.move_to(0, 0);
buf.perform_action(EditorAction::SelectTo { line: 1, col: 100 });
assert!(buf.cursor().selection.is_some());
buf.perform_action(EditorAction::Indent);
assert_eq!(buf.text(), "\t- item one\n\t- item two\n- item three");
assert!(
buf.cursor().selection.is_some(),
"selection should survive first indent"
);
assert_eq!(
buf.selection(),
Some("\t- item one\n\t- item two".to_string())
);
buf.perform_action(EditorAction::Indent);
assert_eq!(buf.text(), "\t\t- item one\n\t\t- item two\n- item three");
assert!(
buf.cursor().selection.is_some(),
"selection should survive second indent"
);
buf.perform_action(EditorAction::Unindent);
assert_eq!(buf.text(), "\t- item one\n\t- item two\n- item three");
assert!(
buf.cursor().selection.is_some(),
"selection should survive unindent"
);
}
#[test]
fn test_line_col_roundtrip() {
let text = "hello\nworld\nfoo";
for (line, line_text) in text.lines().enumerate() {
for (col, _) in line_text.chars().enumerate() {
let offset = line_col_to_byte_offset(text, line, col);
let (rl, rc) = byte_offset_to_line_col(text, offset);
assert_eq!(rl, line, "line mismatch at ({line},{col})");
assert_eq!(rc, col, "col mismatch at ({line},{col})");
}
let col = line_text.chars().count();
let offset = line_col_to_byte_offset(text, line, col);
let (rl, rc) = byte_offset_to_line_col(text, offset);
assert_eq!(rl, line, "line mismatch at end of line {line}");
assert_eq!(rc, col, "col mismatch at end of line {line}");
}
}
#[test]
fn test_selection_normalization_cases() {
type Setup = fn(&EditorBuffer);
let cases = [
(
"shift_left_at_bof_no_selection",
"hello",
(|buf| {
buf.perform_action(EditorAction::Move {
direction: CursorMove::Left,
select: true,
});
}) as Setup,
(0, 0),
None,
),
(
"shift_right_then_back_collapses_selection",
"hello",
(|buf| {
buf.move_to(0, 0);
buf.perform_action(EditorAction::Move {
direction: CursorMove::Right,
select: true,
});
buf.perform_action(EditorAction::Move {
direction: CursorMove::Left,
select: true,
});
}) as Setup,
(0, 0),
None,
),
(
"select_to_then_move_clears_selection",
"abcdef",
(|buf| {
buf.move_to(0, 1);
buf.perform_action(EditorAction::SelectTo { line: 0, col: 4 });
assert_eq!(
buf.selection(),
Some("bcd".to_string()),
"case select_to_then_move_clears_selection (selection before move)"
);
buf.move_to(0, 0);
}) as Setup,
(0, 0),
None,
),
];
for &(name, text, setup, (line, col), selection) in &cases {
let buf = EditorBuffer::with_text(text, None);
setup(&buf);
let cursor = buf.cursor();
assert_eq!(cursor.line, line, "case {name}: line");
assert_eq!(cursor.column, col, "case {name}: col");
assert_eq!(
buf.selection(),
selection.map(str::to_string),
"case {name}: selection"
);
}
}
#[test]
fn test_toggle_line_comment() {
use super::highlight::HighlightLanguage::*;
let cases = [
("add", "hello", "// hello", 0),
("remove", "// hello", "hello", 0),
("remove_with_space", "// hello", " hello", 0),
("preserves_whitespace", " hello", " // hello", 0),
(
"preserves_neighbor_lines",
"first\nsecond\nthird",
"first\n// second\nthird",
1,
),
];
for &(name, input, expected, cursor_line) in &cases {
let buf = EditorBuffer::with_text(input, Some(Rust));
if cursor_line != 0 {
buf.move_to(cursor_line, 0);
}
buf.perform_action(EditorAction::ToggleLineComment);
assert_eq!(buf.text(), expected, "case {name}: text");
}
let cases = [
("noop_unknown", "hello", "hello", None, None),
("rust_hash", "hello", "# hello", Some(Python), None),
("yaml_via_extension", "hello", "# hello", None, Some("yaml")),
(
"unknown_extension_noop",
"hello",
"hello",
None,
Some("xyz"),
),
];
for &(name, input, expected, language, file_ext) in &cases {
let buf = EditorBuffer::with_text(input, language);
if let Some(ext) = file_ext {
buf.set_file_extension(Some(ext));
}
buf.perform_action(EditorAction::ToggleLineComment);
assert_eq!(buf.text(), expected, "case {name}: text");
}
}
#[test]
fn test_jump_to_matching_bracket() {
let cases = [
("forward_paren", "(hello)", 1, 0, 6),
("backward_paren", "(hello)", 6, 0, 1),
("square_bracket", "[hello]", 1, 0, 6),
("brace", "{hello}", 1, 0, 6),
("none", "hello", 3, 0, 3),
];
for &(name, input, cursor_col, expected_line, expected_col) in &cases {
let buf = EditorBuffer::with_text(input, None);
buf.move_to(0, cursor_col);
buf.perform_action(EditorAction::JumpToMatchingBracket);
let cursor = buf.cursor();
assert_eq!(cursor.line, expected_line, "case {name}: line");
assert_eq!(cursor.column, expected_col, "case {name}: col");
}
}
#[test]
fn test_delete_line_cases() {
let lines = "line1\nline2\nline3";
let cases = [
("current_line", lines, 1, None, "line1\nline3"),
("first_line", lines, 0, None, "line2\nline3"),
("last_line", lines, 2, None, "line1\nline2"),
("single_line", "hello", 0, None, ""),
("selected_lines", "a\nb\nc\nd\ne", 1, Some((3, 0)), "a\ne"),
];
for &(name, input, cursor_line, select_to, expected) in &cases {
let buf = EditorBuffer::with_text(input, None);
buf.move_to(cursor_line, 0);
if let Some((line, col)) = select_to {
buf.perform_action(EditorAction::SelectTo { line, col });
}
buf.perform_action(EditorAction::DeleteLine);
assert_eq!(buf.text(), expected, "case {name}: text");
let cursor = buf.cursor();
let line = cursor_line.min(expected.split('\n').count() - 1);
assert_eq!(cursor.line, line, "case {name}: line");
assert_eq!(cursor.column, 0, "case {name}: col");
}
}
#[test]
fn test_duplicate_line_cases() {
let lines = "hello\nworld";
let cases = [
("current_line", lines, 0, None, "hello\nhello\nworld"),
("last_line", lines, 1, None, "hello\nworld\nworld"),
(
"selected_lines",
"a\nb\nc\nd",
1,
Some((2, 0)),
"a\nb\nc\nb\nc\nd",
),
];
for &(name, input, cursor_line, select_to, expected) in &cases {
let buf = EditorBuffer::with_text(input, None);
buf.move_to(cursor_line, 0);
if let Some((line, col)) = select_to {
buf.perform_action(EditorAction::SelectTo { line, col });
}
buf.perform_action(EditorAction::DuplicateLine);
assert_eq!(buf.text(), expected, "case {name}: text");
let duplicated = select_to.map_or(1, |(end, _)| end - cursor_line + 1);
let cursor = buf.cursor();
assert_eq!(cursor.line, cursor_line + duplicated, "case {name}: line");
assert_eq!(cursor.column, 0, "case {name}: col");
}
}
#[test]
fn test_move_line_cases() {
use super::EditorAction::*;
let cases = [
("up", MoveLineUp, "a\nb\nc", 1, None, "b\na\nc", Some(0)),
("down", MoveLineDown, "a\nb\nc", 1, None, "a\nc\nb", Some(2)),
("up_at_top", MoveLineUp, "a\nb", 0, None, "a\nb", None),
(
"down_at_bottom",
MoveLineDown,
"a\nb",
1,
None,
"a\nb",
None,
),
(
"selected_down",
MoveLineDown,
"a\nb\nc\nd",
1,
Some((2, 0)),
"a\nd\nb\nc",
Some(2),
),
];
for &(name, ref action, input, cursor_line, select_to, expected, expected_line) in &cases {
let buf = EditorBuffer::with_text(input, None);
buf.move_to(cursor_line, 0);
if let Some((line, col)) = select_to {
buf.perform_action(SelectTo { line, col });
}
buf.perform_action(action.clone());
assert_eq!(buf.text(), expected, "case {name}: text");
if let Some(line) = expected_line {
let cursor = buf.cursor();
assert_eq!(cursor.line, line, "case {name}: line");
assert_eq!(cursor.column, 0, "case {name}: col");
}
}
}
#[test]
fn test_has_trailing_newline_and_detect_line_ending() {
assert!(has_trailing_newline("hello\n"));
assert!(!has_trailing_newline("hello"));
assert!(!has_trailing_newline(""));
assert_eq!(detect_line_ending("hello\nworld\n"), LineEnding::Lf);
assert_eq!(detect_line_ending("hello\r\nworld\r\n"), LineEnding::Crlf);
}
#[test]
fn test_line_helpers_preserve_crlf_on_move_down() {
let text = "a\r\nb\r\nc";
let mut lines = logical_lines(text);
swap_lines_with_endings(&mut lines, 1, 2);
fix_line_endings(
&mut lines,
has_trailing_newline(text),
detect_line_ending(text),
);
assert_eq!(reassemble_lines(&lines), "a\r\nc\r\nb");
}
#[test]
fn test_line_helpers_preserve_trailing_blank_line() {
let text = "line one\nline two\n\n";
let lines = logical_lines(text);
assert_eq!(lines[2].0, "");
assert_eq!(lines[2].1, "\n");
assert_eq!(reassemble_lines(&lines), text);
}
#[test]
fn test_select_to_duplicate_endpoint_cases() {
let cases = [
("duplicate_endpoint", "hello world", 0, 5, "hello"),
("same_endpoint", "hello", 2, 3, "l"),
];
for &(name, text, start_col, end_col, expected) in &cases {
let buf = EditorBuffer::with_text(text, None);
buf.move_to(0, start_col);
buf.perform_action(EditorAction::SelectTo {
line: 0,
col: end_col,
});
assert!(
buf.cursor().selection.is_some(),
"case {name}: first SelectTo"
);
buf.perform_action(EditorAction::SelectTo {
line: 0,
col: end_col,
});
assert_eq!(
buf.selection(),
Some(expected.to_string()),
"case {name}: duplicate SelectTo"
);
}
}
#[test]
fn test_input_method_preedit_reported() {
let buf = EditorBuffer::with_text("", None);
let widget = EditorWidget::new(&buf);
let state = EditorWidgetState {
preedit: Some(input_method::Preedit {
content: "你好".to_string(),
selection: Some(1..2),
text_size: None,
}),
..EditorWidgetState::default()
};
let bounds = Rectangle::new(Point::ORIGIN, Size::new(200.0, 200.0));
let ime = widget.input_method(&state, bounds);
assert!(ime.to_owned().is_enabled());
match ime {
input_method::InputMethod::Enabled {
preedit: Some(p), ..
} => {
assert_eq!(p.content, "你好");
assert_eq!(p.selection, Some(1..2));
}
other => panic!("expected Enabled with preedit, got {other:?}"),
}
}
#[test]
fn test_cursor_rect_selection_anchor() {
let buf = EditorBuffer::with_text("hello\nworld", None);
buf.move_to(0, 3);
buf.perform_action(EditorAction::SelectTo { line: 1, col: 4 });
let buffer = with_font_system(|font_sys| {
let mut b = buf.borrow_buffer_mut();
reshape_and_shape(&mut b, font_sys, Some(0.0), 0.0, 200.0, 200.0);
b.clone()
});
let geo = TextGeometry {
clip: Rectangle::new(Point::ORIGIN, Size::new(200.0, 200.0)),
x: 8.0,
y: 8.0,
};
let rect = ime_caret_rect(&buffer, &geo, &buf);
let expected = cursor_rect(&buffer, &geo, 0, 3, geo.x);
assert_eq!(rect, expected);
assert!(rect.y < cursor_rect(&buffer, &geo, 1, 4, geo.x).y);
}
#[test]
fn test_masked_render_buffer_builds_dots_without_reborrow() {
let buf = EditorBuffer::with_text("secret\npassword", None);
let mbuffer = with_font_system(|font_sys| {
let buffer = buf.borrow_buffer_mut();
let mb = build_masked_render_buffer(font_sys, &buffer, false, 0.0, 0.0, 300.0, 100.0);
assert_eq!(buffer_text(&buffer), "secret\npassword");
mb
});
assert_eq!(buffer_text(&mbuffer), "••••••\n••••••••");
}
#[test]
fn test_masked_render_buffer_single_line_does_not_wrap() {
let buf = EditorBuffer::with_text("hunter2", None);
let mbuffer = with_font_system(|font_sys| {
let buffer = buf.borrow_buffer_mut();
build_masked_render_buffer(font_sys, &buffer, true, 0.0, 0.0, 100.0, 30.0)
});
assert_eq!(buffer_text(&mbuffer), "•••••••");
}
#[test]
fn test_cursor_follows_focus_state() {
let buf = EditorBuffer::with_text("", None);
let field = EditorWidget::new(&buf).single_line(true).id("field");
let state = EditorWidgetState::default();
assert!(!field.should_draw_cursor(&state));
let focused = EditorWidgetState {
is_focused: true,
..EditorWidgetState::default()
};
assert!(field.should_draw_cursor(&focused));
let masked = EditorWidget::new(&buf)
.single_line(true)
.masked(true)
.id("pw");
assert!(masked.should_draw_cursor(&focused));
let blurred = EditorWidgetState {
is_focused: true,
is_window_focused: false,
..EditorWidgetState::default()
};
assert!(!field.should_draw_cursor(&blurred));
let code = EditorWidget::new(&buf);
assert!(code.should_draw_cursor(&EditorWidgetState::default()));
assert!(
!code
.ignore_keyboard(true)
.should_draw_cursor(&EditorWidgetState::default())
);
assert!(
!EditorWidget::new(&buf)
.code_mode(false)
.should_draw_cursor(&EditorWidgetState::default())
);
}
#[test]
fn mouse_interaction_gated_on_cursor_bounds() {
let bounds = Rectangle::new(Point::new(10.0, 10.0), Size::new(100.0, 40.0));
let inside = mouse::Cursor::Available(Point::new(50.0, 30.0));
assert_eq!(
editor_mouse_interaction(inside, bounds),
mouse::Interaction::Text
);
let outside = mouse::Cursor::Available(Point::new(300.0, 300.0));
assert_eq!(
editor_mouse_interaction(outside, bounds),
mouse::Interaction::default()
);
let levitating = mouse::Cursor::Levitating(Point::new(50.0, 30.0));
assert_eq!(
editor_mouse_interaction(levitating, bounds),
mouse::Interaction::default()
);
assert_eq!(
editor_mouse_interaction(mouse::Cursor::Unavailable, bounds),
mouse::Interaction::default()
);
}