bugzilla 52057 - updated formula test framework to be aware of recently added Functions

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1294595 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2012-02-28 11:53:25 +00:00
parent d52b24dae0
commit c20420591c
7 changed files with 40 additions and 5 deletions

View File

@ -206,6 +206,23 @@
</p> </p>
</section> </section>
<section><title>Testing Framework</title>
<p>Automated testing of the implemented Function is easy.
The source code for this is in the file: o.a.p.h.record.formula.GenericFormulaTestCase.java
This class has a reference to the test xls file (not /a/ test xls, /the/ test xls :)
which may need to be changed for your environment. Once you do that, in the test xls,
locate the entry for the function that you have implemented and enter different tests
in a cell in the FORMULA row. Then copy the "value of" the formula that you entered in the
cell just below it (this is easily done in excel as:
[copy the formula cell] > [go to cell below] > Edit > Paste Special > Values > "ok").
You can enter multiple such formulas and paste their values in the cell below and the
test framework will automatically test if the formula evaluation matches the expected
value (Again, hard to put in words, so if you will, please take time to quickly look
at the code and the currently entered tests in the patch attachment "FormulaEvalTestData.xls"
file).
</p>
</section>
<anchor id="appendixA"/> <anchor id="appendixA"/>
<section> <section>
<title>Appendix A</title> <title>Appendix A</title>
@ -354,4 +371,4 @@
</source> </source>
</section> </section>
</body> </body>
</document> </document>

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.8-beta6" date="2012-??-??"> <release version="3.8-beta6" date="2012-??-??">
<action dev="poi-developers" type="add">52057 - updated formula test framework to be aware of recently added Functions </action>
<action dev="poi-developers" type="add">52574 - support setting header / footer page margins in HSSF </action> <action dev="poi-developers" type="add">52574 - support setting header / footer page margins in HSSF </action>
<action dev="poi-developers" type="add">52583 - fixed WorkbookUtil#createSafeSheetName to escape colon </action> <action dev="poi-developers" type="add">52583 - fixed WorkbookUtil#createSafeSheetName to escape colon </action>
<action dev="poi-developers" type="add">51710 - fixed reading shared formulas in XSSF </action> <action dev="poi-developers" type="add">51710 - fixed reading shared formulas in XSSF </action>

View File

@ -331,7 +331,8 @@ public abstract class NumericFunction implements Function {
double d0 = singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex); double d0 = singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
double d1 = singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex); double d1 = singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
double multi = Math.pow(10d,d1); double multi = Math.pow(10d,d1);
result = Math.floor(d0 * multi) / multi; if(d0 < 0) result = -Math.floor(-d0 * multi) / multi;
else result = Math.floor(d0 * multi) / multi;
checkValue(result); checkValue(result);
}catch (EvaluationException e) { }catch (EvaluationException e) {
return e.getErrorEval(); return e.getErrorEval();

View File

@ -18,6 +18,7 @@
package org.apache.poi.ss.formula.eval; package org.apache.poi.ss.formula.eval;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Collection;
import junit.framework.Assert; import junit.framework.Assert;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
@ -31,6 +32,8 @@ import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* Tests formulas and operators as loaded from a test data spreadsheet.<p/> * Tests formulas and operators as loaded from a test data spreadsheet.<p/>
@ -43,6 +46,7 @@ import org.apache.poi.ss.usermodel.Sheet;
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt; * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
*/ */
public final class TestFormulasFromSpreadsheet extends TestCase { public final class TestFormulasFromSpreadsheet extends TestCase {
private static final POILogger logger = POILogFactory.getLogger(TestFormulasFromSpreadsheet.class);
private static final class Result { private static final class Result {
public static final int SOME_EVALUATIONS_FAILED = -1; public static final int SOME_EVALUATIONS_FAILED = -1;
@ -167,9 +171,7 @@ public final class TestFormulasFromSpreadsheet extends TestCase {
+ _evaluationFailureCount + " evaluation(s). " + successMsg; + _evaluationFailureCount + " evaluation(s). " + successMsg;
throw new AssertionFailedError(msg); throw new AssertionFailedError(msg);
} }
if(false) { // normally no output for successful tests logger.log(POILogger.INFO, getClass().getName() + ": " + successMsg);
System.out.println(getClass().getName() + ": " + successMsg);
}
} }
/** /**
@ -179,6 +181,7 @@ public final class TestFormulasFromSpreadsheet extends TestCase {
*/ */
private void processFunctionGroup(int startRowIndex, String testFocusFunctionName) { private void processFunctionGroup(int startRowIndex, String testFocusFunctionName) {
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(workbook); HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(workbook);
Collection<String> funcs = FunctionEval.getSupportedFunctionNames();
int rowIndex = startRowIndex; int rowIndex = startRowIndex;
while (true) { while (true) {
@ -208,6 +211,12 @@ public final class TestFormulasFromSpreadsheet extends TestCase {
default: default:
throw new RuntimeException("unexpected result"); throw new RuntimeException("unexpected result");
case Result.NO_EVALUATIONS_FOUND: // do nothing case Result.NO_EVALUATIONS_FOUND: // do nothing
String uname = targetFunctionName.toUpperCase();
if(startRowIndex >= SS.START_FUNCTIONS_ROW_INDEX &&
funcs.contains(uname)) {
logger.log(POILogger.WARN, uname + ": function is supported but missing test data");
}
break;
} }
} }
rowIndex += SS.NUMBER_OF_ROWS_PER_FUNCTION; rowIndex += SS.NUMBER_OF_ROWS_PER_FUNCTION;

View File

@ -56,4 +56,11 @@ public final class TestTrunc extends AbstractNumericTestCase {
ValueEval result = F.TRUNC.evaluate(args, -1, (short)-1); ValueEval result = F.TRUNC.evaluate(args, -1, (short)-1);
assertEquals("TRUNC", (new NumberEval(2d)).getNumberValue(), ((NumberEval)result).getNumberValue()); assertEquals("TRUNC", (new NumberEval(2d)).getNumberValue(), ((NumberEval)result).getNumberValue());
} }
public void testNegative() {
ValueEval[] args = { new NumberEval(-8.9), new NumberEval(0) };
@SuppressWarnings("static-access")
ValueEval result = F.TRUNC.evaluate(args, -1, (short)-1);
assertEquals("TRUNC", (new NumberEval(-8)).getNumberValue(), ((NumberEval)result).getNumberValue());
}
} }