Ejemplo n.º 1
0
        public void VisitAndConvert_Single_ThrowsOnNull()
        {
            var expression = (BinaryExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Add);

            Expect.Call(VisitorMock.VisitExpression(expression)).Return(null);

            InvokeAndCheckVisitAndConvertExpression(expression, "VisitMethod");
        }
Ejemplo n.º 2
0
        public void VisitTypeBinary_Unchanged()
        {
            var expression = (TypeBinaryExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.TypeIs);

            Expect.Call(VisitorMock.VisitExpression(expression.Expression)).Return(expression.Expression);
            var result = (TypeBinaryExpression)InvokeAndCheckVisitExpression("VisitTypeBinaryExpression", expression);

            Assert.That(result, Is.SameAs(expression));
        }
Ejemplo n.º 3
0
        public void VisitMemberAssignment_Unchanged()
        {
            MemberAssignment memberAssignment = ExpressionInstanceCreator.CreateMemberAssignment();

            Expect.Call(VisitorMock.VisitExpression(memberAssignment.Expression)).Return(memberAssignment.Expression);
            var result = (MemberAssignment)InvokeAndCheckVisitObject("VisitMemberAssignment", memberAssignment);

            Assert.That(result, Is.SameAs(memberAssignment));
        }
Ejemplo n.º 4
0
        public void VisitUnaryExpression_Unchanges()
        {
            var        expression        = (UnaryExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.UnaryPlus);
            Expression expectedNextVisit = expression.Operand;

            Expect.Call(VisitorMock.VisitExpression(expectedNextVisit)).Return(expectedNextVisit);

            Assert.That(InvokeAndCheckVisitExpression("VisitUnaryExpression", expression), Is.SameAs(expression));
        }
Ejemplo n.º 5
0
        public void VisitInvocationExpression_Unchanged()
        {
            var expression = (InvocationExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Invoke);

            Expect.Call(VisitorMock.VisitExpression(expression.Expression)).Return(expression.Expression);
            Expect.Call(VisitorMock.VisitAndConvert(expression.Arguments, "VisitInvocationExpression")).Return(expression.Arguments);
            var result = (InvocationExpression)InvokeAndCheckVisitExpression("VisitInvocationExpression", expression);

            Assert.That(result, Is.SameAs(expression));
        }
Ejemplo n.º 6
0
        public void VisitListInitExpression_Unchanged()
        {
            var expression = (ListInitExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.ListInit);

            Expect.Call(VisitorMock.VisitExpression(expression.NewExpression)).Return(expression.NewExpression);
            Expect.Call(InvokeVisitMethod("VisitElementInitList", expression.Initializers)).Return(expression.Initializers);
            var result = (ListInitExpression)InvokeAndCheckVisitExpression("VisitListInitExpression", expression);

            Assert.That(result, Is.SameAs(expression));
        }
Ejemplo n.º 7
0
        public void VisitLambdaExpression_Unchanged()
        {
            var expression = (LambdaExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Lambda);

            Expect.Call(VisitorMock.VisitExpression(expression.Body)).Return(expression.Body);
            Expect.Call(VisitorMock.VisitAndConvert(expression.Parameters, "VisitLambdaExpression")).Return(expression.Parameters);
            var result = (LambdaExpression)InvokeAndCheckVisitExpression("VisitLambdaExpression", expression);

            Assert.That(result, Is.SameAs(expression));
        }
Ejemplo n.º 8
0
        public void VisitAndConvert_Single_NoConvert()
        {
            var expression = (BinaryExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Add);

            Expect.Call(VisitorMock.VisitExpression(expression)).Return(expression);

            var result = InvokeAndCheckVisitAndConvertExpression(expression, "Add");

            Assert.That(result, Is.SameAs(expression));
        }
