public void CastIntString(int value, bool allowCast, bool throwCastException, bool result, bool expectException)
        {
            EqualSpecification specification = new EqualSpecification("key", SpecificationValue.AnyOf("1", "2"));

            SpecificationEvaluationSettings settings =
                new SpecificationEvaluationSettings
            {
                IncludeDetails   = true,
                ThrowValueErrors = throwCastException
            };

            settings.ValueSettings.AllowCast = allowCast;
            Dictionary <string, object> values = new Dictionary <string, object> {
                { "key", value }
            };

            if (expectException)
            {
                Assert.Throws <InvalidOperationException>(() => specification.Evaluate(values, settings));
            }
            else
            {
                var res = specification.Evaluate(values, settings);

                Assert.Equal(result, res.IsSatisfied);
            }
        }
        public void SingleAll(int l, int r1, int r2, bool result)
        {
            EqualSpecification specification = new EqualSpecification(
                "key",
                SpecificationValue.AllOf(new List <int> {
                r1, r2
            }));

            Assert.Equal(result, specification.Evaluate(new Dictionary <string, object> {
                { "key", l }
            }).IsSatisfied);
            Assert.Equal(
                result,
                specification.Evaluate(new Dictionary <string, object> {
                { "key", SpecificationValue.Single(l) }
            })
                .IsSatisfied);
        }
        public void SingleSingleDetails()
        {
            EqualSpecification  specification = new EqualSpecification("key", SpecificationValue.Single(1));
            SpecificationResult spr           = specification.Evaluate(new Dictionary <string, object> {
                { "key", 2 }
            });

            Assert.False(spr.IsSatisfied);
            Assert.Equal("Right value(s) 1 not satisfied left value(s) 2 in key equal 1", spr.Details);
        }
        public void AnyAll(int l1, int l2, int r1, int r2, bool result)
        {
            EqualSpecification specification = new EqualSpecification("key", SpecificationValue.AllOf(r1, r2));

            Assert.Equal(
                result,
                specification.Evaluate(new Dictionary <string, object> {
                { "key", new List <int> {
                      l1, l2
                  } }
            })
                .IsSatisfied);
            Assert.Equal(
                result,
                specification.Evaluate(new Dictionary <string, object> {
                { "key", SpecificationValue.AnyOf(l1, l2) }
            })
                .IsSatisfied);
        }
        public void SingleSingle(int l, int r, bool result)
        {
            EqualSpecification  specification = new EqualSpecification("key", SpecificationValue.Single(r));
            SpecificationResult spr           = specification.Evaluate(new Dictionary <string, object> {
                { "key", l }
            });

            Assert.Equal(result, spr.IsSatisfied);
            Assert.Equal(
                result,
                specification.Evaluate(new Dictionary <string, object> {
                { "key", SpecificationValue.Single(l) }
            })
                .IsSatisfied);

            if (result)
            {
                Assert.Null(spr.Details);
            }
        }
        public void EqualReference()
        {
            EqualSpecification specification = new EqualSpecification(
                "key",
                SpecificationValue.Ref("k1"));

            Assert.True(
                specification.Evaluate(new Dictionary <string, object>
            {
                { "k1", new[] { 1, 2 } },
                { "key", 1 }
            })
                .IsSatisfied);
        }
        public void CastUnsupportedType()
        {
            EqualSpecification          specification = new EqualSpecification("key", SpecificationValue.AnyOf("1", "2"));
            Dictionary <string, object> values        =
                new Dictionary <string, object> {
                { "key", new List <TimeSpan> {
                      TimeSpan.FromDays(1)
                  } }
            };

            var ae = Assert.Throws <InvalidOperationException>(() => specification.Evaluate(values));

            Assert.Contains("Unable to resolve left value", ae.Message);
        }
        public void CastIntArrayString(
            int value,
            bool allowCast,
            bool throwCastException,
            bool result,
            bool expectException)
        {
            EqualSpecification specification = new EqualSpecification("key", SpecificationValue.AnyOf("1", "2"));

            SpecificationEvaluationSettings settings =
                new SpecificationEvaluationSettings
            {
                IncludeDetails   = true,
                ThrowValueErrors = throwCastException
            };

            settings.ValueSettings.AllowCast = allowCast;
            Dictionary <string, object> values =
                new Dictionary <string, object> {
                { "key", new List <int> {
                      value, 123
                  } }
            };

            if (expectException)
            {
                var ae = Assert.Throws <InvalidOperationException>(() => specification.Evaluate(values, settings));
                Assert.Contains("can't be compared with type", ae.Message);
            }
            else
            {
                var res = specification.Evaluate(values, settings);

                Assert.Equal(result, res.IsSatisfied);
            }
        }