Skip to content

Commit

Permalink
Merge pull request #91 from asulwer/fix/empty-array-lt-gt-comparison
Browse files Browse the repository at this point in the history
Fix less than/greater than operators in empty arrays
  • Loading branch information
asulwer authored Nov 18, 2024
2 parents 3636edb + 8c5c7ab commit f98800c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 23 deletions.
52 changes: 40 additions & 12 deletions src/RulesEngine/Models/ImplicitObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImplicitObject>
{
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;
}
}
}
39 changes: 32 additions & 7 deletions test/RulesEngine.UnitTest/EmptyArrayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
]
}
Expand All @@ -50,12 +73,14 @@ private async Task EmptyArray_ReturnsExepectedResults()
CancellationTokenSource cancellationTokenSource = new();
List<RuleResultTree> 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);

}

Expand Down
8 changes: 4 additions & 4 deletions test/RulesEngine.UnitTest/NestedRulesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}
Expand Down Expand Up @@ -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));
});
}
Expand Down

0 comments on commit f98800c

Please sign in to comment.