Lines
100 %
Functions
Branches
use super::*;
fn area(text: &str) -> Editor {
Editor::with_buffer(text).multiline(true)
}
fn at(text: &str, cursor: usize) -> Editor {
let mut editor = area(text);
editor.cursor = cursor;
editor
fn numbered(count: usize) -> String {
(0..count)
.map(|i| format!("l{i}"))
.collect::<Vec<_>>()
.join("\n")
#[test]
fn typing_newlines_builds_several_lines() {
let mut editor = Editor::default().multiline(true);
for c in "ab".chars() {
editor.insert_char(c);
editor.insert_newline();
editor.insert_char('c');
assert_eq!(editor.buffer(), "ab\nc");
assert_eq!(editor.text_lines(), ["ab", "c"]);
fn a_single_line_editor_ignores_the_newline_and_moves_as_before() {
let mut editor = Editor::with_buffer("ab\ncd");
editor.move_up();
editor.page_down();
assert_eq!(editor.buffer(), "ab\ncd");
assert_eq!(editor.cursor(), 5);
editor.move_home();
assert_eq!(editor.cursor(), 0);
editor.move_end();
fn up_and_down_keep_the_column_and_clamp_on_short_lines() {
let mut editor = at("abcdef\nab\nabcdef", 5);
editor.move_down();
assert_eq!(editor.cursor(), 9);
assert_eq!(editor.cursor(), 12);
assert_eq!(editor.cursor(), 2);
fn up_on_the_first_and_down_on_the_last_line_stay_put() {
let mut editor = at("ab\ncd", 1);
assert_eq!(editor.cursor(), 1);
assert_eq!(editor.cursor(), 4);
fn home_and_end_go_to_the_line_edges() {
let mut editor = at("ab\ncde\nf", 4);
assert_eq!(editor.cursor(), 3);
assert_eq!(editor.cursor(), 6);
fn wide_characters_keep_the_display_column() {
let mut editor = at("日本語\nabcdef", 2);
assert_eq!(editor.cursor(), 4 + 4);
fn a_target_wide_character_straddling_the_column_is_not_entered() {
let mut editor = at("abc\n日本", 3);
fn multi_byte_characters_count_as_one_cursor_step() {
let mut editor = at("é\nüber", 1);
assert_eq!(editor.cursor_position(), (1, 1));
fn the_lines_and_the_cursor_position_follow_the_text() {
let mut editor = at(&numbered(50), 0);
assert_eq!(editor.text_lines().len(), 50);
assert_eq!(editor.text_lines()[49], "l49");
assert_eq!(editor.cursor_position(), (0, 0));
editor.move_right();
assert_eq!(
at(&numbered(50), numbered(50).chars().count()).cursor_position(),
(49, 3)
);
fn page_keys_move_a_window_at_a_time() {
assert_eq!(editor.cursor_position().0, TEXT_AREA_ROWS);
editor.page_up();
fn the_scroll_is_kept_between_reads_and_a_clone_has_its_own() {
let editor = area("a");
assert_eq!(editor.viewport(), tvui::Viewport::default());
editor.set_viewport(tvui::Viewport { top: 2, left: 3 });
assert_eq!(editor.viewport(), tvui::Viewport { top: 2, left: 3 });
let copy = editor.clone();
copy.set_viewport(tvui::Viewport { top: 7, left: 0 });
fn a_loaded_multiline_text_starts_with_the_cursor_at_its_top() {
let editor = area(&numbered(50));
fn cursor_positions_count_crlf_lines_once() {
let editor = at("ab\r\ncd", 5);
assert_eq!(editor.text_lines(), ["ab", "cd"]);
fn moving_without_editing_leaves_the_text_byte_for_byte() {
for text in [numbered(50), numbered(50).replace('\n', "\r\n")] {
let mut editor = area(&text);
for _ in 0..3 {
editor.move_left();
assert_eq!(editor.buffer(), text);
fn the_cursor_never_rests_inside_a_crlf() {
let mut editor = at("ab\r\ncd", 2);
fn deleting_across_a_crlf_removes_both_characters() {
let mut editor = at("ab\r\ncd", 4);
editor.delete_backward();
assert_eq!(editor.buffer(), "abcd");
editor.delete_forward();
fn a_typed_newline_is_lf_in_a_crlf_text() {
assert_eq!(editor.buffer(), "ab\n\r\ncd");
fn the_text_area_flag_survives_a_reload_of_its_text() {
let mut widget = crate::widgets::Widget::Text(Editor::default().multiline(true));
widget.set_text("a\nb");
assert_eq!(widget.kind(), crate::widgets::WidgetKind::TextArea);
fn kill_to_end_stops_at_the_line_end() {
let mut editor = at("abcd\nef", 2);
editor.kill_to_end();
assert_eq!(editor.buffer(), "ab\nef");
fn kill_to_end_at_a_line_end_joins_the_next_line() {
for (text, cursor) in [("ab\ncd", 2), ("ab\r\ncd", 2)] {
let mut editor = at(text, cursor);
assert_eq!(editor.buffer(), "abcd", "{text:?}");
fn kill_to_end_on_the_last_line_deletes_the_rest_and_then_nothing() {
let mut editor = at("ab\ncdef", 5);
fn kill_to_end_of_a_single_line_editor_is_unchanged() {
editor.cursor = 1;
assert_eq!(editor.buffer(), "a");
fn set_cursor_position_puts_the_cursor_on_a_line_and_column() {
for text in ["ab\ncdef\ngh", "ab\r\ncdef\r\ngh"] {
let mut editor = at(text, 0);
editor.set_cursor_position((1, 3));
assert_eq!(editor.cursor_position(), (1, 3), "{text:?}");
editor.set_cursor_position((0, 9));
assert_eq!(editor.cursor_position(), (0, 2), "{text:?}");
editor.set_cursor_position((9, 1));
assert_eq!(editor.cursor_position(), (2, 1), "{text:?}");