wip: test coverage for handle_edit_input

This commit is contained in:
Jeremy Wall 2024-12-31 15:40:04 -05:00
parent a65ad974ce
commit 10d35ab0d6
2 changed files with 310 additions and 115 deletions

View File

@ -386,7 +386,7 @@ impl<'ws> Workspace<'ws> {
} }
KeyCode::Char('p') if key.modifiers == KeyModifiers::CONTROL => { KeyCode::Char('p') if key.modifiers == KeyModifiers::CONTROL => {
self.text_area 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.text_area.paste();
self.state.dirty = true; self.state.dirty = true;
return Ok(None); return Ok(None);

View File

@ -8,19 +8,19 @@ use super::cmd::{parse, Cmd};
use super::Workspace; use super::Workspace;
#[derive(Default)] #[derive(Default)]
pub struct InputScript{ pub struct InputScript {
events: Vec<Event> events: Vec<Event>,
} }
impl InputScript { impl InputScript {
pub fn char(self, c: char) -> Self { pub fn char(self, c: char) -> Self {
self.event(construct_key_event(KeyCode::Char(c))) self.event(construct_key_event(KeyCode::Char(c)))
} }
pub fn ctrl(self, c: char) -> Self { pub fn ctrl(self, c: char) -> Self {
self.modified_char(c, KeyModifiers::CONTROL) self.modified_char(c, KeyModifiers::CONTROL)
} }
pub fn alt(self, c: char) -> Self { pub fn alt(self, c: char) -> Self {
self.modified_char(c, KeyModifiers::ALT) self.modified_char(c, KeyModifiers::ALT)
} }
@ -28,11 +28,11 @@ impl InputScript {
pub fn tab(self) -> Self { pub fn tab(self) -> Self {
self.event(construct_key_event(KeyCode::Tab)) self.event(construct_key_event(KeyCode::Tab))
} }
pub fn modified_char(self, c: char, mods: KeyModifiers) -> Self { pub fn modified_char(self, c: char, mods: KeyModifiers) -> Self {
self.event(construct_modified_key_event(KeyCode::Char(c), mods)) self.event(construct_modified_key_event(KeyCode::Char(c), mods))
} }
pub fn event(mut self, evt: Event) -> Self { pub fn event(mut self, evt: Event) -> Self {
self.events.push(evt); self.events.push(evt);
self self
@ -257,7 +257,9 @@ fn test_input_navitation_enter_key() {
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
let row = ws.book.location.row; let row = ws.book.location.row;
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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"); .expect("Failed to handle enter key");
assert_eq!(row + 1, ws.book.location.row); 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"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
let col = dbg!(ws.book.location.col); let col = dbg!(ws.book.location.col);
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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"); .expect("Failed to handle enter key");
assert_eq!(col + 1, ws.book.location.col); 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"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
let row = ws.book.location.row; let row = ws.book.location.row;
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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"); .expect("Failed to handle enter key");
assert_eq!(row + 1, ws.book.location.row); assert_eq!(row + 1, ws.book.location.row);
InputScript::default().event(construct_modified_key_event( InputScript::default()
KeyCode::Enter, .event(construct_modified_key_event(
KeyModifiers::SHIFT, KeyCode::Enter,
)).run(&mut ws) KeyModifiers::SHIFT,
.expect("Failed to handle enter key"); ))
.run(&mut ws)
.expect("Failed to handle enter key");
assert_eq!(row, ws.book.location.row); 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"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
let col = dbg!(ws.book.location.col); let col = dbg!(ws.book.location.col);
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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"); .expect("Failed to handle enter key");
assert_eq!(col + 1, ws.book.location.col); assert_eq!(col + 1, ws.book.location.col);
InputScript::default().event(construct_modified_key_event( InputScript::default()
KeyCode::Tab, .event(construct_modified_key_event(
KeyModifiers::SHIFT, KeyCode::Tab,
)).run(&mut ws) KeyModifiers::SHIFT,
.expect("Failed to handle enter key"); ))
.run(&mut ws)
.expect("Failed to handle enter key");
assert_eq!(col, ws.book.location.col); assert_eq!(col, ws.book.location.col);
} }
@ -312,16 +324,19 @@ macro_rules! assert_help_dialog {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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"); .expect("Failed to handle 'i' key");
assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last());
let edit_help = ws.render_help_text(); 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"); .expect("Failed to handle 'alt-h' key event");
assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last());
assert_eq!(edit_help, ws.state.popup); assert_eq!(edit_help, ws.state.popup);
$exit.run(&mut ws) $exit.run(&mut ws).expect("Failed to handle key event");
.expect("Failed to handle key event");
assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); 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"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
let help_text = ws.render_help_text(); 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"); .expect("Failed to handle 'alt-h' key event");
assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last());
assert_eq!(help_text, ws.state.popup); assert_eq!(help_text, ws.state.popup);
@ -363,11 +380,15 @@ fn test_command_mode_help_keycode() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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"); .expect("Failed to handle ':' key");
assert_eq!(Some(&Modality::Command), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Command), ws.state.modality_stack.last());
let edit_help = ws.render_help_text(); 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"); .expect("Failed to handle 'alt-h' key event");
assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last());
assert_eq!(edit_help, ws.state.popup); assert_eq!(edit_help, ws.state.popup);
@ -378,23 +399,36 @@ fn test_edit_mode_esc_keycode() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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"); .expect("Failed to handle 'i' key");
assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); 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"); .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")); assert_eq!("", ws.text_area.lines().join("\n"));
} }
#[test] #[test]
fn test_navigation_numeric_prefix() fn test_navigation_numeric_prefix() {
{
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book
ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); .new_sheet(Some("Sheet2"))
.expect("failed to create sheet2");
ws.book
.new_sheet(Some("Sheet3"))
.expect("failed to create sheet3");
InputScript::default() InputScript::default()
.char('2') .char('2')
.char('3') .char('3')
@ -405,13 +439,16 @@ fn test_navigation_numeric_prefix()
} }
#[test] #[test]
fn test_navigation_numeric_prefix_cancel() fn test_navigation_numeric_prefix_cancel() {
{
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book
ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); .new_sheet(Some("Sheet2"))
.expect("failed to create sheet2");
ws.book
.new_sheet(Some("Sheet3"))
.expect("failed to create sheet3");
InputScript::default() InputScript::default()
.char('2') .char('2')
.char('3') .char('3')
@ -423,23 +460,38 @@ fn test_navigation_numeric_prefix_cancel()
} }
#[test] #[test]
fn test_navigation_tab_next_numeric_prefix() fn test_navigation_tab_next_numeric_prefix() {
{
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2"); ws.book
ws.book.new_sheet(Some("Sheet3")).expect("failed to create sheet3"); .new_sheet(Some("Sheet2"))
InputScript::default().char('2').run(&mut ws) .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"); .expect("Failed to handle '2' key event");
assert_eq!(2, ws.state.get_n_prefix()); 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"); .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()); 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"); .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] #[test]
@ -447,51 +499,89 @@ fn test_range_copy() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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(); 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"); .expect("Failed to handle 'Ctrl-r' key event");
assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); 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.start.is_none());
assert!(ws.state.range_select.end.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"); .expect("Failed to handle key sequence");
assert_eq!(Some(Address {row:1, col:2, }), ws.state.range_select.start); assert_eq!(
Some(Address { row: 1, col: 2 }),
InputScript::default().char('j').char(' ').run(&mut ws) ws.state.range_select.start
);
InputScript::default()
.char('j')
.char(' ')
.run(&mut ws)
.expect("Failed to handle key sequence"); .expect("Failed to handle key sequence");
assert!(ws.state.range_select.original_location.is_none()); assert!(ws.state.range_select.original_location.is_none());
assert_eq!(Some(Address {row:1, col:2, }), ws.state.range_select.start); assert_eq!(
assert_eq!(Some(Address {row:2, col:2, }), ws.state.range_select.end); 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!(original_loc, ws.book.location);
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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(); let original_loc_2 = ws.book.location.clone();
assert_eq!(Address { row: 5, col: 5 }, original_loc_2); 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"); .expect("Failed to handle 'v' key event");
assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); 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.start.is_some());
assert!(ws.state.range_select.end.is_none()); 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"); .expect("Failed to handle key sequence");
assert_eq!(Some(Address {row:5, col: 5, }), ws.state.range_select.start); assert_eq!(
Some(Address { row: 5, col: 5 }),
InputScript::default().char('k').char(' ').run(&mut ws) ws.state.range_select.start
);
InputScript::default()
.char('k')
.char(' ')
.run(&mut ws)
.expect("Failed to handle key sequence"); .expect("Failed to handle key sequence");
assert!(ws.state.range_select.original_location.is_none()); assert!(ws.state.range_select.original_location.is_none());
assert_eq!(Some(Address {row:5, col:5, }), ws.state.range_select.start); assert_eq!(
assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.end); Some(Address { row: 5, col: 5 }),
assert_eq!(Address {row:4, col:5, }, ws.book.location); 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()); 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 = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last()); 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"); .expect("Failed to handle 'e' key event");
assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last()); 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"); .expect("Failed to handle 'Ctrl-r' key event");
assert_eq!(Some(&Modality::RangeSelect), ws.state.modality_stack.last()); 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()); assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
InputScript::default() InputScript::default()
.char('j') .char('j')
.char('j').run(&mut ws) .char('j')
.run(&mut ws)
.expect("failed to handle event sequence"); .expect("failed to handle event sequence");
assert_eq!(ws.book.location, Address { row: 3, col: 1 }); assert_eq!(ws.book.location, Address { row: 3, col: 1 });
InputScript::default() InputScript::default()
@ -535,7 +630,8 @@ fn test_h_j_k_l_movement() {
InputScript::default() InputScript::default()
.char('2') .char('2')
.char('j') .char('j')
.char('l').run(&mut ws) .char('l')
.run(&mut ws)
.expect("failed to handle event sequence"); .expect("failed to handle event sequence");
assert_eq!(ws.book.location, Address { row: 3, col: 2 }); assert_eq!(ws.book.location, Address { row: 3, col: 2 });
InputScript::default() InputScript::default()
@ -558,25 +654,29 @@ macro_rules! assert_copy_paste {
assert_copy_paste!($c, $p, $source, $expected) assert_copy_paste!($c, $p, $source, $expected)
}; };
($c: expr, $p: expr, $source: expr, $expected: expr) => {{ ($c: expr, $p: expr, $source: expr, $expected: expr) => {{
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
InputScript::default() InputScript::default()
.char('j') .char('j')
.char('l') .char('l')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run script"); .expect("Failed to run script");
ws.book.edit_current_cell($source).expect("Failed to edit cell"); ws.book
ws.book.evaluate(); .edit_current_cell($source)
InputScript::default() .expect("Failed to edit cell");
.event($c) ws.book.evaluate();
.char('l') InputScript::default()
.char('j') .event($c)
.event($p) .char('l')
.run(&mut ws) .char('j')
.expect("Failed to run script"); .event($p)
let copy = ws.book.get_current_cell_contents() .run(&mut ws)
.expect("Failed to get cell contents"); .expect("Failed to run script");
assert_eq!(copy, $expected); 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() { fn test_clear_cell() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); 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"); .expect("failed to edit cell");
ws.book.evaluate(); 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() InputScript::default()
.char('d') .char('d')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .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] #[test]
fn test_clear_cell_all() { fn test_clear_cell_all() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); 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"); .expect("failed to edit cell");
ws.book.evaluate(); 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() InputScript::default()
.char('D') .char('D')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .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] #[test]
fn test_sheet_navigation() { fn test_sheet_navigation() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); 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
ws.book.new_sheet(Some("sheet 3")).expect("Failed to set sheet name"); .new_sheet(Some("sheet 2"))
ws.book.new_sheet(Some("sheet 4")).expect("Failed to set sheet name"); .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() InputScript::default()
.ctrl('n') .ctrl('n')
.ctrl('n') .ctrl('n')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .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() InputScript::default()
.ctrl('p') .ctrl('p')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .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] #[test]
@ -677,13 +811,19 @@ fn test_sheet_column_sizing() {
.ctrl('l') .ctrl('l')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .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() InputScript::default()
.char('1') .char('1')
.ctrl('h') .ctrl('h')
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .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] #[test]
@ -701,9 +841,16 @@ fn test_quit() {
fn test_cell_replace() { fn test_cell_replace() {
let mut ws = let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook"); 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"); ws.book
assert_eq!("foo", .edit_current_cell("foo")
ws.book.get_current_cell_contents().expect("failed to get cell contents").as_str()); .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() InputScript::default()
.char('s') .char('s')
.char('b') .char('b')
@ -712,7 +859,55 @@ fn test_cell_replace() {
.enter() .enter()
.run(&mut ws) .run(&mut ws)
.expect("Failed to run input script"); .expect("Failed to run input script");
assert_eq!("bar", assert_eq!(
ws.book.get_current_cell_contents().expect("failed to get cell contents").as_str()); "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());
}