FIX: Add test for get_defined_name_list

Also uses the scope instead of the opaque sheet_id
This commit is contained in:
Nicolás Hatcher
2024-12-25 22:04:37 +01:00
committed by Nicolás Hatcher Andrés
parent 2ed5fb9bbc
commit 86213a8434
6 changed files with 92 additions and 12 deletions

View File

@@ -865,7 +865,11 @@ impl Model {
let worksheet_names = worksheets.iter().map(|s| s.get_name()).collect(); let worksheet_names = worksheets.iter().map(|s| s.get_name()).collect();
let defined_names = workbook.get_defined_names_with_scope(); let defined_names = workbook
.get_defined_names_with_scope()
.iter()
.map(|s| (s.0.to_owned(), s.1))
.collect();
// add all tables // add all tables
// let mut tables = Vec::new(); // let mut tables = Vec::new();
// for worksheet in worksheets { // for worksheet in worksheets {

View File

@@ -144,7 +144,12 @@ impl Model {
/// Reparses all formulas and defined names /// Reparses all formulas and defined names
pub(crate) fn reset_parsed_structures(&mut self) { pub(crate) fn reset_parsed_structures(&mut self) {
let defined_names = self.workbook.get_defined_names_with_scope(); let defined_names = self
.workbook
.get_defined_names_with_scope()
.iter()
.map(|s| (s.0.to_owned(), s.1))
.collect();
self.parser self.parser
.set_worksheets_and_names(self.workbook.get_worksheet_names(), defined_names); .set_worksheets_and_names(self.workbook.get_worksheet_names(), defined_names);
self.parsed_formulas = vec![]; self.parsed_formulas = vec![];

View File

@@ -15,6 +15,11 @@ fn create_defined_name() {
Ok("42".to_string()) Ok("42".to_string())
); );
assert_eq!(
model.get_defined_name_list(),
vec![("myName".to_string(), None, "Sheet1!$A$1".to_string())]
);
// delete it // delete it
model.delete_defined_name("myName", None).unwrap(); model.delete_defined_name("myName", None).unwrap();
assert_eq!( assert_eq!(
@@ -22,6 +27,8 @@ fn create_defined_name() {
Ok("#NAME?".to_string()) Ok("#NAME?".to_string())
); );
assert_eq!(model.get_defined_name_list().len(), 0);
model.undo().unwrap(); model.undo().unwrap();
assert_eq!( assert_eq!(
model.get_formatted_cell_value(0, 5, 7), model.get_formatted_cell_value(0, 5, 7),
@@ -324,3 +331,68 @@ fn invalid_formula() {
Ok("#NAME?".to_string()) Ok("#NAME?".to_string())
); );
} }
#[test]
fn undo_redo() {
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
model.set_user_input(0, 1, 1, "Hello").unwrap();
model.set_user_input(0, 2, 1, "Hola").unwrap();
model.set_user_input(0, 1, 2, r#"=MyName&"!""#).unwrap();
model
.new_defined_name("MyName", None, "Sheet1!$A$1")
.unwrap();
assert_eq!(
model.get_formatted_cell_value(0, 1, 2),
Ok("Hello!".to_string())
);
model.undo().unwrap();
assert_eq!(model.get_defined_name_list().len(), 0);
assert_eq!(
model.get_formatted_cell_value(0, 1, 2),
Ok("#NAME?".to_string())
);
model.redo().unwrap();
assert_eq!(model.get_defined_name_list().len(), 1);
assert_eq!(
model.get_formatted_cell_value(0, 1, 2),
Ok("Hello!".to_string())
);
model
.update_defined_name("MyName", None, "MyName", None, "Sheet1!$A$2")
.unwrap();
assert_eq!(model.get_defined_name_list().len(), 1);
assert_eq!(
model.get_formatted_cell_value(0, 1, 2),
Ok("Hola!".to_string())
);
model.undo().unwrap();
assert_eq!(model.get_defined_name_list().len(), 1);
assert_eq!(
model.get_formatted_cell_value(0, 1, 2),
Ok("Hello!".to_string())
);
model.redo().unwrap();
assert_eq!(model.get_defined_name_list().len(), 1);
assert_eq!(
model.get_formatted_cell_value(0, 1, 2),
Ok("Hola!".to_string())
);
let send_queue = model.flush_send_queue();
let mut model2 = UserModel::new_empty("model", "en", "UTC").unwrap();
model2.apply_external_diffs(&send_queue).unwrap();
assert_eq!(model2.get_defined_name_list().len(), 1);
assert_eq!(
model2.get_formatted_cell_value(0, 1, 2),
Ok("Hola!".to_string())
);
}

View File

@@ -13,8 +13,8 @@ use crate::{
}, },
model::Model, model::Model,
types::{ types::{
Alignment, BorderItem, CellType, Col, DefinedName, HorizontalAlignment, SheetProperties, Alignment, BorderItem, CellType, Col, HorizontalAlignment, SheetProperties, SheetState,
SheetState, Style, VerticalAlignment, Style, VerticalAlignment,
}, },
utils::is_valid_hex_color, utils::is_valid_hex_color,
}; };
@@ -1735,8 +1735,8 @@ impl UserModel {
} }
/// Returns the list of defined names /// Returns the list of defined names
pub fn get_defined_name_list(&self) -> Vec<DefinedName> { pub fn get_defined_name_list(&self) -> Vec<(String, Option<u32>, String)> {
self.model.workbook.defined_names.clone() self.model.workbook.get_defined_names_with_scope()
} }
/// Delete an existing defined name /// Delete an existing defined name

View File

@@ -29,7 +29,7 @@ impl Workbook {
} }
/// Returns the a list of defined names in the workbook with their scope /// Returns the a list of defined names in the workbook with their scope
pub(crate) fn get_defined_names_with_scope(&self) -> Vec<(String, Option<u32>)> { pub(crate) fn get_defined_names_with_scope(&self) -> Vec<(String, Option<u32>, String)> {
let sheet_id_index: Vec<u32> = self.worksheets.iter().map(|s| s.sheet_id).collect(); let sheet_id_index: Vec<u32> = self.worksheets.iter().map(|s| s.sheet_id).collect();
let defined_names = self let defined_names = self
@@ -45,7 +45,7 @@ impl Workbook {
// convert Option<usize> to Option<u32> // convert Option<usize> to Option<u32>
.map(|pos| pos as u32); .map(|pos| pos as u32);
(dn.name.clone(), index) (dn.name.clone(), index, dn.formula.clone())
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
defined_names defined_names

View File

@@ -568,12 +568,11 @@ impl Model {
.get_defined_name_list() .get_defined_name_list()
.iter() .iter()
.map(|s| DefinedName { .map(|s| DefinedName {
name: s.name.to_string(), name: s.0.to_owned(),
scope: s.sheet_id, scope: s.1,
formula: s.formula.to_string(), formula: s.2.to_owned(),
}) })
.collect(); .collect();
// Ok(data)
serde_wasm_bindgen::to_value(&data).map_err(|e| to_js_error(e.to_string())) serde_wasm_bindgen::to_value(&data).map_err(|e| to_js_error(e.to_string()))
} }