Skip to content

Commit

Permalink
Fix missing parenthesis after applying XUnit code fixes (#248)
Browse files Browse the repository at this point in the history
* Fix missing parenthesis after applying XUnit code fixes

* Update Expressions.cs

* Fix generic tests
  • Loading branch information
jairbubbles authored Dec 8, 2023
1 parent 1608c95 commit 104442f
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 17 deletions.
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);
}

/// <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;
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

0 comments on commit 104442f

Please sign in to comment.