Skip to content

Commit

Permalink
Added minValue() and maxValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnmbond committed Oct 18, 2023
1 parent 23b88f6 commit 859b0ed
Show file tree
Hide file tree
Showing 9 changed files with 587 additions and 412 deletions.
35 changes: 35 additions & 0 deletions PanoramicData.NCalcExtensions.Test/MaxValueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace PanoramicData.NCalcExtensions.Test;

public class MaxValueTests
{
[Theory]
[InlineData("sbyte", sbyte.MaxValue)]
[InlineData("byte", byte.MaxValue)]
[InlineData("short", short.MaxValue)]
[InlineData("ushort", ushort.MaxValue)]
[InlineData("int", int.MaxValue)]
[InlineData("uint", uint.MaxValue)]
[InlineData("long", long.MaxValue)]
[InlineData("ulong", ulong.MaxValue)]
[InlineData("float", float.MaxValue)]
[InlineData("double", double.MaxValue)]
public void MaxValue_ReturnsExpectedValue(string type, object expectedOutput)
{
var expression = new ExtendedExpression($"maxValue('{type}')");
expression.Evaluate().Should().BeEquivalentTo(expectedOutput);
}

[Fact]
public void MaxValue_ForDecimal_ReturnsExpectedValue()
{
var expression = new ExtendedExpression($"maxValue('decimal')");
expression.Evaluate().Should().BeEquivalentTo(decimal.MaxValue);
}

[Fact]
public void MaxValue_ForUnsupportedType_ThrowsFormatException()
{
var expression = new ExtendedExpression($"maxValue('unsupportedType')");
expression.Invoking(x => x.Evaluate()).Should().Throw<FormatException>();
}
}
35 changes: 35 additions & 0 deletions PanoramicData.NCalcExtensions.Test/MinValueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace PanoramicData.NCalcExtensions.Test;

public class MinValueTests
{
[Theory]
[InlineData("sbyte", sbyte.MinValue)]
[InlineData("byte", byte.MinValue)]
[InlineData("short", short.MinValue)]
[InlineData("ushort", ushort.MinValue)]
[InlineData("int", int.MinValue)]
[InlineData("uint", uint.MinValue)]
[InlineData("long", long.MinValue)]
[InlineData("ulong", ulong.MinValue)]
[InlineData("float", float.MinValue)]
[InlineData("double", double.MinValue)]
public void MinValue_ReturnsExpectedValue(string type, object expectedOutput)
{
var expression = new ExtendedExpression($"minValue('{type}')");
expression.Evaluate().Should().BeEquivalentTo(expectedOutput);
}

[Fact]
public void MinValue_ForDecimal_ReturnsExpectedValue()
{
var expression = new ExtendedExpression($"minValue('decimal')");
expression.Evaluate().Should().BeEquivalentTo(decimal.MinValue);
}

[Fact]
public void MinValue_ForUnsupportedType_ThrowsFormatException()
{
var expression = new ExtendedExpression($"minValue('unsupportedType')");
expression.Invoking(x => x.Evaluate()).Should().Throw<FormatException>();
}
}
6 changes: 6 additions & 0 deletions PanoramicData.NCalcExtensions/ExtendedExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,15 @@ internal void Extend(string functionName, FunctionArgs functionArgs)
case ExtensionFunction.Max:
Max.Evaluate(functionArgs);
return;
case ExtensionFunction.MaxValue:
MaxValue.Evaluate(functionArgs);
return;
case ExtensionFunction.Min:
Min.Evaluate(functionArgs);
return;
case ExtensionFunction.MinValue:
MinValue.Evaluate(functionArgs);
return;
case ExtensionFunction.NullCoalesce:
NullCoalesce.Evaluate(functionArgs);
return;
Expand Down
2 changes: 2 additions & 0 deletions PanoramicData.NCalcExtensions/ExtensionFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public static class ExtensionFunction
public const string List = "list";
public const string ListOf = "listOf";
public const string Max = "max";
public const string MaxValue = "maxValue";
public const string Min = "min";
public const string MinValue = "minValue";
public const string NewJObject = "jObject";
public const string NullCoalesce = "nullCoalesce";
public const string OrderBy = "orderBy";
Expand Down
34 changes: 34 additions & 0 deletions PanoramicData.NCalcExtensions/Extensions/MaxValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace PanoramicData.NCalcExtensions.Extensions;

internal static class MaxValue
{
private const string ErrorMessage = $"{ExtensionFunction.MaxValue} takes exactly one string parameter, which must be one of 'sbyte', 'byte', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'float', 'double' or 'decimal'.";

internal static void Evaluate(FunctionArgs functionArgs)
{
var originalList = functionArgs.Parameters[0].Evaluate();

if (functionArgs.Parameters.Length != 1)
{
throw new FormatException(ErrorMessage);
}

functionArgs.Result = originalList switch
{
"sbyte" => sbyte.MaxValue,
"byte" => byte.MaxValue,
"short" => short.MaxValue,
"ushort" => ushort.MaxValue,
"int" => int.MaxValue,
"uint" => uint.MaxValue,
"long" => long.MaxValue,
"ulong" => ulong.MaxValue,
"float" => float.MaxValue,
"double" => double.MaxValue,
"decimal" => decimal.MaxValue,
_ => throw new FormatException(ErrorMessage)
};

return;
}
}
34 changes: 34 additions & 0 deletions PanoramicData.NCalcExtensions/Extensions/MinValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace PanoramicData.NCalcExtensions.Extensions;

internal static class MinValue
{
private const string ErrorMessage = $"{ExtensionFunction.MinValue} takes exactly one string parameter, which must be one of 'sbyte', 'byte', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'float', 'double' or 'decimal'.";

internal static void Evaluate(FunctionArgs functionArgs)
{
var originalList = functionArgs.Parameters[0].Evaluate();

if (functionArgs.Parameters.Length != 1)
{
throw new FormatException(ErrorMessage);
}

functionArgs.Result = originalList switch
{
"sbyte" => sbyte.MinValue,
"byte" => byte.MinValue,
"short" => short.MinValue,
"ushort" => ushort.MinValue,
"int" => int.MinValue,
"uint" => uint.MinValue,
"long" => long.MinValue,
"ulong" => ulong.MinValue,
"float" => float.MinValue,
"double" => double.MinValue,
"decimal" => decimal.MinValue,
_ => throw new FormatException(ErrorMessage)
};

return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<EmbedAllSources>true</EmbedAllSources>
<DebugType>portable</DebugType>

<PackageReleaseNotes>Added support for count(String x).</PackageReleaseNotes>
<PackageReleaseNotes>Added support for minValue() and maxValue().</PackageReleaseNotes>

<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
Loading

0 comments on commit 859b0ed

Please sign in to comment.