Spreadsheet/Expressions: Fixed issue #3363.

This commit is contained in:
Eivind Kvedalen
2018-03-24 09:22:52 +01:00
committed by wmayer
parent acbd182e15
commit 53d4757ba7
2 changed files with 31 additions and 2 deletions

View File

@@ -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<ObjectIdentifier> &props) const

View File

@@ -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)