Ejemplo n.º 9
0
        public void VisitMemberAssignment_Changed()
        {
            MemberAssignment memberAssignment    = ExpressionInstanceCreator.CreateMemberAssignment();
            MemberAssignment newMemberAssignment = Expression.Bind(typeof(SimpleClass).GetField("Value"), Expression.Constant("2"));

            Expect.Call(VisitorMock.VisitExpression(memberAssignment.Expression)).Return(newMemberAssignment.Expression);

            var result = (MemberAssignment)InvokeAndCheckVisitObject("VisitMemberAssignment", memberAssignment);

            Assert.That(result, Is.Not.SameAs(memberAssignment));
        }
Ejemplo n.º 10
0
        public void VisitConditionalExpression_Unchanged()
        {
            var expression = (ConditionalExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Conditional);

            Expect.Call(VisitorMock.VisitExpression(expression.Test)).Return(expression.Test);
            Expect.Call(VisitorMock.VisitExpression(expression.IfFalse)).Return(expression.IfFalse);
            Expect.Call(VisitorMock.VisitExpression(expression.IfTrue)).Return(expression.IfTrue);
            var result = (ConditionalExpression)InvokeAndCheckVisitExpression("VisitConditionalExpression", expression);

            Assert.That(result, Is.SameAs(expression));
        }
Ejemplo n.º 11
0
        public void VisitList_Unchanged()
        {
            Expression expr1 = Expression.Constant(1);
            ReadOnlyCollection <Expression> expressions = new List <Expression> (new[] { expr1 }).AsReadOnly();

            Expect.Call(VisitorMock.VisitExpression(expr1)).Return(expr1);

            var result = VisitorMock.VisitList(expressions, arg => InvokeAndCheckVisitAndConvertExpression(expr1, "VisitAndConvert"));

            Assert.That(result, Is.SameAs(expressions));
        }
Ejemplo n.º 12
0
        public void VisitAndConvert_Collection_ExceptionUsesCallerName()
        {
            var constantExpression = Expression.Constant(1);
            var expressions        = new List <ConstantExpression> (new[] { constantExpression }).AsReadOnly();

            var newExpression = (NewExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.New);

            Expect.Call(VisitorMock.VisitExpression(constantExpression)).Return(newExpression);
            VisitorMock.Replay();

            VisitorMock.VisitAndConvert(expressions, "Whatever");
        }
Ejemplo n.º 13
0
        public void VisitMemberExpression_ChangedExpression()
        {
            var        expression    = (MemberExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.MemberAccess);
            Expression newExpression = Expression.Constant(DateTime.Now);

            Expect.Call(VisitorMock.VisitExpression(expression.Expression)).Return(newExpression);
            var result = (MemberExpression)InvokeAndCheckVisitExpression("VisitMemberExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.MemberAccess));
            Assert.That(result.Expression, Is.SameAs(newExpression));
        }
Ejemplo n.º 14
0
        public void VisitBinaryExpression_Unchanged()
        {
            BinaryExpression expression = (BinaryExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Add);

            Expect.Call(VisitorMock.VisitExpression(expression.Left)).Return(expression.Left);
            Expect.Call(VisitorMock.VisitExpression(expression.Right)).Return(expression.Right);
            Expect.Call(VisitorMock.VisitExpression(expression.Conversion)).Return(expression.Conversion);

            BinaryExpression result = (BinaryExpression)InvokeAndCheckVisitExpression("VisitBinaryExpression", expression);

            Assert.That(result, Is.SameAs(expression));
        }
Ejemplo n.º 15
0
        public void VisitTypeBinary_Changed()
        {
            var        expression    = (TypeBinaryExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.TypeIs);
            Expression newExpression = Expression.Constant(1);

            Expect.Call(VisitorMock.VisitExpression(expression.Expression)).Return(newExpression);
            var result = (TypeBinaryExpression)InvokeAndCheckVisitExpression("VisitTypeBinaryExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.TypeIs));
            Assert.That(result.Expression, Is.SameAs(newExpression));
        }
Ejemplo n.º 16
0
        public void VisitAndConvert_Collection()
        {
            var expr1       = Expression.Constant(1);
            var expressions = new List <ConstantExpression> (new[] { expr1 }).AsReadOnly();

            Expect.Call(VisitorMock.VisitExpression(expr1)).Return(expr1);
            VisitorMock.Replay();

            var result = VisitorMock.VisitAndConvert(expressions, "Whatever");

            Assert.That(result, Is.SameAs(expressions));
            VisitorMock.VerifyAllExpectations();
        }
Ejemplo n.º 17
0
        public void VisitUnaryExpression_Negate_Changes()
        {
            var        expression        = (UnaryExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Negate);
            Expression expectedNextVisit = expression.Operand;
            Expression newOperand        = Expression.Constant(1);

            Expect.Call(VisitorMock.VisitExpression(expectedNextVisit)).Return(newOperand);

            var result = (UnaryExpression)InvokeAndCheckVisitExpression("VisitUnaryExpression", expression);

            Assert.That(result.Operand, Is.SameAs(newOperand));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Negate));
        }
