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 BadMethod() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.MethodOnChild("Customer.FirstName", "NotAMethod", "ohn"); Mre engine = new Mre(); var c1123 = engine.CompileRule <Order>(rule); bool passes = c1123(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 NotACollection() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("Customer[1]", MreOperator.Equal, "123"); Mre engine = new Mre(); var c1123 = engine.CompileRule <Order>(rule); bool passes = c1123(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 EnumProperties() { Order order = ExampleUsage.GetOrder(); order.Total = null; Rule rule = Rule.Create("Status", MreOperator.Equal, Status.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 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 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 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 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 ListTest() { Order order = ExampleUsage.GetOrder(); Rule rule = Rule.Create("Items[1].Cost", MreOperator.Equal, 5.25m); 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 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 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 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 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 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 NullAsWord() { 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 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 JsonVisualizationTest() { var order = ExampleUsage.GetOrder(); var members = Mre.Member.GetFields(order.GetType()); var serializer = new DataContractJsonSerializer(members.GetType()); string text; using (var stream1 = new MemoryStream()) { serializer.WriteObject(stream1, members); stream1.Position = 0; var sr = new StreamReader(stream1); text = sr.ReadToEnd(); } Assert.IsTrue(text.Length > 100); }
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 BooleanMethods2() { Order order = ExampleUsage.GetOrder(); //The Order Object Contains a method named 'HasItem(string itemCode)' that returns true/false Rule rule = Rule.Method("HasItem", "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 JsonSerialization() { var serializer = new DataContractJsonSerializer(typeof(Rule)); string text; using (var stream1 = new MemoryStream()) { Rule rule = Rule.Create("Customer.LastName", MreOperator.Equal, "Doe") & (Rule.Create("Customer.FirstName", MreOperator.Equal, "John") | Rule.Create("Customer.FirstName", MreOperator.Equal, "Jane")) & Rule.Create("Items[1].Cost", MreOperator.Equal, 5.25m); serializer.WriteObject(stream1, rule); stream1.Position = 0; var sr = new StreamReader(stream1); text = sr.ReadToEnd(); } Rule newRule; // add breakpoint here, to view JSON in text. using (var stream2 = new MemoryStream(Encoding.UTF8.GetBytes(text))) { newRule = (Rule)serializer.ReadObject(stream2); } var order = ExampleUsage.GetOrder(); Mre engine = new Mre(); var fakeName = engine.CompileRule <Order>(newRule); bool passes = fakeName(order); Assert.IsTrue(passes); order.Customer.FirstName = "Philip"; passes = fakeName(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 XmlSerialization() { var serializer = new DataContractSerializer(typeof(Rule)); string text; using (var writer = new StringWriter()) { Rule rule = Rule.Create("Customer.LastName", MreOperator.Equal, "Doe") & (Rule.Create("Customer.FirstName", MreOperator.Equal, "John") | Rule.Create("Customer.FirstName", MreOperator.Equal, "Jane")); using (var xw = XmlWriter.Create(writer)) serializer.WriteObject(xw, rule); text = writer.ToString(); } Rule newRule; // add breakpoint here, to view XML in text. using (var reader = new StringReader(text)) using (var xr = XmlReader.Create(reader)) { newRule = (Rule)serializer.ReadObject(xr); } var order = ExampleUsage.GetOrder(); Mre engine = new Mre(); var fakeName = engine.CompileRule <Order>(newRule); bool passes = fakeName(order); Assert.IsTrue(passes); order.Customer.FirstName = "Philip"; passes = fakeName(order); Assert.IsFalse(passes); }