Skip to content

Commit 9168205

Browse files
committed
Support for translating more-than-one argument initialisation additions, e.g. for Dictionaries
1 parent d2a6a5a commit 9168205

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

ReadableExpressions.UnitTests/WhenTranslatingObjectCreations.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ public void ShouldTranslateANewListExpressionWithAdditions()
107107
Assert.AreEqual("new List<decimal> { 1m, 2.005m, 3m }", translated);
108108
}
109109

110+
[TestMethod]
111+
public void ShouldTranslateANewDictionaryExpressionWithAdditions()
112+
{
113+
Expression<Func<Dictionary<int, decimal>>> createList =
114+
() => new Dictionary<int, decimal> { { 1, 1.0m }, { 2, 2.0m } };
115+
116+
var translated = createList.Body.ToReadableString();
117+
118+
const string EXPECTED = @"
119+
new Dictionary<int, decimal>
120+
{
121+
{ 1, 1m },
122+
{ 2, 2m }
123+
}";
124+
125+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
126+
}
127+
110128
[TestMethod]
111129
public void ShouldTranslateANewArrayExpression()
112130
{

ReadableExpressions/Translators/InitialisationExpressionTranslator.Helpers.List.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ protected override IEnumerable<string> GetMemberInitialisations(
2828
return context.Translate(initialisation.Arguments.First());
2929
}
3030

31-
var listAddCall = _methodCallTranslator
32-
.GetMethodCall(initialisation.AddMethod, initialisation.Arguments, context);
31+
var additionArguments = string.Join(", ", initialisation.Arguments.Select(context.Translate));
3332

34-
return listAddCall;
33+
return "{ " + additionArguments + " }";
3534
});
3635
}
3736
}

0 commit comments

Comments
 (0)