]> source.dussan.org Git - poi.git/commitdiff
Added test to confirm proper handling of MissingArgEval when returned from IF() or...
authorJosh Micich <josh@apache.org>
Thu, 26 Nov 2009 02:43:47 +0000 (02:43 +0000)
committerJosh Micich <josh@apache.org>
Thu, 26 Nov 2009 02:43:47 +0000 (02:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@884374 13f79535-47bb-0310-9956-ffa450edef68

src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java

index 41aa0ab57b073943f23f9520df432515f3a6deeb..7e9149adfa456ee82a98e276f5b7ec5b23343858 100644 (file)
@@ -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());
+       }
 }