public void VisitNewExpression_ChangedArguments_WithMembers_AndConversionRequired() { NewExpression expression = Expression.New( typeof(KeyValuePair <object, object>).GetConstructor(new[] { typeof(object), typeof(object) }), new Expression[] { Expression.Constant(null), Expression.Constant(null) }, typeof(KeyValuePair <object, object>).GetProperty("Key"), typeof(KeyValuePair <object, object>).GetProperty("Value")); var newArguments = new List <Expression> { Expression.Constant("testKey"), Expression.Constant("testValue") }.AsReadOnly(); Expect.Call(VisitorMock.VisitAndConvert(expression.Arguments, "VisitNewExpression")).Return(newArguments); var result = (NewExpression)InvokeAndCheckVisitExpression("VisitNewExpression", expression); Assert.That(result, Is.Not.SameAs(expression)); Assert.That(result.NodeType, Is.EqualTo(ExpressionType.New)); Assert.That(result.Arguments.Count, Is.EqualTo(2)); var expectedArgument1 = Expression.Convert(newArguments[0], typeof(object)); ExpressionTreeComparer.CheckAreEqualTrees(expectedArgument1, result.Arguments[0]); var expectedArgument2 = Expression.Convert(newArguments[1], typeof(object)); ExpressionTreeComparer.CheckAreEqualTrees(expectedArgument2, result.Arguments[1]); Assert.That(result.Members, Is.SameAs(expression.Members)); }
public void VisitNewArrayExpression_Unchanged() { var expression = (NewArrayExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.NewArrayInit); Expect.Call(VisitorMock.VisitAndConvert(expression.Expressions, "VisitNewArrayExpression")).Return(expression.Expressions); var result = (NewArrayExpression)InvokeAndCheckVisitExpression("VisitNewArrayExpression", expression); Assert.That(result, Is.SameAs(expression)); }
public void VisitElementInit_Unchanged() { ElementInit elementInit = ExpressionInstanceCreator.CreateElementInit(); Expect.Call(VisitorMock.VisitAndConvert(elementInit.Arguments, "VisitElementInit")).Return(elementInit.Arguments); var result = (ElementInit)InvokeAndCheckVisitObject("VisitElementInit", elementInit); Assert.That(result, Is.SameAs(elementInit)); }
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)); }
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)); }
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.Visit(constantExpression)).Return(newExpression); VisitorMock.Replay(); VisitorMock.VisitAndConvert(expressions, "Whatever"); }
public void VisitAndConvert_Collection() { var expr1 = Expression.Constant(1); var expressions = new List <ConstantExpression> (new[] { expr1 }).AsReadOnly(); Expect.Call(VisitorMock.Visit(expr1)).Return(expr1); VisitorMock.Replay(); var result = VisitorMock.VisitAndConvert(expressions, "Whatever"); Assert.That(result, Is.SameAs(expressions)); VisitorMock.VerifyAllExpectations(); }
public void VisitElementInit_Changed() { ElementInit elementInit = ExpressionInstanceCreator.CreateElementInit(); ReadOnlyCollection <Expression> newArguments = new List <Expression> (new Expression[] { Expression.Constant(214578) }).AsReadOnly(); Expect.Call(VisitorMock.VisitAndConvert(elementInit.Arguments, "VisitElementInit")).Return(newArguments); var result = (ElementInit)InvokeAndCheckVisitObject("VisitElementInit", elementInit); Assert.That(result, Is.Not.SameAs(elementInit)); Assert.That(result.AddMethod, Is.SameAs(elementInit.AddMethod)); Assert.That(result.Arguments, Is.EqualTo(newArguments)); }
public void VisitNewArrayExpression_Bounds_Changed() { var expression = (NewArrayExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.NewArrayBounds); ReadOnlyCollection <Expression> newExpressions = new List <Expression> (new Expression[] { Expression.Constant(214578) }).AsReadOnly(); Expect.Call(VisitorMock.VisitAndConvert(expression.Expressions, "VisitNewArrayExpression")).Return(newExpressions); var result = (NewArrayExpression)InvokeAndCheckVisitExpression("VisitNewArrayExpression", expression); Assert.That(result, Is.Not.SameAs(expression)); Assert.That(result.NodeType, Is.EqualTo(ExpressionType.NewArrayBounds)); Assert.That(result.Expressions, Is.EqualTo(newExpressions)); Assert.That(result.Type, Is.EqualTo(typeof(int).MakeArrayType())); }
public void VisitNewExpression_ChangedArguments() { var expression = (NewExpression)ExpressionInstanceCreator.GetExpressionInstance(ExpressionType.New); ReadOnlyCollection <Expression> newArguments = new List <Expression> { Expression.Constant(214578) }.AsReadOnly(); Expect.Call(VisitorMock.VisitAndConvert(expression.Arguments, "VisitNewExpression")).Return(newArguments); var result = (NewExpression)InvokeAndCheckVisitExpression("VisitNewExpression", expression); Assert.That(result, Is.Not.SameAs(expression)); Assert.That(result.NodeType, Is.EqualTo(ExpressionType.New)); Assert.That(result.Arguments, Is.EqualTo(newArguments)); }
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)); }
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)); }
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)); }
public void VisitNewExpression_ChangedArguments_NoMembers() { NewExpression expression = Expression.New(TypeForNewExpression.GetConstructor(typeof(int)), Expression.Constant(0)); var newArguments = new List <Expression> { Expression.Constant(214578) }.AsReadOnly(); Expect.Call(VisitorMock.VisitAndConvert(expression.Arguments, "VisitNewExpression")).Return(newArguments); var result = (NewExpression)InvokeAndCheckVisitExpression("VisitNewExpression", expression); Assert.That(result, Is.Not.SameAs(expression)); Assert.That(result.NodeType, Is.EqualTo(ExpressionType.New)); Assert.That(result.Arguments, Is.EqualTo(newArguments)); }
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)); }