FIX: Correct order when stringify -(A1^1.22) and (-A1)^1.22 (#484)

Fixes #483
This commit is contained in:
Nicolás Hatcher Andrés
2025-10-27 19:09:31 +01:00
committed by GitHub
parent c88bcb94ae
commit 1edfb2df1c
5 changed files with 44 additions and 3 deletions

View File

@@ -520,6 +520,7 @@ fn stringify(
let x = match **left {
BooleanKind(_)
| NumberKind(_)
| UnaryKind { .. }
| StringKind(_)
| ReferenceKind { .. }
| RangeKind { .. }
@@ -535,7 +536,6 @@ fn stringify(
| FunctionKind { .. }
| InvalidFunctionKind { .. }
| ArrayKind(_)
| UnaryKind { .. }
| ErrorKind(_)
| ParseErrorKind { .. }
| OpSumKind { .. }
@@ -630,7 +630,6 @@ fn stringify(
| OpRangeKind { .. }
| OpConcatenateKind { .. }
| OpProductKind { .. }
| OpPowerKind { .. }
| FunctionKind { .. }
| InvalidFunctionKind { .. }
| ArrayKind(_)
@@ -643,7 +642,7 @@ fn stringify(
| ParseErrorKind { .. }
| EmptyArgKind => false,
OpSumKind { .. } | UnaryKind { .. } => true,
OpPowerKind { .. } | OpSumKind { .. } | UnaryKind { .. } => true,
};
if needs_parentheses {
format!(

View File

@@ -3,6 +3,7 @@ mod test_arrays;
mod test_general;
mod test_implicit_intersection;
mod test_issue_155;
mod test_issue_483;
mod test_move_formula;
mod test_ranges;
mod test_stringify;

View File

@@ -0,0 +1,27 @@
#![allow(clippy::panic)]
use std::collections::HashMap;
use crate::expressions::parser::stringify::to_string;
use crate::expressions::parser::{Node, Parser};
use crate::expressions::types::CellReferenceRC;
#[test]
fn issue_483_parser() {
let worksheets = vec!["Sheet1".to_string()];
let mut parser = Parser::new(worksheets, vec![], HashMap::new());
// Reference cell is Sheet1!A1
let cell_reference = CellReferenceRC {
sheet: "Sheet1".to_string(),
row: 2,
column: 2,
};
let t = parser.parse("-(A1^1.22)", &cell_reference);
assert!(matches!(t, Node::UnaryKind { .. }));
assert_eq!(to_string(&t, &cell_reference), "-(A1^1.22)");
let t = parser.parse("-A1^1.22", &cell_reference);
assert!(matches!(t, Node::OpPowerKind { .. }));
assert_eq!(to_string(&t, &cell_reference), "-A1^1.22");
}

View File

@@ -68,6 +68,7 @@ mod test_geomean;
mod test_get_cell_content;
mod test_implicit_intersection;
mod test_issue_155;
mod test_issue_483;
mod test_ln;
mod test_log;
mod test_log10;

View File

@@ -0,0 +1,13 @@
#![allow(clippy::unwrap_used)]
use crate::test::util::new_empty_model;
#[test]
fn issue_155() {
let mut model = new_empty_model();
model._set("A1", "123");
model._set("D2", "=-(A1^1.22)");
model.evaluate();
assert_eq!(model._get_formula("D2"), "=-(A1^1.22)".to_string());
}