Skip to content

Commit e3f65ad

Browse files
committed
Maintaining surrounding parenthesis when assigning an invocation result to a variable, re: issue #9
1 parent e9d8656 commit e3f65ad

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ReadableExpressions.UnitTests/WhenFormattingCode.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,23 @@ public void ShouldNotRemoveParenthesesFromMultiParameterLambdaArguments()
547547
Assert.AreEqual(EXPECTED, translated);
548548
}
549549

550+
// See https://github.com/agileobjects/ReadableExpressions/issues/9
551+
[TestMethod]
552+
public void ShouldNotRemoveParenthesesFromALambdaInvokeResultAssignment()
553+
{
554+
Expression<Func<int, int, int>> intsAdder = (a, b) => a + b;
555+
var one = Expression.Constant(1);
556+
var two = Expression.Constant(2);
557+
var lambdaInvocation = Expression.Invoke(intsAdder, one, two);
558+
var result = Expression.Variable(typeof(int), "result");
559+
var assignInvokeResult = Expression.Assign(result, lambdaInvocation);
560+
561+
const string EXPECTED = "result = ((a, b) => a + b).Invoke(1, 2)";
562+
var translated = assignInvokeResult.ToReadableString();
563+
564+
Assert.AreEqual(EXPECTED, translated);
565+
}
566+
550567
[TestMethod]
551568
public void ShouldUseMethodGroupsForStaticMethods()
552569
{

ReadableExpressions/StringExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ private static bool KeepSurroundingParentheses(Expression expression)
118118
}
119119

120120
return (expression.NodeType == ExpressionType.Convert);
121+
122+
case ExpressionType.Invoke:
123+
var invocation = (InvocationExpression)expression;
124+
125+
return invocation.Expression.NodeType == ExpressionType.Lambda;
121126
}
122127

123128
return false;

0 commit comments

Comments
 (0)