From f9b27daea3067970f76ad97b0f608d8c86fc1c55 Mon Sep 17 00:00:00 2001 From: Jason Boggs Date: Mon, 18 Nov 2024 10:23:29 -0600 Subject: [PATCH 1/2] fix: add operators for less than, greater than, less than equal, greater than equal, equal, not equal to ImplicitOperator to assist with empty arrays --- src/RulesEngine/Models/ImplicitObject.cs | 52 ++++++++++++++++----- test/RulesEngine.UnitTest/EmptyArrayTest.cs | 39 +++++++++++++--- 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/src/RulesEngine/Models/ImplicitObject.cs b/src/RulesEngine/Models/ImplicitObject.cs index 19ef52cf..12b10ac9 100644 --- a/src/RulesEngine/Models/ImplicitObject.cs +++ b/src/RulesEngine/Models/ImplicitObject.cs @@ -2,33 +2,61 @@ // Licensed under the MIT License. // https://github.com/asulwer/RulesEngine/issues/75 +using System; using System.Diagnostics.CodeAnalysis; namespace RulesEngine.Models { [ExcludeFromCodeCoverage] - public class ImplicitObject + public class ImplicitObject : IEquatable { - public static implicit operator ImplicitObject(bool value) => default; + public static implicit operator ImplicitObject(bool _) => default; - public static implicit operator ImplicitObject(bool? value) => default; + public static implicit operator ImplicitObject(bool? _) => default; - public static implicit operator ImplicitObject(char value) => default; + public static implicit operator ImplicitObject(char _) => default; - public static implicit operator ImplicitObject(char? value) => default; + public static implicit operator ImplicitObject(char? _) => default; - public static implicit operator ImplicitObject(string value) => default; + public static implicit operator ImplicitObject(string _) => default; - public static implicit operator ImplicitObject(int value) => default; + public static implicit operator ImplicitObject(int _) => default; - public static implicit operator ImplicitObject(int? value) => default; + public static implicit operator ImplicitObject(int? _) => default; - public static implicit operator ImplicitObject(decimal value) => default; + public static implicit operator ImplicitObject(decimal _) => default; - public static implicit operator ImplicitObject(decimal? value) => default; + public static implicit operator ImplicitObject(decimal? _) => default; - public static implicit operator ImplicitObject(float value) => default; + public static implicit operator ImplicitObject(float _) => default; - public static implicit operator ImplicitObject(float? value) => default; + public static implicit operator ImplicitObject(float? _) => default; + + public static bool operator <(ImplicitObject l, ImplicitObject r) => default; + + public static bool operator >(ImplicitObject l, ImplicitObject r) => default; + + public static bool operator <=(ImplicitObject l, ImplicitObject r) => default; + + public static bool operator >=(ImplicitObject l, ImplicitObject r) => default; + + public static bool operator ==(ImplicitObject l, ImplicitObject r) => default; + + public static bool operator !=(ImplicitObject l, ImplicitObject r) => default; + + public bool Equals(ImplicitObject other) + { + return default; + } + + public override bool Equals(object obj) + { + return default; + } + + public override int GetHashCode() + { + return default; + } } } \ No newline at end of file diff --git a/test/RulesEngine.UnitTest/EmptyArrayTest.cs b/test/RulesEngine.UnitTest/EmptyArrayTest.cs index b4bb38c5..133bd8a5 100644 --- a/test/RulesEngine.UnitTest/EmptyArrayTest.cs +++ b/test/RulesEngine.UnitTest/EmptyArrayTest.cs @@ -25,9 +25,32 @@ private async Task EmptyArray_ReturnsExepectedResults() WorkflowName = "Workflow", Rules = [ new() { - RuleName = "empty array", - Expression = "not things.Any(a == 3)", - RuleExpressionType = RuleExpressionType.LambdaExpression + RuleName = "equal string", + Expression = "not things.Any(a == \"widget\")" + }, + new() { + RuleName = "equal integer", + Expression = "not things.Any(a == 3)" + }, + new() { + RuleName = "not equal integer", + Expression = "not things.Any(a != 3)" + }, + new() { + RuleName = "greater than integer", + Expression = "not things.Any(a > 3)" + }, + new() { + RuleName = "less than integer", + Expression = "not things.Any(a < 3)" + }, + new() { + RuleName = "greater than equal to integer", + Expression = "not things.Any(a >= 3)" + }, + new() { + RuleName = "less than equal to integer", + Expression = "not things.Any(a <= 3)" } ] } @@ -50,12 +73,14 @@ private async Task EmptyArray_ReturnsExepectedResults() CancellationTokenSource cancellationTokenSource = new(); List results = await rulesEngine.ExecuteAllRulesAsync("Workflow", cancellationTokenSource.Token, target); - Assert.Single(results); + Assert.True(results.TrueForAll(r => r.IsSuccess)); - var result = results[0]; + foreach (var result in results) + { + Assert.Equal(result.ExceptionMessage, string.Empty); + Assert.True(result.IsSuccess); + } - Assert.Equal(result.ExceptionMessage, string.Empty); - Assert.True(result.IsSuccess); } From 8c5c7ab08480258720802c128a655d193b64274c Mon Sep 17 00:00:00 2001 From: Jason Boggs Date: Mon, 18 Nov 2024 10:34:58 -0600 Subject: [PATCH 2/2] fix unit test syntax warnings --- test/RulesEngine.UnitTest/NestedRulesTest.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/RulesEngine.UnitTest/NestedRulesTest.cs b/test/RulesEngine.UnitTest/NestedRulesTest.cs index d0e93283..85d3424a 100644 --- a/test/RulesEngine.UnitTest/NestedRulesTest.cs +++ b/test/RulesEngine.UnitTest/NestedRulesTest.cs @@ -44,14 +44,14 @@ public async Task NestedRulesShouldFollowExecutionMode(NestedRuleExecutionMode m Assert.All(andResults, c => { Assert.Equal(c.IsSuccess, c.ChildResults.Last().IsSuccess); - Assert.Single(c.ChildResults.Where(d => c.IsSuccess == d.IsSuccess)); + Assert.Single(c.ChildResults, d => c.IsSuccess == d.IsSuccess); Assert.True(c.ChildResults.SkipLast(1).All(d => d.IsSuccess == true)); }); Assert.All(orResults, c => { Assert.Equal(c.IsSuccess, c.ChildResults.Last().IsSuccess); - Assert.Single(c.ChildResults.Where(d => c.IsSuccess == d.IsSuccess)); + Assert.Single(c.ChildResults, d => c.IsSuccess == d.IsSuccess); Assert.True(c.ChildResults.SkipLast(1).All(d => d.IsSuccess == false)); }); } @@ -240,14 +240,14 @@ public async Task NestedRulesShouldFollowExecutionMode(NestedRuleExecutionMode m Assert.All(andResults, c => { Assert.Equal(c.IsSuccess, c.ChildResults.Last().IsSuccess); - Assert.Single(c.ChildResults.Where(d => c.IsSuccess == d.IsSuccess)); + Assert.Single(c.ChildResults, d => c.IsSuccess == d.IsSuccess); Assert.True(c.ChildResults.SkipLast(1).All(d => d.IsSuccess == true)); }); Assert.All(orResults, c => { Assert.Equal(c.IsSuccess, c.ChildResults.Last().IsSuccess); - Assert.Single(c.ChildResults.Where(d => c.IsSuccess == d.IsSuccess)); + Assert.Single(c.ChildResults, d => c.IsSuccess == d.IsSuccess); Assert.True(c.ChildResults.SkipLast(1).All(d => d.IsSuccess == false)); }); }