Skip to content

Commit

Permalink
Merge branch 'main' of github.com:panoramicdata/PanoramicData.NCalcEx…
Browse files Browse the repository at this point in the history
…tensions
  • Loading branch information
TheCakeMonster committed Oct 19, 2023
2 parents e28d1d8 + 859b0ed commit 626e244
Show file tree
Hide file tree
Showing 11 changed files with 616 additions and 416 deletions.
8 changes: 8 additions & 0 deletions PanoramicData.NCalcExtensions.Test/CountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public void Count_OfListOfString_ReturnsExpectedResult()
result.Should().Be(_stringList.Count);
}

[Fact]
public void Count_WithString_ReturnsExpectedResult()
{
var expression = new ExtendedExpression($"count('a piece of string')");
var result = expression.Evaluate();
result.Should().Be(17);
}

[Fact]
public void Count_WithLambda_ReturnsExpectedResult()
{
Expand Down
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
24 changes: 20 additions & 4 deletions PanoramicData.NCalcExtensions/Extensions/Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@ internal static class Count
{
internal static void Evaluate(FunctionArgs functionArgs)
{
var list = functionArgs.Parameters[0].Evaluate() as IEnumerable<object?>
?? throw new FormatException($"{ExtensionFunction.Count}() requires IEnumerable parameter.");
var listObject = functionArgs.Parameters[0].Evaluate();
var listEnumerable = listObject as IEnumerable<object?>;

if (functionArgs.Parameters.Length == 1)
{
functionArgs.Result = list.Count();
if (listObject is string text)
{
functionArgs.Result = text.Length;
return;
}

if (listEnumerable is null)
{
throw new FormatException($"{ExtensionFunction.Count}() requires IEnumerable parameter.");
}

functionArgs.Result = listEnumerable.Count();
return;
}

if (listEnumerable is null)
{
throw new FormatException($"{ExtensionFunction.Count}() requires IEnumerable parameter.");
}

var predicate = functionArgs.Parameters[1].Evaluate() as string
?? throw new FormatException($"Second {ExtensionFunction.Count} parameter must be a string.");

Expand All @@ -23,7 +39,7 @@ internal static void Evaluate(FunctionArgs functionArgs)

var lambda = new Lambda(predicate, lambdaString, functionArgs.Parameters[0].Parameters);

functionArgs.Result = list
functionArgs.Result = listEnumerable
.Count(value =>
{
var result = lambda.Evaluate(value) as bool?;
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 extend().</PackageReleaseNotes>
<PackageReleaseNotes>Added support for minValue() and maxValue().</PackageReleaseNotes>

<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
Loading

0 comments on commit 626e244

Please sign in to comment.