Skip to content

Commit 4c65817

Browse files
committed
Fixing double-indenting of multi-line if statement tests
1 parent 75c7581 commit 4c65817

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

ReadableExpressions.UnitTests/WhenFormattingCode.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using System.IO;
66
using System.Linq;
77
using System.Linq.Expressions;
8-
using System.Reflection;
98
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using NetStandardPolyfills;
1010

1111
[TestClass]
1212
public class WhenFormattingCode
@@ -37,7 +37,7 @@ public void ShouldSplitLongConstructorArgumentListsOntoMultipleLines()
3737
public void ShouldSplitMultipleArgumentListsOntoMultipleLines()
3838
{
3939
var intsMethod = typeof(HelperClass)
40-
.GetMethod("GiveMeFourInts", BindingFlags.Public | BindingFlags.Instance);
40+
.GetPublicInstanceMethod("GiveMeFourInts");
4141

4242
var helperVariable = Expression.Variable(typeof(HelperClass), "helper");
4343
var intVariable = Expression.Variable(typeof(int), "intVariable");
@@ -66,7 +66,7 @@ public void ShouldSplitMultipleArgumentListsOntoMultipleLines()
6666
public void ShouldSplitLongArgumentListsOntoMultipleLines()
6767
{
6868
var intsMethod = typeof(HelperClass)
69-
.GetMethod("GiveMeSomeInts", BindingFlags.Public | BindingFlags.Instance);
69+
.GetPublicInstanceMethod("GiveMeSomeInts");
7070

7171
var helperVariable = Expression.Variable(typeof(HelperClass), "helper");
7272
var longVariable = Expression.Variable(typeof(int), "thisVariableReallyHasAVeryLongNameIndeed");
@@ -480,6 +480,34 @@ public void ShouldNotIndentParamsArrayArguments()
480480
Assert.AreEqual(EXPECTED.TrimStart(), translated);
481481
}
482482

483+
[TestMethod]
484+
public void ShouldIndentParamsArrayArgumentsInAnIfTest()
485+
{
486+
Expression<Func<bool>> stringTest = () =>
487+
string.Join(",", "[", "i", "]", "[", "j", "]", "[", "k", "]") != string.Empty;
488+
489+
var doNothing = Expression.Default(typeof(void));
490+
var ifTestDoNothing = Expression.IfThen(stringTest.Body, doNothing);
491+
492+
const string EXPECTED = @"
493+
if (string.Join(
494+
"","",
495+
""["",
496+
""i"",
497+
""]"",
498+
""["",
499+
""j"",
500+
""]"",
501+
""["",
502+
""k"",
503+
""]"") != string.Empty)
504+
{
505+
}";
506+
var translated = ifTestDoNothing.ToReadableString();
507+
508+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
509+
}
510+
483511
[TestMethod]
484512
public void ShouldTranslateAnExtensionExpressionType()
485513
{

ReadableExpressions.UnitTests/WhenTranslatingConditionals.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Linq.Expressions;
55
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using NetStandardPolyfills;
67

78
[TestClass]
89
public class WhenTranslatingConditionals
@@ -276,7 +277,7 @@ public void ShouldNotWrapSingleExpressionTernaryConditionsInParentheses()
276277
[TestMethod]
277278
public void ShouldNotWrapMethodCallTernaryConditionsInParentheses()
278279
{
279-
var method = typeof(MethodCallHelper).GetMethod("MultipleParameterMethod");
280+
var method = typeof(MethodCallHelper).GetPublicInstanceMethod("MultipleParameterMethod");
280281

281282
var methodCall = Expression.Call(
282283
Expression.Variable(typeof(MethodCallHelper), "helper"),

ReadableExpressions/Translators/ConditionalExpressionTranslator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public override string Translate(Expression expression, TranslationContext conte
4242
}
4343

4444
private static string GetTest(ConditionalExpression conditional, TranslationContext context)
45-
{
46-
return new FormattedCondition(conditional.Test, context);
47-
}
45+
=> new FormattedCondition(conditional.Test, context);
4846

4947
private static bool HasNoElseCondition(ConditionalExpression conditional)
5048
{

ReadableExpressions/Translators/Formatting/FormattedCondition.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ public FormattedCondition(
2020

2121
if (test.IsMultiLine())
2222
{
23-
test = test.Indented().TrimStart();
23+
if (test.StartsWith(Environment.NewLine + '{'))
24+
{
25+
test = test.Indented();
26+
}
27+
28+
test = test.TrimStart();
2429
}
2530

2631
_singleLineTest = test.WithSurroundingParentheses(CheckExistingParentheses());

0 commit comments

Comments
 (0)