mirror of
https://github.com/zaphar/sheetsui.git
synced 2025-07-23 05:19:48 -04:00
Compare commits
7 Commits
e984d7324c
...
a65ad974ce
Author | SHA1 | Date | |
---|---|---|---|
a65ad974ce | |||
19d01f057b | |||
282df18177 | |||
babe2525a0 | |||
7bbdc26f13 | |||
92b02eea78 | |||
247674530b |
2
Makefile
2
Makefile
@ -8,7 +8,7 @@ build: $(rust-files)
|
|||||||
cargo build
|
cargo build
|
||||||
|
|
||||||
tarpaulin-report.%: $(rust_files)
|
tarpaulin-report.%: $(rust_files)
|
||||||
cargo tarpaulin --skip-clean --test --out $*
|
cargo tarpaulin --skip-clean --engine llvm --out $*
|
||||||
|
|
||||||
cover: tarpaulin-report.html
|
cover: tarpaulin-report.html
|
||||||
|
|
||||||
|
324
src/ui/test.rs
324
src/ui/test.rs
@ -7,6 +7,63 @@ use crate::ui::{Address, Modality};
|
|||||||
use super::cmd::{parse, Cmd};
|
use super::cmd::{parse, Cmd};
|
||||||
use super::Workspace;
|
use super::Workspace;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct InputScript{
|
||||||
|
events: Vec<Event>
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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
|
||||||
|
}
|
||||||
|
|
||||||
|
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<Option<ExitCode>> {
|
||||||
|
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]
|
#[test]
|
||||||
fn test_write_cmd() {
|
fn test_write_cmd() {
|
||||||
let input = "write foo.xlsx";
|
let input = "write foo.xlsx";
|
||||||
@ -194,50 +251,13 @@ fn test_cmd_rename_sheet_with_idx_and_name() {
|
|||||||
assert_eq!(cmd, Cmd::RenameSheet(Some(0), "test"));
|
assert_eq!(cmd, Cmd::RenameSheet(Some(0), "test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct InputScript{
|
|
||||||
events: Vec<Event>
|
|
||||||
}
|
|
||||||
|
|
||||||
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<Option<ExitCode>> {
|
|
||||||
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]
|
#[test]
|
||||||
fn test_input_navitation_enter_key() {
|
fn test_input_navitation_enter_key() {
|
||||||
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");
|
||||||
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());
|
||||||
ws.handle_input(construct_key_event(KeyCode::Enter))
|
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);
|
||||||
}
|
}
|
||||||
@ -248,7 +268,7 @@ 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());
|
||||||
ws.handle_input(construct_key_event(KeyCode::Tab))
|
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);
|
||||||
}
|
}
|
||||||
@ -259,13 +279,13 @@ 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());
|
||||||
ws.handle_input(construct_key_event(KeyCode::Enter))
|
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);
|
||||||
ws.handle_input(construct_modified_key_event(
|
InputScript::default().event(construct_modified_key_event(
|
||||||
KeyCode::Enter,
|
KeyCode::Enter,
|
||||||
KeyModifiers::SHIFT,
|
KeyModifiers::SHIFT,
|
||||||
))
|
)).run(&mut ws)
|
||||||
.expect("Failed to handle enter key");
|
.expect("Failed to handle enter key");
|
||||||
assert_eq!(row, ws.book.location.row);
|
assert_eq!(row, ws.book.location.row);
|
||||||
}
|
}
|
||||||
@ -276,30 +296,54 @@ 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());
|
||||||
ws.handle_input(construct_key_event(KeyCode::Tab))
|
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);
|
||||||
ws.handle_input(construct_modified_key_event(
|
InputScript::default().event(construct_modified_key_event(
|
||||||
KeyCode::Tab,
|
KeyCode::Tab,
|
||||||
KeyModifiers::SHIFT,
|
KeyModifiers::SHIFT,
|
||||||
))
|
)).run(&mut ws)
|
||||||
.expect("Failed to handle enter key");
|
.expect("Failed to handle enter key");
|
||||||
assert_eq!(col, ws.book.location.col);
|
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]
|
#[test]
|
||||||
fn test_edit_mode_help_keycode() {
|
fn test_edit_mode_help_keycode_esc() {
|
||||||
let mut ws =
|
assert_help_dialog!(InputScript::default().esc());
|
||||||
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')))
|
#[test]
|
||||||
.expect("Failed to handle 'i' key");
|
fn test_edit_mode_help_keycode_enter() {
|
||||||
assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last());
|
assert_help_dialog!(InputScript::default().enter());
|
||||||
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");
|
#[test]
|
||||||
assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last());
|
fn test_edit_mode_help_keycode_q() {
|
||||||
assert_eq!(edit_help, ws.state.popup);
|
assert_help_dialog!(InputScript::default().char('q'));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_edit_mode_help_keycode_alt_h() {
|
||||||
|
assert_help_dialog!(InputScript::default().alt('h'));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -308,8 +352,8 @@ 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();
|
||||||
ws.handle_input(construct_modified_key_event(KeyCode::Char('h'), KeyModifiers::ALT))
|
InputScript::default().alt('h').run(&mut ws)
|
||||||
.expect("Failed to handle 'ctrl-?' 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);
|
||||||
}
|
}
|
||||||
@ -319,12 +363,12 @@ 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());
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char(':')))
|
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();
|
||||||
ws.handle_input(construct_modified_key_event(KeyCode::Char('h'), KeyModifiers::ALT))
|
InputScript::default().alt('h').run(&mut ws)
|
||||||
.expect("Failed to handle 'ctrl-?' 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);
|
||||||
}
|
}
|
||||||
@ -334,13 +378,11 @@ 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());
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char('i')))
|
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());
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char('a')))
|
InputScript::default().char('a').esc().run(&mut ws)
|
||||||
.expect("Failed to handle 'a' key event");
|
.expect("Failed to handle key squence");
|
||||||
ws.handle_input(construct_key_event(KeyCode::Esc))
|
|
||||||
.expect("Failed to handle 'esc' key event");
|
|
||||||
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"));
|
||||||
}
|
}
|
||||||
@ -354,9 +396,9 @@ fn test_navigation_numeric_prefix()
|
|||||||
ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2");
|
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("Sheet3")).expect("failed to create sheet3");
|
||||||
InputScript::default()
|
InputScript::default()
|
||||||
.add_char('2')
|
.char('2')
|
||||||
.add_char('3')
|
.char('3')
|
||||||
.add_char('9')
|
.char('9')
|
||||||
.run(&mut ws)
|
.run(&mut ws)
|
||||||
.expect("Failed to run script");
|
.expect("Failed to run script");
|
||||||
assert_eq!(239, ws.state.get_n_prefix());
|
assert_eq!(239, ws.state.get_n_prefix());
|
||||||
@ -371,10 +413,10 @@ fn test_navigation_numeric_prefix_cancel()
|
|||||||
ws.book.new_sheet(Some("Sheet2")).expect("failed to create sheet2");
|
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("Sheet3")).expect("failed to create sheet3");
|
||||||
InputScript::default()
|
InputScript::default()
|
||||||
.add_char('2')
|
.char('2')
|
||||||
.add_char('3')
|
.char('3')
|
||||||
.add_char('9')
|
.char('9')
|
||||||
.add_event(construct_key_event(KeyCode::Esc))
|
.esc()
|
||||||
.run(&mut ws)
|
.run(&mut ws)
|
||||||
.expect("Failed to run script");
|
.expect("Failed to run script");
|
||||||
assert_eq!(1, ws.state.get_n_prefix());
|
assert_eq!(1, ws.state.get_n_prefix());
|
||||||
@ -388,14 +430,14 @@ fn test_navigation_tab_next_numeric_prefix()
|
|||||||
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.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("Sheet3")).expect("failed to create sheet3");
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char('2')))
|
InputScript::default().char('2').run(&mut ws)
|
||||||
.expect("Failed to handle '3' 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());
|
||||||
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");
|
.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());
|
||||||
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");
|
.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"));
|
||||||
}
|
}
|
||||||
@ -408,23 +450,19 @@ fn test_range_copy() {
|
|||||||
|
|
||||||
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();
|
||||||
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");
|
.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());
|
||||||
|
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char('l')))
|
InputScript::default().char('l').char(' ').run(&mut ws)
|
||||||
.expect("Failed to handle 'l' key event");
|
.expect("Failed to handle key sequence");
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char(' ')))
|
|
||||||
.expect("Failed to handle ' ' key event");
|
|
||||||
assert_eq!(Some(Address {row:1, col:2, }), ws.state.range_select.start);
|
assert_eq!(Some(Address {row:1, col:2, }), ws.state.range_select.start);
|
||||||
|
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char('j')))
|
InputScript::default().char('j').char(' ').run(&mut ws)
|
||||||
.expect("Failed to handle 'j' key event");
|
.expect("Failed to handle key sequence");
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char(' ')))
|
|
||||||
.expect("Failed to handle ' ' key event");
|
|
||||||
|
|
||||||
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!(Some(Address {row:1, col:2, }), ws.state.range_select.start);
|
||||||
@ -436,28 +474,24 @@ fn test_range_copy() {
|
|||||||
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);
|
||||||
|
|
||||||
ws.handle_input(construct_modified_key_event(KeyCode::Char('r'), KeyModifiers::CONTROL))
|
InputScript::default().char('v').run(&mut ws)
|
||||||
.expect("Failed to handle 'Ctrl-r' 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_none());
|
assert!(ws.state.range_select.start.is_some());
|
||||||
assert!(ws.state.range_select.end.is_none());
|
assert!(ws.state.range_select.end.is_none());
|
||||||
|
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char('h')))
|
InputScript::default().char('h').char(' ').run(&mut ws)
|
||||||
.expect("Failed to handle 'h' key event");
|
.expect("Failed to handle key sequence");
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char(' ')))
|
assert_eq!(Some(Address {row:5, col: 5, }), ws.state.range_select.start);
|
||||||
.expect("Failed to handle ' ' key event");
|
|
||||||
assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.start);
|
|
||||||
|
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char('k')))
|
InputScript::default().char('k').char(' ').run(&mut ws)
|
||||||
.expect("Failed to handle 'k' key event");
|
.expect("Failed to handle key sequence");
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char(' ')))
|
|
||||||
.expect("Failed to handle ' ' key event");
|
|
||||||
|
|
||||||
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:4, }), ws.state.range_select.start);
|
assert_eq!(Some(Address {row:5, col:5, }), ws.state.range_select.start);
|
||||||
assert_eq!(Some(Address {row:4, col:4, }), ws.state.range_select.end);
|
assert_eq!(Some(Address {row:5, col:4, }), ws.state.range_select.end);
|
||||||
assert_eq!(original_loc_2, ws.book.location);
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -466,10 +500,10 @@ 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());
|
||||||
ws.handle_input(construct_key_event(KeyCode::Char('e')))
|
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());
|
||||||
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");
|
.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());
|
||||||
}
|
}
|
||||||
@ -480,19 +514,39 @@ fn test_gg_movement() {
|
|||||||
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()
|
InputScript::default()
|
||||||
.add_char('j')
|
.char('j')
|
||||||
.add_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()
|
||||||
.add_char('l')
|
.char('l')
|
||||||
.add_char('g')
|
.char('g')
|
||||||
.add_char('g')
|
.char('g')
|
||||||
.run(&mut ws)
|
.run(&mut ws)
|
||||||
.expect("failed to handle event sequence");
|
.expect("failed to handle event sequence");
|
||||||
assert_eq!(ws.book.location, Address { row: 1, col: 2 });
|
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('h')
|
||||||
|
.char('2')
|
||||||
|
.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 {
|
macro_rules! assert_copy_paste {
|
||||||
($c: expr, $p: expr, $source: expr,) => {
|
($c: expr, $p: expr, $source: expr,) => {
|
||||||
assert_copy_paste!($c, $p, $source, $source)
|
assert_copy_paste!($c, $p, $source, $source)
|
||||||
@ -507,17 +561,17 @@ macro_rules! assert_copy_paste {
|
|||||||
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()
|
||||||
.add_char('j')
|
.char('j')
|
||||||
.add_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.edit_current_cell($source).expect("Failed to edit cell");
|
||||||
ws.book.evaluate();
|
ws.book.evaluate();
|
||||||
InputScript::default()
|
InputScript::default()
|
||||||
.add_event($c)
|
.event($c)
|
||||||
.add_char('l')
|
.char('l')
|
||||||
.add_char('j')
|
.char('j')
|
||||||
.add_event($p)
|
.event($p)
|
||||||
.run(&mut ws)
|
.run(&mut ws)
|
||||||
.expect("Failed to run script");
|
.expect("Failed to run script");
|
||||||
let copy = ws.book.get_current_cell_contents()
|
let copy = ws.book.get_current_cell_contents()
|
||||||
@ -573,7 +627,7 @@ fn test_clear_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()
|
||||||
.add_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"));
|
||||||
@ -588,7 +642,7 @@ fn test_clear_cell_all() {
|
|||||||
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()
|
||||||
.add_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"));
|
||||||
@ -602,13 +656,13 @@ fn test_sheet_navigation() {
|
|||||||
ws.book.new_sheet(Some("sheet 3")).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 4")).expect("Failed to set sheet name");
|
||||||
InputScript::default()
|
InputScript::default()
|
||||||
.add_modified_char('n', KeyModifiers::CONTROL)
|
.ctrl('n')
|
||||||
.add_modified_char('n', KeyModifiers::CONTROL)
|
.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()
|
||||||
.add_modified_char('p', KeyModifiers::CONTROL)
|
.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"));
|
||||||
@ -619,14 +673,14 @@ fn test_sheet_column_sizing() {
|
|||||||
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()
|
||||||
.add_char('3')
|
.char('3')
|
||||||
.add_modified_char('l', KeyModifiers::CONTROL)
|
.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()
|
||||||
.add_char('1')
|
.char('1')
|
||||||
.add_modified_char('h', KeyModifiers::CONTROL)
|
.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"));
|
||||||
@ -637,8 +691,28 @@ fn test_quit() {
|
|||||||
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");
|
||||||
let result = InputScript::default()
|
let result = InputScript::default()
|
||||||
.add_char('q')
|
.char('q')
|
||||||
.run(&mut ws)
|
.run(&mut ws)
|
||||||
.expect("Failed to run input script");
|
.expect("Failed to run input script");
|
||||||
assert!(result.is_some());
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user