Skip to content

Commit f4cf94f

Browse files
committed
Support for using a member init expression as the return value of a multi-line block
1 parent dab5a50 commit f4cf94f

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

ReadableExpressions.UnitTests/WhenTranslatingBlocks.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Linq.Expressions;
88
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using NetStandardPolyfills;
910

1011
[TestClass]
1112
public class WhenTranslatingBlocks
@@ -360,6 +361,45 @@ public void ShouldTranslateASwitchWithMultipleVariableAssignments()
360361
Assert.AreEqual(EXPECTED.TrimStart(), translated);
361362
}
362363

364+
[TestMethod]
365+
public void ShouldTranslateAMemberInitReturnValue()
366+
{
367+
var company = Expression.Variable(typeof(Company), "c");
368+
var ceo = Expression.Variable(typeof(Employee), "ceo");
369+
var ceoAddress = Expression.Property(ceo, "Address");
370+
371+
var assignCeo = Expression.Assign(ceo, Expression.Property(company, "Ceo"));
372+
373+
var newAddress = Expression.MemberInit(
374+
Expression.New(typeof(Address).GetPublicInstanceConstructor()),
375+
Expression.Bind(
376+
typeof(Address).GetPublicInstanceMember("Line1"),
377+
Expression.Property(ceoAddress, "Line1")));
378+
379+
var newEmployee = Expression.MemberInit(
380+
Expression.New(typeof(Employee).GetPublicInstanceConstructor()),
381+
Expression.Bind(
382+
typeof(Employee).GetPublicInstanceMember("Address"),
383+
newAddress)
384+
);
385+
386+
var block = Expression.Block(assignCeo, newEmployee);
387+
388+
var translated = block.ToReadableString();
389+
390+
const string EXPECTED = @"
391+
var ceo = c.Ceo;
392+
393+
return new WhenTranslatingBlocks.Employee
394+
{
395+
Address = new WhenTranslatingBlocks.Address
396+
{
397+
Line1 = ceo.Address.Line1
398+
}
399+
};";
400+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
401+
}
402+
363403
[TestMethod]
364404
public void ShouldIgnoreABlankLabelTargetLine()
365405
{
@@ -375,10 +415,7 @@ public void ShouldIgnoreABlankLabelTargetLine()
375415

376416
var translated = intAssignmentBlock.ToReadableString();
377417

378-
const string EXPECTED = @"var i = 0;";
379-
380-
381-
Assert.AreEqual(EXPECTED, translated);
418+
Assert.AreEqual("var i = 0;", translated);
382419
}
383420

384421
[TestMethod]
@@ -543,6 +580,18 @@ public void ShouldIncludeAReturnKeywordForANewArrayInitStatement()
543580

544581
#region Helper Classes
545582

583+
private class Company
584+
{
585+
// ReSharper disable once UnusedMember.Local
586+
public Employee Ceo { get; set; }
587+
}
588+
589+
private class Employee
590+
{
591+
// ReSharper disable once UnusedMember.Local
592+
public Address Address { get; set; }
593+
}
594+
546595
private class Address
547596
{
548597
// ReSharper disable once UnusedMember.Local

ReadableExpressions/Translators/Formatting/CodeBlock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private class BlockInfo
206206
public BlockInfo(string[] blockLines)
207207
{
208208
_blockLines = blockLines;
209-
_lastNonIndentedStatement = blockLines.Last(line => line.IsNotIndented());
209+
_lastNonIndentedStatement = blockLines.Last(line => line.IsNonIndentedCodeLine());
210210

211211
_lastStatementLines = _lastNonIndentedStatement.SplitToLines();
212212
_lastNonIndentedLine = _lastStatementLines.Last(line => line.IsNonIndentedCodeLine());

0 commit comments

Comments
 (0)