diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java index ecce0635b..188ee52d8 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java @@ -55,7 +55,25 @@ package org.apache.poi.hssf.model; import junit.framework.TestCase; -import org.apache.poi.hssf.record.formula.*; +import org.apache.poi.hssf.record.formula.AbstractFunctionPtg; +import org.apache.poi.hssf.record.formula.AddPtg; +import org.apache.poi.hssf.record.formula.AttrPtg; +import org.apache.poi.hssf.record.formula.BoolPtg; +import org.apache.poi.hssf.record.formula.EqualPtg; +import org.apache.poi.hssf.record.formula.FuncVarPtg; +import org.apache.poi.hssf.record.formula.IntPtg; +import org.apache.poi.hssf.record.formula.LessEqualPtg; +import org.apache.poi.hssf.record.formula.LessThanPtg; +import org.apache.poi.hssf.record.formula.NamePtg; +import org.apache.poi.hssf.record.formula.NotEqualPtg; +import org.apache.poi.hssf.record.formula.Ptg; +import org.apache.poi.hssf.record.formula.ReferencePtg; +import org.apache.poi.hssf.record.formula.StringPtg; +import org.apache.poi.hssf.record.formula.UnaryMinusPtg; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * Test the low level formula parser functionality. High level tests are to @@ -315,6 +333,63 @@ public class TestFormulaParser extends TestCase { assertTrue("second ptg is string",ptg[1] instanceof StringPtg); } + public void testWorksheetReferences() + { + HSSFWorkbook wb = new HSSFWorkbook(); + + wb.createSheet("NoQuotesNeeded"); + wb.createSheet("Quotes Needed Here &#$@"); + + HSSFSheet sheet = wb.createSheet("Test"); + HSSFRow row = sheet.createRow(0); + HSSFCell cell; + + cell = row.createCell((short)0); + cell.setCellFormula("NoQuotesNeeded!A1"); + + cell = row.createCell((short)1); + cell.setCellFormula("'Quotes Needed Here &#$@'!A1"); + } + + public void testUnaryMinus() + { + FormulaParser fp = new FormulaParser("-A1", null); + fp.parse(); + Ptg[] ptg = fp.getRPNPtg(); + assertTrue("got 2 ptgs", ptg.length == 2); + assertTrue("first ptg is reference",ptg[0] instanceof ReferencePtg); + assertTrue("second ptg is string",ptg[1] instanceof UnaryMinusPtg); + } + + public void testLeadingSpaceInString() + { + String value = " hi "; + FormulaParser fp = new FormulaParser("\"" + value + "\"", null); + fp.parse(); + Ptg[] ptg = fp.getRPNPtg(); + + assertTrue("got 1 ptg", ptg.length == 1); + assertTrue("ptg0 is a StringPtg", ptg[0] instanceof StringPtg); + assertTrue("ptg0 contains exact value", ((StringPtg)ptg[0]).getValue().equals(value)); + } + + public void testLookupAndMatchFunctionArgs() + { + FormulaParser fp = new FormulaParser("lookup(A1, A3:A52, B3:B52)", null); + fp.parse(); + Ptg[] ptg = fp.getRPNPtg(); + + assertTrue("got 4 ptg", ptg.length == 4); + assertTrue("ptg0 has Value class", ptg[0].getPtgClass() == Ptg.CLASS_VALUE); + + fp = new FormulaParser("match(A1, A3:A52)", null); + fp.parse(); + ptg = fp.getRPNPtg(); + + assertTrue("got 3 ptg", ptg.length == 3); + assertTrue("ptg0 has Value class", ptg[0].getPtgClass() == Ptg.CLASS_VALUE); + } + public static void main(String [] args) { System.out.println("Testing org.apache.poi.hssf.record.formula.FormulaParser"); junit.textui.TestRunner.run(TestFormulaParser.class);