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
This commit is contained in:
Josh Micich 2009-11-26 02:43:47 +00:00
parent 6602faf140
commit 9bf6c8f8dc

View File

@ -17,6 +17,7 @@
package org.apache.poi.ss.formula; package org.apache.poi.ss.formula;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples; 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.IntPtg;
import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.hssf.record.formula.RefErrorPtg; 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.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.NumberEval;
import org.apache.poi.hssf.record.formula.eval.ValueEval; import org.apache.poi.hssf.record.formula.eval.ValueEval;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; 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.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellValue;
/** /**
* Tests {@link WorkbookEvaluator}. * Tests {@link WorkbookEvaluator}.
@ -159,4 +164,42 @@ public class TestWorkbookEvaluator extends TestCase {
HSSFCell cell = wb.getSheetAt(sheetIndex).getRow(rowIndex).getCell(columnIndex); HSSFCell cell = wb.getSheetAt(sheetIndex).getRow(rowIndex).getCell(columnIndex);
assertEquals(expectedFormula, cell.getCellFormula()); 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());
}
} }