wip: more unit tests and some ui fixes

This commit is contained in:
Jeremy Wall 2024-11-18 18:00:16 -05:00
parent 285e614aec
commit 0fa37e2504
3 changed files with 48 additions and 13 deletions

View File

@ -131,6 +131,12 @@ impl Book {
self.model self.model
.insert_rows(self.current_sheet, row_idx as i32, count as i32) .insert_rows(self.current_sheet, row_idx as i32, count as i32)
.map_err(|e| anyhow!("Unable to insert row(s): {}", e))?; .map_err(|e| anyhow!("Unable to insert row(s): {}", e))?;
if self.location.row >= row_idx {
self.move_to(Address {
row: self.location.row + count,
col: self.location.col,
})?;
}
Ok(()) Ok(())
} }
@ -138,6 +144,12 @@ impl Book {
self.model self.model
.insert_columns(self.current_sheet, col_idx as i32, count as i32) .insert_columns(self.current_sheet, col_idx as i32, count as i32)
.map_err(|e| anyhow!("Unable to insert column(s): {}", e))?; .map_err(|e| anyhow!("Unable to insert column(s): {}", e))?;
if self.location.col >= col_idx {
self.move_to(Address {
row: self.location.row,
col: self.location.col + count,
})?;
}
Ok(()) Ok(())
} }
@ -206,8 +218,9 @@ impl Book {
impl Default for Book { impl Default for Book {
fn default() -> Self { fn default() -> Self {
let mut book = Book::new(Model::new_empty("default_name", "en", "America/New_York").unwrap()); let mut book =
book.update_entry(&Address{row: 1, col: 1}, "").unwrap(); Book::new(Model::new_empty("default_name", "en", "America/New_York").unwrap());
book.update_entry(&Address { row: 1, col: 1 }, "").unwrap();
book book
} }
} }

View File

@ -63,3 +63,29 @@ fn test_book_insert_cell_new_column() {
assert_eq!((1, 1, 1, 2), (min_row, max_row, min_column, max_column)); assert_eq!((1, 1, 1, 2), (min_row, max_row, min_column, max_column));
assert_eq!((1, 2), book.get_size().expect("Failed to get size")); assert_eq!((1, 2), book.get_size().expect("Failed to get size"));
} }
#[test]
fn test_book_insert_rows() {
let mut book = Book::default();
book.update_entry(&Address { row: 2, col: 2 }, "1")
.expect("failed to edit cell");
book.move_to(Address { row: 2, col: 2 }).expect("Failed to move to location");
assert_eq!((2, 2), book.get_size().expect("Failed to get size"));
book.insert_rows(1, 5).expect("Failed to insert rows");
assert_eq!((7, 2), book.get_size().expect("Failed to get size"));
assert_eq!(Address {row: 7, col: 2, }, book.location);
assert_eq!("1", book.get_current_cell_rendered().expect("Failed to get rendered content"));
}
#[test]
fn test_book_insert_columns() {
let mut book = Book::default();
book.update_entry(&Address { row: 2, col: 2 }, "1")
.expect("failed to edit cell");
book.move_to(Address { row: 2, col: 2 }).expect("Failed to move to location");
assert_eq!((2, 2), book.get_size().expect("Failed to get size"));
book.insert_columns(1, 5).expect("Failed to insert rows");
assert_eq!((2, 7), book.get_size().expect("Failed to get size"));
assert_eq!(Address {row: 2, col: 7, }, book.location);
assert_eq!("1", book.get_current_cell_rendered().expect("Failed to get rendered content"));
}

View File

@ -6,7 +6,6 @@ use crate::book::Book;
use anyhow::Result; use anyhow::Result;
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers}; use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use ironcalc::base::worksheet::WorksheetDimension;
use ratatui::{ use ratatui::{
self, self,
layout::{Constraint, Flex, Layout}, layout::{Constraint, Flex, Layout},
@ -81,14 +80,13 @@ impl<'ws> Workspace<'ws> {
} else { } else {
Book::default() Book::default()
}; };
//book.move_to(Address { row: 0, col: 0 })?;
Ok(Workspace::new(book, path.clone())) Ok(Workspace::new(book, path.clone()))
} }
pub fn move_down(&mut self) -> Result<()> { pub fn move_down(&mut self) -> Result<()> {
let mut loc = self.book.location.clone(); let mut loc = self.book.location.clone();
let WorksheetDimension { min_row: _, max_row, min_column: _, max_column: _ } = self.book.get_dimensions()?; let (row_count, _) = self.book.get_size()?;
if loc.row <= max_row as usize { if loc.row < row_count {
loc.row += 1; loc.row += 1;
self.book.move_to(loc)?; self.book.move_to(loc)?;
} }
@ -97,8 +95,7 @@ impl<'ws> Workspace<'ws> {
pub fn move_up(&mut self) -> Result<()> { pub fn move_up(&mut self) -> Result<()> {
let mut loc = self.book.location.clone(); let mut loc = self.book.location.clone();
let WorksheetDimension { min_row, max_row: _, min_column: _, max_column: _ } = self.book.get_dimensions()?; if loc.row > 1 {
if loc.row > min_row as usize {
loc.row -= 1; loc.row -= 1;
self.book.move_to(loc)?; self.book.move_to(loc)?;
} }
@ -107,8 +104,7 @@ impl<'ws> Workspace<'ws> {
pub fn move_left(&mut self) -> Result<()> { pub fn move_left(&mut self) -> Result<()> {
let mut loc = self.book.location.clone(); let mut loc = self.book.location.clone();
let WorksheetDimension { min_row: _, max_row: _, min_column, max_column: _ } = self.book.get_dimensions()?; if loc.col > 1 {
if loc.col > min_column as usize {
loc.col -= 1; loc.col -= 1;
self.book.move_to(loc)?; self.book.move_to(loc)?;
} }
@ -117,8 +113,8 @@ impl<'ws> Workspace<'ws> {
pub fn move_right(&mut self) -> Result<()> { pub fn move_right(&mut self) -> Result<()> {
let mut loc = self.book.location.clone(); let mut loc = self.book.location.clone();
let WorksheetDimension { min_row: _, max_row: _, min_column: _, max_column} = self.book.get_dimensions()?; let (_, col_count) = self.book.get_size()?;
if loc.col < max_column as usize { if loc.col < col_count {
loc.col += 1; loc.col += 1;
self.book.move_to(loc)?; self.book.move_to(loc)?;
} }
@ -206,7 +202,7 @@ impl<'ws> Workspace<'ws> {
} }
self.handle_movement_change(); self.handle_movement_change();
} }
KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => { KeyCode::Char('t') if key.modifiers == KeyModifiers::CONTROL => {
let (_, col_count) = self.book.get_size()?; let (_, col_count) = self.book.get_size()?;
self.book.update_entry(&Address {row: 1, col: col_count+1 }, "")?; self.book.update_entry(&Address {row: 1, col: col_count+1 }, "")?;
} }