Compare commits

...

4 Commits

Author SHA1 Message Date
e984d7324c wip: test coverage for
* sheet navigation
* column sizing
* quit
2024-12-28 10:53:59 -05:00
52d1909868 wip: add test for cell deletion 2024-12-28 10:53:59 -05:00
c5cbc1e75c wip: test coverage for copy paste 2024-12-28 10:53:59 -05:00
704f9f7746 feat: clear char queue on Esc 2024-12-28 10:51:21 -05:00
2 changed files with 217 additions and 23 deletions

View File

@ -652,6 +652,7 @@ impl<'ws> Workspace<'ws> {
match key.code {
KeyCode::Esc => {
self.state.reset_n_prefix();
self.state.char_queue.clear();
}
KeyCode::Char(d) if d.is_ascii_digit() => {
self.handle_numeric_prefix(d);
@ -691,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()?,
@ -727,12 +728,6 @@ impl<'ws> Workspace<'ws> {
Ok(())
})?;
}
KeyCode::Char('s')
if key.modifiers == KeyModifiers::HYPER
|| key.modifiers == KeyModifiers::SUPER =>
{
self.save_file()?;
}
KeyCode::Char('l') if key.modifiers == KeyModifiers::CONTROL => {
self.run_with_prefix(|ws: &mut Workspace<'_>| -> Result<()> {
let Address { row: _, col } = &ws.book.location;

View File

@ -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<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())
}
@ -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,166 @@ 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",
);
}
#[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"));
}
#[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());
}