Skip to content

Commit 44adfa4

Browse files
committed
Test coverage for checked multi-line assignments
1 parent cc37cfb commit 44adfa4

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

ReadableExpressions.UnitTests/WhenTranslatingAssignments.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,42 @@ public void ShouldTranslateASubtractionAssignment()
5454
}
5555

5656
[TestMethod]
57-
public void ShouldTranslateACheckedSubtractionAssignment()
57+
public void ShouldTranslateAMultiLineCheckedSubtractionAssignment()
5858
{
5959
var intVariable = Expression.Variable(typeof(int), "i");
60-
var substractOneAndAssign = Expression.SubtractAssignChecked(intVariable, Expression.Constant(1));
60+
61+
Expression<Action> consoleRead = () => Console.Read();
62+
63+
var variableOne = Expression.Variable(typeof(int), "one");
64+
var variableTwo = Expression.Variable(typeof(int), "two");
65+
66+
var variableOneAssignment = Expression.Assign(variableOne, consoleRead.Body);
67+
var variableTwoAssignment = Expression.Assign(variableTwo, consoleRead.Body);
68+
69+
var variableOnePlusTwo = Expression.Add(variableOne, variableTwo);
70+
71+
var valueBlock = Expression.Block(
72+
new[] { variableOne, variableTwo },
73+
variableOneAssignment,
74+
variableTwoAssignment,
75+
variableOnePlusTwo);
76+
77+
var substractOneAndAssign = Expression.SubtractAssignChecked(intVariable, valueBlock);
6178

6279
var translated = substractOneAndAssign.ToReadableString();
6380

64-
Assert.AreEqual("checked { i -= 1 }", translated);
81+
const string EXPECTED = @"
82+
checked
83+
{
84+
i -=
85+
{
86+
var one = Console.Read();
87+
var two = Console.Read();
88+
return (one + two);
89+
}
90+
}";
91+
92+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
6593
}
6694

6795
[TestMethod]

ReadableExpressions/Extensions/InternalExpressionExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@ public static bool IsReturnable(this Expression expression)
2828
case ExpressionType.Constant:
2929
return !expression.IsComment();
3030

31+
case ExpressionType.Add:
32+
case ExpressionType.AddChecked:
3133
case ExpressionType.Call:
3234
case ExpressionType.Conditional:
3335
case ExpressionType.Convert:
3436
case ExpressionType.ConvertChecked:
3537
case ExpressionType.Default:
38+
case ExpressionType.Divide:
3639
case ExpressionType.Invoke:
3740
case ExpressionType.MemberAccess:
41+
case ExpressionType.Multiply:
42+
case ExpressionType.MultiplyChecked:
3843
case ExpressionType.Parameter:
44+
case ExpressionType.Subtract:
45+
case ExpressionType.SubtractChecked:
3946
return true;
4047
}
4148

0 commit comments

Comments
 (0)