Пример #1
0
        public void Comparison_Inequality_Method()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.Add;
            Type           type     = typeof(DateTime);
            MethodInfo     method   = typeof(DateTime).GetMethod(
                "op_Addition",
                new Type[] { typeof(DateTime), typeof(TimeSpan) }
                );

            // Act
            BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(
                nodeType,
                type,
                method
                );
            BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(
                nodeType,
                type,
                null /* method */
                );

            // Assert
            Assert.NotEqual(fingerprint1, fingerprint2);
        }
Пример #2
0
        public void Comparison_Equality()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.Add;
            Type           type     = typeof(DateTime);
            MethodInfo     method   = typeof(DateTime).GetMethod(
                "op_Addition",
                new Type[] { typeof(DateTime), typeof(TimeSpan) }
                );

            // Act
            BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(
                nodeType,
                type,
                method
                );
            BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(
                nodeType,
                type,
                method
                );

            // Assert
            Assert.Equal(fingerprint1, fingerprint2);
            Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
        }
Пример #3
0
            public override bool Equals(object obj)
            {
                BinaryExpressionFingerprint other = obj as BinaryExpressionFingerprint;

                return((other != null) &&
                       Equals(this.Method, other.Method) &&
                       this.Equals(other));
            }
Пример #4
0
        public void Create_UnknownRight()
        {
            // Arrange
            BinaryExpression expression = Expression.Coalesce(Expression.Constant(5, typeof(int?)), ExpressionHelper.GetUnknownExpression(typeof(int?)));

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

            // Assert
            Assert.IsNull(fingerprint, "Unknown right operands cannot be parsed.");
        }
Пример #5
0
        public void Create_ConversionLambdasAreRejected()
        {
            // Arrange
            Expression <Func <int?, int?> > identityFunc = i => i;
            BinaryExpression expression = Expression.Coalesce(Expression.Constant(5, typeof(int?)), Expression.Constant(6, typeof(int?)), identityFunc);

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

            // Assert
            Assert.IsNull(fingerprint, "Any BinaryExpression with a non-null Conversion property should be rejected.");
        }
        public void Comparison_Inequality_Type() {
            // Arrange
            ExpressionType nodeType = ExpressionType.Add;
            Type type = typeof(DateTime);
            MethodInfo method = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });

            // Act
            BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(nodeType, type, method);
            BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(nodeType, typeof(object), method);

            // Assert
            Assert.AreNotEqual(fingerprint1, fingerprint2, "Fingerprints should not have been equal (differ by Type).");
        }
        public void Comparison_Inequality_Method()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.Add;
            Type type = typeof(DateTime);
            MethodInfo method = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });

            // Act
            BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(nodeType, type, method);
            BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(nodeType, type, null /* method */);

            // Assert
            Assert.NotEqual(fingerprint1, fingerprint2);
        }
        public void Comparison_Equality() {
            // Arrange
            ExpressionType nodeType = ExpressionType.Add;
            Type type = typeof(DateTime);
            MethodInfo method = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });

            // Act
            BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(nodeType, type, method);
            BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(nodeType, type, method);

            // 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.Add;
            Type expectedType = typeof(DateTime);
            MethodInfo expectedMethod = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });

            // Act
            BinaryExpressionFingerprint fingerprint = new BinaryExpressionFingerprint(expectedNodeType, expectedType, expectedMethod);

            // Assert
            Assert.AreEqual(expectedNodeType, fingerprint.NodeType);
            Assert.AreEqual(expectedType, fingerprint.Type);
            Assert.AreEqual(expectedMethod, fingerprint.Method);
        }
        public void Comparison_Inequality_Type()
        {
            // Arrange
            ExpressionType nodeType = ExpressionType.Add;
            Type           type     = typeof(DateTime);
            MethodInfo     method   = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });

            // Act
            BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(nodeType, type, method);
            BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(nodeType, typeof(object), method);

            // Assert
            Assert.AreNotEqual(fingerprint1, fingerprint2, "Fingerprints should not have been equal (differ by Type).");
        }
