From 98143ddc55a33c2ac2015521dae1086f9fa1bcc7 Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Thu, 27 Nov 2025 22:39:22 -0400 Subject: [PATCH] More test refactoring --- core/src/editor_buffer/mod.rs | 64 ++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/core/src/editor_buffer/mod.rs b/core/src/editor_buffer/mod.rs index 9a75195..6a227b0 100644 --- a/core/src/editor_buffer/mod.rs +++ b/core/src/editor_buffer/mod.rs @@ -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 = 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 = 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 = 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 = 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 = target .buffer .iter_chars()