Exemple #1
0
        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);
        }
Exemple #3
0
        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);
        }