Skip to content

Commit

Permalink
Merge pull request #538 from manfred-brands/issue/534
Browse files Browse the repository at this point in the history
Keep leading and trailing trivia when replacing statements with Assert.Multiple
  • Loading branch information
manfred-brands authored May 2, 2023
2 parents 6c49665 + 136fb27 commit 095b93d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,62 @@ private sealed class Configuration
}");
RoslynAssert.FixAll(analyzer, fix, expectedDiagnostic, code, fixedCode);
}

[Test]
public void VerifyKeepsTrivia(
[Values("", "\r\n")] string newline,
[Values("", "// ")] string preComment,
[Values("", "// Same line Comment", "\r\n // Final Comment on next line")] string postComment)
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
public void TestMethod()
{{
const bool True = true;
const bool False = false;
// Verify that our bool constants are correct
↓Assert.That(True, Is.True);
Assert.That(False, Is.False);{newline}
{preComment}Console.WriteLine(""Next Statement"");{postComment}
}}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
public void TestMethod()
{{
const bool True = true;
const bool False = false;
Assert.Multiple(() =>
{{
// Verify that our bool constants are correct
Assert.That(True, Is.True);
Assert.That(False, Is.False);
}});{newline}
{preComment}Console.WriteLine(""Next Statement"");{postComment}
}}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode);
}

[Test]
public void VerifyKeepsTrivia()
{
var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
public void TestMethod()
{{
// Verify that boolean work as expected
↓Assert.That(true, Is.True);
Assert.That(false, Is.False);
}}");
var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
public void TestMethod()
{{
Assert.Multiple(() =>
{{
// Verify that boolean work as expected
Assert.That(true, Is.True);
Assert.That(false, Is.False);
}});
}}");
RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode);
}
}
}
32 changes: 30 additions & 2 deletions src/nunit.analyzers/UseAssertMultiple/UseAssertMultipleCodeFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
statements[when].Add(statement);
}

// If there was an empty line between the code above the Assert, then keep it.
SyntaxTrivia? endOfLineTrivia = default;

var firstStatement = statementsInsideAssertMultiple[0];
if (firstStatement.HasLeadingTrivia)
{
SyntaxTriviaList trivia = firstStatement.GetLeadingTrivia();
SyntaxTrivia firstTrivia = trivia.First();
if (firstTrivia.IsKind(SyntaxKind.EndOfLineTrivia))
{
// Remember the trivia and delete it from the first statement inside the Assert.Multiple
endOfLineTrivia = firstTrivia;
statementsInsideAssertMultiple[0] = firstStatement.ReplaceTrivia(firstTrivia, Enumerable.Empty<SyntaxTrivia>());
}
}

ParenthesizedLambdaExpressionSyntax parenthesizedLambdaExpression =
SyntaxFactory.ParenthesizedLambdaExpression(
SyntaxFactory.Block(statementsInsideAssertMultiple));
Expand All @@ -111,10 +127,22 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
SyntaxFactory.SeparatedList(new[]
{
SyntaxFactory.Argument(parenthesizedLambdaExpression)
})))).WithAdditionalAnnotations(Formatter.Annotation);
}))))
.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed)
.WithAdditionalAnnotations(Formatter.Annotation);

if (endOfLineTrivia is not null)
{
// Add the remembered blank line to go before the Assert.Multiple statement.
assertMultiple = assertMultiple.WithLeadingTrivia(endOfLineTrivia.Value);
}

// Comments at the end of a block are not associated with the last statement but with the closing brace
// Keep the exising block's open and close braces with associated trivia in our updated block.
var updatedBlock = SyntaxFactory.Block(
statementsBeforeAssertMultiple.Append(assertMultiple).Concat(statementsAfterAssertMultiple));
block.OpenBraceToken,
SyntaxFactory.List(statementsBeforeAssertMultiple.Append(assertMultiple).Concat(statementsAfterAssertMultiple)),
block.CloseBraceToken);

SyntaxNode newRoot = root.ReplaceNode(block, updatedBlock);

Expand Down

0 comments on commit 095b93d

Please sign in to comment.