Skip to content

Commit 941ee9d

Browse files
committed
Support for empty array initialisations
1 parent 14476eb commit 941ee9d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

ReadableExpressions.UnitTests/WhenTranslatingObjectCreations.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ public void ShouldTranslateAnExplicitTypeNewArrayExpressionWithAdditions()
187187
Assert.AreEqual(EXPECTED.TrimStart(), translated);
188188
}
189189

190+
[TestMethod]
191+
public void ShouldTranslateAnEmptyNewArrayExpression()
192+
{
193+
var newArray = Expression.NewArrayInit(typeof(int), new List<Expression>(0));
194+
195+
var translated = newArray.ToReadableString();
196+
197+
Assert.AreEqual("new int[0]", translated);
198+
}
199+
190200
[TestMethod]
191201
public void ShouldTranslateAStringConstantConstructorParameter()
192202
{

ReadableExpressions/Translators/InitialisationExpressionTranslator.Helpers.Array.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ public ArrayInitExpressionHelper()
1616

1717
protected override string GetNewExpressionString(NewArrayExpression initialisation, TranslationContext context)
1818
{
19+
if (initialisation.Expressions.Count == 0)
20+
{
21+
return "new " + GetExplicitArrayType(initialisation) + "[0]";
22+
}
23+
1924
var explicitType = GetExplicitArrayTypeIfRequired(initialisation);
2025

2126
return "new" + explicitType + "[]";
2227
}
2328

29+
private static string GetExplicitArrayType(Expression initialisation)
30+
{
31+
return initialisation.Type.GetElementType().GetFriendlyName();
32+
}
33+
2434
private static string GetExplicitArrayTypeIfRequired(NewArrayExpression initialisation)
2535
{
2636
var expressionTypes = initialisation
@@ -34,7 +44,7 @@ private static string GetExplicitArrayTypeIfRequired(NewArrayExpression initiali
3444
return null;
3545
}
3646

37-
return " " + initialisation.Type.GetElementType().GetFriendlyName();
47+
return " " + GetExplicitArrayType(initialisation);
3848
}
3949

4050
protected override IEnumerable<string> GetMemberInitialisations(

0 commit comments

Comments
 (0)