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 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 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 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 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 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 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 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 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 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 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 DataRowTest() { var dr = GetDataRow(); // (int) dr["Column2"] == 123 && (string) dr["Column1"] == "Test" Rule rule = DataRule.Create <int>("Column2", MreOperator.Equal, "123") & DataRule.Create <string>("Column1", MreOperator.Equal, "Test"); Mre engine = new Mre(); var c1123 = engine.CompileRule <DataRow>(rule); bool passes = c1123(dr); Assert.IsTrue(passes); dr["Column2"] = 456; dr["Column1"] = "Hello"; passes = c1123(dr); 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 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 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 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 DataRowTest_RuntimeType() { var dt = CreateEmptyDataTable(); Rule rule = DataRule.Create("Column2", MreOperator.Equal, 123, dt.Columns["Column2"].DataType); Mre engine = new Mre(); var c1123 = engine.CompileRule <DataRow>(rule); var dr = GetDataRow(dt); bool passes = c1123(dr); Assert.IsTrue(passes); dr["Column2"] = 456; dr["Column1"] = "Hello"; passes = c1123(dr); Assert.IsFalse(passes); }
public void DataRowTest_DBNull() { Rule rule = DataRule.Create <int?>("Column2", MreOperator.Equal, (int?)null) & DataRule.Create <string>("Column1", MreOperator.Equal, null); Mre engine = new Mre(); var c1123 = engine.CompileRule <DataRow>(rule); var dt = CreateEmptyDataTable(); var dr = GetDataRowDbNull(dt); bool passes = c1123(dr); Assert.IsTrue(passes); dr["Column2"] = 456; dr["Column1"] = "Hello"; passes = c1123(dr); 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 CoerceMethod() { Order order = GetOrder(); Rule rule = new Rule { MemberName = "Codes", Operator = "Contains", TargetValue = "243", Inputs = new List <object>() { "243" } }; Mre engine = new Mre(); var compiledRule = engine.CompileRule <Order>(rule); bool passes = compiledRule(order); Assert.IsTrue(passes); order.Codes.Clear(); passes = compiledRule(order); Assert.IsFalse(passes); }
public void BooleanMethods_ByType() { Order order = 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(typeof(Order), rule); bool passes = (bool)boolMethod.DynamicInvoke(order); Assert.IsTrue(passes); var item = order.Items.First(x => x.ItemCode == "Test"); item.ItemCode = "Changed"; passes = (bool)boolMethod.DynamicInvoke(order); Assert.IsFalse(passes); }