Added more tests and removing unused code.

pull/5/head
Sean McArde 2023-07-24 14:14:51 -07:00
parent 739ed9021c
commit 7f623d9c87
2 changed files with 55 additions and 20 deletions

View File

@ -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,34 @@ 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() {
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));
}
}
}

View File

@ -253,17 +253,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>