Skip to content

Commit e836f82

Browse files
committed
Including return keywords in code blocks which end with new, new array or array init statements / Correctly terminating init statement lines
1 parent 0ab5236 commit e836f82

File tree

3 files changed

+109
-10
lines changed

3 files changed

+109
-10
lines changed

ReadableExpressions.UnitTests/WhenTranslatingBlocks.cs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public void ShouldIgnoreABlankLabelTargetLine()
343343
}
344344

345345
[TestMethod]
346-
public void ShouldIncludeAReturnStatementForACoalesce()
346+
public void ShouldIncludeAReturnKeywordForACoalesce()
347347
{
348348
var stringVariable1 = Expression.Variable(typeof(string), "myString");
349349
var stringVariable2 = Expression.Variable(typeof(string), "yourString");
@@ -363,5 +363,82 @@ public void ShouldIncludeAReturnStatementForACoalesce()
363363

364364
Assert.AreEqual(EXPECTED.TrimStart(), translated);
365365
}
366+
367+
[TestMethod]
368+
public void ShouldIncludeAReturnKeywordForANewObjectStatement()
369+
{
370+
var exception = Expression.Variable(typeof(Exception), "ex");
371+
var newList = Expression.New(typeof(List<string>).GetConstructors().First());
372+
var rethrow = Expression.Rethrow(newList.Type);
373+
var globalCatchAndRethrow = Expression.Catch(exception, rethrow);
374+
var tryCatch = Expression.TryCatch(newList, globalCatchAndRethrow);
375+
376+
var tryCatchBlock = Expression.Block(tryCatch);
377+
378+
var translated = tryCatchBlock.ToReadableString();
379+
380+
const string EXPECTED = @"
381+
try
382+
{
383+
return new List<string>();
384+
}
385+
catch
386+
{
387+
throw;
388+
}";
389+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
390+
}
391+
392+
[TestMethod]
393+
public void ShouldIncludeAReturnKeywordForANewArrayStatement()
394+
{
395+
var exception = Expression.Variable(typeof(Exception), "ex");
396+
var zero = Expression.Constant(0, typeof(int));
397+
var newArray = Expression.NewArrayBounds(typeof(int), zero);
398+
var rethrow = Expression.Rethrow(newArray.Type);
399+
var globalCatchAndRethrow = Expression.Catch(exception, rethrow);
400+
var tryCatch = Expression.TryCatch(newArray, globalCatchAndRethrow);
401+
402+
var tryCatchBlock = Expression.Block(tryCatch);
403+
404+
var translated = tryCatchBlock.ToReadableString();
405+
406+
const string EXPECTED = @"
407+
try
408+
{
409+
return new int[0];
410+
}
411+
catch
412+
{
413+
throw;
414+
}";
415+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
416+
}
417+
418+
[TestMethod]
419+
public void ShouldIncludeAReturnKeywordForANewArrayInitStatement()
420+
{
421+
var exception = Expression.Variable(typeof(Exception), "ex");
422+
var zero = Expression.Constant(0, typeof(int));
423+
var newArray = Expression.NewArrayInit(typeof(int), zero);
424+
var rethrow = Expression.Rethrow(newArray.Type);
425+
var globalCatchAndRethrow = Expression.Catch(exception, rethrow);
426+
var tryCatch = Expression.TryCatch(newArray, globalCatchAndRethrow);
427+
428+
var tryCatchBlock = Expression.Block(tryCatch);
429+
430+
var translated = tryCatchBlock.ToReadableString();
431+
432+
const string EXPECTED = @"
433+
try
434+
{
435+
return new[] { 0 };
436+
}
437+
catch
438+
{
439+
throw;
440+
}";
441+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
442+
}
366443
}
367444
}

ReadableExpressions/Extensions/InternalExpressionExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public static bool IsReturnable(this Expression expression)
3535
case ExpressionType.MemberAccess:
3636
case ExpressionType.Multiply:
3737
case ExpressionType.MultiplyChecked:
38+
case ExpressionType.New:
39+
case ExpressionType.NewArrayBounds:
40+
case ExpressionType.NewArrayInit:
3841
case ExpressionType.Parameter:
3942
case ExpressionType.Subtract:
4043
case ExpressionType.SubtractChecked:

ReadableExpressions/StringExtensions.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,30 @@ namespace AgileObjects.ReadableExpressions
88

99
internal static class StringExtensions
1010
{
11-
private static readonly char[] _terminatingCharacters = { ';', '}', ':', ',' };
11+
private static readonly char[] _terminatingCharacters = { ';', ':', ',' };
1212

1313
public static bool IsTerminated(this string codeLine)
1414
{
15-
return codeLine.EndsWithAny(_terminatingCharacters) || codeLine.IsComment();
15+
if (!codeLine.EndsWith('}'))
16+
{
17+
return codeLine.EndsWithAny(_terminatingCharacters) || codeLine.IsComment();
18+
}
19+
20+
var lastNewLine = codeLine.LastIndexOf(Environment.NewLine, StringComparison.Ordinal);
21+
22+
var index = (lastNewLine != -1)
23+
? lastNewLine + Environment.NewLine.Length
24+
: 0;
25+
26+
for (var end = codeLine.Length - 1; index < end; ++index)
27+
{
28+
if (codeLine[index] != ' ')
29+
{
30+
return false;
31+
}
32+
}
33+
34+
return true;
1635
}
1736

1837
public static string Unterminated(this string codeLine)
@@ -149,17 +168,17 @@ public static bool StartsWith(this string value, char character)
149168

150169
public static bool EndsWith(this string value, char character)
151170
{
152-
if (value == string.Empty)
153-
{
154-
return false;
155-
}
156-
157-
return value[value.Length - 1] == character;
171+
return (value != string.Empty) && value.EndsWithNoEmptyCheck(character);
158172
}
159173

160174
private static bool EndsWithAny(this string value, IEnumerable<char> characters)
161175
{
162-
return characters.Any(value.EndsWith);
176+
return (value != string.Empty) && characters.Any(value.EndsWithNoEmptyCheck);
177+
}
178+
179+
private static bool EndsWithNoEmptyCheck(this string value, char character)
180+
{
181+
return value[value.Length - 1] == character;
163182
}
164183

165184
private const string CommentString = "// ";

0 commit comments

Comments
 (0)