Skip to content

Commit

Permalink
Added unit tests for cursor moving through editor (ilai-deutel#270)
Browse files Browse the repository at this point in the history
* Add tests for cursor moving

* Fixed failing tests

* Fixed failing tests

* Fixed rust fmt

* Solved feedback
  • Loading branch information
BogdanPaul15 authored Jan 12, 2024
1 parent fc34ccb commit 9dd6c5f
Showing 1 changed file with 161 additions and 1 deletion.
162 changes: 161 additions & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ mod tests {
#[test]
fn editor_delete_char() {
let mut editor = Editor::default();
for b in "Hello world!".as_bytes() {
for b in b"Hello world!" {
editor.insert_byte(*b);
}
editor.delete_char();
Expand All @@ -924,4 +924,164 @@ mod tests {
editor.delete_char();
assert_eq!(editor.rows[0].chars, "Helo world".as_bytes());
}

#[test]
fn editor_move_cursor_left() {
let mut editor = Editor::default();
for b in b"Hello world!\nHappy New Year!" {
if *b == b'\n' {
editor.insert_new_line();
} else {
editor.insert_byte(*b);
}
}

// check current position
assert_eq!(editor.cursor.x, 15);
assert_eq!(editor.cursor.y, 1);

editor.move_cursor(&AKey::Left, true);
assert_eq!(editor.cursor.x, 10);
assert_eq!(editor.cursor.y, 1);

editor.move_cursor(&AKey::Left, false);
assert_eq!(editor.cursor.x, 9);
assert_eq!(editor.cursor.y, 1);

editor.move_cursor(&AKey::Left, true);
assert_eq!(editor.cursor.x, 6);
assert_eq!(editor.cursor.y, 1);

editor.move_cursor(&AKey::Left, true);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 1);

editor.move_cursor(&AKey::Left, false);
assert_eq!(editor.cursor.x, 12);
assert_eq!(editor.cursor.y, 0);

editor.move_cursor(&AKey::Left, true);
assert_eq!(editor.cursor.x, 6);
assert_eq!(editor.cursor.y, 0);

editor.move_cursor(&AKey::Left, true);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 0);

editor.move_cursor(&AKey::Left, false);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 0);
}

#[test]
fn editor_move_cursor_up() {
let mut editor = Editor::default();
for b in b"abcdefgh\nij\nklmnopqrstuvwxyz" {
if *b == b'\n' {
editor.insert_new_line();
} else {
editor.insert_byte(*b);
}
}

// check current position
assert_eq!(editor.cursor.x, 16);
assert_eq!(editor.cursor.y, 2);

editor.move_cursor(&AKey::Up, false);
assert_eq!(editor.cursor.x, 2);
assert_eq!(editor.cursor.y, 1);

editor.move_cursor(&AKey::Up, true);
assert_eq!(editor.cursor.x, 2);
assert_eq!(editor.cursor.y, 0);

editor.move_cursor(&AKey::Up, false);
assert_eq!(editor.cursor.x, 2);
assert_eq!(editor.cursor.y, 0);
}

#[test]
fn editor_move_cursor_right() {
let mut editor = Editor::default();
for b in b"Hello world\nHappy New Year" {
if *b == b'\n' {
editor.insert_new_line();
} else {
editor.insert_byte(*b);
}
}

// check current position
assert_eq!(editor.cursor.x, 14);
assert_eq!(editor.cursor.y, 1);

editor.move_cursor(&AKey::Right, false);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 2);

editor.move_cursor(&AKey::Right, false);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 2);

editor.move_cursor(&AKey::Up, true);
editor.move_cursor(&AKey::Up, true);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 0);

editor.move_cursor(&AKey::Right, true);
assert_eq!(editor.cursor.x, 5);
assert_eq!(editor.cursor.y, 0);

editor.move_cursor(&AKey::Right, true);
assert_eq!(editor.cursor.x, 11);
assert_eq!(editor.cursor.y, 0);
}

#[test]
fn editor_move_cursor_down() {
let mut editor = Editor::default();
for b in b"abcdefgh\nij\nklmnopqrstuvwxyz" {
if *b == b'\n' {
editor.insert_new_line();
} else {
editor.insert_byte(*b);
}
}

// check current position
assert_eq!(editor.cursor.x, 16);
assert_eq!(editor.cursor.y, 2);

editor.move_cursor(&AKey::Down, false);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 3);

editor.move_cursor(&AKey::Up, false);
editor.move_cursor(&AKey::Up, false);
editor.move_cursor(&AKey::Up, false);

assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 0);

editor.move_cursor(&AKey::Right, true);
assert_eq!(editor.cursor.x, 8);
assert_eq!(editor.cursor.y, 0);

editor.move_cursor(&AKey::Down, true);
assert_eq!(editor.cursor.x, 2);
assert_eq!(editor.cursor.y, 1);

editor.move_cursor(&AKey::Down, true);
assert_eq!(editor.cursor.x, 2);
assert_eq!(editor.cursor.y, 2);

editor.move_cursor(&AKey::Down, true);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 3);

editor.move_cursor(&AKey::Down, false);
assert_eq!(editor.cursor.x, 0);
assert_eq!(editor.cursor.y, 3);
}
}

0 comments on commit 9dd6c5f

Please sign in to comment.