You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestIfna.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.atp;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import org.apache.poi.hssf.usermodel.HSSFCell;
  18. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  20. import org.apache.poi.ss.formula.eval.ErrorEval;
  21. import org.apache.poi.ss.usermodel.CellType;
  22. import org.apache.poi.ss.usermodel.CellValue;
  23. import org.junit.jupiter.api.Test;
  24. import org.junit.jupiter.api.BeforeEach;
  25. /**
  26. * IfNa unit tests.
  27. */
  28. class TestIfna {
  29. HSSFWorkbook wb;
  30. HSSFCell cell;
  31. HSSFFormulaEvaluator fe;
  32. @BeforeEach
  33. void setup() {
  34. wb = new HSSFWorkbook();
  35. cell = wb.createSheet().createRow(0).createCell(0);
  36. fe = new HSSFFormulaEvaluator(wb);
  37. }
  38. @Test
  39. void testNumbericArgsWorkCorrectly() {
  40. confirmResult(fe, cell, "IFNA(-1,42)", new CellValue(-1.0));
  41. confirmResult(fe, cell, "IFNA(NA(),42)", new CellValue(42.0));
  42. }
  43. @Test
  44. void testStringArgsWorkCorrectly() {
  45. confirmResult(fe, cell, "IFNA(\"a1\",\"a2\")", new CellValue("a1"));
  46. confirmResult(fe, cell, "IFNA(NA(),\"a2\")", new CellValue("a2"));
  47. }
  48. @Test
  49. void testUsageErrorsThrowErrors() {
  50. confirmError(fe, cell, "IFNA(1)", ErrorEval.VALUE_INVALID);
  51. confirmError(fe, cell, "IFNA(1,2,3)", ErrorEval.VALUE_INVALID);
  52. }
  53. @Test
  54. void testErrorInArgSelectsNAResult() {
  55. confirmError(fe, cell, "IFNA(1/0,42)", ErrorEval.DIV_ZERO);
  56. }
  57. @Test
  58. void testErrorFromNAArgPassesThrough() {
  59. confirmError(fe, cell, "IFNA(NA(),1/0)", ErrorEval.DIV_ZERO);
  60. }
  61. @Test
  62. void testNaArgNotEvaledIfUnneeded() {
  63. confirmResult(fe, cell, "IFNA(42,1/0)", new CellValue(42.0));
  64. }
  65. private static void confirmResult(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText,
  66. CellValue expectedResult) {
  67. fe.setDebugEvaluationOutputForNextEval(true);
  68. cell.setCellFormula(formulaText);
  69. fe.notifyUpdateCell(cell);
  70. CellValue result = fe.evaluate(cell);
  71. assertEquals(expectedResult.getCellType(), result.getCellType(), "Testing result type for: " + formulaText);
  72. assertEquals(expectedResult.formatAsString(), result.formatAsString(), "Testing result for: " + formulaText);
  73. }
  74. private static void confirmError(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText,
  75. ErrorEval expectedError) {
  76. fe.setDebugEvaluationOutputForNextEval(true);
  77. cell.setCellFormula(formulaText);
  78. fe.notifyUpdateCell(cell);
  79. CellValue result = fe.evaluate(cell);
  80. assertEquals(CellType.ERROR, result.getCellType(), "Testing result type for: " + formulaText);
  81. assertEquals(expectedError.getErrorString(), result.formatAsString(), "Testing error type for: " + formulaText);
  82. }
  83. }