]> source.dussan.org Git - poi.git/commitdiff
[bug-65606] allow return type (second param) to be omitted - still other scenarios...
authorPJ Fanning <fanningpj@apache.org>
Sun, 3 Oct 2021 12:28:19 +0000 (12:28 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sun, 3 Oct 2021 12:28:19 +0000 (12:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1893851 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java
poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java [new file with mode: 0644]

index 0ace888b383883944f24f2ba06d764eb16b2aa36..04fb1dec7e4482f646dfe5d24daf2c9a6e432a6f 100644 (file)
@@ -44,6 +44,7 @@ import org.apache.poi.util.LocaleUtil;
  */
 public class WeekNum extends Fixed2ArgFunction implements FreeRefFunction {
     public static final FreeRefFunction instance = new WeekNum();
+    private static final NumberEval DEFAULT_RETURN_TYPE = new NumberEval(1);
 
     @Override
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval serialNumVE, ValueEval returnTypeVE) {
@@ -82,7 +83,9 @@ public class WeekNum extends Fixed2ArgFunction implements FreeRefFunction {
 
     @Override
     public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
-        if (args.length == 2) {
+        if (args.length == 1) {
+            return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], DEFAULT_RETURN_TYPE);
+        } else if (args.length == 2) {
             return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);
         }
         return ErrorEval.VALUE_INVALID;
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java
new file mode 100644 (file)
index 0000000..2a915b7
--- /dev/null
@@ -0,0 +1,76 @@
+/* ====================================================================
+   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.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.*;
+import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.util.StringUtil;
+import org.junit.jupiter.api.Test;
+
+import java.time.LocalDate;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/**
+ * Tests WEEKNUM(date[, return_type]) excep function
+ * https://support.microsoft.com/en-us/office/weeknum-function-e5c43a03-b4ab-426c-b411-b18c13c75340
+ */
+class TestWeekNumFunc {
+    private static final double TOLERANCE = 0.001;
+
+    @Test
+    void testEvaluate() {
+        assertEvaluateEquals(10.0, DateUtil.getExcelDate(LocalDate.parse("2012-03-09")));
+        //next assert returns 10 when it should be 11.0.
+        //assertEvaluateEquals(11.0, DateUtil.getExcelDate(LocalDate.parse("2012-03-09")), 2);
+    }
+
+    private static final OperationEvaluationContext DEFAULT_CONTEXT =
+            new OperationEvaluationContext(null, null, 0, 1, 0, null);
+
+    // for testing invalid invocations
+    private void assertEvaluateEquals(String message, ErrorEval expected, ValueEval... args) {
+        String formula = "WEEKNUM(" + StringUtil.join(args, ", ") + ")";
+        ValueEval result = WeekNum.instance.evaluate(args, DEFAULT_CONTEXT);
+        assertEquals(expected, result, formula + ": " + message);
+    }
+
+    private void assertEvaluateEquals(double expected, double dateValue) {
+        String formula = "WEEKNUM(" + dateValue + ")";
+        ValueEval[] args = new ValueEval[] { new NumberEval(dateValue) };
+        ValueEval result = WeekNum.instance.evaluate(args, DEFAULT_CONTEXT);
+        if (result instanceof NumberEval) {
+            assertEquals(expected, ((NumberEval)result).getNumberValue(), TOLERANCE, formula);
+        } else {
+            fail("unexpected eval result " + result);
+        }
+    }
+
+    private void assertEvaluateEquals(double expected, double dateValue, double return_type) {
+        String formula = "WEEKNUM(" + dateValue + ", " + return_type + ")";
+        ValueEval[] args = new ValueEval[] { new NumberEval(dateValue), new NumberEval(return_type) };
+        ValueEval result = WeekNum.instance.evaluate(args, DEFAULT_CONTEXT);
+        if (result instanceof NumberEval) {
+            assertEquals(expected, ((NumberEval)result).getNumberValue(), TOLERANCE, formula);
+        } else {
+            fail("unexpected eval result " + result);
+        }
+    }
+}