Ejemplo n.º 18
0
        public void VisitLambdaExpression_ChangedBody()
        {
            var        expression = (LambdaExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Lambda);
            Expression newBody    = Expression.Constant(1);

            Expect.Call(VisitorMock.VisitExpression(expression.Body)).Return(newBody);
            Expect.Call(VisitorMock.VisitAndConvert(expression.Parameters, "VisitLambdaExpression")).Return(expression.Parameters);
            var result = (LambdaExpression)InvokeAndCheckVisitExpression("VisitLambdaExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Lambda));
            Assert.That(result.Body, Is.SameAs(newBody));
            Assert.That(result.Parameters, Is.SameAs(expression.Parameters));
        }
Ejemplo n.º 19
0
        public void VisitInvocationExpression_ChangedObject()
        {
            var        expression    = (InvocationExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Invoke);
            Expression newExpression = Expression.Lambda(Expression.Constant(1), Expression.Parameter(typeof(int), "i"));

            Expect.Call(VisitorMock.VisitExpression(expression.Expression)).Return(newExpression);
            Expect.Call(VisitorMock.VisitAndConvert(expression.Arguments, "VisitInvocationExpression")).Return(expression.Arguments);
            var result = (InvocationExpression)InvokeAndCheckVisitExpression("VisitInvocationExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Invoke));
            Assert.That(result.Expression, Is.SameAs(newExpression));
            Assert.That(result.Arguments, Is.SameAs(expression.Arguments));
        }
Ejemplo n.º 20
0
        public void VisitListInitExpression_ChangedNewExpression()
        {
            var           expression       = (ListInitExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.ListInit);
            NewExpression newNewExpression = Expression.New(typeof(List <int>));

            Expect.Call(VisitorMock.VisitExpression(expression.NewExpression)).Return(newNewExpression);
            Expect.Call(InvokeVisitMethod("VisitElementInitList", expression.Initializers)).Return(expression.Initializers);
            var result = (ListInitExpression)InvokeAndCheckVisitExpression("VisitListInitExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.ListInit));
            Assert.That(result.NewExpression, Is.SameAs(newNewExpression));
            Assert.That(result.Initializers, Is.SameAs(expression.Initializers));
        }
Ejemplo n.º 21
0
        public void VisitMethodCallExpression_ChangedObject()
        {
            var        expression = (MethodCallExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Call);
            Expression newObject  = Expression.Constant(1);

            Expect.Call(VisitorMock.VisitExpression(expression.Object)).Return(newObject);
            Expect.Call(VisitorMock.VisitAndConvert(expression.Arguments, "VisitMethodCallExpression")).Return(expression.Arguments);
            var result = (MethodCallExpression)InvokeAndCheckVisitExpression("VisitMethodCallExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Call));
            Assert.That(result.Object, Is.SameAs(newObject));
            Assert.That(result.Arguments, Is.SameAs(expression.Arguments));
        }
Ejemplo n.º 22
0
        public void VisitListInitExpression_InvalidNewExpression()
        {
            var expression = (ListInitExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.ListInit);

            Expect.Call(VisitorMock.VisitExpression(expression.NewExpression)).Return(Expression.Constant(0));
            Expect.Call(InvokeVisitMethod("VisitElementInitList", expression.Initializers)).Return(expression.Initializers);
            try
            {
                InvokeAndCheckVisitExpression("VisitListInitExpression", expression);
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
Ejemplo n.º 23
0
        public void VisitListInitExpression_ChangedInitializers()
        {
            var expression = (ListInitExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.ListInit);
            ReadOnlyCollection <ElementInit> newInitializers =
                new List <ElementInit> (new[] { Expression.ElementInit(typeof(List <int>).GetMethod("Add"), Expression.Constant(214578)) }).AsReadOnly();

            Expect.Call(VisitorMock.VisitExpression(expression.NewExpression)).Return(expression.NewExpression);
            Expect.Call(InvokeVisitMethod("VisitElementInitList", expression.Initializers)).Return(newInitializers);
            var result = (ListInitExpression)InvokeAndCheckVisitExpression("VisitListInitExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.ListInit));
            Assert.That(result.Initializers, Is.EqualTo(newInitializers));
            Assert.That(result.NewExpression, Is.SameAs(expression.NewExpression));
        }
Ejemplo n.º 24
0
        public void VisitConditionalExpression_ChangedTrue()
        {
            var        expression = (ConditionalExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Conditional);
            Expression newTrue    = Expression.Constant(1);

            Expect.Call(VisitorMock.VisitExpression(expression.Test)).Return(expression.Test);
            Expect.Call(VisitorMock.VisitExpression(expression.IfFalse)).Return(expression.IfFalse);
            Expect.Call(VisitorMock.VisitExpression(expression.IfTrue)).Return(newTrue);
            var result = (ConditionalExpression)InvokeAndCheckVisitExpression("VisitConditionalExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Conditional));
            Assert.That(result.IfTrue, Is.SameAs(newTrue));
            Assert.That(result.Test, Is.SameAs(expression.Test));
            Assert.That(result.IfFalse, Is.SameAs(expression.IfFalse));
        }
Ejemplo n.º 25
0
        public void VisitLambdaExpression_ChangedParameters()
        {
            var expression = (LambdaExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Lambda);
            ReadOnlyCollection <ParameterExpression> newParameters = new List <ParameterExpression> {
                Expression.Parameter(typeof(int), "i")
            }.AsReadOnly();

            Expect.Call(VisitorMock.VisitExpression(expression.Body)).Return(expression.Body);
            Expect.Call(VisitorMock.VisitAndConvert(expression.Parameters, "VisitLambdaExpression")).Return(newParameters);
            var result = (LambdaExpression)InvokeAndCheckVisitExpression("VisitLambdaExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Lambda));
            Assert.That(result.Parameters, Is.EqualTo(newParameters));
            Assert.That(result.Body, Is.SameAs(expression.Body));
        }
Ejemplo n.º 26
0
        public void VisitInvocationExpression_ChangedArguments()
        {
            var expression = (InvocationExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Invoke);
            ReadOnlyCollection <Expression> newParameters = new List <Expression> {
                Expression.Constant(214578)
            }.AsReadOnly();

            Expect.Call(VisitorMock.VisitExpression(expression.Expression)).Return(expression.Expression);
            Expect.Call(VisitorMock.VisitAndConvert(expression.Arguments, "VisitInvocationExpression")).Return(newParameters);
            var result = (InvocationExpression)InvokeAndCheckVisitExpression("VisitInvocationExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Invoke));
            Assert.That(result.Arguments, Is.EqualTo(newParameters));
            Assert.That(result.Expression, Is.SameAs(expression.Expression));
        }
Ejemplo n.º 27
0
        public void VisitBinaryExpression_RightChanged()
        {
            BinaryExpression expression = (BinaryExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.Subtract);
            Expression       newOperand = Expression.Constant(1);

            Expect.Call(VisitorMock.VisitExpression(expression.Left)).Return(expression.Left);
            Expect.Call(VisitorMock.VisitExpression(expression.Right)).Return(newOperand);
            Expect.Call(VisitorMock.VisitExpression(expression.Conversion)).Return(expression.Conversion);

            BinaryExpression result = (BinaryExpression)InvokeAndCheckVisitExpression("VisitBinaryExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.Subtract));
            Assert.That(result.Left, Is.SameAs(expression.Left));
            Assert.That(result.Right, Is.SameAs(newOperand));
        }
Ejemplo n.º 28
0
        public void VisitMemberInitExpression_ChangedBindings()
        {
            var expression       = (MemberInitExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.MemberInit);
            var capacityProperty = expression.NewExpression.Constructor.DeclaringType.GetProperty("Capacity");

            ReadOnlyCollection <MemberBinding> newBindings = new List <MemberBinding> {
                Expression.Bind(capacityProperty, Expression.Constant(214578))
            }.AsReadOnly();

            Expect.Call(VisitorMock.VisitExpression(expression.NewExpression)).Return(expression.NewExpression);
            Expect.Call(InvokeVisitMethod("VisitMemberBindingList", expression.Bindings)).Return(newBindings);
            var result = (MemberInitExpression)InvokeAndCheckVisitExpression("VisitMemberInitExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.NodeType, Is.EqualTo(ExpressionType.MemberInit));
            Assert.That(result.Bindings, Is.EqualTo(newBindings));
            Assert.That(result.NewExpression, Is.SameAs(expression.NewExpression));
        }
Ejemplo n.º 29
0
        public void VisitBinaryExpression_ConversionExpression_Unchanged()
        {
            ParameterExpression conversionParameter = Expression.Parameter(typeof(string), "s");
            LambdaExpression    conversion          = Expression.Lambda(conversionParameter, conversionParameter);
            BinaryExpression    expression          = Expression.MakeBinary(
                ExpressionType.Coalesce,
                Expression.Constant("0"),
                Expression.Constant("0"),
                false,
                null,
                conversion);

            Expect.Call(VisitorMock.VisitExpression(expression.Left)).Return(expression.Left);
            Expect.Call(VisitorMock.VisitExpression(expression.Right)).Return(expression.Right);
            Expect.Call(VisitorMock.VisitExpression(expression.Conversion)).Return(expression.Conversion);

            BinaryExpression result = (BinaryExpression)InvokeAndCheckVisitExpression("VisitBinaryExpression", expression);

            Assert.That(result, Is.SameAs(expression));
        }
Ejemplo n.º 30
0
        public void VisitBinaryExpression_RespectsMethod()
        {
            MethodInfo method = ((Func <int, int, bool>)((i1, i2) => i1 > i2)).Method;

            Expression left  = Expression.Constant(0, typeof(int?));
            Expression right = Expression.Constant(0, typeof(int?));

            BinaryExpression expression = Expression.MakeBinary(ExpressionType.GreaterThan, left, right, true, method);

            Expression newOperand = Expression.Constant(1, typeof(int?));

            Expect.Call(VisitorMock.VisitExpression(expression.Left)).Return(newOperand);
            Expect.Call(VisitorMock.VisitExpression(expression.Right)).Return(expression.Right);
            Expect.Call(VisitorMock.VisitExpression(expression.Conversion)).Return(expression.Conversion);

            BinaryExpression result = (BinaryExpression)InvokeAndCheckVisitExpression("VisitBinaryExpression", expression);

            Assert.That(result, Is.Not.SameAs(expression));
            Assert.That(result.Method, Is.SameAs(method));
        }