From 61fc4d0329a186a6a0b5e0654c14c1945654e493 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Thu, 26 Nov 2009 02:43:47 +0000 Subject: [PATCH] Added test to confirm proper handling of MissingArgEval when returned from IF() or CHOOSE() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@884374 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/TestWorkbookEvaluator.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java b/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java index 41aa0ab57b..7e9149adfa 100644 --- a/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java +++ b/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java @@ -17,6 +17,7 @@ package org.apache.poi.ss.formula; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -27,13 +28,17 @@ import org.apache.poi.hssf.record.formula.DeletedRef3DPtg; import org.apache.poi.hssf.record.formula.IntPtg; import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.RefErrorPtg; +import org.apache.poi.hssf.record.formula.eval.BlankEval; import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.MissingArgEval; import org.apache.poi.hssf.record.formula.eval.NumberEval; import org.apache.poi.hssf.record.formula.eval.ValueEval; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; +import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.CellValue; /** * Tests {@link WorkbookEvaluator}. @@ -159,4 +164,42 @@ public class TestWorkbookEvaluator extends TestCase { HSSFCell cell = wb.getSheetAt(sheetIndex).getRow(rowIndex).getCell(columnIndex); assertEquals(expectedFormula, cell.getCellFormula()); } + + /** + * This test makes sure that any {@link MissingArgEval} that propagates to + * the result of a function gets translated to {@link BlankEval}. + */ + public void testMissingArg() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet("Sheet1"); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); + cell.setCellFormula("1+IF(1,,)"); + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + CellValue cv; + try { + cv = fe.evaluate(cell); + } catch (RuntimeException e) { + throw new AssertionFailedError("Missing arg result not being handled correctly."); + } + assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType()); + // adding blank to 1.0 gives 1.0 + assertEquals(1.0, cv.getNumberValue(), 0.0); + + // check with string operand + cell.setCellFormula("\"abc\"&IF(1,,)"); + fe.notifySetFormula(cell); + cv = fe.evaluate(cell); + assertEquals(HSSFCell.CELL_TYPE_STRING, cv.getCellType()); + // adding blank to "abc" gives "abc" + assertEquals("abc", cv.getStringValue()); + + // check CHOOSE() + cell.setCellFormula("\"abc\"&CHOOSE(2,5,,9)"); + fe.notifySetFormula(cell); + cv = fe.evaluate(cell); + assertEquals(HSSFCell.CELL_TYPE_STRING, cv.getCellType()); + // adding blank to "abc" gives "abc" + assertEquals("abc", cv.getStringValue()); + } } -- 2.39.5