Exemplo n.º 1
0
            public override bool Equals(object obj)
            {
                MemberExpressionFingerprint other = obj as MemberExpressionFingerprint;

                return((other != null) &&
                       Equals(this.Member, other.Member) &&
                       this.Equals(other));
            }
        public void Create_UnknownTarget()
        {
            // Arrange
            PropertyInfo     propInfo   = typeof(TimeSpan).GetProperty("Hours");
            MemberExpression expression = Expression.MakeMemberAccess(ExpressionHelper.GetUnknownExpression(typeof(TimeSpan)), propInfo);

            // Act
            MemberExpressionFingerprint fingerprint = MemberExpressionFingerprint.Create(expression, new ParserContext());

            // Assert
            Assert.IsNull(fingerprint, "Unknown operands cannot be parsed.");
        }
        public void Comparison_Inequality_Type() {
            // Arrange
            ExpressionType nodeType = ExpressionType.MemberAccess;
            Type type = typeof(int);
            MemberInfo member = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
            MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, typeof(object), member);

            // Assert
            Assert.AreNotEqual(fingerprint1, fingerprint2, "Fingerprints should not have been equal (differ by Type).");
        }
        public void Create_StaticMember()
        {
            // Arrange
            FieldInfo        fieldInfo  = typeof(TimeSpan).GetField("Zero");
            MemberExpression expression = Expression.MakeMemberAccess(null, fieldInfo);

            // Act
            MemberExpressionFingerprint fingerprint = MemberExpressionFingerprint.Create(expression, new ParserContext());

            // Assert
            Assert.IsNull(fingerprint.Target, "Static member should have null target fingerprint.");
            Assert.AreEqual(fieldInfo, expression.Member);
        }
Exemplo n.º 5
0
        public void Comparison_Inequality_Type()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.MemberAccess;
            Type           type     = typeof(int);
            MemberInfo     member   = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
            MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, typeof(object), member);

            // Assert
            Assert.NotEqual(fingerprint1, fingerprint2);
        }
        public void Comparison_Equality() {
            // Arrange
            ExpressionType nodeType = ExpressionType.MemberAccess;
            Type type = typeof(int);
            MemberInfo member = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
            MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, type, member);

            // Assert
            Assert.AreEqual(fingerprint1, fingerprint2, "Fingerprints should have been equal.");
            Assert.AreEqual(fingerprint1.GetHashCode(), fingerprint2.GetHashCode(), "Fingerprints should have been different.");
        }
        public void Properties() {
            // Arrange
            ExpressionType expectedNodeType = ExpressionType.MemberAccess;
            Type expectedType = typeof(int);
            MemberInfo expectedMember = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint = new MemberExpressionFingerprint(expectedNodeType, expectedType, expectedMember);

            // Assert
            Assert.AreEqual(expectedNodeType, fingerprint.NodeType);
            Assert.AreEqual(expectedType, fingerprint.Type);
            Assert.AreEqual(expectedMember, fingerprint.Member);
        }
        public void Comparison_Inequality_Member()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.MemberAccess;
            Type type = typeof(int);
            MemberInfo member = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
            MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, type, null /* member */);

            // Assert
            Assert.NotEqual(fingerprint1, fingerprint2);
        }
        public void Comparison_Inequality_Member()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.MemberAccess;
            Type           type     = typeof(int);
            MemberInfo     member   = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
            MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, type, null /* member */);

            // Assert
            Assert.AreNotEqual(fingerprint1, fingerprint2, "Fingerprints should not have been equal (differ by Member).");
        }
        public void Comparison_Inequality_FingerprintType()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.MemberAccess;
            Type           type     = typeof(int);
            MemberInfo     member   = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
            DummyExpressionFingerprint  fingerprint2 = new DummyExpressionFingerprint(nodeType, type);

            // Assert
            Assert.AreNotEqual(fingerprint1, fingerprint2, "Fingerprints should not have been equal ('other' is wrong type).");
        }
