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

Fix missing parenthesis after applying XUnit code fixes #248

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
2 changes: 1 addition & 1 deletion src/FluentAssertions.Analyzers.Tests/DiagnosticVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static void VerifyFix(string language, DiagnosticAnalyzer analyzer, Code

//after applying all of the code fixes, compare the resulting string to the inputted one
var actual = GetStringFromDocument(document);
;
actual.Should().Be(newSource);
Meir017 marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public class XunitTests
[DataRow(
/* oldAssertion: */ "Assert.True(bool.Parse(\"true\"), \"because it's possible\");",
/* newAssertion: */ "bool.Parse(\"true\").Should().BeTrue(\"because it's possible\");")]
[DataRow(
/* oldAssertion: */ "Assert.True(!actual);",
/* newAssertion: */ "(!actual).Should().BeTrue();")]
[DataRow(
/* oldAssertion: */ "Assert.True(actual == false);",
/* newAssertion: */ "(actual == false).Should().BeTrue();")]
[Implemented]
public void AssertTrue_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertTrueCodeFix, AssertTrueAnalyzer>("bool actual", oldAssertion, newAssertion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override ExpressionSyntax GetNewExpression(
switch (properties.VisitorName)
{
case nameof(AssertIsAssignableFromAnalyzer.AssertIsAssignableFromGenericTypeSyntaxVisitor):
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsAssignableFrom", "BeAssignableTo");
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsAssignableFrom", "BeAssignableTo", argumentIndex: 0);
case nameof(AssertIsAssignableFromAnalyzer.AssertIsAssignableFromTypeSyntaxVisitor):
var newExpression = RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsAssignableFrom", "BeAssignableTo");
return ReplaceTypeOfArgumentWithGenericTypeIfExists(newExpression, "BeAssignableTo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override ExpressionSyntax GetNewExpression(
switch (properties.VisitorName)
{
case nameof(AssertIsNotAssignableFromAnalyzer.AssertIsNotAssignableFromGenericTypeSyntaxVisitor):
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsNotAssignableFrom", "NotBeAssignableTo");
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsNotAssignableFrom", "NotBeAssignableTo", argumentIndex: 0);
case nameof(AssertIsNotAssignableFromAnalyzer.AssertIsNotAssignableFromTypeSyntaxVisitor):
var newExpression = RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsNotAssignableFrom", "NotBeAssignableTo");
return ReplaceTypeOfArgumentWithGenericTypeIfExists(newExpression, "NotBeAssignableTo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override ExpressionSyntax GetNewExpression(
switch (properties.VisitorName)
{
case nameof(AssertIsNotTypeAnalyzer.AssertIsNotTypeGenericTypeSyntaxVisitor):
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsNotType", "NotBeOfType");
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsNotType", "NotBeOfType", argumentIndex: 0);
case nameof(AssertIsNotTypeAnalyzer.AssertIsNotTypeTypeSyntaxVisitor):
var newExpression = RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsNotType", "NotBeOfType");
return ReplaceTypeOfArgumentWithGenericTypeIfExists(newExpression, "NotBeOfType");
Expand Down
2 changes: 1 addition & 1 deletion src/FluentAssertions.Analyzers/Tips/Xunit/AssertIsType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override ExpressionSyntax GetNewExpression(
switch (properties.VisitorName)
{
case nameof(AssertIsTypeAnalyzer.AssertIsTypeGenericTypeSyntaxVisitor):
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsType", "BeOfType");
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsType", "BeOfType", argumentIndex: 0);
case nameof(AssertIsTypeAnalyzer.AssertIsTypeTypeSyntaxVisitor):
var newExpression = RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsType", "BeOfType");
return ReplaceTypeOfArgumentWithGenericTypeIfExists(newExpression, "BeOfType");
Expand Down
11 changes: 2 additions & 9 deletions src/FluentAssertions.Analyzers/Utilities/Expressions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Simplification;
Meir017 marked this conversation as resolved.
Show resolved Hide resolved
using SF = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace FluentAssertions.Analyzers;
Expand All @@ -26,16 +27,8 @@ public static ArgumentSyntax OptionsUsing(ArgumentSyntax comparer)

public static InvocationExpressionSyntax SubjectShould(ExpressionSyntax subject)
{
if (subject.IsKind(SyntaxKind.CastExpression))
{
return SF.InvocationExpression(
SF.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SF.ParenthesizedExpression(subject), SF.IdentifierName("Should")),
SF.ArgumentList()
);
}

return SF.InvocationExpression(
SF.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, subject, SF.IdentifierName("Should")),
SF.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SF.ParenthesizedExpression(subject).WithAdditionalAnnotations(Simplifier.Annotation), SF.IdentifierName("Should")),
SF.ArgumentList()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ protected ExpressionSyntax RenameMethodAndReplaceWithSubjectShould(ExpressionSyn
return ReplaceIdentifier(newExpression, AssertClassName, Expressions.SubjectShould(rename.Argument.Expression));
}

protected ExpressionSyntax RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(ExpressionSyntax expression, string oldName, string newName)
protected ExpressionSyntax RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(ExpressionSyntax expression, string oldName, string newName, int argumentIndex = 1)
{
var rename = NodeReplacement.RenameAndExtractArguments(oldName, newName);
var newExpression = GetNewExpression(expression, rename);

var actual = rename.Arguments[1];
var actual = rename.Arguments[argumentIndex];

newExpression = ReplaceIdentifier(newExpression, AssertClassName, Expressions.SubjectShould(actual.Expression));

return GetNewExpression(newExpression, NodeReplacement.WithArguments(newName, rename.Arguments.RemoveAt(1)));
return GetNewExpression(newExpression, NodeReplacement.WithArguments(newName, rename.Arguments.RemoveAt(argumentIndex)));
}

protected ExpressionSyntax ReplaceTypeOfArgumentWithGenericTypeIfExists(ExpressionSyntax expression, string method)
Expand Down
Loading