More test refactoring

This commit is contained in:
Matthew Gordon 2025-11-27 22:39:22 -04:00
parent ea49d93e14
commit 98143ddc55
1 changed files with 33 additions and 31 deletions

View File

@ -142,10 +142,12 @@ mod tests {
use std::io::{Read, Seek, SeekFrom, Write};
use tempfile::NamedTempFile;
fn test_command(editor_buffer: EditorBuffer, command: Command) -> EditorBuffer {
let result = editor_buffer.execute(command);
assert!(result.is_ok());
result.buffer
macro_rules! test_command {
( $editor_buffer:ident, $command:expr ) => {
let result = $editor_buffer.execute($command);
assert!(result.is_ok());
let $editor_buffer = result.buffer;
}
}
fn create_simple_test_file() -> NamedTempFile {
@ -179,7 +181,7 @@ mod tests {
.read_to_string(&mut expected_contents)
.expect("Reading text file");
let target = EditorBuffer::new();
let target = test_command(target, Command::OpenFile(test_file.path().into()));
test_command!(target, Command::OpenFile(test_file.path().into()));
let found_contents: String = target.buffer.iter_chars().collect();
assert_eq!(expected_contents, found_contents);
}
@ -196,12 +198,12 @@ mod tests {
fn move_cursor_to_point_in_file() {
let test_file = create_simple_test_file();
let target = EditorBuffer::new();
let target = test_command(target, Command::OpenFile(test_file.path().into()));
let target = test_command(target, Command::MoveCursorTo(Point::LineColumn(0, 5)));
test_command!(target, Command::OpenFile(test_file.path().into()));
test_command!(target, Command::MoveCursorTo(Point::LineColumn(0, 5)));
assert_eq!(Point::LineColumn(0, 5), target.get_cursor_position());
let target = test_command(target, Command::MoveCursorTo(Point::LineColumn(3, 11)));
test_command!(target, Command::MoveCursorTo(Point::LineColumn(3, 11)));
assert_eq!(Point::LineColumn(3, 11), target.get_cursor_position());
let target = test_command(target, Command::MoveCursorTo(Point::LineColumn(3, 0)));
test_command!(target, Command::MoveCursorTo(Point::LineColumn(3, 0)));
assert_eq!(Point::LineColumn(3, 0), target.get_cursor_position());
}
@ -217,9 +219,9 @@ mod tests {
expected_lines[2] = "Xbíth a menmasam fri seilgg";
let expected_lines = expected_lines;
let target = EditorBuffer::new();
let target = test_command(target, Command::OpenFile(test_file.path().into()));
let target = test_command(target, Command::MoveCursorTo(Point::LineColumn(3, 0)));
let target = test_command(target, Command::InsertChar('X'));
test_command!(target, Command::OpenFile(test_file.path().into()));
test_command!(target, Command::MoveCursorTo(Point::LineColumn(3, 0)));
test_command!(target, Command::InsertChar('X'));
let found_lines: Vec<String> = target
.buffer
.iter_chars()
@ -239,9 +241,9 @@ mod tests {
expected_lines[2] = "bXíth a menmasam fri seilgg";
let expected_lines = expected_lines;
let target = EditorBuffer::new();
let target = test_command(target, Command::OpenFile(test_file.path().into()));
let target = test_command(target, Command::MoveCursorTo(Point::LineColumn(3, 1)));
let target = test_command(target, Command::InsertChar('X'));
test_command!(target, Command::OpenFile(test_file.path().into()));
test_command!(target, Command::MoveCursorTo(Point::LineColumn(3, 1)));
test_command!(target, Command::InsertChar('X'));
let found_lines: Vec<String> = target
.buffer
.iter_chars()
@ -261,9 +263,9 @@ mod tests {
expected_lines[2] = "bíXth a menmasam fri seilgg";
let expected_lines = expected_lines;
let target = EditorBuffer::new();
let target = test_command(target, Command::OpenFile(test_file.path().into()));
let target = test_command(target, Command::MoveCursorTo(Point::LineColumn(3, 2)));
let target = test_command(target, Command::InsertChar('X'));
test_command!(target, Command::OpenFile(test_file.path().into()));
test_command!(target, Command::MoveCursorTo(Point::LineColumn(3, 2)));
test_command!(target, Command::InsertChar('X'));
let found_lines: Vec<String> = target
.buffer
.iter_chars()
@ -283,9 +285,9 @@ mod tests {
expected_lines[2] = "bíth a menmXasam fri seilgg";
let expected_lines = expected_lines;
let target = EditorBuffer::new();
let target = test_command(target, Command::OpenFile(test_file.path().into()));
let target = test_command(target, Command::MoveCursorTo(Point::LineColumn(3, 11)));
let target = test_command(target, Command::InsertChar('X'));
test_command!(target, Command::OpenFile(test_file.path().into()));
test_command!(target, Command::MoveCursorTo(Point::LineColumn(3, 11)));
test_command!(target, Command::InsertChar('X'));
let found_lines: Vec<String> = target
.buffer
.iter_chars()
@ -308,16 +310,16 @@ mod tests {
expected_lines.insert(6, "abc 123".into());
let target = EditorBuffer::new();
let target = test_command(target, Command::OpenFile(test_file.path().into()));
let target = test_command(target, Command::MoveCursorToLine(7));
let target = test_command(target, Command::InsertChar('a'));
let target = test_command(target, Command::InsertChar('b'));
let target = test_command(target, Command::InsertChar('c'));
let target = test_command(target, Command::InsertChar(' '));
let target = test_command(target, Command::InsertChar('1'));
let target = test_command(target, Command::InsertChar('2'));
let target = test_command(target, Command::InsertChar('3'));
let target = test_command(target, Command::InsertChar('\n'));
test_command!(target, Command::OpenFile(test_file.path().into()));
test_command!(target, Command::MoveCursorToLine(7));
test_command!(target, Command::InsertChar('a'));
test_command!(target, Command::InsertChar('b'));
test_command!(target, Command::InsertChar('c'));
test_command!(target, Command::InsertChar(' '));
test_command!(target, Command::InsertChar('1'));
test_command!(target, Command::InsertChar('2'));
test_command!(target, Command::InsertChar('3'));
test_command!(target, Command::InsertChar('\n'));
let found_lines: Vec<String> = target
.buffer
.iter_chars()