From b10fe8d246737d18da2aa06fe6e418277c86f444 Mon Sep 17 00:00:00 2001 From: m365solutioninsights Date: Sun, 29 Sep 2024 17:05:48 +0530 Subject: [PATCH] add a new file Workday.txt and handling blank, error values, unexpected type and coercions in Workday --- .../Localization/Strings.cs | 6 ++- .../Texl/Builtins/DateTime.cs | 25 +++------- .../Functions/Library.cs | 2 +- .../Functions/LibraryDate.cs | 16 ++---- .../ExpressionTestCases/Workday.txt | 50 +++++++++++++++++++ .../TexlTests.cs | 2 + 6 files changed, 67 insertions(+), 34 deletions(-) create mode 100644 src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Workday.txt diff --git a/src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs b/src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs index 18811a3724..0e99d41325 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs @@ -389,11 +389,15 @@ internal static class TexlStrings public static StringGetter DateTimeValueArg2 = (b) => StringResources.Get("DateTimeValueArg2", b); public static StringGetter AboutDateAdd = (b) => StringResources.Get("AboutDateAdd", b); - public static StringGetter AboutWorkday = (b) => StringResources.Get("AboutWorkday", b); public static StringGetter DateAddArg1 = (b) => StringResources.Get("DateAddArg1", b); public static StringGetter DateAddArg2 = (b) => StringResources.Get("DateAddArg2", b); public static StringGetter DateAddArg3 = (b) => StringResources.Get("DateAddArg3", b); + public static StringGetter AboutWorkday = (b) => StringResources.Get("AboutWorkday", b); + public static StringGetter WorkdayArg1 = (b) => StringResources.Get("WorkdayArg1", b); + public static StringGetter WorkdayArg2 = (b) => StringResources.Get("WorkdayArg2", b); + public static StringGetter WorkdayArg3 = (b) => StringResources.Get("WorkdayArg3", b); + public static StringGetter AboutDateAddT = (b) => StringResources.Get("AboutDateAddT", b); public static StringGetter DateAddTArg1 = (b) => StringResources.Get("DateAddTArg1", b); public static StringGetter DateAddTArg2 = (b) => StringResources.Get("DateAddTArg2", b); diff --git a/src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/DateTime.cs b/src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/DateTime.cs index 64b5c373ac..571b189fbb 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/DateTime.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/DateTime.cs @@ -620,9 +620,9 @@ public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DTyp return fValid; } - } - - // Workday(timestamp: d, delta: n : d + } + + // Workday(timestamp: d, delta: n) : d internal sealed class WorkdayFunction : BuiltinFunction { public override bool IsSelfContained => true; @@ -634,20 +634,7 @@ public WorkdayFunction() public override IEnumerable GetSignatures() { - yield return new[] { TexlStrings.DateAddArg1, TexlStrings.DateAddArg2 }; - } - - public override IEnumerable GetRequiredEnumNames() - { - return new List() { LanguageConstants.TimeUnitEnumString }; - } - - // This method returns true if there are special suggestions for a particular parameter of the function. - public override bool HasSuggestionsForParam(int argumentIndex) - { - Contracts.Assert(argumentIndex >= 0); - - return argumentIndex == 2; + yield return new[] { TexlStrings.WorkdayArg1, TexlStrings.WorkdayArg2 }; } public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DType[] argTypes, IErrorContainer errors, out DType returnType, out Dictionary nodeToCoercedTypeMap) @@ -666,9 +653,9 @@ public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DTyp if (fValid) { - if (type0.Kind == DKind.Date || type0.Kind == DKind.DateTime) + if (type0.Kind == DKind.Date || type0.Kind == DKind.DateTime || type0.Kind == DKind.Time) { - // Arg0 should be a DateTime or Date. + // Arg0 should be a Time, DateTime or Date. returnType = type0; } else if (nodeToCoercedTypeMap != null && nodeToCoercedTypeMap.TryGetValue(args[0], out var coercedType)) diff --git a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs index 23f981000f..fb8217350a 100644 --- a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs +++ b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs @@ -474,7 +474,7 @@ static Library() BuiltinFunctionsCore.Workday, StandardErrorHandling( BuiltinFunctionsCore.Workday.Name, - expandArguments: InsertDefaultValues(outputArgsCount: 3, fillWith: new BlankValue(IRContext.NotInSource(FormulaType.Blank))), + expandArguments: InsertDefaultValues(outputArgsCount: 2, fillWith: new BlankValue(IRContext.NotInSource(FormulaType.Blank))), replaceBlankValues: ReplaceBlankWith( new DateTimeValue(IRContext.NotInSource(FormulaType.DateTime), _epoch), new NumberValue(IRContext.NotInSource(FormulaType.Number), 0)), diff --git a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryDate.cs b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryDate.cs index 4552a1c491..702cba068c 100644 --- a/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryDate.cs +++ b/src/libraries/Microsoft.PowerFx.Interpreter/Functions/LibraryDate.cs @@ -193,7 +193,8 @@ public static FormulaValue Workday(EvalVisitor runner, EvalVisitorContext contex } else { - throw new NotImplementedException(); + //throw new NotImplementedException(); + return CommonErrors.RuntimeTypeMismatch(args[1].IRContext); } var useUtcConversion = NeedToConvertToUtc(runner, dateTime, timeUnit); @@ -230,18 +231,7 @@ public static FormulaValue Workday(EvalVisitor runner, EvalVisitorContext contex dateTime = MakeValidDateTime(runner, dateTime, timeZoneInfo); - if (irContext.ResultType._type.Kind == Core.Types.DKind.Date) - { - return new DateValue(irContext, dateTime); - } - else if (irContext.ResultType._type.Kind == Core.Types.DKind.Time) - { - return new TimeValue(irContext, dateTime.Subtract(_epoch)); - } - else - { - return new DateTimeValue(irContext, dateTime); - } + return new DateValue(irContext, dateTime); } catch { diff --git a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Workday.txt b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Workday.txt new file mode 100644 index 0000000000..1e369773c3 --- /dev/null +++ b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ExpressionTestCases/Workday.txt @@ -0,0 +1,50 @@ +>> Workday(Date(2024,9,13), 10) +Date(2024,9,27) + +>> Workday(Date(2024,9,13), -10) +Date(2024,8,30) + +>> Workday(45565,1) +Date(2024,10,1) + +>> Workday(Date(2024,9,30), "true") + + +>> DateAdd(false, 1) +Error 8-13: Invalid argument type (Boolean). Expecting a DateTime value instead. +Error 0-7: The function 'DateAdd' has some invalid arguments. + +>> Workday("false",1) + + +>> Workday([Date(2000,1,1)],1) +Error 8-24: Invalid argument type (Table). Expecting a DateTime value instead. + +Error 0-7: The function 'Workday' has some invalid arguments. +>> Workday(null,1) +Error 8-12: Use of a reserved word that is currently not supported. +Error 8-12: Invalid argument type (Error). Expecting a DateTime value instead. +Error 0-7: The function 'Workday' has some invalid arguments. + +>> Workday(Date(2024,9,30), null) +Error 25-29: Use of a reserved word that is currently not supported. +Error 25-29: Invalid argument type (Error). Expecting a Number value instead. +Error 0-7: The function 'Workday' has some invalid arguments. + +>> Workday(Date(2024,9,30), []) +Error 25-27: Invalid argument type (Table). Expecting a Number value instead. +Error 0-7: The function 'Workday' has some invalid arguments. + +>> Workday(,) +Error 8-9: Unexpected characters. Characters are used in the formula in an unexpected way. +Error 9-10: Unexpected characters. Characters are used in the formula in an unexpected way. +Error 10-10: Unexpected characters. The formula contains 'Eof' where 'ParenClose' is expected. +Error 8-9: Invalid argument type (Error). Expecting a DateTime value instead. +Error 9-10: Invalid argument type (Error). Expecting a Number value instead. +Error 0-7: The function 'Workday' has some invalid arguments. + +>> Workday(DateTimeValue(\"1 Jan 2015\"), 2)) +Error 22-23: Unexpected characters. Characters are used in the formula in an unexpected way. +Error 23-36: Expected operator. We expect an operator such as +, *, or & at this point in the formula. +Error 36-37: Unexpected characters. Characters are used in the formula in an unexpected way. +Error 0-42: Invalid number of arguments: received 1, expected 2. \ No newline at end of file diff --git a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/TexlTests.cs b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/TexlTests.cs index 69196a9c9d..a8985af698 100644 --- a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/TexlTests.cs +++ b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/TexlTests.cs @@ -93,6 +93,8 @@ public void TexlDateOverloads_Negative(string script) [InlineData("DateAdd(\"2000-01-01\", 1)", "d")] // Coercion on date argument from string [InlineData("DateAdd(45678, 1)", "d")] // Coercion on date argument from number [InlineData("DateAdd(Time(12,34,56), 1)", "T")] // Coercion on date argument from time + [InlineData("Workday(Date(2024,9,13),999)", "D")] + [InlineData("Workday(Date(2024,9,13),-101)", "D")] public void TexlDateAdd(string script, string expectedType) { Assert.True(DType.TryParse(expectedType, out var type));