From 4cce90f31e6593b3021894d1431fa1bc6c27876d Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 3 Oct 2021 12:28:19 +0000 Subject: [PATCH] [bug-65606] allow return type (second param) to be omitted - still other scenarios to be fixed though git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1893851 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/functions/WeekNum.java | 5 +- .../ss/formula/functions/TestWeekNumFunc.java | 76 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java index 0ace888b38..04fb1dec7e 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java @@ -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 index 0000000000..2a915b7742 --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java @@ -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); + } + } +} -- 2.39.5