From 53d4757ba728133474a680d6f5d09e6a02c39b52 Mon Sep 17 00:00:00 2001 From: Eivind Kvedalen Date: Sat, 24 Mar 2018 09:22:52 +0100 Subject: [PATCH] Spreadsheet/Expressions: Fixed issue #3363. --- src/App/Expression.cpp | 14 ++++++++++++-- src/Mod/Spreadsheet/TestSpreadsheet.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/App/Expression.cpp b/src/App/Expression.cpp index 7720adc06c..6f6fb6cbab 100644 --- a/src/App/Expression.cpp +++ b/src/App/Expression.cpp @@ -1589,7 +1589,17 @@ Expression *ConditionalExpression::simplify() const std::string ConditionalExpression::toString() const { - return condition->toString() + " ? " + trueExpr->toString() + " : " + falseExpr->toString(); + std::string cstr = condition->toString(); + std::string tstr = trueExpr->toString(); + std::string fstr = falseExpr->toString(); + + if (trueExpr->priority() <= priority()) + tstr = "(" + tstr + ")"; + + if (falseExpr->priority() <= priority()) + fstr = "(" + fstr + ")"; + + return cstr + " ? " + tstr + " : " + fstr; } Expression *ConditionalExpression::copy() const @@ -1599,7 +1609,7 @@ Expression *ConditionalExpression::copy() const int ConditionalExpression::priority() const { - return 0; + return 2; } void ConditionalExpression::getDeps(std::set &props) const diff --git a/src/Mod/Spreadsheet/TestSpreadsheet.py b/src/Mod/Spreadsheet/TestSpreadsheet.py index 11d41a5afc..56a0acf92e 100644 --- a/src/Mod/Spreadsheet/TestSpreadsheet.py +++ b/src/Mod/Spreadsheet/TestSpreadsheet.py @@ -832,6 +832,25 @@ class SpreadsheetCases(unittest.TestCase): self.assertEqual(sheet.A1, Units.Quantity('1')) self.assertEqual(sheet.A2, Units.Quantity('1 kg/mm')) + def testIssue3363(self): + """ Regression test for issue 3363; Nested conditionals statement fails with additional conditional statement in false-branch""" + sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet') + sheet.set('A1', '1') + sheet.set('B1', '=A1==1?11:(A1==2?12:13)') + sheet.set('C1', '=A1==1?(A1==2?12:13) : 11') + self.doc.recompute() + + # Save and close first document + self.doc.saveAs(self.TempPath + os.sep + 'conditionals.fcstd') + FreeCAD.closeDocument(self.doc.Name) + + # Open documents again + self.doc = FreeCAD.openDocument(self.TempPath + os.sep + 'conditionals.fcstd') + + sheet = self.doc.getObject('Spreadsheet') + self.assertEqual(sheet.getContents('B1'), '=A1 == 1 ? 11 : (A1 == 2 ? 12 : 13)') + self.assertEqual(sheet.getContents('C1'), '=A1 == 1 ? (A1 == 2 ? 12 : 13) : 11') + def tearDown(self): #closing doc FreeCAD.closeDocument(self.doc.Name)