Exemplo n.º 11
0
        public void Properties()
        {
            // Arrange
            ExpressionType expectedNodeType = ExpressionType.MemberAccess;
            Type           expectedType     = typeof(int);
            MemberInfo     expectedMember   = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint = new MemberExpressionFingerprint(expectedNodeType, expectedType, expectedMember);

            // Assert
            Assert.Equal(expectedNodeType, fingerprint.NodeType);
            Assert.Equal(expectedType, fingerprint.Type);
            Assert.Equal(expectedMember, fingerprint.Member);
        }
        public void Comparison_Equality()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.MemberAccess;
            Type           type     = typeof(int);
            MemberInfo     member   = typeof(TimeSpan).GetProperty("Seconds");

            // Act
            MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
            MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, type, member);

            // Assert
            Assert.AreEqual(fingerprint1, fingerprint2, "Fingerprints should have been equal.");
            Assert.AreEqual(fingerprint1.GetHashCode(), fingerprint2.GetHashCode(), "Fingerprints should have been different.");
        }
        public void Create_InstanceMember()
        {
            // Arrange
            PropertyInfo  propInfo = typeof(TimeSpan).GetProperty("Hours");
            ParserContext context  = new ParserContext()
            {
                ModelParameter = Expression.Parameter(typeof(TimeSpan), "model")
            };
            MemberExpression expression = Expression.MakeMemberAccess(context.ModelParameter, propInfo);

            // Act
            MemberExpressionFingerprint fingerprint = MemberExpressionFingerprint.Create(expression, context);

            // Assert
            Assert.AreEqual(ExpressionType.Parameter, fingerprint.Target.NodeType);
            Assert.AreEqual(propInfo, expression.Member);
        }
        public void ToExpression()
        {
            // Arrange
            ParserContext context = new ParserContext()
            {
                ModelParameter = Expression.Parameter(typeof(TimeSpan), "model")
            };
            PropertyInfo propInfo = typeof(TimeSpan).GetProperty("Hours");

            MemberExpression            expression  = Expression.MakeMemberAccess(context.ModelParameter, propInfo);
            MemberExpressionFingerprint fingerprint = MemberExpressionFingerprint.Create(expression, context);

            // Act
            MemberExpression result = (MemberExpression)fingerprint.ToExpression(context);

            // Assert
            Assert.AreEqual(context.ModelParameter, result.Expression);
            Assert.AreEqual(propInfo, result.Member);
        }
        public void Comparison_DifferByMember()
        {
            // fingerprint1 is MEMBER_ACCESS(TimeSpan.Hours, PARAM:TimeSpan):int
            // fingerprint2 is MEMBER_ACCESS(TimeSpan.Minutes, PARAM:TimeSpan):int, so not equivalent

            // Arrange
            ParserContext context = new ParserContext()
            {
                ModelParameter = Expression.Parameter(typeof(TimeSpan), "model")
            };

            MemberExpressionFingerprint fingerprint1 = MemberExpressionFingerprint.Create(Expression.MakeMemberAccess(context.ModelParameter, typeof(TimeSpan).GetProperty("Hours")), context);
            MemberExpressionFingerprint fingerprint2 = MemberExpressionFingerprint.Create(Expression.MakeMemberAccess(context.ModelParameter, typeof(TimeSpan).GetProperty("Minutes")), context);

            // Act
            bool areEqual = Object.Equals(fingerprint1, fingerprint2);

            // Assert
            Assert.IsFalse(areEqual, "Fingerprints should not have been equivalent - differ by MemberExpressionFingerprint.Member.");
        }
        public void Comparison_EquivalentExpressions()
        {
            // both fingerprints are MEMBER_ACCESS(String.Length, PARAM:string):int, so are equivalent

            // Arrange
            ParserContext context = new ParserContext()
            {
                ModelParameter = Expression.Parameter(typeof(string), "model")
            };
            PropertyInfo propInfo = typeof(string).GetProperty("Length");

            MemberExpressionFingerprint fingerprint1 = MemberExpressionFingerprint.Create(Expression.MakeMemberAccess(context.ModelParameter, propInfo), context);
            MemberExpressionFingerprint fingerprint2 = MemberExpressionFingerprint.Create(Expression.MakeMemberAccess(context.ModelParameter, propInfo), context);

            // Act
            HashSet <ExpressionFingerprint> set = new HashSet <ExpressionFingerprint>()
            {
                fingerprint1,
                fingerprint2
            };

            // Assert
            Assert.AreEqual(1, set.Count, "Fingerprints should have been equivalent.");
        }