mirror of https://github.com/sean-m/McRule.git
Merge branch 'main' into dev
commit
634aacf702
|
@ -2,17 +2,17 @@ namespace McRule.Tests {
|
|||
public class Filtering {
|
||||
|
||||
People[] things = new[] {
|
||||
new People("Sean", "Confused", 35, new[] {"muggle"}),
|
||||
new People("Sean", "Actor", 90, new[] {"muggle", "metallurgist"}),
|
||||
new People("Bean", "Runt", 20, new[] {"muggle", "giant"}),
|
||||
new People("Robin", "Comedian", 63, new[] {"muggle", "hilarious"}),
|
||||
new People("Tim", "Enchantor", 999, new[] {"magical", "grumpy"}),
|
||||
new People("Ragnar", "Viking", 25, new[] {"muggle", "grumpy"}),
|
||||
new People("Lars", "Viking", 30, new[] {"muggle", "grumpy"}),
|
||||
new People("Ferris", "Student", 17, new[] {"muggle"}),
|
||||
new People("Sean", "Confused", 35, true, new[] {"muggle"}),
|
||||
new People("Sean", "Actor", 90, false, new[] {"muggle", "metallurgist"}),
|
||||
new People("Bean", "Runt", 20, false, new[] {"muggle", "giant"}),
|
||||
new People("Robin", "Comedian", 63, false, new[] {"muggle", "hilarious"}),
|
||||
new People("Tim", "Enchantor", 999, false, new[] {"magical", "grumpy"}),
|
||||
new People("Ragnar", "Viking", 25, true, new[] {"muggle", "grumpy"}),
|
||||
new People("Lars", "Viking", 30, false, new[] {"muggle", "grumpy"}),
|
||||
new People("Ferris", "Student", 17, true, new[] {"muggle"}),
|
||||
};
|
||||
|
||||
public record People(string name, string kind, int number, string[] tags = null);
|
||||
public record People(string name, string kind, int? number, bool stillWithUs, string[] tags = null);
|
||||
|
||||
ExpressionPolicy notSean = new ExpressionPolicy {
|
||||
Name = "Not named Sean",
|
||||
|
@ -63,6 +63,23 @@ namespace McRule.Tests {
|
|||
RuleOperator = PredicateExpressionPolicyExtensions.RuleOperator.And
|
||||
};
|
||||
|
||||
ExpressionPolicy notQuiteDead = new ExpressionPolicy {
|
||||
Rules = new List<ExpressionRule>
|
||||
{
|
||||
("People", "stillWithUs", "true").ToFilterRule(),
|
||||
},
|
||||
RuleOperator = PredicateExpressionPolicyExtensions.RuleOperator.And
|
||||
};
|
||||
|
||||
ExpressionPolicy deadOrViking = new ExpressionPolicy {
|
||||
Rules = new List<ExpressionRule>
|
||||
{
|
||||
("People", "stillWithUs", "false").ToFilterRule(),
|
||||
("People", "kind", "Viking").ToFilterRule(),
|
||||
},
|
||||
RuleOperator = PredicateExpressionPolicyExtensions.RuleOperator.Or
|
||||
};
|
||||
|
||||
[SetUp]
|
||||
public void Setup() {
|
||||
|
||||
|
@ -146,5 +163,35 @@ namespace McRule.Tests {
|
|||
Assert.NotNull(folks);
|
||||
Assert.IsTrue(folks.All(x => x.tags.Contains("muggle")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BoolConditional() {
|
||||
var filter = notQuiteDead.GetExpression<People>()?.Compile();
|
||||
var folks = things.Where(filter);
|
||||
|
||||
Assert.NotNull(folks);
|
||||
Assert.IsTrue(folks.Count() > 0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NullFilterWhenNoMatchingTypes() {
|
||||
// Shouldn't have any filters in the policy for string objects.
|
||||
var filter = notQuiteDead.GetExpression<string>()?.Compile();
|
||||
|
||||
Assert.Null(filter);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPolicyWithOrConditional() {
|
||||
var filter = deadOrViking.GetExpression<People>()?.Compile();
|
||||
var folks = things.Where(filter);
|
||||
|
||||
Assert.NotNull(folks);
|
||||
Assert.NotNull(folks.Where(x => x.kind == "Viking" && x.stillWithUs == false));
|
||||
Assert.NotNull(folks.Where(x => x.kind == "Viking" && x.stillWithUs == true));
|
||||
|
||||
// Should be either a viking or dead, not neither.
|
||||
Assert.Null(folks.FirstOrDefault(x => x.kind != "Viking" && x.stillWithUs == true));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -27,8 +27,7 @@ public static class PredicateExpressionPolicyExtensions {
|
|||
if (!(filterType == "StartsWith" || filterType == "EndsWith" || filterType == "Contains" || filterType == "Equals"))
|
||||
{
|
||||
throw new Exception($"filterType must equal StartsWith, EndsWith or Contains. Passed {filterType}");
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
// Check that the property isn't null, otherwise we'd hit null object exceptions at runtime
|
||||
var notNull = Expression.NotEqual(lambda.Body, Expression.Constant(null));
|
||||
|
@ -253,17 +252,6 @@ public static class PredicateExpressionPolicyExtensions {
|
|||
return final;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combine two given expressions based on a given enum.
|
||||
/// </summary>
|
||||
public static Expression<Func<T, bool>>? CombinePredicates<T>(Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, PredicateExpressionPolicyExtensions.RuleOperator op) {
|
||||
var predicates = new List<Expression<Func<T, bool>>> { first, second }.Where(x => x != null);
|
||||
if (op == RuleOperator.And) {
|
||||
return CombineAnd(predicates);
|
||||
}
|
||||
return CombineOr(predicates);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combine a list of expressions based on the given operator enum.
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue