2024-11-30 18:45:12 -05:00
|
|
|
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
|
|
|
|
|
|
|
use crate::ui::Modality;
|
|
|
|
|
2024-11-21 15:45:22 -05:00
|
|
|
use super::cmd::{parse, Cmd};
|
2024-11-30 18:45:12 -05:00
|
|
|
use super::Workspace;
|
2024-11-21 15:45:22 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_cmd() {
|
|
|
|
let input = "write foo.xlsx";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::Write(Some("foo.xlsx")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_short_write_cmd() {
|
|
|
|
let input = "w foo.xlsx";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::Write(Some("foo.xlsx")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-11-21 20:55:55 -05:00
|
|
|
fn test_insert_rows_cmd() {
|
|
|
|
let input = "insert-rows 1";
|
2024-11-21 15:45:22 -05:00
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::InsertRow(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-11-21 20:55:55 -05:00
|
|
|
fn test_insert_rows_cmd_short() {
|
2024-11-21 15:45:22 -05:00
|
|
|
let input = "ir 1";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::InsertRow(1));
|
|
|
|
}
|
2024-11-21 20:55:55 -05:00
|
|
|
|
2024-11-22 18:01:26 -05:00
|
|
|
#[test]
|
2024-11-21 20:55:55 -05:00
|
|
|
fn test_insert_cols_cmd() {
|
|
|
|
let input = "insert-cols 1";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::InsertColumns(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_insert_cols_cmd_short() {
|
|
|
|
let input = "ic 1";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::InsertColumns(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_edit_cmd() {
|
|
|
|
let input = "edit path.txt";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::Edit("path.txt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_edit_cmd_short() {
|
|
|
|
let input = "e path.txt";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::Edit("path.txt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_help_cmd() {
|
|
|
|
let input = "help topic";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::Help(Some("topic")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_help_cmd_short() {
|
|
|
|
let input = "? topic";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::Help(Some("topic")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quit_cmd_short() {
|
|
|
|
let input = "q";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::Quit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quit_cmd() {
|
|
|
|
let input = "quit";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::Quit);
|
|
|
|
}
|
2024-11-23 21:46:11 -05:00
|
|
|
|
2024-11-30 18:15:42 -05:00
|
|
|
#[test]
|
|
|
|
fn test_cmd_new_sheet_with_name() {
|
|
|
|
let input = "new-sheet test";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::NewSheet(Some("test")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_new_sheet_no_name() {
|
|
|
|
let input = "new-sheet";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::NewSheet(None));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_select_sheet_with_name() {
|
|
|
|
let input = "select-sheet test";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::SelectSheet("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_rename_sheet_with_name() {
|
|
|
|
let input = "rename-sheet test";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::RenameSheet(None, "test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_rename_sheet_with_idx_and_name() {
|
|
|
|
let input = "rename-sheet 0 test";
|
|
|
|
let result = parse(input);
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let output = result.unwrap();
|
|
|
|
assert!(output.is_some());
|
|
|
|
let cmd = output.unwrap();
|
|
|
|
assert_eq!(cmd, Cmd::RenameSheet(Some(0), "test"));
|
|
|
|
}
|
|
|
|
|
2024-11-30 18:45:12 -05:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2024-11-23 21:46:11 -05:00
|
|
|
// TODO(zaphar): Interaction testing for input.
|
2024-11-30 18:45:12 -05:00
|
|
|
#[test]
|
|
|
|
fn test_input_navitation_enter_key() {
|
|
|
|
let mut ws =
|
|
|
|
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))
|
|
|
|
.expect("Failed to handle enter key");
|
|
|
|
assert_eq!(row + 1, ws.book.location.row);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_input_navitation_tab_key() {
|
|
|
|
let mut ws =
|
|
|
|
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))
|
|
|
|
.expect("Failed to handle enter key");
|
|
|
|
assert_eq!(col + 1, ws.book.location.col);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 row = ws.book.location.row;
|
|
|
|
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
|
|
|
|
ws.handle_input(construct_key_event(KeyCode::Enter))
|
|
|
|
.expect("Failed to handle enter key");
|
|
|
|
assert_eq!(row + 1, ws.book.location.row);
|
|
|
|
ws.handle_input(construct_modified_key_event(KeyCode::Enter, KeyModifiers::SHIFT))
|
|
|
|
.expect("Failed to handle enter key");
|
|
|
|
assert_eq!(row, ws.book.location.row);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 col = dbg!(ws.book.location.col);
|
|
|
|
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
|
|
|
|
ws.handle_input(construct_key_event(KeyCode::Tab))
|
|
|
|
.expect("Failed to handle enter key");
|
|
|
|
assert_eq!(col + 1, ws.book.location.col);
|
|
|
|
ws.handle_input(construct_modified_key_event(KeyCode::Tab, KeyModifiers::SHIFT))
|
|
|
|
.expect("Failed to handle enter key");
|
|
|
|
assert_eq!(col, ws.book.location.col);
|
|
|
|
}
|