Пример #1
0
		public void Test_Equal_ValueType()
		{
			EqualSpecification s = new EqualSpecification();
			s.RefValueExpression = new ConstantExpression(1);
			Assert.IsTrue(s.Test(1).Success);
			Assert.IsFalse(s.Test(0).Success);
			Assert.IsFalse(s.Test(null).Success);
		}
Пример #2
0
		public void Test_Equal_Null()
		{
			EqualSpecification s = new EqualSpecification();
			s.RefValueExpression = new ConstantExpression(null);

			Assert.IsTrue(s.Test(null).Success);
			Assert.IsFalse(s.Test(new object()).Success);
			Assert.IsFalse(s.Test(1).Success);
		}
Пример #3
0
		public void Test_Equal_ReferenceType()
		{
			object x = new object();
			object y = new object();

			EqualSpecification s = new EqualSpecification();
			s.RefValueExpression = new ConstantExpression(x);

			Assert.IsTrue(s.Test(x).Success);
			Assert.IsFalse(s.Test(y).Success);
			Assert.IsFalse(s.Test(null).Success);
		}
Пример #4
0
		public void Test_Equal_CoerceTypes()
		{
			EqualSpecification s = new EqualSpecification();
			s.RefValueExpression = new ConstantExpression("1");

			Assert.IsTrue(s.Test(1).Success);
			Assert.IsTrue(s.Test(1.0).Success);
			Assert.IsTrue(s.Test("1").Success);
			Assert.IsFalse(s.Test(null).Success);
			Assert.IsFalse(s.Test(0).Success);
			Assert.IsFalse(s.Test(0.0).Success);
			Assert.IsFalse(s.Test("0").Success);
		}
Пример #5
0
        public void Test_Equal_CoerceTypes()
        {
            EqualSpecification s = new EqualSpecification();

            s.RefValueExpression = new ConstantExpression("1");

            Assert.IsTrue(s.Test(1).Success);
            Assert.IsTrue(s.Test(1.0).Success);
            Assert.IsTrue(s.Test("1").Success);
            Assert.IsFalse(s.Test(null).Success);
            Assert.IsFalse(s.Test(0).Success);
            Assert.IsFalse(s.Test(0.0).Success);
            Assert.IsFalse(s.Test("0").Success);
        }
        public void ReplaceInconsistentTypes()
        {
            EqualSpecification equal = new EqualSpecification(
                "k1",
                SpecificationValue.Ref("{p1}"));

            ValueReferenceVisitor visitor =
                new ValueReferenceVisitor(new Dictionary <string, object> {
                { "{p1}", TimeSpan.FromDays(1) }
            });

            InvalidOperationException exc = Assert.Throws <InvalidOperationException>(() => visitor.Visit(equal));

            Assert.Contains("Unable to resolve value reference for k1 equal ref({p1}). Value of type System.TimeSpan not supported.", exc.Message);
        }
        public void Test_Equal_CoerceChar2()
        {
            EqualSpecification s = new EqualSpecification();

            s.RefValueExpression = new ConstantExpression("x");

            Assert.IsTrue(s.Test("x").Success);
            Assert.IsTrue(s.Test('x').Success);

            Assert.IsFalse(s.Test('\0').Success);
            Assert.IsFalse(s.Test('y').Success);
            Assert.IsFalse(s.Test("").Success);
            Assert.IsFalse(s.Test("xy").Success);
            Assert.IsFalse(s.Test("yx").Success);
        }
        public void ReplaceNoKeysThrowDefault()
        {
            EqualSpecification equal = new EqualSpecification(
                "k1",
                SpecificationValue.Ref("{p1}"));

            ValueReferenceVisitor visitor =
                new ValueReferenceVisitor(new Dictionary <string, object> {
                { "{p2}", "v1" }
            });

            InvalidOperationException exc = Assert.Throws <InvalidOperationException>(() => visitor.Visit(equal));

            Assert.Contains("Unable to resolve value reference for k1 equal ref({p1}). Key {p1} is missing", exc.Message);
        }
