From c5cbc1e75ca9ffadd78cc175c91ebee39fbf9de9 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 23 Dec 2024 19:20:23 -0500 Subject: [PATCH 01/15] wip: test coverage for copy paste --- src/ui/mod.rs | 2 +- src/ui/test.rs | 152 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 137 insertions(+), 17 deletions(-) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index b80aff0..956b91d 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -692,7 +692,7 @@ impl<'ws> Workspace<'ws> { KeyCode::Char('C') if key .modifiers - .contains(KeyModifiers::CONTROL | KeyModifiers::SHIFT) => + .contains(KeyModifiers::CONTROL) => { self.state.clipboard = Some(ClipboardContents::Cell( self.book.get_current_cell_rendered()?, diff --git a/src/ui/test.rs b/src/ui/test.rs index 72e4280..2fb4c21 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -1,3 +1,5 @@ +use std::process::ExitCode; + use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crate::ui::{Address, Modality}; @@ -192,6 +194,35 @@ fn test_cmd_rename_sheet_with_idx_and_name() { assert_eq!(cmd, Cmd::RenameSheet(Some(0), "test")); } +#[derive(Default)] +pub struct InputScript{ + events: Vec +} + +impl InputScript { + pub fn add_char(self, c: char) -> Self { + self.add_event(construct_key_event(KeyCode::Char(c))) + } + + pub fn add_modified_char(self, c: char, mods: KeyModifiers) -> Self { + self.add_event(construct_modified_key_event(KeyCode::Char(c), mods)) + } + + pub fn add_event(mut self, evt: Event) -> Self { + self.events.push(evt); + self + } + + pub fn run(self, ws: &mut Workspace) -> anyhow::Result> { + for evt in self.events { + if let Some(e) = ws.handle_input(evt)? { + return Ok(Some(e)); + } + } + Ok(None) + } +} + fn construct_key_event(code: KeyCode) -> Event { construct_modified_key_event(code, KeyModifiers::empty()) } @@ -322,15 +353,33 @@ fn test_navigation_numeric_prefix() assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); - ws.handle_input(construct_key_event(KeyCode::Char('2'))) - .expect("Failed to handle '3' key event"); - ws.handle_input(construct_key_event(KeyCode::Char('3'))) - .expect("Failed to handle '3' key event"); - ws.handle_input(construct_key_event(KeyCode::Char('9'))) - .expect("Failed to handle '3' key event"); + InputScript::default() + .add_char('2') + .add_char('3') + .add_char('9') + .run(&mut ws) + .expect("Failed to run script"); assert_eq!(239, ws.state.get_n_prefix()); } +#[test] +fn test_navigation_numeric_prefix_cancel() +{ + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); + ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); + InputScript::default() + .add_char('2') + .add_char('3') + .add_char('9') + .add_event(construct_key_event(KeyCode::Esc)) + .run(&mut ws) + .expect("Failed to run script"); + assert_eq!(1, ws.state.get_n_prefix()); +} + #[test] fn test_navigation_tab_next_numeric_prefix() { @@ -430,16 +479,87 @@ fn test_gg_movement() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Char('j'))) - .expect("Failed to handle 'e' key event"); - ws.handle_input(construct_key_event(KeyCode::Char('j'))) - .expect("Failed to handle 'e' key event"); + InputScript::default() + .add_char('j') + .add_char('j').run(&mut ws) + .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 3, col: 1 }); - ws.handle_input(construct_key_event(KeyCode::Char('l'))) - .expect("Failed to handle 'e' key event"); - ws.handle_input(construct_key_event(KeyCode::Char('g'))) - .expect("Failed to handle 'e' key event"); - ws.handle_input(construct_key_event(KeyCode::Char('g'))) - .expect("Failed to handle 'e' key event"); + InputScript::default() + .add_char('l') + .add_char('g') + .add_char('g') + .run(&mut ws) + .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 1, col: 2 }); } + +macro_rules! assert_copy_paste { + ($c: expr, $p: expr, $source: expr,) => { + assert_copy_paste!($c, $p, $source, $source) + }; + ($c: expr, $p: expr, $source: expr) => { + assert_copy_paste!($c, $p, $source, $source) + }; + ($c: expr, $p: expr, $source: expr, $expected: expr,) => { + assert_copy_paste!($c, $p, $source, $expected) + }; + ($c: expr, $p: expr, $source: expr, $expected: expr) => {{ + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + InputScript::default() + .add_char('j') + .add_char('l') + .run(&mut ws) + .expect("Failed to run script"); + ws.book.edit_current_cell($source).expect("Failed to edit cell"); + ws.book.evaluate(); + InputScript::default() + .add_event($c) + .add_char('l') + .add_char('j') + .add_event($p) + .run(&mut ws) + .expect("Failed to run script"); + let copy = ws.book.get_current_cell_contents() + .expect("Failed to get cell contents"); + assert_eq!(copy, $expected); + }}; +} + +#[test] +fn test_y_p_copy_paste() { + assert_copy_paste!( + construct_key_event(KeyCode::Char('y')), + construct_key_event(KeyCode::Char('p')), + "foo", + ); +} + +#[test] +fn test_traditional_copy_paste() { + assert_copy_paste!( + construct_modified_key_event(KeyCode::Char('c'), KeyModifiers::CONTROL), + construct_modified_key_event(KeyCode::Char('v'), KeyModifiers::CONTROL), + "foo", + ); +} + +#[test] +fn test_y_p_copy_paste_rendered() { + assert_copy_paste!( + construct_key_event(KeyCode::Char('Y')), + construct_key_event(KeyCode::Char('p')), + "=1+2", + "3", + ); +} + +#[test] +fn test_traditional_copy_paste_rendered() { + assert_copy_paste!( + construct_modified_key_event(KeyCode::Char('C'), KeyModifiers::CONTROL), + construct_modified_key_event(KeyCode::Char('v'), KeyModifiers::CONTROL), + "=1+2", + "3", + ); +} From 52d19098680315e5993d5fa6abe11f3684eb38c2 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sat, 28 Dec 2024 10:27:50 -0500 Subject: [PATCH 02/15] wip: add test for cell deletion --- src/ui/test.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/ui/test.rs b/src/ui/test.rs index 2fb4c21..f4f1b10 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -563,3 +563,34 @@ fn test_traditional_copy_paste_rendered() { "3", ); } + +#[test] +fn test_clear_cell() { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + ws.book.edit_current_cell("foo") + .expect("failed to edit cell"); + ws.book.evaluate(); + assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents")); + InputScript::default() + .add_char('d') + .run(&mut ws) + .expect("Failed to run input script"); + assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); +} + +#[test] +fn test_clear_cell_all() { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + ws.book.edit_current_cell("foo") + .expect("failed to edit cell"); + ws.book.evaluate(); + assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents")); + InputScript::default() + .add_char('D') + .run(&mut ws) + .expect("Failed to run input script"); + assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); +} + From e984d7324c6516893d6b43f8b26c712fbf3b5e9a Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sat, 28 Dec 2024 10:49:28 -0500 Subject: [PATCH 03/15] wip: test coverage for * sheet navigation * column sizing * quit --- src/ui/test.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/ui/test.rs b/src/ui/test.rs index f4f1b10..d76cfdd 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -594,3 +594,51 @@ fn test_clear_cell_all() { assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); } +#[test] +fn test_sheet_navigation() { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + ws.book.new_sheet(Some("sheet 2")).expect("Failed to set sheet name"); + ws.book.new_sheet(Some("sheet 3")).expect("Failed to set sheet name"); + ws.book.new_sheet(Some("sheet 4")).expect("Failed to set sheet name"); + InputScript::default() + .add_modified_char('n', KeyModifiers::CONTROL) + .add_modified_char('n', KeyModifiers::CONTROL) + .run(&mut ws) + .expect("Failed to run input script"); + assert_eq!("sheet 3", ws.book.get_sheet_name().expect("Failed to get sheet name")); + InputScript::default() + .add_modified_char('p', KeyModifiers::CONTROL) + .run(&mut ws) + .expect("Failed to run input script"); + assert_eq!("sheet 2", ws.book.get_sheet_name().expect("Failed to get sheet name")); +} + +#[test] +fn test_sheet_column_sizing() { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + InputScript::default() + .add_char('3') + .add_modified_char('l', KeyModifiers::CONTROL) + .run(&mut ws) + .expect("Failed to run input script"); + assert_eq!(28, ws.book.get_col_size(1).expect("Failed to get column size")); + InputScript::default() + .add_char('1') + .add_modified_char('h', KeyModifiers::CONTROL) + .run(&mut ws) + .expect("Failed to run input script"); + assert_eq!(27, ws.book.get_col_size(1).expect("Failed to get column size")); +} + +#[test] +fn test_quit() { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let result = InputScript::default() + .add_char('q') + .run(&mut ws) + .expect("Failed to run input script"); + assert!(result.is_some()); +} From 247674530b1facbf0304dd51b6c7b8f65f2f64af Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 29 Dec 2024 19:09:42 -0500 Subject: [PATCH 04/15] chore: refator InputScript api for ergonomics --- src/ui/test.rs | 92 ++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/src/ui/test.rs b/src/ui/test.rs index d76cfdd..01ecccc 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -200,19 +200,31 @@ pub struct InputScript{ } impl InputScript { - pub fn add_char(self, c: char) -> Self { - self.add_event(construct_key_event(KeyCode::Char(c))) + pub fn char(self, c: char) -> Self { + self.event(construct_key_event(KeyCode::Char(c))) } - pub fn add_modified_char(self, c: char, mods: KeyModifiers) -> Self { - self.add_event(construct_modified_key_event(KeyCode::Char(c), mods)) + pub fn ctrl(self, c: char) -> Self { + self.modified_char(c, KeyModifiers::CONTROL) } - pub fn add_event(mut self, evt: Event) -> Self { + pub fn modified_char(self, c: char, mods: KeyModifiers) -> Self { + self.event(construct_modified_key_event(KeyCode::Char(c), mods)) + } + + pub fn event(mut self, evt: Event) -> Self { self.events.push(evt); self } + pub fn enter(self) -> Self { + self.event(construct_key_event(KeyCode::Enter)) + } + + pub fn esc(self) -> Self { + self.event(construct_key_event(KeyCode::Esc)) + } + pub fn run(self, ws: &mut Workspace) -> anyhow::Result> { for evt in self.events { if let Some(e) = ws.handle_input(evt)? { @@ -354,9 +366,9 @@ fn test_navigation_numeric_prefix() ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); InputScript::default() - .add_char('2') - .add_char('3') - .add_char('9') + .char('2') + .char('3') + .char('9') .run(&mut ws) .expect("Failed to run script"); assert_eq!(239, ws.state.get_n_prefix()); @@ -371,10 +383,10 @@ fn test_navigation_numeric_prefix_cancel() ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); InputScript::default() - .add_char('2') - .add_char('3') - .add_char('9') - .add_event(construct_key_event(KeyCode::Esc)) + .char('2') + .char('3') + .char('9') + .esc() .run(&mut ws) .expect("Failed to run script"); assert_eq!(1, ws.state.get_n_prefix()); @@ -436,18 +448,18 @@ fn test_range_copy() { let original_loc_2 = ws.book.location.clone(); assert_eq!(Address { row: 5, col: 5 }, original_loc_2); - ws.handle_input(construct_modified_key_event(KeyCode::Char('r'), KeyModifiers::CONTROL)) - .expect("Failed to handle 'Ctrl-r' key event"); + InputScript::default().char('v').run(&mut ws) + .expect("Failed to handle 'v' key event"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); assert_eq!(Some(original_loc_2.clone()), ws.state.range_select.original_location); - assert!(ws.state.range_select.start.is_none()); + assert!(ws.state.range_select.start.is_some()); assert!(ws.state.range_select.end.is_none()); ws.handle_input(construct_key_event(KeyCode::Char('h'))) .expect("Failed to handle 'h' key event"); ws.handle_input(construct_key_event(KeyCode::Char(' '))) .expect("Failed to handle ' ' key event"); - assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.start); + assert_eq!(Some(Address {row:5, col: 5, }), ws.state.range_select.start); ws.handle_input(construct_key_event(KeyCode::Char('k'))) .expect("Failed to handle 'k' key event"); @@ -455,9 +467,9 @@ fn test_range_copy() { .expect("Failed to handle ' ' key event"); assert!(ws.state.range_select.original_location.is_none()); - assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.start); - assert_eq!(Some(Address {row:4, col:4, }), ws.state.range_select.end); - assert_eq!(original_loc_2, ws.book.location); + assert_eq!(Some(Address {row:5, col:5, }), ws.state.range_select.start); + assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.end); + assert_eq!(Address {row:4, col:5, }, ws.book.location); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); } @@ -480,14 +492,14 @@ fn test_gg_movement() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() - .add_char('j') - .add_char('j').run(&mut ws) + .char('j') + .char('j').run(&mut ws) .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 3, col: 1 }); InputScript::default() - .add_char('l') - .add_char('g') - .add_char('g') + .char('l') + .char('g') + .char('g') .run(&mut ws) .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 1, col: 2 }); @@ -507,17 +519,17 @@ macro_rules! assert_copy_paste { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); InputScript::default() - .add_char('j') - .add_char('l') + .char('j') + .char('l') .run(&mut ws) .expect("Failed to run script"); ws.book.edit_current_cell($source).expect("Failed to edit cell"); ws.book.evaluate(); InputScript::default() - .add_event($c) - .add_char('l') - .add_char('j') - .add_event($p) + .event($c) + .char('l') + .char('j') + .event($p) .run(&mut ws) .expect("Failed to run script"); let copy = ws.book.get_current_cell_contents() @@ -573,7 +585,7 @@ fn test_clear_cell() { ws.book.evaluate(); assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents")); InputScript::default() - .add_char('d') + .char('d') .run(&mut ws) .expect("Failed to run input script"); assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); @@ -588,7 +600,7 @@ fn test_clear_cell_all() { ws.book.evaluate(); assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents")); InputScript::default() - .add_char('D') + .char('D') .run(&mut ws) .expect("Failed to run input script"); assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); @@ -602,13 +614,13 @@ fn test_sheet_navigation() { ws.book.new_sheet(Some("sheet 3")).expect("Failed to set sheet name"); ws.book.new_sheet(Some("sheet 4")).expect("Failed to set sheet name"); InputScript::default() - .add_modified_char('n', KeyModifiers::CONTROL) - .add_modified_char('n', KeyModifiers::CONTROL) + .ctrl('n') + .ctrl('n') .run(&mut ws) .expect("Failed to run input script"); assert_eq!("sheet 3", ws.book.get_sheet_name().expect("Failed to get sheet name")); InputScript::default() - .add_modified_char('p', KeyModifiers::CONTROL) + .ctrl('p') .run(&mut ws) .expect("Failed to run input script"); assert_eq!("sheet 2", ws.book.get_sheet_name().expect("Failed to get sheet name")); @@ -619,14 +631,14 @@ fn test_sheet_column_sizing() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); InputScript::default() - .add_char('3') - .add_modified_char('l', KeyModifiers::CONTROL) + .char('3') + .ctrl('l') .run(&mut ws) .expect("Failed to run input script"); assert_eq!(28, ws.book.get_col_size(1).expect("Failed to get column size")); InputScript::default() - .add_char('1') - .add_modified_char('h', KeyModifiers::CONTROL) + .char('1') + .ctrl('h') .run(&mut ws) .expect("Failed to run input script"); assert_eq!(27, ws.book.get_col_size(1).expect("Failed to get column size")); @@ -637,7 +649,7 @@ fn test_quit() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let result = InputScript::default() - .add_char('q') + .char('q') .run(&mut ws) .expect("Failed to run input script"); assert!(result.is_some()); From 92b02eea7828ac5ab17723bf20175eacff43acd9 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 29 Dec 2024 19:09:42 -0500 Subject: [PATCH 05/15] wip: test coverage for cell replacement --- src/ui/test.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/ui/test.rs b/src/ui/test.rs index 01ecccc..c0fcd29 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -654,3 +654,23 @@ fn test_quit() { .expect("Failed to run input script"); assert!(result.is_some()); } + +#[test] +fn test_cell_replace() { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + ws.book.edit_current_cell("foo").expect("Failed to edit current cell"); + assert_eq!("foo", + ws.book.get_current_cell_contents().expect("failed to get cell contents").as_str()); + InputScript::default() + .char('s') + .char('b') + .char('a') + .char('r') + .enter() + .run(&mut ws) + .expect("Failed to run input script"); + assert_eq!("bar", + ws.book.get_current_cell_contents().expect("failed to get cell contents").as_str()); +} + From 7bbdc26f13654cabbbb473d7241b65deaf71e76a Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 29 Dec 2024 19:09:42 -0500 Subject: [PATCH 06/15] chore: makefile adjustments --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7488fc1..e7b1196 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ build: $(rust-files) cargo build tarpaulin-report.%: $(rust_files) - cargo tarpaulin --skip-clean --test --out $* + cargo tarpaulin --skip-clean --engine llvm --out $* cover: tarpaulin-report.html From babe2525a0d6728aa95e7e4b9ab831962efee444 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 29 Dec 2024 19:09:42 -0500 Subject: [PATCH 07/15] chore: Move InputScript to the top add alt helper method --- src/ui/test.rs | 114 +++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 55 deletions(-) diff --git a/src/ui/test.rs b/src/ui/test.rs index c0fcd29..bc1686f 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -7,6 +7,59 @@ use crate::ui::{Address, Modality}; use super::cmd::{parse, Cmd}; use super::Workspace; +#[derive(Default)] +pub struct InputScript{ + events: Vec +} + +impl InputScript { + pub fn char(self, c: char) -> Self { + self.event(construct_key_event(KeyCode::Char(c))) + } + + pub fn ctrl(self, c: char) -> Self { + self.modified_char(c, KeyModifiers::CONTROL) + } + + pub fn alt(self, c: char) -> Self { + self.modified_char(c, KeyModifiers::ALT) + } + + pub fn modified_char(self, c: char, mods: KeyModifiers) -> Self { + self.event(construct_modified_key_event(KeyCode::Char(c), mods)) + } + + pub fn event(mut self, evt: Event) -> Self { + self.events.push(evt); + self + } + + pub fn enter(self) -> Self { + self.event(construct_key_event(KeyCode::Enter)) + } + + pub fn esc(self) -> Self { + self.event(construct_key_event(KeyCode::Esc)) + } + + pub fn run(self, ws: &mut Workspace) -> anyhow::Result> { + for evt in self.events { + if let Some(e) = ws.handle_input(evt)? { + return Ok(Some(e)); + } + } + Ok(None) + } +} + +fn construct_key_event(code: KeyCode) -> Event { + construct_modified_key_event(code, KeyModifiers::empty()) +} + +fn construct_modified_key_event(code: KeyCode, mods: KeyModifiers) -> Event { + Event::Key(KeyEvent::new(code, mods)) +} + #[test] fn test_write_cmd() { let input = "write foo.xlsx"; @@ -194,55 +247,6 @@ fn test_cmd_rename_sheet_with_idx_and_name() { assert_eq!(cmd, Cmd::RenameSheet(Some(0), "test")); } -#[derive(Default)] -pub struct InputScript{ - events: Vec -} - -impl InputScript { - pub fn char(self, c: char) -> Self { - self.event(construct_key_event(KeyCode::Char(c))) - } - - pub fn ctrl(self, c: char) -> Self { - self.modified_char(c, KeyModifiers::CONTROL) - } - - pub fn modified_char(self, c: char, mods: KeyModifiers) -> Self { - self.event(construct_modified_key_event(KeyCode::Char(c), mods)) - } - - pub fn event(mut self, evt: Event) -> Self { - self.events.push(evt); - self - } - - pub fn enter(self) -> Self { - self.event(construct_key_event(KeyCode::Enter)) - } - - pub fn esc(self) -> Self { - self.event(construct_key_event(KeyCode::Esc)) - } - - pub fn run(self, ws: &mut Workspace) -> anyhow::Result> { - for evt in self.events { - if let Some(e) = ws.handle_input(evt)? { - return Ok(Some(e)); - } - } - Ok(None) - } -} - -fn construct_key_event(code: KeyCode) -> Event { - construct_modified_key_event(code, KeyModifiers::empty()) -} - -fn construct_modified_key_event(code: KeyCode, mods: KeyModifiers) -> Event { - Event::Key(KeyEvent::new(code, mods)) -} - #[test] fn test_input_navitation_enter_key() { let mut ws = @@ -308,8 +312,8 @@ fn test_edit_mode_help_keycode() { .expect("Failed to handle 'i' key"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); let edit_help = ws.render_help_text(); - ws.handle_input(construct_modified_key_event(KeyCode::Char('h'), KeyModifiers::ALT)) - .expect("Failed to handle 'ctrl-?' key event"); + InputScript::default().alt('h').run(&mut ws) + .expect("Failed to handle 'alt-h' key event"); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(edit_help, ws.state.popup); } @@ -320,8 +324,8 @@ fn test_navigation_mode_help_keycode() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); let help_text = ws.render_help_text(); - ws.handle_input(construct_modified_key_event(KeyCode::Char('h'), KeyModifiers::ALT)) - .expect("Failed to handle 'ctrl-?' key event"); + InputScript::default().alt('h').run(&mut ws) + .expect("Failed to handle 'alt-h' key event"); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(help_text, ws.state.popup); } @@ -335,8 +339,8 @@ fn test_command_mode_help_keycode() { .expect("Failed to handle ':' key"); assert_eq!(Some(&Modality::Command), ws.state.modality_stack.last()); let edit_help = ws.render_help_text(); - ws.handle_input(construct_modified_key_event(KeyCode::Char('h'), KeyModifiers::ALT)) - .expect("Failed to handle 'ctrl-?' key event"); + InputScript::default().alt('h').run(&mut ws) + .expect("Failed to handle 'alt-h' key event"); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(edit_help, ws.state.popup); } From 282df181775ca1d96c9a30851db6323504eafae3 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 29 Dec 2024 19:09:42 -0500 Subject: [PATCH 08/15] wip: test coverage for all movement keys --- src/ui/test.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/ui/test.rs b/src/ui/test.rs index bc1686f..b58de75 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -509,6 +509,26 @@ fn test_gg_movement() { assert_eq!(ws.book.location, Address { row: 1, col: 2 }); } +#[test] +fn test_h_j_k_l_movement() { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + InputScript::default() + .char('2') + .char('j') + .char('l').run(&mut ws) + .expect("failed to handle event sequence"); + assert_eq!(ws.book.location, Address { row: 3, col: 2 }); + InputScript::default() + .char('2') + .char('h') + .char('k') + .run(&mut ws) + .expect("failed to handle event sequence"); + assert_eq!(ws.book.location, Address { row: 1, col: 1 }); +} + macro_rules! assert_copy_paste { ($c: expr, $p: expr, $source: expr,) => { assert_copy_paste!($c, $p, $source, $source) From 19d01f057bfda2781c04ecfa4a048fa6fc6fcfbe Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 29 Dec 2024 19:23:30 -0500 Subject: [PATCH 09/15] chore: cleanup the rest of out inputscript usage --- src/ui/test.rs | 72 +++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/src/ui/test.rs b/src/ui/test.rs index b58de75..ae4792b 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -24,6 +24,10 @@ impl InputScript { pub fn alt(self, c: char) -> Self { self.modified_char(c, KeyModifiers::ALT) } + + pub fn tab(self) -> Self { + self.event(construct_key_event(KeyCode::Tab)) + } pub fn modified_char(self, c: char, mods: KeyModifiers) -> Self { self.event(construct_modified_key_event(KeyCode::Char(c), mods)) @@ -253,7 +257,7 @@ fn test_input_navitation_enter_key() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let row = ws.book.location.row; assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Enter)) + InputScript::default().enter().run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(row + 1, ws.book.location.row); } @@ -264,7 +268,7 @@ fn test_input_navitation_tab_key() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let col = dbg!(ws.book.location.col); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Tab)) + InputScript::default().tab().run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(col + 1, ws.book.location.col); } @@ -275,13 +279,13 @@ fn test_input_navitation_shift_enter_key() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let row = ws.book.location.row; assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Enter)) + InputScript::default().enter().run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(row + 1, ws.book.location.row); - ws.handle_input(construct_modified_key_event( + InputScript::default().event(construct_modified_key_event( KeyCode::Enter, KeyModifiers::SHIFT, - )) + )).run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(row, ws.book.location.row); } @@ -292,13 +296,13 @@ fn test_input_navitation_shift_tab_key() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let col = dbg!(ws.book.location.col); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Tab)) + InputScript::default().tab().run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(col + 1, ws.book.location.col); - ws.handle_input(construct_modified_key_event( + InputScript::default().event(construct_modified_key_event( KeyCode::Tab, KeyModifiers::SHIFT, - )) + )).run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(col, ws.book.location.col); } @@ -308,7 +312,7 @@ fn test_edit_mode_help_keycode() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Char('i'))) + InputScript::default().char('i').run(&mut ws) .expect("Failed to handle 'i' key"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); let edit_help = ws.render_help_text(); @@ -335,7 +339,7 @@ fn test_command_mode_help_keycode() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Char(':'))) + InputScript::default().char(':').run(&mut ws) .expect("Failed to handle ':' key"); assert_eq!(Some(&Modality::Command), ws.state.modality_stack.last()); let edit_help = ws.render_help_text(); @@ -350,13 +354,11 @@ fn test_edit_mode_esc_keycode() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Char('i'))) + InputScript::default().char('i').run(&mut ws) .expect("Failed to handle 'i' key"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Char('a'))) - .expect("Failed to handle 'a' key event"); - ws.handle_input(construct_key_event(KeyCode::Esc)) - .expect("Failed to handle 'esc' key event"); + InputScript::default().char('a').esc().run(&mut ws) + .expect("Failed to handle key squence"); assert_eq!("", ws.book.get_current_cell_contents().expect("Failed to get current cell contents")); assert_eq!("", ws.text_area.lines().join("\n")); } @@ -404,14 +406,14 @@ fn test_navigation_tab_next_numeric_prefix() assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); - ws.handle_input(construct_key_event(KeyCode::Char('2'))) - .expect("Failed to handle '3' key event"); + InputScript::default().char('2').run(&mut ws) + .expect("Failed to handle '2' key event"); assert_eq!(2, ws.state.get_n_prefix()); - ws.handle_input(construct_modified_key_event(KeyCode::Char('n'), KeyModifiers::CONTROL)) + InputScript::default().ctrl('n').run(&mut ws) .expect("Failed to handle 'Ctrl-n' key event"); assert_eq!("Sheet3", ws.book.get_sheet_name().expect("Failed to get sheet name")); assert_eq!(1, ws.state.get_n_prefix()); - ws.handle_input(construct_modified_key_event(KeyCode::Char('n'), KeyModifiers::CONTROL)) + InputScript::default().ctrl('n').run(&mut ws) .expect("Failed to handle 'Ctrl-n' key event"); assert_eq!("Sheet1", ws.book.get_sheet_name().expect("Failed to get sheet name")); } @@ -424,23 +426,19 @@ fn test_range_copy() { ws.book.move_to(&Address { row: 1, col: 1, }).expect("Failed to move to row"); let original_loc = ws.book.location.clone(); - ws.handle_input(construct_modified_key_event(KeyCode::Char('r'), KeyModifiers::CONTROL)) + InputScript::default().ctrl('r').run(&mut ws) .expect("Failed to handle 'Ctrl-r' key event"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); assert_eq!(Some(original_loc.clone()), ws.state.range_select.original_location); assert!(ws.state.range_select.start.is_none()); assert!(ws.state.range_select.end.is_none()); - ws.handle_input(construct_key_event(KeyCode::Char('l'))) - .expect("Failed to handle 'l' key event"); - ws.handle_input(construct_key_event(KeyCode::Char(' '))) - .expect("Failed to handle ' ' key event"); + InputScript::default().char('l').char(' ').run(&mut ws) + .expect("Failed to handle key sequence"); assert_eq!(Some(Address {row:1, col:2, }), ws.state.range_select.start); - ws.handle_input(construct_key_event(KeyCode::Char('j'))) - .expect("Failed to handle 'j' key event"); - ws.handle_input(construct_key_event(KeyCode::Char(' '))) - .expect("Failed to handle ' ' key event"); + InputScript::default().char('j').char(' ').run(&mut ws) + .expect("Failed to handle key sequence"); assert!(ws.state.range_select.original_location.is_none()); assert_eq!(Some(Address {row:1, col:2, }), ws.state.range_select.start); @@ -459,16 +457,12 @@ fn test_range_copy() { assert!(ws.state.range_select.start.is_some()); assert!(ws.state.range_select.end.is_none()); - ws.handle_input(construct_key_event(KeyCode::Char('h'))) - .expect("Failed to handle 'h' key event"); - ws.handle_input(construct_key_event(KeyCode::Char(' '))) - .expect("Failed to handle ' ' key event"); + InputScript::default().char('h').char(' ').run(&mut ws) + .expect("Failed to handle key sequence"); assert_eq!(Some(Address {row:5, col: 5, }), ws.state.range_select.start); - ws.handle_input(construct_key_event(KeyCode::Char('k'))) - .expect("Failed to handle 'k' key event"); - ws.handle_input(construct_key_event(KeyCode::Char(' '))) - .expect("Failed to handle ' ' key event"); + InputScript::default().char('k').char(' ').run(&mut ws) + .expect("Failed to handle key sequence"); assert!(ws.state.range_select.original_location.is_none()); assert_eq!(Some(Address {row:5, col:5, }), ws.state.range_select.start); @@ -482,10 +476,10 @@ fn test_range_copy_mode_from_edit_mode() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.handle_input(construct_key_event(KeyCode::Char('e'))) + InputScript::default().char('e').run(&mut ws) .expect("Failed to handle 'e' key event"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); - ws.handle_input(construct_modified_key_event(KeyCode::Char('r'), KeyModifiers::CONTROL)) + InputScript::default().ctrl('r').run(&mut ws) .expect("Failed to handle 'Ctrl-r' key event"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); } @@ -521,8 +515,8 @@ fn test_h_j_k_l_movement() { .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 3, col: 2 }); InputScript::default() - .char('2') .char('h') + .char('2') .char('k') .run(&mut ws) .expect("failed to handle event sequence"); From a65ad974cefba7a81422529571da301bc5aac7b2 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 29 Dec 2024 20:01:35 -0500 Subject: [PATCH 10/15] wip: test coverage for handle_dialog_input --- src/ui/test.rs | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/ui/test.rs b/src/ui/test.rs index ae4792b..d240fcd 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -307,19 +307,43 @@ fn test_input_navitation_shift_tab_key() { assert_eq!(col, ws.book.location.col); } +macro_rules! assert_help_dialog { + ($exit : expr) => {{ + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + InputScript::default().char('i').run(&mut ws) + .expect("Failed to handle 'i' key"); + assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); + let edit_help = ws.render_help_text(); + InputScript::default().alt('h').run(&mut ws) + .expect("Failed to handle 'alt-h' key event"); + assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); + assert_eq!(edit_help, ws.state.popup); + $exit.run(&mut ws) + .expect("Failed to handle key event"); + assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); + }}; +} + #[test] -fn test_edit_mode_help_keycode() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); - assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().char('i').run(&mut ws) - .expect("Failed to handle 'i' key"); - assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); - let edit_help = ws.render_help_text(); - InputScript::default().alt('h').run(&mut ws) - .expect("Failed to handle 'alt-h' key event"); - assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); - assert_eq!(edit_help, ws.state.popup); +fn test_edit_mode_help_keycode_esc() { + assert_help_dialog!(InputScript::default().esc()); +} + +#[test] +fn test_edit_mode_help_keycode_enter() { + assert_help_dialog!(InputScript::default().enter()); +} + +#[test] +fn test_edit_mode_help_keycode_q() { + assert_help_dialog!(InputScript::default().char('q')); +} + +#[test] +fn test_edit_mode_help_keycode_alt_h() { + assert_help_dialog!(InputScript::default().alt('h')); } #[test] From 10d35ab0d653b57d58d02b2d54a9f423aa239a69 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Tue, 31 Dec 2024 15:40:04 -0500 Subject: [PATCH 11/15] wip: test coverage for handle_edit_input --- src/ui/mod.rs | 2 +- src/ui/test.rs | 423 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 310 insertions(+), 115 deletions(-) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 956b91d..d36d28c 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -386,7 +386,7 @@ impl<'ws> Workspace<'ws> { } KeyCode::Char('p') if key.modifiers == KeyModifiers::CONTROL => { self.text_area - .set_yank_text(self.selected_range_to_string()); + .set_yank_text(dbg!(self.selected_range_to_string())); self.text_area.paste(); self.state.dirty = true; return Ok(None); diff --git a/src/ui/test.rs b/src/ui/test.rs index d240fcd..2d1b6a0 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -8,19 +8,19 @@ use super::cmd::{parse, Cmd}; use super::Workspace; #[derive(Default)] -pub struct InputScript{ - events: Vec +pub struct InputScript { + events: Vec, } impl InputScript { pub fn char(self, c: char) -> Self { self.event(construct_key_event(KeyCode::Char(c))) } - + pub fn ctrl(self, c: char) -> Self { self.modified_char(c, KeyModifiers::CONTROL) } - + pub fn alt(self, c: char) -> Self { self.modified_char(c, KeyModifiers::ALT) } @@ -28,11 +28,11 @@ impl InputScript { pub fn tab(self) -> Self { self.event(construct_key_event(KeyCode::Tab)) } - + pub fn modified_char(self, c: char, mods: KeyModifiers) -> Self { self.event(construct_modified_key_event(KeyCode::Char(c), mods)) } - + pub fn event(mut self, evt: Event) -> Self { self.events.push(evt); self @@ -257,7 +257,9 @@ fn test_input_navitation_enter_key() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let row = ws.book.location.row; assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().enter().run(&mut ws) + InputScript::default() + .enter() + .run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(row + 1, ws.book.location.row); } @@ -268,7 +270,9 @@ fn test_input_navitation_tab_key() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let col = dbg!(ws.book.location.col); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().tab().run(&mut ws) + InputScript::default() + .tab() + .run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(col + 1, ws.book.location.col); } @@ -279,14 +283,18 @@ fn test_input_navitation_shift_enter_key() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let row = ws.book.location.row; assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().enter().run(&mut ws) + InputScript::default() + .enter() + .run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(row + 1, ws.book.location.row); - InputScript::default().event(construct_modified_key_event( - KeyCode::Enter, - KeyModifiers::SHIFT, - )).run(&mut ws) - .expect("Failed to handle enter key"); + InputScript::default() + .event(construct_modified_key_event( + KeyCode::Enter, + KeyModifiers::SHIFT, + )) + .run(&mut ws) + .expect("Failed to handle enter key"); assert_eq!(row, ws.book.location.row); } @@ -296,14 +304,18 @@ fn test_input_navitation_shift_tab_key() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); let col = dbg!(ws.book.location.col); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().tab().run(&mut ws) + InputScript::default() + .tab() + .run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(col + 1, ws.book.location.col); - InputScript::default().event(construct_modified_key_event( - KeyCode::Tab, - KeyModifiers::SHIFT, - )).run(&mut ws) - .expect("Failed to handle enter key"); + InputScript::default() + .event(construct_modified_key_event( + KeyCode::Tab, + KeyModifiers::SHIFT, + )) + .run(&mut ws) + .expect("Failed to handle enter key"); assert_eq!(col, ws.book.location.col); } @@ -312,16 +324,19 @@ macro_rules! assert_help_dialog { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().char('i').run(&mut ws) + InputScript::default() + .char('i') + .run(&mut ws) .expect("Failed to handle 'i' key"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); let edit_help = ws.render_help_text(); - InputScript::default().alt('h').run(&mut ws) + InputScript::default() + .alt('h') + .run(&mut ws) .expect("Failed to handle 'alt-h' key event"); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(edit_help, ws.state.popup); - $exit.run(&mut ws) - .expect("Failed to handle key event"); + $exit.run(&mut ws).expect("Failed to handle key event"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); }}; } @@ -352,7 +367,9 @@ fn test_navigation_mode_help_keycode() { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); let help_text = ws.render_help_text(); - InputScript::default().alt('h').run(&mut ws) + InputScript::default() + .alt('h') + .run(&mut ws) .expect("Failed to handle 'alt-h' key event"); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(help_text, ws.state.popup); @@ -363,11 +380,15 @@ fn test_command_mode_help_keycode() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().char(':').run(&mut ws) + InputScript::default() + .char(':') + .run(&mut ws) .expect("Failed to handle ':' key"); assert_eq!(Some(&Modality::Command), ws.state.modality_stack.last()); let edit_help = ws.render_help_text(); - InputScript::default().alt('h').run(&mut ws) + InputScript::default() + .alt('h') + .run(&mut ws) .expect("Failed to handle 'alt-h' key event"); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(edit_help, ws.state.popup); @@ -378,23 +399,36 @@ fn test_edit_mode_esc_keycode() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().char('i').run(&mut ws) + InputScript::default() + .char('i') + .run(&mut ws) .expect("Failed to handle 'i' key"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); - InputScript::default().char('a').esc().run(&mut ws) + InputScript::default() + .char('a') + .esc() + .run(&mut ws) .expect("Failed to handle key squence"); - assert_eq!("", ws.book.get_current_cell_contents().expect("Failed to get current cell contents")); + assert_eq!( + "", + ws.book + .get_current_cell_contents() + .expect("Failed to get current cell contents") + ); assert_eq!("", ws.text_area.lines().join("\n")); } #[test] -fn test_navigation_numeric_prefix() -{ +fn test_navigation_numeric_prefix() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); - ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); + ws.book + .new_sheet(Some("Sheet2")) + .expect("failed to create sheet2"); + ws.book + .new_sheet(Some("Sheet3")) + .expect("failed to create sheet3"); InputScript::default() .char('2') .char('3') @@ -405,13 +439,16 @@ fn test_navigation_numeric_prefix() } #[test] -fn test_navigation_numeric_prefix_cancel() -{ +fn test_navigation_numeric_prefix_cancel() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); - ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); + ws.book + .new_sheet(Some("Sheet2")) + .expect("failed to create sheet2"); + ws.book + .new_sheet(Some("Sheet3")) + .expect("failed to create sheet3"); InputScript::default() .char('2') .char('3') @@ -423,23 +460,38 @@ fn test_navigation_numeric_prefix_cancel() } #[test] -fn test_navigation_tab_next_numeric_prefix() -{ +fn test_navigation_tab_next_numeric_prefix() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); - ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); - InputScript::default().char('2').run(&mut ws) + ws.book + .new_sheet(Some("Sheet2")) + .expect("failed to create sheet2"); + ws.book + .new_sheet(Some("Sheet3")) + .expect("failed to create sheet3"); + InputScript::default() + .char('2') + .run(&mut ws) .expect("Failed to handle '2' key event"); assert_eq!(2, ws.state.get_n_prefix()); - InputScript::default().ctrl('n').run(&mut ws) + InputScript::default() + .ctrl('n') + .run(&mut ws) .expect("Failed to handle 'Ctrl-n' key event"); - assert_eq!("Sheet3", ws.book.get_sheet_name().expect("Failed to get sheet name")); + assert_eq!( + "Sheet3", + ws.book.get_sheet_name().expect("Failed to get sheet name") + ); assert_eq!(1, ws.state.get_n_prefix()); - InputScript::default().ctrl('n').run(&mut ws) + InputScript::default() + .ctrl('n') + .run(&mut ws) .expect("Failed to handle 'Ctrl-n' key event"); - assert_eq!("Sheet1", ws.book.get_sheet_name().expect("Failed to get sheet name")); + assert_eq!( + "Sheet1", + ws.book.get_sheet_name().expect("Failed to get sheet name") + ); } #[test] @@ -447,51 +499,89 @@ fn test_range_copy() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - - ws.book.move_to(&Address { row: 1, col: 1, }).expect("Failed to move to row"); + + ws.book + .move_to(&Address { row: 1, col: 1 }) + .expect("Failed to move to row"); let original_loc = ws.book.location.clone(); - InputScript::default().ctrl('r').run(&mut ws) + InputScript::default() + .ctrl('r') + .run(&mut ws) .expect("Failed to handle 'Ctrl-r' key event"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); - assert_eq!(Some(original_loc.clone()), ws.state.range_select.original_location); + assert_eq!( + Some(original_loc.clone()), + ws.state.range_select.original_location + ); assert!(ws.state.range_select.start.is_none()); assert!(ws.state.range_select.end.is_none()); - - InputScript::default().char('l').char(' ').run(&mut ws) + + InputScript::default() + .char('l') + .char(' ') + .run(&mut ws) .expect("Failed to handle key sequence"); - assert_eq!(Some(Address {row:1, col:2, }), ws.state.range_select.start); - - InputScript::default().char('j').char(' ').run(&mut ws) + assert_eq!( + Some(Address { row: 1, col: 2 }), + ws.state.range_select.start + ); + + InputScript::default() + .char('j') + .char(' ') + .run(&mut ws) .expect("Failed to handle key sequence"); - + assert!(ws.state.range_select.original_location.is_none()); - assert_eq!(Some(Address {row:1, col:2, }), ws.state.range_select.start); - assert_eq!(Some(Address {row:2, col:2, }), ws.state.range_select.end); + assert_eq!( + Some(Address { row: 1, col: 2 }), + ws.state.range_select.start + ); + assert_eq!(Some(Address { row: 2, col: 2 }), ws.state.range_select.end); assert_eq!(original_loc, ws.book.location); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - - ws.book.move_to(&Address { row: 5, col: 5, }).expect("Failed to move to row"); + + ws.book + .move_to(&Address { row: 5, col: 5 }) + .expect("Failed to move to row"); let original_loc_2 = ws.book.location.clone(); assert_eq!(Address { row: 5, col: 5 }, original_loc_2); - - InputScript::default().char('v').run(&mut ws) + + InputScript::default() + .char('v') + .run(&mut ws) .expect("Failed to handle 'v' key event"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); - assert_eq!(Some(original_loc_2.clone()), ws.state.range_select.original_location); + assert_eq!( + Some(original_loc_2.clone()), + ws.state.range_select.original_location + ); assert!(ws.state.range_select.start.is_some()); assert!(ws.state.range_select.end.is_none()); - - InputScript::default().char('h').char(' ').run(&mut ws) + + InputScript::default() + .char('h') + .char(' ') + .run(&mut ws) .expect("Failed to handle key sequence"); - assert_eq!(Some(Address {row:5, col: 5, }), ws.state.range_select.start); - - InputScript::default().char('k').char(' ').run(&mut ws) + assert_eq!( + Some(Address { row: 5, col: 5 }), + ws.state.range_select.start + ); + + InputScript::default() + .char('k') + .char(' ') + .run(&mut ws) .expect("Failed to handle key sequence"); - + assert!(ws.state.range_select.original_location.is_none()); - assert_eq!(Some(Address {row:5, col:5, }), ws.state.range_select.start); - assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.end); - assert_eq!(Address {row:4, col:5, }, ws.book.location); + assert_eq!( + Some(Address { row: 5, col: 5 }), + ws.state.range_select.start + ); + assert_eq!(Some(Address { row: 5, col: 4 }), ws.state.range_select.end); + assert_eq!(Address { row: 4, col: 5 }, ws.book.location); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); } @@ -500,10 +590,14 @@ fn test_range_copy_mode_from_edit_mode() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().char('e').run(&mut ws) + InputScript::default() + .char('e') + .run(&mut ws) .expect("Failed to handle 'e' key event"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); - InputScript::default().ctrl('r').run(&mut ws) + InputScript::default() + .ctrl('r') + .run(&mut ws) .expect("Failed to handle 'Ctrl-r' key event"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); } @@ -515,7 +609,8 @@ fn test_gg_movement() { assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char('j') - .char('j').run(&mut ws) + .char('j') + .run(&mut ws) .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 3, col: 1 }); InputScript::default() @@ -535,7 +630,8 @@ fn test_h_j_k_l_movement() { InputScript::default() .char('2') .char('j') - .char('l').run(&mut ws) + .char('l') + .run(&mut ws) .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 3, col: 2 }); InputScript::default() @@ -558,25 +654,29 @@ macro_rules! assert_copy_paste { assert_copy_paste!($c, $p, $source, $expected) }; ($c: expr, $p: expr, $source: expr, $expected: expr) => {{ - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); - InputScript::default() - .char('j') - .char('l') - .run(&mut ws) - .expect("Failed to run script"); - ws.book.edit_current_cell($source).expect("Failed to edit cell"); - ws.book.evaluate(); - InputScript::default() - .event($c) - .char('l') - .char('j') - .event($p) - .run(&mut ws) - .expect("Failed to run script"); - let copy = ws.book.get_current_cell_contents() - .expect("Failed to get cell contents"); - assert_eq!(copy, $expected); + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + InputScript::default() + .char('j') + .char('l') + .run(&mut ws) + .expect("Failed to run script"); + ws.book + .edit_current_cell($source) + .expect("Failed to edit cell"); + ws.book.evaluate(); + InputScript::default() + .event($c) + .char('l') + .char('j') + .event($p) + .run(&mut ws) + .expect("Failed to run script"); + let copy = ws + .book + .get_current_cell_contents() + .expect("Failed to get cell contents"); + assert_eq!(copy, $expected); }}; } @@ -622,50 +722,84 @@ fn test_traditional_copy_paste_rendered() { fn test_clear_cell() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); - ws.book.edit_current_cell("foo") + ws.book + .edit_current_cell("foo") .expect("failed to edit cell"); ws.book.evaluate(); - assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents")); + assert_eq!( + "foo", + ws.book + .get_current_cell_contents() + .expect("failed to get cell contents") + ); InputScript::default() .char('d') .run(&mut ws) .expect("Failed to run input script"); - assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); + assert_eq!( + "", + ws.book + .get_current_cell_contents() + .expect("failed to get cell contents") + ); } #[test] fn test_clear_cell_all() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); - ws.book.edit_current_cell("foo") + ws.book + .edit_current_cell("foo") .expect("failed to edit cell"); ws.book.evaluate(); - assert_eq!("foo", ws.book.get_current_cell_contents().expect("failed to get cell contents")); + assert_eq!( + "foo", + ws.book + .get_current_cell_contents() + .expect("failed to get cell contents") + ); InputScript::default() .char('D') .run(&mut ws) .expect("Failed to run input script"); - assert_eq!("", ws.book.get_current_cell_contents().expect("failed to get cell contents")); + assert_eq!( + "", + ws.book + .get_current_cell_contents() + .expect("failed to get cell contents") + ); } #[test] fn test_sheet_navigation() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); - ws.book.new_sheet(Some("sheet 2")).expect("Failed to set sheet name"); - ws.book.new_sheet(Some("sheet 3")).expect("Failed to set sheet name"); - ws.book.new_sheet(Some("sheet 4")).expect("Failed to set sheet name"); + ws.book + .new_sheet(Some("sheet 2")) + .expect("Failed to set sheet name"); + ws.book + .new_sheet(Some("sheet 3")) + .expect("Failed to set sheet name"); + ws.book + .new_sheet(Some("sheet 4")) + .expect("Failed to set sheet name"); InputScript::default() .ctrl('n') .ctrl('n') .run(&mut ws) .expect("Failed to run input script"); - assert_eq!("sheet 3", ws.book.get_sheet_name().expect("Failed to get sheet name")); + assert_eq!( + "sheet 3", + ws.book.get_sheet_name().expect("Failed to get sheet name") + ); InputScript::default() .ctrl('p') .run(&mut ws) .expect("Failed to run input script"); - assert_eq!("sheet 2", ws.book.get_sheet_name().expect("Failed to get sheet name")); + assert_eq!( + "sheet 2", + ws.book.get_sheet_name().expect("Failed to get sheet name") + ); } #[test] @@ -677,13 +811,19 @@ fn test_sheet_column_sizing() { .ctrl('l') .run(&mut ws) .expect("Failed to run input script"); - assert_eq!(28, ws.book.get_col_size(1).expect("Failed to get column size")); + assert_eq!( + 28, + ws.book.get_col_size(1).expect("Failed to get column size") + ); InputScript::default() .char('1') .ctrl('h') .run(&mut ws) .expect("Failed to run input script"); - assert_eq!(27, ws.book.get_col_size(1).expect("Failed to get column size")); + assert_eq!( + 27, + ws.book.get_col_size(1).expect("Failed to get column size") + ); } #[test] @@ -701,9 +841,16 @@ fn test_quit() { fn test_cell_replace() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); - ws.book.edit_current_cell("foo").expect("Failed to edit current cell"); - assert_eq!("foo", - ws.book.get_current_cell_contents().expect("failed to get cell contents").as_str()); + ws.book + .edit_current_cell("foo") + .expect("Failed to edit current cell"); + assert_eq!( + "foo", + ws.book + .get_current_cell_contents() + .expect("failed to get cell contents") + .as_str() + ); InputScript::default() .char('s') .char('b') @@ -712,7 +859,55 @@ fn test_cell_replace() { .enter() .run(&mut ws) .expect("Failed to run input script"); - assert_eq!("bar", - ws.book.get_current_cell_contents().expect("failed to get cell contents").as_str()); + assert_eq!( + "bar", + ws.book + .get_current_cell_contents() + .expect("failed to get cell contents") + .as_str() + ); } +macro_rules! assert_command_finish { + ($script : expr) => { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + InputScript::default() + .char(':') + .run(&mut ws) + .expect("Failed to handle ':' key"); + assert_eq!(Some(&Modality::Command), ws.state.modality_stack.last()); + $script + .run(&mut ws) + .expect("Failed to handle script"); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + }; +} + +#[test] +fn test_command_mode_esc() { + assert_command_finish!(InputScript::default().esc()); +} + +#[test] +fn test_command_mode_enter() { + assert_command_finish!(InputScript::default().enter()); +} + +#[test] +fn test_edit_mode_paste() { + let mut ws = + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + ws.state.range_select.start = Some(Address { row: 1, col: 1, }); + ws.state.range_select.end = Some(Address { row: 2, col: 2, }); + dbg!(ws.selected_range_to_string()); + InputScript::default() + .char('e') + .ctrl('p') + .run(&mut ws) + .expect("Failed to handle input script"); + assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); + assert_eq!(vec!["A1:B2".to_string()], ws.text_area.into_lines()); +} From 7de19e8e3b316369281986072573d08a4034e15f Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 1 Jan 2025 09:56:12 -0500 Subject: [PATCH 12/15] chore: utility function for making a workspace --- src/ui/test.rs | 92 +++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/src/ui/test.rs b/src/ui/test.rs index 2d1b6a0..9e52b74 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -253,8 +253,7 @@ fn test_cmd_rename_sheet_with_idx_and_name() { #[test] fn test_input_navitation_enter_key() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); let row = ws.book.location.row; assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() @@ -266,8 +265,7 @@ fn test_input_navitation_enter_key() { #[test] fn test_input_navitation_tab_key() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); let col = dbg!(ws.book.location.col); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() @@ -279,8 +277,7 @@ fn test_input_navitation_tab_key() { #[test] fn test_input_navitation_shift_enter_key() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); let row = ws.book.location.row; assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() @@ -300,8 +297,7 @@ fn test_input_navitation_shift_enter_key() { #[test] fn test_input_navitation_shift_tab_key() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); let col = dbg!(ws.book.location.col); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() @@ -321,8 +317,7 @@ fn test_input_navitation_shift_tab_key() { macro_rules! assert_help_dialog { ($exit : expr) => {{ - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char('i') @@ -363,8 +358,7 @@ fn test_edit_mode_help_keycode_alt_h() { #[test] fn test_navigation_mode_help_keycode() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); let help_text = ws.render_help_text(); InputScript::default() @@ -377,8 +371,7 @@ fn test_navigation_mode_help_keycode() { #[test] fn test_command_mode_help_keycode() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char(':') @@ -396,8 +389,7 @@ fn test_command_mode_help_keycode() { #[test] fn test_edit_mode_esc_keycode() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char('i') @@ -420,8 +412,7 @@ fn test_edit_mode_esc_keycode() { #[test] fn test_navigation_numeric_prefix() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); ws.book .new_sheet(Some("Sheet2")) @@ -440,8 +431,7 @@ fn test_navigation_numeric_prefix() { #[test] fn test_navigation_numeric_prefix_cancel() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); ws.book .new_sheet(Some("Sheet2")) @@ -461,8 +451,7 @@ fn test_navigation_numeric_prefix_cancel() { #[test] fn test_navigation_tab_next_numeric_prefix() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); ws.book .new_sheet(Some("Sheet2")) @@ -496,8 +485,7 @@ fn test_navigation_tab_next_numeric_prefix() { #[test] fn test_range_copy() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); ws.book @@ -587,8 +575,7 @@ fn test_range_copy() { #[test] fn test_range_copy_mode_from_edit_mode() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char('e') @@ -604,8 +591,7 @@ fn test_range_copy_mode_from_edit_mode() { #[test] fn test_gg_movement() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char('j') @@ -624,8 +610,7 @@ fn test_gg_movement() { #[test] fn test_h_j_k_l_movement() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char('2') @@ -654,8 +639,7 @@ macro_rules! assert_copy_paste { assert_copy_paste!($c, $p, $source, $expected) }; ($c: expr, $p: expr, $source: expr, $expected: expr) => {{ - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); InputScript::default() .char('j') .char('l') @@ -720,8 +704,7 @@ fn test_traditional_copy_paste_rendered() { #[test] fn test_clear_cell() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); ws.book .edit_current_cell("foo") .expect("failed to edit cell"); @@ -746,8 +729,7 @@ fn test_clear_cell() { #[test] fn test_clear_cell_all() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); ws.book .edit_current_cell("foo") .expect("failed to edit cell"); @@ -772,8 +754,7 @@ fn test_clear_cell_all() { #[test] fn test_sheet_navigation() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); ws.book .new_sheet(Some("sheet 2")) .expect("Failed to set sheet name"); @@ -804,8 +785,7 @@ fn test_sheet_navigation() { #[test] fn test_sheet_column_sizing() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); InputScript::default() .char('3') .ctrl('l') @@ -839,8 +819,7 @@ fn test_quit() { #[test] fn test_cell_replace() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); ws.book .edit_current_cell("foo") .expect("Failed to edit current cell"); @@ -870,8 +849,7 @@ fn test_cell_replace() { macro_rules! assert_command_finish { ($script : expr) => { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char(':') @@ -897,8 +875,7 @@ fn test_command_mode_enter() { #[test] fn test_edit_mode_paste() { - let mut ws = - Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); ws.state.range_select.start = Some(Address { row: 1, col: 1, }); ws.state.range_select.end = Some(Address { row: 2, col: 2, }); @@ -911,3 +888,26 @@ fn test_edit_mode_paste() { assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); assert_eq!(vec!["A1:B2".to_string()], ws.text_area.into_lines()); } + +#[test] +fn test_range_select_esc() { + let mut ws = new_workspace(); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + InputScript::default().char('v').run(&mut ws).expect("Failed to handle script"); + assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); + InputScript::default().esc().run(&mut ws).expect("Failed to handle script"); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + InputScript::default().char('v').chars("123").run(&mut ws) + .expect("Failed to handle script"); + assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); + assert_eq!(3, ws.state.numeric_prefix.len()); + InputScript::default().esc().run(&mut ws).expect("Failed to handle script"); + assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); + assert_eq!(0, ws.state.numeric_prefix.len()); + InputScript::default().esc().run(&mut ws).expect("Failed to handle script"); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); +} + +fn new_workspace<'a>() -> Workspace<'a> { + Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook") +} From 61e9a4631f8e15f233b8eec28a034921b4168cc4 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 6 Jan 2025 20:39:48 -0500 Subject: [PATCH 13/15] wip: test coverage for range select --- .gitignore | 1 + src/ui/mod.rs | 26 +++++-- src/ui/test.rs | 202 ++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 210 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 1d33feb..7137221 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ result/ *.json tarpaulin-report.* +*.profraw diff --git a/src/ui/mod.rs b/src/ui/mod.rs index d36d28c..ba05d9d 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -386,7 +386,7 @@ impl<'ws> Workspace<'ws> { } KeyCode::Char('p') if key.modifiers == KeyModifiers::CONTROL => { self.text_area - .set_yank_text(dbg!(self.selected_range_to_string())); + .set_yank_text(self.selected_range_to_string()); self.text_area.paste(); self.state.dirty = true; return Ok(None); @@ -549,7 +549,9 @@ impl<'ws> Workspace<'ws> { self.maybe_update_range_end(); } KeyCode::Char(' ') | KeyCode::Enter => { - self.update_range_selection()?; + if self.update_range_selection()? { + self.exit_range_select_mode()?; + } } KeyCode::Char('n') if key.modifiers == KeyModifiers::CONTROL => { self.state.range_select.reset_range_selection(); @@ -570,16 +572,24 @@ impl<'ws> Workspace<'ws> { KeyCode::Char('C') if key .modifiers - .contains(KeyModifiers::CONTROL | KeyModifiers::SHIFT) => + .contains(KeyModifiers::CONTROL) => { // TODO(zaphar): Share the algorithm below between both copies self.copy_range(true)?; + self.exit_range_select_mode()?; + } + KeyCode::Char('Y') => { + self.copy_range(true)?; + self.exit_range_select_mode()?; } - KeyCode::Char('Y') => self.copy_range(true)?, KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => { self.copy_range(false)?; + self.exit_range_select_mode()?; + } + KeyCode::Char('y') => { + self.copy_range(false)?; + self.exit_range_select_mode()?; } - KeyCode::Char('y') => self.copy_range(false)?, KeyCode::Char('x') => { if let (Some(from), Some(to)) = (self.state.range_select.start.as_ref(), self.state.range_select.end.as_ref()) { self.book.extend_to(from, to)?; @@ -627,17 +637,17 @@ impl<'ws> Workspace<'ws> { })); } } - self.exit_range_select_mode()?; Ok(()) } - fn update_range_selection(&mut self) -> Result<(), anyhow::Error> { + fn update_range_selection(&mut self) -> Result { Ok(if self.state.range_select.start.is_none() { self.state.range_select.start = Some(self.book.location.clone()); self.state.range_select.end = Some(self.book.location.clone()); + false } else { self.state.range_select.end = Some(self.book.location.clone()); - self.exit_range_select_mode()?; + true }) } diff --git a/src/ui/test.rs b/src/ui/test.rs index 9e52b74..1b1a618 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -17,6 +17,10 @@ impl InputScript { self.event(construct_key_event(KeyCode::Char(c))) } + pub fn chars(self, cs: &str) -> Self { + cs.chars().fold(self, |s, c| s.char(c)) + } + pub fn ctrl(self, c: char) -> Self { self.modified_char(c, KeyModifiers::CONTROL) } @@ -849,16 +853,14 @@ fn test_cell_replace() { macro_rules! assert_command_finish { ($script : expr) => { - let mut ws = new_workspace(); + let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); InputScript::default() .char(':') .run(&mut ws) .expect("Failed to handle ':' key"); assert_eq!(Some(&Modality::Command), ws.state.modality_stack.last()); - $script - .run(&mut ws) - .expect("Failed to handle script"); + $script.run(&mut ws).expect("Failed to handle script"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); }; } @@ -877,8 +879,8 @@ fn test_command_mode_enter() { fn test_edit_mode_paste() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - ws.state.range_select.start = Some(Address { row: 1, col: 1, }); - ws.state.range_select.end = Some(Address { row: 2, col: 2, }); + ws.state.range_select.start = Some(Address { row: 1, col: 1 }); + ws.state.range_select.end = Some(Address { row: 2, col: 2 }); dbg!(ws.selected_range_to_string()); InputScript::default() .char('e') @@ -893,21 +895,199 @@ fn test_edit_mode_paste() { fn test_range_select_esc() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().char('v').run(&mut ws).expect("Failed to handle script"); + InputScript::default() + .char('v') + .run(&mut ws) + .expect("Failed to handle script"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); - InputScript::default().esc().run(&mut ws).expect("Failed to handle script"); + InputScript::default() + .esc() + .run(&mut ws) + .expect("Failed to handle script"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default().char('v').chars("123").run(&mut ws) + InputScript::default() + .char('v') + .chars("123") + .run(&mut ws) .expect("Failed to handle script"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); assert_eq!(3, ws.state.numeric_prefix.len()); - InputScript::default().esc().run(&mut ws).expect("Failed to handle script"); + InputScript::default() + .esc() + .run(&mut ws) + .expect("Failed to handle script"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); assert_eq!(0, ws.state.numeric_prefix.len()); - InputScript::default().esc().run(&mut ws).expect("Failed to handle script"); + InputScript::default() + .esc() + .run(&mut ws) + .expect("Failed to handle script"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); } +macro_rules! assert_range_clear { + ($script : expr) => {{ + let mut ws = new_workspace(); + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); + let first_corner = Address { row: 1, col: 1 }; + let second_corner = Address { row: 2, col: 2 }; + ws.book + .update_cell(&first_corner, "foo") + .expect("Failed to update cell"); + ws.book + .update_cell(&second_corner, "bar") + .expect("Failed to update cell"); + assert_eq!( + "foo".to_string(), + ws.book + .get_cell_addr_contents(&first_corner) + .expect("failed to get cell contents") + ); + assert_eq!( + "bar".to_string(), + ws.book + .get_cell_addr_contents(&second_corner) + .expect("failed to get cell contents") + ); + InputScript::default() + .char('v') + .run(&mut ws) + .expect("Failed to handle script"); + assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); + $script.run(&mut ws) + .expect("Failed to handle script"); + assert_eq!( + "".to_string(), + ws.book + .get_cell_addr_contents(&first_corner) + .expect("failed to get cell contents") + ); + assert_eq!( + "".to_string(), + ws.book + .get_cell_addr_contents(&second_corner) + .expect("failed to get cell contents") + ); + }}; +} + +#[test] +fn test_range_select_clear_upper_d() { + assert_range_clear!(InputScript::default() + .char('j') + .char('l') + .char('D')); +} + +#[test] +fn test_range_select_movement() { + let mut ws = new_workspace(); + ws.book.new_sheet(Some("s2")).expect("Unable create s2 sheet"); + ws.book.new_sheet(Some("s3")).expect("Unable create s3 sheet"); + InputScript::default().ctrl('r').run(&mut ws) + .expect("failed to run script"); + assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); + InputScript::default() + .char('3') + .char('j') + .char('3') + .char('l') + .char('1') + .char('h') + .char('1') + .char('k') + .run(&mut ws) + .expect("failed to run script"); + assert_eq!(&Address { row: 3, col: 3 }, &ws.book.location); + assert_eq!(0, ws.book.current_sheet); + InputScript::default() + .ctrl('n') + .run(&mut ws) + .expect("Unable to run script"); + assert_eq!(1, ws.book.current_sheet); + InputScript::default() + .ctrl('p') + .run(&mut ws) + .expect("Unable to run script"); + assert_eq!(0, ws.book.current_sheet); +} + +#[test] +fn test_range_select_clear_lower_d() { + assert_range_clear!(InputScript::default() + .char('j') + .char('l') + .char('d')); +} + +macro_rules! assert_range_copy { + ($script: expr) => {{ + let mut ws = new_workspace(); + let top_left_addr = Address { row: 2, col: 2 }; + let bot_right_addr = Address { row: 4, col: 4 }; + ws.book.update_cell(&top_left_addr, "top_left").expect("Failed to update top left"); + ws.book.update_cell(&bot_right_addr, "bot_right").expect("Failed to update top left"); + assert!(ws.state.clipboard.is_none()); + InputScript::default() + .ctrl('r') + .char('j') + .char('l') + .char(' ') + .run(&mut ws) + .expect("failed to run script"); + assert_eq!(&top_left_addr, ws.state.range_select.start.as_ref().expect("Didn't find a start of range")); + InputScript::default() + .char('2') + .char('j') + .char('2') + .char('l') + .run(&mut ws) + .expect("failed to run script"); + assert_eq!(&bot_right_addr, ws.state.range_select.end.as_ref().expect("Didn't find a start of range")); + assert_eq!(&Address { row: 1, col: 1}, ws.state.range_select.original_location + .as_ref().expect("Expected an original location")); + assert_eq!(0, ws.state.range_select.original_sheet. + expect("Expected an original sheet")); + assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.iter().last()); + dbg!(ws.state.range_select.get_range()); + $script.run(&mut ws) + .expect("failed to run script"); + assert!(ws.state.clipboard.is_some()); + match ws.state.clipboard.unwrap() { + crate::ui::ClipboardContents::Cell(_) => assert!(false, "Not rows in Clipboard"), + crate::ui::ClipboardContents::Range(rows) => { + assert_eq!(vec![ + vec!["top_left".to_string(), "".to_string(), "".to_string()], + vec!["".to_string(), "".to_string(), "".to_string()], + vec!["".to_string(), "".to_string(), "bot_right".to_string()], + ], rows); + }, + } + assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.iter().last()); + }}; +} + +#[test] +fn test_range_select_copy_c() { + assert_range_copy!(InputScript::default().ctrl('c')); +} + +#[test] +fn test_range_select_copy_y() { + assert_range_copy!(InputScript::default().char('y')); +} + +#[test] +fn test_range_select_copy_capital_y() { + assert_range_copy!(InputScript::default().char('Y')); +} + +#[test] +fn test_range_select_copy_capital_c() { + assert_range_copy!(InputScript::default().ctrl('C')); +} + + fn new_workspace<'a>() -> Workspace<'a> { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook") } From c3ab2b72debaabe51e8476876f4ce174b1cc9495 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 6 Jan 2025 21:04:01 -0500 Subject: [PATCH 14/15] wip: test coverage for extend to range --- src/ui/test.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/ui/test.rs b/src/ui/test.rs index 1b1a618..4792dac 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -1087,6 +1087,21 @@ fn test_range_select_copy_capital_c() { assert_range_copy!(InputScript::default().ctrl('C')); } +#[test] +fn test_extend_to_range() { + let mut ws = new_workspace(); + ws.book.edit_current_cell("=B1+1").expect("Failed to edit cell"); + ws.book.evaluate(); + InputScript::default() + .char('v') + .char('j') + .char('x') + .run(&mut ws) + .expect("Unable to run script"); + let extended_cell = ws.book.get_cell_addr_contents(&Address { row: 2, col: 1 }) + .expect("Failed to get cell contents"); + assert_eq!("=B2+1".to_string(), extended_cell); +} fn new_workspace<'a>() -> Workspace<'a> { Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook") From b38af78ad77bf30378b42042424bce7af8506517 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Tue, 7 Jan 2025 19:02:42 -0500 Subject: [PATCH 15/15] wip: InputScript constructor helper --- src/ui/test.rs | 138 +++++++++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 67 deletions(-) diff --git a/src/ui/test.rs b/src/ui/test.rs index 4792dac..90df20d 100644 --- a/src/ui/test.rs +++ b/src/ui/test.rs @@ -60,6 +60,10 @@ impl InputScript { } } +fn script() -> InputScript { + InputScript::default() +} + fn construct_key_event(code: KeyCode) -> Event { construct_modified_key_event(code, KeyModifiers::empty()) } @@ -260,7 +264,7 @@ fn test_input_navitation_enter_key() { let mut ws = new_workspace(); let row = ws.book.location.row; assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .enter() .run(&mut ws) .expect("Failed to handle enter key"); @@ -272,7 +276,7 @@ fn test_input_navitation_tab_key() { let mut ws = new_workspace(); let col = dbg!(ws.book.location.col); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .tab() .run(&mut ws) .expect("Failed to handle enter key"); @@ -284,12 +288,12 @@ fn test_input_navitation_shift_enter_key() { let mut ws = new_workspace(); let row = ws.book.location.row; assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .enter() .run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(row + 1, ws.book.location.row); - InputScript::default() + script() .event(construct_modified_key_event( KeyCode::Enter, KeyModifiers::SHIFT, @@ -304,12 +308,12 @@ fn test_input_navitation_shift_tab_key() { let mut ws = new_workspace(); let col = dbg!(ws.book.location.col); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .tab() .run(&mut ws) .expect("Failed to handle enter key"); assert_eq!(col + 1, ws.book.location.col); - InputScript::default() + script() .event(construct_modified_key_event( KeyCode::Tab, KeyModifiers::SHIFT, @@ -323,13 +327,13 @@ macro_rules! assert_help_dialog { ($exit : expr) => {{ let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char('i') .run(&mut ws) .expect("Failed to handle 'i' key"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); let edit_help = ws.render_help_text(); - InputScript::default() + script() .alt('h') .run(&mut ws) .expect("Failed to handle 'alt-h' key event"); @@ -342,22 +346,22 @@ macro_rules! assert_help_dialog { #[test] fn test_edit_mode_help_keycode_esc() { - assert_help_dialog!(InputScript::default().esc()); + assert_help_dialog!(script().esc()); } #[test] fn test_edit_mode_help_keycode_enter() { - assert_help_dialog!(InputScript::default().enter()); + assert_help_dialog!(script().enter()); } #[test] fn test_edit_mode_help_keycode_q() { - assert_help_dialog!(InputScript::default().char('q')); + assert_help_dialog!(script().char('q')); } #[test] fn test_edit_mode_help_keycode_alt_h() { - assert_help_dialog!(InputScript::default().alt('h')); + assert_help_dialog!(script().alt('h')); } #[test] @@ -365,7 +369,7 @@ fn test_navigation_mode_help_keycode() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); let help_text = ws.render_help_text(); - InputScript::default() + script() .alt('h') .run(&mut ws) .expect("Failed to handle 'alt-h' key event"); @@ -377,13 +381,13 @@ fn test_navigation_mode_help_keycode() { fn test_command_mode_help_keycode() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char(':') .run(&mut ws) .expect("Failed to handle ':' key"); assert_eq!(Some(&Modality::Command), ws.state.modality_stack.last()); let edit_help = ws.render_help_text(); - InputScript::default() + script() .alt('h') .run(&mut ws) .expect("Failed to handle 'alt-h' key event"); @@ -395,12 +399,12 @@ fn test_command_mode_help_keycode() { fn test_edit_mode_esc_keycode() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char('i') .run(&mut ws) .expect("Failed to handle 'i' key"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); - InputScript::default() + script() .char('a') .esc() .run(&mut ws) @@ -424,7 +428,7 @@ fn test_navigation_numeric_prefix() { ws.book .new_sheet(Some("Sheet3")) .expect("failed to create sheet3"); - InputScript::default() + script() .char('2') .char('3') .char('9') @@ -443,7 +447,7 @@ fn test_navigation_numeric_prefix_cancel() { ws.book .new_sheet(Some("Sheet3")) .expect("failed to create sheet3"); - InputScript::default() + script() .char('2') .char('3') .char('9') @@ -463,12 +467,12 @@ fn test_navigation_tab_next_numeric_prefix() { ws.book .new_sheet(Some("Sheet3")) .expect("failed to create sheet3"); - InputScript::default() + script() .char('2') .run(&mut ws) .expect("Failed to handle '2' key event"); assert_eq!(2, ws.state.get_n_prefix()); - InputScript::default() + script() .ctrl('n') .run(&mut ws) .expect("Failed to handle 'Ctrl-n' key event"); @@ -477,7 +481,7 @@ fn test_navigation_tab_next_numeric_prefix() { ws.book.get_sheet_name().expect("Failed to get sheet name") ); assert_eq!(1, ws.state.get_n_prefix()); - InputScript::default() + script() .ctrl('n') .run(&mut ws) .expect("Failed to handle 'Ctrl-n' key event"); @@ -496,7 +500,7 @@ fn test_range_copy() { .move_to(&Address { row: 1, col: 1 }) .expect("Failed to move to row"); let original_loc = ws.book.location.clone(); - InputScript::default() + script() .ctrl('r') .run(&mut ws) .expect("Failed to handle 'Ctrl-r' key event"); @@ -508,7 +512,7 @@ fn test_range_copy() { assert!(ws.state.range_select.start.is_none()); assert!(ws.state.range_select.end.is_none()); - InputScript::default() + script() .char('l') .char(' ') .run(&mut ws) @@ -518,7 +522,7 @@ fn test_range_copy() { ws.state.range_select.start ); - InputScript::default() + script() .char('j') .char(' ') .run(&mut ws) @@ -539,7 +543,7 @@ fn test_range_copy() { let original_loc_2 = ws.book.location.clone(); assert_eq!(Address { row: 5, col: 5 }, original_loc_2); - InputScript::default() + script() .char('v') .run(&mut ws) .expect("Failed to handle 'v' key event"); @@ -551,7 +555,7 @@ fn test_range_copy() { assert!(ws.state.range_select.start.is_some()); assert!(ws.state.range_select.end.is_none()); - InputScript::default() + script() .char('h') .char(' ') .run(&mut ws) @@ -561,7 +565,7 @@ fn test_range_copy() { ws.state.range_select.start ); - InputScript::default() + script() .char('k') .char(' ') .run(&mut ws) @@ -581,12 +585,12 @@ fn test_range_copy() { fn test_range_copy_mode_from_edit_mode() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char('e') .run(&mut ws) .expect("Failed to handle 'e' key event"); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); - InputScript::default() + script() .ctrl('r') .run(&mut ws) .expect("Failed to handle 'Ctrl-r' key event"); @@ -597,13 +601,13 @@ fn test_range_copy_mode_from_edit_mode() { fn test_gg_movement() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char('j') .char('j') .run(&mut ws) .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 3, col: 1 }); - InputScript::default() + script() .char('l') .char('g') .char('g') @@ -616,14 +620,14 @@ fn test_gg_movement() { fn test_h_j_k_l_movement() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char('2') .char('j') .char('l') .run(&mut ws) .expect("failed to handle event sequence"); assert_eq!(ws.book.location, Address { row: 3, col: 2 }); - InputScript::default() + script() .char('h') .char('2') .char('k') @@ -644,7 +648,7 @@ macro_rules! assert_copy_paste { }; ($c: expr, $p: expr, $source: expr, $expected: expr) => {{ let mut ws = new_workspace(); - InputScript::default() + script() .char('j') .char('l') .run(&mut ws) @@ -653,7 +657,7 @@ macro_rules! assert_copy_paste { .edit_current_cell($source) .expect("Failed to edit cell"); ws.book.evaluate(); - InputScript::default() + script() .event($c) .char('l') .char('j') @@ -719,7 +723,7 @@ fn test_clear_cell() { .get_current_cell_contents() .expect("failed to get cell contents") ); - InputScript::default() + script() .char('d') .run(&mut ws) .expect("Failed to run input script"); @@ -744,7 +748,7 @@ fn test_clear_cell_all() { .get_current_cell_contents() .expect("failed to get cell contents") ); - InputScript::default() + script() .char('D') .run(&mut ws) .expect("Failed to run input script"); @@ -768,7 +772,7 @@ fn test_sheet_navigation() { ws.book .new_sheet(Some("sheet 4")) .expect("Failed to set sheet name"); - InputScript::default() + script() .ctrl('n') .ctrl('n') .run(&mut ws) @@ -777,7 +781,7 @@ fn test_sheet_navigation() { "sheet 3", ws.book.get_sheet_name().expect("Failed to get sheet name") ); - InputScript::default() + script() .ctrl('p') .run(&mut ws) .expect("Failed to run input script"); @@ -790,7 +794,7 @@ fn test_sheet_navigation() { #[test] fn test_sheet_column_sizing() { let mut ws = new_workspace(); - InputScript::default() + script() .char('3') .ctrl('l') .run(&mut ws) @@ -799,7 +803,7 @@ fn test_sheet_column_sizing() { 28, ws.book.get_col_size(1).expect("Failed to get column size") ); - InputScript::default() + script() .char('1') .ctrl('h') .run(&mut ws) @@ -814,7 +818,7 @@ fn test_sheet_column_sizing() { fn test_quit() { let mut ws = Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); - let result = InputScript::default() + let result = script() .char('q') .run(&mut ws) .expect("Failed to run input script"); @@ -834,7 +838,7 @@ fn test_cell_replace() { .expect("failed to get cell contents") .as_str() ); - InputScript::default() + script() .char('s') .char('b') .char('a') @@ -855,7 +859,7 @@ macro_rules! assert_command_finish { ($script : expr) => { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char(':') .run(&mut ws) .expect("Failed to handle ':' key"); @@ -867,12 +871,12 @@ macro_rules! assert_command_finish { #[test] fn test_command_mode_esc() { - assert_command_finish!(InputScript::default().esc()); + assert_command_finish!(script().esc()); } #[test] fn test_command_mode_enter() { - assert_command_finish!(InputScript::default().enter()); + assert_command_finish!(script().enter()); } #[test] @@ -882,7 +886,7 @@ fn test_edit_mode_paste() { ws.state.range_select.start = Some(Address { row: 1, col: 1 }); ws.state.range_select.end = Some(Address { row: 2, col: 2 }); dbg!(ws.selected_range_to_string()); - InputScript::default() + script() .char('e') .ctrl('p') .run(&mut ws) @@ -895,30 +899,30 @@ fn test_edit_mode_paste() { fn test_range_select_esc() { let mut ws = new_workspace(); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char('v') .run(&mut ws) .expect("Failed to handle script"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); - InputScript::default() + script() .esc() .run(&mut ws) .expect("Failed to handle script"); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); - InputScript::default() + script() .char('v') .chars("123") .run(&mut ws) .expect("Failed to handle script"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); assert_eq!(3, ws.state.numeric_prefix.len()); - InputScript::default() + script() .esc() .run(&mut ws) .expect("Failed to handle script"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); assert_eq!(0, ws.state.numeric_prefix.len()); - InputScript::default() + script() .esc() .run(&mut ws) .expect("Failed to handle script"); @@ -949,7 +953,7 @@ macro_rules! assert_range_clear { .get_cell_addr_contents(&second_corner) .expect("failed to get cell contents") ); - InputScript::default() + script() .char('v') .run(&mut ws) .expect("Failed to handle script"); @@ -973,7 +977,7 @@ macro_rules! assert_range_clear { #[test] fn test_range_select_clear_upper_d() { - assert_range_clear!(InputScript::default() + assert_range_clear!(script() .char('j') .char('l') .char('D')); @@ -984,10 +988,10 @@ fn test_range_select_movement() { let mut ws = new_workspace(); ws.book.new_sheet(Some("s2")).expect("Unable create s2 sheet"); ws.book.new_sheet(Some("s3")).expect("Unable create s3 sheet"); - InputScript::default().ctrl('r').run(&mut ws) + script().ctrl('r').run(&mut ws) .expect("failed to run script"); assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); - InputScript::default() + script() .char('3') .char('j') .char('3') @@ -1000,12 +1004,12 @@ fn test_range_select_movement() { .expect("failed to run script"); assert_eq!(&Address { row: 3, col: 3 }, &ws.book.location); assert_eq!(0, ws.book.current_sheet); - InputScript::default() + script() .ctrl('n') .run(&mut ws) .expect("Unable to run script"); assert_eq!(1, ws.book.current_sheet); - InputScript::default() + script() .ctrl('p') .run(&mut ws) .expect("Unable to run script"); @@ -1014,7 +1018,7 @@ fn test_range_select_movement() { #[test] fn test_range_select_clear_lower_d() { - assert_range_clear!(InputScript::default() + assert_range_clear!(script() .char('j') .char('l') .char('d')); @@ -1028,7 +1032,7 @@ macro_rules! assert_range_copy { ws.book.update_cell(&top_left_addr, "top_left").expect("Failed to update top left"); ws.book.update_cell(&bot_right_addr, "bot_right").expect("Failed to update top left"); assert!(ws.state.clipboard.is_none()); - InputScript::default() + script() .ctrl('r') .char('j') .char('l') @@ -1036,7 +1040,7 @@ macro_rules! assert_range_copy { .run(&mut ws) .expect("failed to run script"); assert_eq!(&top_left_addr, ws.state.range_select.start.as_ref().expect("Didn't find a start of range")); - InputScript::default() + script() .char('2') .char('j') .char('2') @@ -1069,22 +1073,22 @@ macro_rules! assert_range_copy { #[test] fn test_range_select_copy_c() { - assert_range_copy!(InputScript::default().ctrl('c')); + assert_range_copy!(script().ctrl('c')); } #[test] fn test_range_select_copy_y() { - assert_range_copy!(InputScript::default().char('y')); + assert_range_copy!(script().char('y')); } #[test] fn test_range_select_copy_capital_y() { - assert_range_copy!(InputScript::default().char('Y')); + assert_range_copy!(script().char('Y')); } #[test] fn test_range_select_copy_capital_c() { - assert_range_copy!(InputScript::default().ctrl('C')); + assert_range_copy!(script().ctrl('C')); } #[test] @@ -1092,7 +1096,7 @@ fn test_extend_to_range() { let mut ws = new_workspace(); ws.book.edit_current_cell("=B1+1").expect("Failed to edit cell"); ws.book.evaluate(); - InputScript::default() + script() .char('v') .char('j') .char('x')