コード例 #1
0
        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.
        }
コード例 #2
0
        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.
        }
コード例 #3
0
        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.
        }
コード例 #4
0
        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.
        }
コード例 #5
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #6
0
ファイル: NewAPI.cs プロジェクト: aldass/MicroRuleEngine
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #9
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #10
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #11
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #12
0
ファイル: NewAPI.cs プロジェクト: aldass/MicroRuleEngine
        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);
        }
コード例 #13
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #14
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #15
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #16
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #17
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #18
0
ファイル: NewAPI.cs プロジェクト: aldass/MicroRuleEngine
        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);
        }
コード例 #19
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #22
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
        }
コード例 #23
0
        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);
        }
コード例 #24
0
ファイル: NewAPI.cs プロジェクト: yesuvadian/MicroRuleEngine
        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);
            }
        }
コード例 #25
0
        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);
        }