Пример #9
0
		// this test is related to bug #5909 - should be able to compare enums to strings
		public void Test_Equal_CoerceEnum()
		{
			EqualSpecification s = new EqualSpecification();

			s.RefValueExpression = new ConstantExpression("Blue");
			Assert.IsTrue(s.Test(Color.Blue).Success);
			Assert.IsFalse(s.Test(null).Success);
			Assert.IsFalse(s.Test(Color.Red).Success);

			s.RefValueExpression = new ConstantExpression(Color.Red);
			Assert.IsTrue(s.Test(Color.Red).Success);
			Assert.IsTrue(s.Test("Red").Success);
			Assert.IsFalse(s.Test(null).Success);
			Assert.IsFalse(s.Test("").Success);
			Assert.IsFalse(s.Test(Color.Blue).Success);
		}
        // this test is related to bug #5909 - should be able to compare enums to strings
        public void Test_Equal_CoerceEnum()
        {
            EqualSpecification s = new EqualSpecification();

            s.RefValueExpression = new ConstantExpression("Blue");
            Assert.IsTrue(s.Test(Color.Blue).Success);
            Assert.IsFalse(s.Test(null).Success);
            Assert.IsFalse(s.Test(Color.Red).Success);

            s.RefValueExpression = new ConstantExpression(Color.Red);
            Assert.IsTrue(s.Test(Color.Red).Success);
            Assert.IsTrue(s.Test("Red").Success);
            Assert.IsFalse(s.Test(null).Success);
            Assert.IsFalse(s.Test("").Success);
            Assert.IsFalse(s.Test(Color.Blue).Success);
        }
        public void ReplaceCircular()
        {
            EqualSpecification equal = new EqualSpecification(
                "k1",
                SpecificationValue.Ref("{p1}"));

            ValueReferenceVisitor visitor = new ValueReferenceVisitor(
                new Dictionary <string, object>
            {
                { "{p1}", SpecificationValue.Ref("{p2}") },
                { "{p2}", SpecificationValue.Ref("{p1}") },
            });

            var exc = Assert.Throws <InvalidOperationException>(() => visitor.Visit(equal));

            Assert.Contains("Circular references detected", exc.Message);
        }
        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);
        }