Пример #11
0
        public void Properties()
        {
            // Arrange
            ExpressionType expectedNodeType = ExpressionType.Add;
            Type           expectedType     = typeof(DateTime);
            MethodInfo     expectedMethod   = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });

            // Act
            BinaryExpressionFingerprint fingerprint = new BinaryExpressionFingerprint(expectedNodeType, expectedType, expectedMethod);

            // Assert
            Assert.Equal(expectedNodeType, fingerprint.NodeType);
            Assert.Equal(expectedType, fingerprint.Type);
            Assert.Equal(expectedMethod, fingerprint.Method);
        }
Пример #12
0
        public void Create()
        {
            // Arrange
            ParserContext context = new ParserContext()
            {
                ModelParameter = Expression.Parameter(typeof(object[]), "model")
            };
            BinaryExpression expression = Expression.ArrayIndex(context.ModelParameter, Expression.Constant(5));

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

            // Assert
            Assert.AreEqual(ExpressionType.ArrayIndex, fingerprint.NodeType);
            Assert.AreEqual(ExpressionType.Parameter, fingerprint.Left.NodeType);
            Assert.AreEqual(ExpressionType.Constant, fingerprint.Right.NodeType);
        }
Пример #13
0
        public void Comparison_EquivalentExpressions()
        {
            // both fingerprints are ARRAY_INDEX(CONST:object[], CONST:int):object, so are equivalent

            // Arrange
            ExpressionFingerprint fingerprint1 = BinaryExpressionFingerprint.Create(Expression.ArrayIndex(Expression.Constant(new object[0]), Expression.Constant(0)), new ParserContext());
            ExpressionFingerprint fingerprint2 = BinaryExpressionFingerprint.Create(Expression.ArrayIndex(Expression.Constant(new object[0]), Expression.Constant(0)), new ParserContext());

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

            // Assert
            Assert.AreEqual(1, set.Count, "Fingerprints should have been equivalent.");
        }
Пример #14
0
        public void Comparison_DifferByRight()
        {
            // fingerprint1 is OP_ADD(CONST:int, PARAM:int):int
            // fingerprint2 is OP_ADD(CONST:int, CONST:int):int, so not equivalent

            // Arrange
            ParserContext context = new ParserContext()
            {
                ModelParameter = Expression.Parameter(typeof(int), "model")
            };
            ExpressionFingerprint fingerprint1 = BinaryExpressionFingerprint.Create(Expression.Add(Expression.Constant(0), context.ModelParameter), context);
            ExpressionFingerprint fingerprint2 = BinaryExpressionFingerprint.Create(Expression.Add(Expression.Constant(0), Expression.Constant(0)), context);

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

            // Assert
            Assert.IsFalse(areEqual, "Fingerprints should not have been equivalent - differ by BinaryExpressionFingerprint.Right.");
        }
Пример #15
0
        public void Comparison_DifferByMethod()
        {
            // fingerprint1 is OP_ADD(TimeSpan.op_Addition, CONST:TimeSpan, CONST:TimeSpan):TimeSpan
            // fingerprint2 is OP_ADD(TimeSpan.op_Subtraction, CONST:TimeSpan, CONST:TimeSpan):TimeSpan, so not equivalent
            // might be strange using op_Subtraction() for ADD, but at least it makes the Methods different

            // Arrange
            MethodInfo addMethod      = typeof(TimeSpan).GetMethod("op_Addition", BindingFlags.Static | BindingFlags.Public);
            MethodInfo subtractMethod = typeof(TimeSpan).GetMethod("op_Subtraction", BindingFlags.Static | BindingFlags.Public);

            ExpressionFingerprint fingerprint1 = BinaryExpressionFingerprint.Create(Expression.Add(Expression.Constant(TimeSpan.Zero), Expression.Constant(TimeSpan.Zero), addMethod), new ParserContext());
            ExpressionFingerprint fingerprint2 = BinaryExpressionFingerprint.Create(Expression.Add(Expression.Constant(TimeSpan.Zero), Expression.Constant(TimeSpan.Zero), subtractMethod), new ParserContext());

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

            // Assert
            Assert.IsFalse(areEqual, "Fingerprints should not have been equivalent - differ by BinaryExpressionFingerprint.Method.");
        }