diff options
author | PJ Fanning <fanningpj@apache.org> | 2022-02-25 12:17:51 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2022-02-25 12:17:51 +0000 |
commit | 18d868173dec5845c1056a06e12ffd25ae597275 (patch) | |
tree | 82dcc28ddb864f72862bc7b7a3b18e86fae3f9c9 | |
parent | c8e0ab8ac083fe61d45b1353667b7dad6261cdb3 (diff) | |
download | poi-18d868173dec5845c1056a06e12ffd25ae597275.tar.gz poi-18d868173dec5845c1056a06e12ffd25ae597275.zip |
add tests for 'OR' function
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898413 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi/src/test/java/org/apache/poi/ss/formula/functions/TestOrFunction.java | 105 | ||||
-rw-r--r-- | poi/src/test/java/org/apache/poi/ss/util/Utils.java | 8 |
2 files changed, 113 insertions, 0 deletions
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestOrFunction.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestOrFunction.java new file mode 100644 index 0000000000..2fe6a045c1 --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestOrFunction.java @@ -0,0 +1,105 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.formula.functions; + +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.formula.OperationEvaluationContext; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.apache.poi.ss.util.Utils.addRow; +import static org.apache.poi.ss.util.Utils.assertBoolean; +import static org.apache.poi.ss.util.Utils.assertDouble; +import static org.apache.poi.ss.util.Utils.assertString; + +/** + * Tests for {@link BooleanFunction#OR} + */ +final class TestOrFunction { + + private static final OperationEvaluationContext ec = new OperationEvaluationContext(null, null, 0, 0, 0, null); + + @Test + void testMicrosoftExample0() throws IOException { + //https://support.microsoft.com/en-us/office/or-function-7d17ad14-8700-4281-b308-00b131e22af0 + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFSheet sheet = wb.createSheet(); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); + assertBoolean(fe, cell, "OR(TRUE,TRUE)", true); + assertBoolean(fe, cell, "OR(TRUE,FALSE)", true); + assertBoolean(fe, cell, "OR(1=1,2=2,3=3)", true); + assertBoolean(fe, cell, "OR(1=2,2=3,3=4)", false); + } + } + + @Test + void testMicrosoftExample1() throws IOException { + //https://support.microsoft.com/en-us/office/or-function-7d17ad14-8700-4281-b308-00b131e22af0 + try (HSSFWorkbook wb = initWorkbook1()) { + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100); + assertBoolean(fe, cell, "OR(A2>1,A2<100)", true); + assertDouble(fe, cell, "IF(OR(A2>1,A2<100),A3,\"The value is out of range\")", 100); + assertString(fe, cell, "IF(OR(A2<0,A2>50),A2,\"The value is out of range\")", "The value is out of range"); + } + } + + @Test + void testMicrosoftExample2() throws IOException { + //https://support.microsoft.com/en-us/office/or-function-7d17ad14-8700-4281-b308-00b131e22af0 + try (HSSFWorkbook wb = initWorkbook2()) { + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFCell cell = wb.getSheetAt(0).getRow(13).createCell(3); + assertDouble(fe, cell, "IF(OR(B14>=$B$4,C14>=$B$5),B14*$B$6,0)", 314); + } + } + + private HSSFWorkbook initWorkbook1() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + addRow(sheet, 0, "Values"); + addRow(sheet, 1, 50); + addRow(sheet, 2, 100); + return wb; + } + + private HSSFWorkbook initWorkbook2() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + addRow(sheet, 0, "Goals"); + addRow(sheet, 2, "Criteria", "Amount"); + addRow(sheet, 3, "Sales Goal", 8500); + addRow(sheet, 4, "Account Goal", 5); + addRow(sheet, 5, "Commission Rate", 0.02); + addRow(sheet, 6, "Bonus Goal", 12500); + addRow(sheet, 7, "Bonus %", 0.015); + addRow(sheet, 9, "Commission Calculations"); + addRow(sheet, 11, "Salesperson", "Total Sales", "Accounts", "Commission", "Bonus"); + addRow(sheet, 12, "Millicent Shelton", 10260, 9); + addRow(sheet, 13, "Miguel Ferrari", 15700, 7); + return wb; + } + +} diff --git a/poi/src/test/java/org/apache/poi/ss/util/Utils.java b/poi/src/test/java/org/apache/poi/ss/util/Utils.java index d1e3b78a7d..d838bea27c 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/Utils.java +++ b/poi/src/test/java/org/apache/poi/ss/util/Utils.java @@ -80,6 +80,14 @@ public class Utils { assertEquals(expectedResult, result.getNumberValue(), tolerance); } + public static void assertBoolean(FormulaEvaluator fe, Cell cell, String formulaText, boolean expectedResult) { + cell.setCellFormula(formulaText); + fe.notifyUpdateCell(cell); + CellValue result = fe.evaluate(cell); + assertEquals(CellType.BOOLEAN, result.getCellType()); + assertEquals(expectedResult, result.getBooleanValue()); + } + public static void assertError(FormulaEvaluator fe, Cell cell, String formulaText, FormulaError expectedError) { cell.setCellFormula(formulaText); fe.notifyUpdateCell(cell); |