Пример #13
0
        public void Test_Equal_CoerceFloat4()
        {
            EqualSpecification s = new EqualSpecification();

            s.RefValueExpression = new ConstantExpression("1.1");

            Assert.IsTrue(s.Test(1.1f).Success);
            Assert.IsTrue(s.Test(1.1d).Success);
            Assert.IsTrue(s.Test((decimal)1.1).Success);
            Assert.IsTrue(s.Test("1.1").Success);

            Assert.IsFalse(s.Test(1).Success);
            Assert.IsFalse(s.Test("1").Success);
            Assert.IsFalse(s.Test("1.10").Success);
            Assert.IsFalse(s.Test(null).Success);
            Assert.IsFalse(s.Test(0.0).Success);
            Assert.IsFalse(s.Test("0").Success);
            Assert.IsFalse(s.Test(0).Success);
        }
        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 ReplaceReferenceIncorrectType()
        {
            EqualSpecification equal = new EqualSpecification(
                "k1",
                SpecificationValue.Ref("{p1}"));

            ValueReferenceVisitor visitor = new ValueReferenceVisitor(
                new Dictionary <string, object>
            {
                { "{p1}", SpecificationValue.Ref("{p2}") },
                { "{p2}", TimeSpan.FromDays(1) },
            });

            InvalidOperationException exc = Assert.Throws <InvalidOperationException>(() => visitor.Visit(equal));

            Assert.Contains("Unable to resolve", exc.Message);
            Assert.Contains("{p1}", exc.ToString());
            Assert.Contains("{p2}", exc.ToString());
        }
        public void ReplaceInt()
        {
            EqualSpecification equal = new EqualSpecification(
                "k1",
                SpecificationValue.Ref("{p1}"));

            ValueReferenceVisitor visitor =
                new ValueReferenceVisitor(new Dictionary <string, object> {
                { "{p1}", 2 }
            });

            Specification result = visitor.Visit(equal);

            var r = Assert.IsType <EqualSpecification>(result);

            Assert.NotSame(equal, r);
            Assert.Contains(2, r.Value.Values);
            Assert.DoesNotContain("{p1}", r.Value.Values);
            Assert.Equal(SpecificationValue.DataType.Int, r.Value.ValueType);
        }
        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 ReplaceNoKeysIgnore()
        {
            EqualSpecification equal = new EqualSpecification(
                "k1",
                SpecificationValue.Ref("{p1}"));

            ValueReferenceVisitor visitor = new ValueReferenceVisitor(
                new Dictionary <string, object> {
                { "{p2}", "v1" }
            },
                new ReferenceResolutionSettings {
                ThrowValueErrors = false
            });

            Specification result = visitor.Visit(equal);

            var r = Assert.IsType <EqualSpecification>(result);

            Assert.Same(equal, r);
            Assert.Contains("{p1}", r.Value.Values);
            Assert.DoesNotContain("v1", r.Value.Values);
        }
        public void ReplaceSpecification()
        {
            EqualSpecification equal = new EqualSpecification(
                "k1",
                SpecificationValue.Ref("{p1}"));

            ValueReferenceVisitor visitor = new ValueReferenceVisitor(
                new Dictionary <string, object> {
                { "{p1}", SpecificationValue.Single("v1") }
            });

            Specification result = visitor.Visit(equal);

            var r = Assert.IsType <EqualSpecification>(result);

            Assert.NotSame(equal, r);
            Assert.Contains("v1", r.Value.Values);
            Assert.DoesNotContain("{p1}", r.Value.Values);
            Assert.Equal(equal.Key, r.Key);
            Assert.Equal(SpecificationValue.Single("v1").ValueMultiplicity, r.Value.ValueMultiplicity);
            Assert.Equal(SpecificationValue.DataType.String, r.Value.ValueType);
        }
        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);
            }
        }
        public override Specification VisitEqual(EqualSpecification eq)
        {
            this.WriteKeyValueSpecification(eq, Consts.Eq);

            return(eq);
        }
		public void Test_Equal_CoerceFloat5()
		{
			EqualSpecification s = new EqualSpecification();
			s.RefValueExpression = new ConstantExpression(1.1);

			Assert.IsTrue(s.Test(1.1f).Success);
			Assert.IsTrue(s.Test(1.1d).Success);
			Assert.IsTrue(s.Test((decimal)1.1).Success);
			Assert.IsTrue(s.Test("1.1").Success);
			Assert.IsTrue(s.Test("1.10").Success);
			Assert.IsTrue(s.Test("1.100").Success);

			Assert.IsFalse(s.Test(1).Success);
			Assert.IsFalse(s.Test(1.0).Success);
			Assert.IsFalse(s.Test(null).Success);
			Assert.IsFalse(s.Test(0).Success);
			Assert.IsFalse(s.Test(0.0).Success);
			Assert.IsFalse(s.Test("0").Success);
		}
		public void Test_Equal_CoerceChar2()
		{
			EqualSpecification s = new EqualSpecification();
			s.RefValueExpression = new ConstantExpression("x");

			Assert.IsTrue(s.Test("x").Success);
			Assert.IsTrue(s.Test('x').Success);

			Assert.IsFalse(s.Test('\0').Success);
			Assert.IsFalse(s.Test('y').Success);
			Assert.IsFalse(s.Test("").Success);
			Assert.IsFalse(s.Test("xy").Success);
			Assert.IsFalse(s.Test("yx").Success);
		}
Пример #24
0
 public virtual bool VisitEqual(EqualSpecification eq)
 {
     return(false);
 }
Пример #25
0
		public void Test_Equal_Strict()
		{
			EqualSpecification s = new EqualSpecification();
			s.RefValueExpression = new ConstantExpression(1.0);
			s.Strict = true;

			// this should fail because in strict mode we don't do type coercion,
			// and Object.Equals(x, y) returns false when comparing different types
			Assert.IsFalse(s.Test(1).Success);
		}
Пример #26
0
 public virtual Specification VisitEqual(EqualSpecification eq)
 {
     return(eq);
 }