Skip to content

Commit 14476eb

Browse files
committed
Including return statements for blocks which return a list init
1 parent 98c0f5b commit 14476eb

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

ReadableExpressions.UnitTests/WhenTranslatingBlocks.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,35 @@ public void ShouldIncludeAReturnKeywordForANewObjectStatement()
389389
Assert.AreEqual(EXPECTED.TrimStart(), translated);
390390
}
391391

392+
[TestMethod]
393+
public void ShouldIncludeAReturnKeywordForANewListInitStatement()
394+
{
395+
var exception = Expression.Variable(typeof(Exception), "ex");
396+
var listConstructor = typeof(List<int>).GetConstructor(new[] { typeof(int) });
397+
var one = Expression.Constant(1);
398+
// ReSharper disable once AssignNullToNotNullAttribute
399+
var newList = Expression.New(listConstructor, one);
400+
var newListInit = Expression.ListInit(newList, one);
401+
var rethrow = Expression.Rethrow(newListInit.Type);
402+
var globalCatchAndRethrow = Expression.Catch(exception, rethrow);
403+
var tryCatch = Expression.TryCatch(newListInit, globalCatchAndRethrow);
404+
405+
var tryCatchBlock = Expression.Block(tryCatch);
406+
407+
var translated = tryCatchBlock.ToReadableString();
408+
409+
const string EXPECTED = @"
410+
try
411+
{
412+
return new List<int>(1) { 1 };
413+
}
414+
catch
415+
{
416+
throw;
417+
}";
418+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
419+
}
420+
392421
[TestMethod]
393422
public void ShouldIncludeAReturnKeywordForANewArrayStatement()
394423
{

ReadableExpressions/Extensions/InternalExpressionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static bool IsReturnable(this Expression expression)
3232
case ExpressionType.Default:
3333
case ExpressionType.Divide:
3434
case ExpressionType.Invoke:
35+
case ExpressionType.ListInit:
3536
case ExpressionType.MemberAccess:
3637
case ExpressionType.Multiply:
3738
case ExpressionType.MultiplyChecked:

0 commit comments

Comments
 (0)