Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not replace line breaks in code fixes #160

Merged
merged 2 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,21 @@ public void TestMethod()
}");
AnalyzerAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: CodeFixConstants.TransformToConstraintModelDescription);
}

[Test]
public void CodeFixPreservesLineBreakBeforeMessage()
{
var code = TestUtility.WrapInTestMethod($@"
Assert.AreEqual(2d, 3d, 0.0000001d,
""message"",
Guid.NewGuid());");

var fixedCode = TestUtility.WrapInTestMethod(@"
Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d),
""message"",
Guid.NewGuid());");

AnalyzerAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: CodeFixConstants.TransformToConstraintModelDescription);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,23 @@ public void FixesEqualsMethodWithAssertFalseWithMessage()

AnalyzerAssert.CodeFix(analyzer, fix, equalConstraintDiagnostic, code, fixedCode);
}

[Test]
public void CodeFixPreservesLineBreakBeforeMessage()
{
var code = TestUtility.WrapInTestMethod(@"
var actual = ""abc"";

Assert.False(actual.Equals(""bcd""),
""Assertion message from new line"");");

var fixedCode = TestUtility.WrapInTestMethod(@"
var actual = ""abc"";

Assert.That(actual, Is.Not.EqualTo(""bcd""),
""Assertion message from new line"");");

AnalyzerAssert.CodeFix(analyzer, fix, equalConstraintDiagnostic, code, fixedCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NUnit.Analyzers.Constants;
using NUnit.Analyzers.Extensions;

namespace NUnit.Analyzers.ClassicModelAssertUsage
{
Expand Down Expand Up @@ -41,26 +42,8 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var arguments = invocationNode.ArgumentList.Arguments.ToList();
this.UpdateArguments(diagnostic, arguments);

var totalArgumentCount = (2 * arguments.Count) - 1;
var newArgumentList = new SyntaxNodeOrToken[totalArgumentCount];

context.CancellationToken.ThrowIfCancellationRequested();

for (var x = 0; x < totalArgumentCount; x++)
{
if (x % 2 == 0)
{
newArgumentList[x] = arguments[x / 2];
}
else
{
newArgumentList[x] = SyntaxFactory.Token(SyntaxKind.CommaToken);
}
}

newInvocationNode = newInvocationNode.WithArgumentList(
SyntaxFactory.ArgumentList(
SyntaxFactory.SeparatedList<ArgumentSyntax>(newArgumentList)));
var newArgumentsList = invocationNode.ArgumentList.WithArguments(arguments);
newInvocationNode = newInvocationNode.WithArgumentList(newArgumentsList);

context.CancellationToken.ThrowIfCancellationRequested();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected static InvocationExpressionSyntax UpdateAssertNode(InvocationExpressio
SyntaxFactory.Argument(constraintExpression)
}.Union(remainingArguments);

var newArgumentsList = SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(newArguments));
var newArgumentsList = assertNode.ArgumentList.WithArguments(newArguments);

return assertNode
.WithExpression(newExpression)
Expand Down
40 changes: 40 additions & 0 deletions src/nunit.analyzers/Extensions/ArgumentListSyntaxExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace NUnit.Analyzers.Extensions
{
internal static class ArgumentListSyntaxExtensions
{
public static ArgumentListSyntax WithArguments(
this ArgumentListSyntax @this,
IEnumerable<ArgumentSyntax> newArguments)
{
var originalArguments = @this.Arguments;
var originalSeparators = originalArguments.GetSeparators();

var nodesAndTokens = new List<SyntaxNodeOrToken> { newArguments.First() };

foreach (var newArgument in newArguments.Skip(1))
{
// If argument is not replaced - take original separator. Otherwise - comma
var oldIndex = originalArguments.IndexOf(newArgument);
var separator = originalSeparators.ElementAtOrDefault(oldIndex - 1);

if (separator == default(SyntaxToken))
{
separator = SyntaxFactory.Token(SyntaxKind.CommaToken);
}

nodesAndTokens.Add(separator);
nodesAndTokens.Add(newArgument);
}

var newSeparatedList = SyntaxFactory.SeparatedList<ArgumentSyntax>(nodesAndTokens);

return @this.WithArguments(newSeparatedList);
}
}
}