public void ShouldNotBePossibleEvaluateRuleWithEqualsConstraintAndMissingProperty()
        {
            var constraint = new EqualsConstraint("Name", "John");
            var result     = constraint.Evaluate(ObjectHelper.ToDictionary(new { }));

            Assert.False(result);
        }
Example #2
0
        private IConstraint ConvertJsonToConstraint(JObject jObject)
        {
            if (jObject["and"] != null)
            {
                var andConstraint = new AndConstraint();
                foreach (var item in jObject["and"])
                {
                    andConstraint.Add(ConvertJsonToConstraint((item as JObject)));
                }

                return(andConstraint);
            }
            if (jObject["or"] != null)
            {
                var orConstraint = new OrConstraint();
                foreach (var item in jObject["or"])
                {
                    orConstraint.Add(ConvertJsonToConstraint((item as JObject)));
                }

                return(orConstraint);
            }
            if (jObject["equals"] != null)
            {
                var equalsConstraint = new EqualsConstraint();

                equalsConstraint.Property     = jObject["equals"][0]["doc"]?.ToString();
                equalsConstraint.ValueToEqual = jObject["equals"][1]?.ToString();

                return(equalsConstraint);
            }
            if (jObject["in"] != null)
            {
                var inConstraint = new InConstraint();

                inConstraint.Property      = jObject["in"][0]["doc"]?.ToString();
                inConstraint.ValuesToEqual = jObject["in"][1]?.ToObject <string[]>();

                return(inConstraint);
            }
            if (jObject["paths"] != null)
            {
                var pathsConstraint = new PathConstraint();

                pathsConstraint.Fields = jObject["paths"][0]["doc"]?.ToString();

                return(pathsConstraint);
            }
            if (jObject["not"] != null)
            {
                var notConstraint = new NotConstraint();

                notConstraint.ConstraintToInvert = ConvertJsonToConstraint(jObject["not"] as JObject);

                return(notConstraint);
            }

            return(null);
        }
        public void ShouldEvaluatePrimitiveDate()
        {
            var fakeData = new Dictionary <string, object>()
            {
                { "myProperty", new DateTime(2016, 12, 21) }
            };
            var sut = new EqualsConstraint("myProperty", new DateTime(2016, 12, 21));

            var result = sut.Evaluate(fakeData);

            Assert.True(result);
        }
        public void ShouldEvaluateDecimalValue()
        {
            var fakeData = new Dictionary <string, object>()
            {
                { "myProperty", -18.4m }
            };
            var sut = new EqualsConstraint("myProperty", -18.4m);

            var result = sut.Evaluate(fakeData);

            Assert.True(result);
        }
        public void ShouldEvaluatePrimitiveInteger()
        {
            var fakeData = new Dictionary <string, object>()
            {
                { "myProperty", 18 }
            };
            var sut = new EqualsConstraint("myProperty", 18);

            var result = sut.Evaluate(fakeData);

            Assert.True(result);
        }
        public void ShouldEvaluateByValueComplexTypeWithEqualsOverrided()
        {
            var fakeData = new Dictionary <string, object>()
            {
                { "myProperty", new Money(12.3m, Currency.DOLAR) }
            };

            var sut = new EqualsConstraint("myProperty", new Money(12.3m, Currency.REAL));

            Assert.False(sut.Evaluate(fakeData));

            sut = new EqualsConstraint("myProperty", new Money(12.3m, Currency.DOLAR));
            Assert.True(sut.Evaluate(fakeData));
        }
        public void ShouldEvaluateByReferenceComplexTypeWithoutEqualsOverrided()
        {
            var aComplexTypeWithNoEquals = new DummyClass(21);
            var fakeData = new Dictionary <string, object>()
            {
                { "myProperty", aComplexTypeWithNoEquals }
            };

            var sut = new EqualsConstraint("myProperty", new DummyClass(21));

            Assert.False(sut.Evaluate(fakeData));

            sut = new EqualsConstraint("myProperty", aComplexTypeWithNoEquals);
            Assert.True(sut.Evaluate(fakeData));
        }
Example #8
0
        public void EqualsConstraint()
        {
            IConstraint sut = new EqualsConstraint(42);

            Assert.IsTrue(sut.Validate(42));
        }