public void ConditionalLogic() { Order order = this.GetOrder(); Rule rule = new Rule() { Operator = System.Linq.Expressions.ExpressionType.AndAlso.ToString("g"), Rules = new List<Rule>() { new Rule(){ MemberName = "Customer.LastName", TargetValue = "Doe", Operator = "Equal"}, new Rule(){ Operator = "Or", Rules = new List<Rule>(){ new Rule(){ MemberName = "Customer.FirstName", TargetValue = "John", Operator = "Equal"}, new Rule(){ MemberName = "Customer.FirstName", TargetValue = "Jane", Operator = "Equal"} } } } }; MRE engine = new MRE(); var fakeName = engine.CompileRule<Order>(rule); bool passes = fakeName(order); Assert.IsTrue(passes); order.Customer.FirstName = "Philip"; passes = fakeName(order); Assert.IsFalse(passes); }
public void MemberEqualsMember() { var testObj = new MemberOperaterMemberTestObject() { Source = "bob", Target = "bob" }; var rule = new Rule { MemberName = "Source", Operator = "Equal", TargetValue = "*.Target" }; var mre = new MRE(); var func = mre.CompileRule <MemberOperaterMemberTestObject>(rule); Assert.IsTrue(func(testObj)); testObj.Target = "notBob"; Assert.IsFalse(func(testObj)); }
public void AnyOperator() { Order order = GetOrder(); //order.Items.Any(a => a.ItemCode == "test") Rule rule = new Rule { MemberName = "Items",// The array property Operator = "Any", Rules = new[] { new Rule { MemberName = "ItemCode", // the property in the above array item Operator = "Equal", TargetValue = "Test", } } }; MRE engine = new MRE(); var boolMethod = engine.CompileRule <Order>(rule); bool passes = boolMethod(order); Assert.IsTrue(passes); var item = order.Items.First(x => x.ItemCode == "Test"); item.ItemCode = "Changed"; passes = boolMethod(order); Assert.IsFalse(passes); }
public void BAdMethod() { Order order = ExampleUsage.GetOrder(); Rule rule = DataRule.Create <int>("Column2", mreOperator.Equal, "123"); MRE engine = new MRE(); var c1_123 = engine.CompileRule <Order>(rule); bool passes = c1_123(order); Assert.IsTrue(false); // should not get here. }
public void NotACollection() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("Customer[1]", mreOperator.Equal, "123"); MRE engine = new MRE(); var c1_123 = engine.CompileRule <Order>(rule); bool passes = c1_123(order); Assert.IsTrue(false); // should not get here. }
public void BadPropertyName() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("NotAProperty", mreOperator.Equal, 1); MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(false); // should not get here. }
public void BadMethod() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.MethodOnChild("Customer.FirstName", "NotAMethod", "ohn"); MRE engine = new MRE(); var c1_123 = engine.CompileRule <Order>(rule); bool passes = c1_123(order); Assert.IsTrue(false); // should not get here. }
public void EnumAsWord() { Order order = ExampleUsage.GetOrder(); order.Total = null; Rule rule = Rule.Create("Status", mreOperator.Equal, "Open"); MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); }
public void Time_OutOfRange_Days() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("OrderDate", mreOperator.GreaterThanOrEqual, "#NOW-90D"); order.OrderDate = DateTime.Now.AddDays(-100); MRE engine = new MRE(); var boolMethod = engine.CompileRule <Order>(rule); bool passes = boolMethod(order); Assert.IsFalse(passes); }
public void Prebuild2() { MRE engine = new MRE(); Rule rule1 = Rule.MethodOnChild("Customer.FirstName", "EndsWith", "ohn"); Rule rule2 = Rule.Create("Customer.Country.CountryCode", mreOperator.Equal, "AUS"); var endsWithOhn = engine.CompileRule <Order>(rule1); var inAus = engine.CompileRule <Order>(rule2); Order order = ExampleUsage.GetOrder(); int reps = 1000; for (int i = 0; i < reps; ++i) { bool passes = endsWithOhn(order); Assert.IsTrue(passes); passes = inAus(order); Assert.IsTrue(passes); } }
public void IsFloat_Bad_Word() { var target = new IsTypeClass { NumAsString = "1234.567", OtherField = "Hello, World" }; Rule rule = Rule.IsFloat("OtherField"); MRE engine = new MRE(); var compiledRule = engine.CompileRule <IsTypeClass>(rule); bool passes = compiledRule(target); Assert.IsFalse(passes); }
public void IsDouble_OK_Int() { var target = new IsTypeClass { NumAsString = "1234", OtherField = "Hello, World" }; Rule rule = Rule.IsDouble("NumAsString"); MRE engine = new MRE(); var compiledRule = engine.CompileRule <IsTypeClass>(rule); bool passes = compiledRule(target); Assert.IsTrue(passes); }
public void DecimalProperties() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("Total", mreOperator.GreaterThan, 12.00m); MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); order.Total = 9.99m; passes = compiledRule(order); Assert.IsFalse(passes); }
public void DateProperties() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("OrderDate", mreOperator.LessThan, "1800-01-01"); MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); order.OrderDate = new DateTime(1814, 9, 13); passes = compiledRule(order); Assert.IsFalse(passes); }
public void IntProperties() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("OrderId", mreOperator.Equal, 1); MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); order.OrderId = 5; passes = compiledRule(order); Assert.IsFalse(passes); }
public void ChildProperties2() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("Customer.Country.CountryCode", mreOperator.Equal, "AUS"); MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); order.Customer.Country.CountryCode = "USA"; passes = compiledRule(order); Assert.IsFalse(passes); }
public void ArrayTest() { var array = new ArrayInside(); Rule rule = Rule.Create("Dbl[1]", mreOperator.Equal, 22.222); MRE engine = new MRE(); var compiledRule = engine.CompileRule <ArrayInside>(rule); bool passes = compiledRule(array); Assert.IsTrue(passes); array.Dbl[1] = .0001; passes = compiledRule(array); Assert.IsFalse(passes); }
public void SelfReferenialTest() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("Items[1].Cost", mreOperator.Equal, "*.Items[0].Cost"); MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); order.Items[1].Cost = 6.99m; passes = compiledRule(order); Assert.IsFalse(passes); }
public void ConditionalLogic2() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("Customer.LastName", mreOperator.Equal, "Doe") & (Rule.Create("Customer.FirstName", mreOperator.Equal, "John") | Rule.Create("Customer.FirstName", mreOperator.Equal, "Jane")); MRE engine = new MRE(); var fakeName = engine.CompileRule <Order>(rule); bool passes = fakeName(order); Assert.IsTrue(passes); order.Customer.FirstName = "Philip"; passes = fakeName(order); Assert.IsFalse(passes); }
public void ChildPropertyBooleanMethods2() { Order order = ExampleUsage.GetOrder(); //Regular method that exists on string.. As a note expression methods are not available Rule rule = Rule.MethodOnChild("Customer.FirstName", "EndsWith", "ohn"); MRE engine = new MRE(); var childPropCheck = engine.CompileRule <Order>(rule); bool passes = childPropCheck(order); Assert.IsTrue(passes); order.Customer.FirstName = "jane"; passes = childPropCheck(order); Assert.IsFalse(passes); }
public void RegexIsMatch2()//Had to add a Regex evaluator to make it feel 'Complete' { Order order = ExampleUsage.GetOrder(); // Regex = Capital letter, vowel, then two constanants ("John" passes, "Jane" fails) Rule rule = Rule.Create("Customer.FirstName", mreOperator.IsMatch, @"^[A-Z][aeiou][bcdfghjklmnpqrstvwxyz]{2}$"); MRE engine = new MRE(); var regexCheck = engine.CompileRule <Order>(rule); bool passes = regexCheck(order); Assert.IsTrue(passes); order.Customer.FirstName = "Jane"; passes = regexCheck(order); Assert.IsFalse(passes); }
public void ChildPropertiesOfNull() { Order order = GetOrder(); order.Customer = null; Rule rule = new Rule { MemberName = "Customer.Country.CountryCode", Operator = ExpressionType.Equal.ToString("g"), TargetValue = "AUS" }; MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsFalse(passes); }
public void DataRowTest() { var dr = GetDataRow(); Rule rule = DataRule.Create <int>("Column2", mreOperator.Equal, "123") & DataRule.Create <string>("Column1", mreOperator.Equal, "Test"); MRE engine = new MRE(); var c1_123 = engine.CompileRule <DataRow>(rule); bool passes = c1_123(dr); Assert.IsTrue(passes); dr["Column2"] = 456; dr["Column1"] = "Hello"; passes = c1_123(dr); Assert.IsFalse(passes); }
public void NullableProperties() { Order order = ExampleUsage.GetOrder(); order.Total = null; Rule rule = Rule.Create("Total", mreOperator.Equal, null); MRE engine = new MRE(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); order.Total = 9.99m; passes = compiledRule(order); Assert.IsFalse(passes); }
public void AnyOperator() { Order order = ExampleUsage.GetOrder(); //order.Items.Any(a => a.ItemCode == "test") Rule rule = Rule.Any("Items", Rule.Create("ItemCode", mreOperator.Equal, "Test")); MRE engine = new MRE(); var boolMethod = engine.CompileRule <Order>(rule); bool passes = boolMethod(order); Assert.IsTrue(passes); var item = order.Items.First(x => x.ItemCode == "Test"); item.ItemCode = "Changed"; passes = boolMethod(order); Assert.IsFalse(passes); }
public void IsInInput_NoExactMatch() { var value = "world"; var rule = new Rule() { Operator = "IsInInput", Inputs = new List <string> { "hello", "World" } }; var mre = new MRE(); var ruleFunc = mre.CompileRule <string>(rule); Assert.IsFalse(ruleFunc(value)); }
public void IsInInput_MultiValue() { var value = "hello"; var rule = new Rule() { Operator = "IsInInput", Inputs = new List <string> { "hello", "World" } }; var mre = new MRE(); var ruleFunc = mre.CompileRule <string>(rule); Assert.IsTrue(ruleFunc(value)); }
public void BareString() { var rule = new Rule() { Operator = "StartsWith", Inputs = new[] { "FDX" } }; var engine = new MRE(); var childPropCheck = engine.CompileRule <string>(rule); var passes = childPropCheck("FDX 123456"); Assert.IsTrue(passes); passes = childPropCheck("BOB 123456"); Assert.IsFalse(passes); }
public void BooleanMethods() { Order order = this.GetOrder(); Rule rule = new Rule() { Operator = "HasItem",//The Order Object Contains a method named 'HasItem' that returns true/false Inputs = new List<object>{"Test"} }; MRE engine = new MRE(); var boolMethod = engine.CompileRule<Order>(rule); bool passes = boolMethod(order); Assert.IsTrue(passes); var item = order.Items.First(x => x.ItemCode == "Test"); item.ItemCode = "Changed"; passes = boolMethod(order); Assert.IsFalse(passes); }
public void ChildProperties() { Order order = this.GetOrder(); Rule rule = new Rule() { MemberName = "Customer.Country.CountryCode", Operator = System.Linq.Expressions.ExpressionType.Equal.ToString("g"), TargetValue = "AUS" }; MRE engine = new MRE(); var compiledRule = engine.CompileRule<Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); order.Customer.Country.CountryCode = "USA"; passes = compiledRule(order); Assert.IsFalse(passes); }
public void AllOperator() { Order order = ExampleUsage.GetOrder(); //order.Items.All(a => a.Cost > 2.00m) Rule rule = Rule.All("Items", Rule.Create("Cost", mreOperator.GreaterThan, "2.00")); MRE engine = new MRE(); var boolMethod = engine.CompileRule <Order>(rule); bool passes = boolMethod(order); Assert.IsTrue(passes); var item = order.Items.First(x => x.ItemCode == "Test"); item.Cost = 1.99m; passes = boolMethod(order); Assert.IsFalse(passes); }
public void RegexIsMatch()//Had to add a Regex evaluator to make it feel 'Complete' { Order order = GetOrder(); Rule rule = new Rule { MemberName = "Customer.FirstName", Operator = "IsMatch", TargetValue = @"^[a-zA-Z0-9]*$" }; MRE engine = new MRE(); var regexCheck = engine.CompileRule <Order>(rule); bool passes = regexCheck(order); Assert.IsTrue(passes); order.Customer.FirstName = "--NoName"; passes = regexCheck(order); Assert.IsFalse(passes); }
public void ChildPropertyOfNullBooleanMethods() { Order order = GetOrder(); order.Customer = null; Rule rule = new Rule { MemberName = "Customer.FirstName", Operator = "EndsWith", //Regular method that exists on string.. As a note expression methods are not available Inputs = new List <object> { "ohn" } }; MRE engine = new MRE(); var childPropCheck = engine.CompileRule <Order>(rule); bool passes = childPropCheck(order); Assert.IsFalse(passes); }
public void RegexIsMatch()//Had to add a Regex evaluator to make it feel 'Complete' { Order order = this.GetOrder(); Rule rule = new Rule() { MemberName = "Customer.FirstName", Operator = "IsMatch", TargetValue = @"^[a-zA-Z0-9]*$" }; MRE engine = new MRE(); var regexCheck = engine.CompileRule<Order>(rule); bool passes = regexCheck(order); Assert.IsTrue(passes); order.Customer.FirstName = "--NoName"; passes = regexCheck(order); Assert.IsFalse(passes); }
public void ChildPropertyBooleanMethods() { Order order = this.GetOrder(); Rule rule = new Rule() { MemberName = "Customer.FirstName", Operator = "EndsWith",//Regular method that exists on string.. As a note expression methods are not available Inputs = new List<object> { "ohn" } }; MRE engine = new MRE(); var childPropCheck = engine.CompileRule<Order>(rule); bool passes = childPropCheck(order); Assert.IsTrue(passes); order.Customer.FirstName = "jane"; passes = childPropCheck(order); Assert.IsFalse(passes); }