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

add support for xunit StringAsserts Equals #229

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ public void AssertObjectEqualWithComparer_TestCodeFix(string oldAssertion, strin
public void AssertObjectEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("object actual, object expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Equal(expected, actual);")]
[Implemented]
public void AssertStringEqual_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertEqualAnalyzer>("string actual, string expected", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equal(expected, actual);",
/* newAssertion: */ "actual.Should().Be(expected);")]
[Implemented]
public void AssertStringEqual_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("string actual, string expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.StrictEqual(expected, actual);")]
[Implemented]
Expand Down
15 changes: 14 additions & 1 deletion src/FluentAssertions.Analyzers/Tips/Xunit/AssertEqual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class AssertEqualAnalyzer : XunitAnalyzer
protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[]
{
new AssertStringEqualSyntaxVisitor(),
new AssertFloatEqualWithToleranceSyntaxVisitor(),
new AssertDoubleEqualWithToleranceSyntaxVisitor(),
new AssertDoubleEqualWithPrecisionSyntaxVisitor(),
Expand All @@ -30,7 +31,18 @@ public class AssertEqualAnalyzer : XunitAnalyzer
new AssertObjectEqualWithComparerSyntaxVisitor(),
new AssertObjectEqualSyntaxVisitor()
};


// public static void Equal(string? expected, string? actual)
public class AssertStringEqualSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public AssertStringEqualSyntaxVisitor() : base(
MemberValidator.ArgumentsMatch("Equal",
ArgumentValidator.IsType(TypeSelector.GetStringType),
ArgumentValidator.IsType(TypeSelector.GetStringType)))
{
}
}

// public static void Equal(float expected, float actual, float tolerance)
public class AssertFloatEqualWithToleranceSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
Expand Down Expand Up @@ -137,6 +149,7 @@ public class AssertEqualCodeFix : XunitCodeFixProvider
case nameof(AssertEqualAnalyzer.AssertDateTimeEqualSyntaxVisitor):
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Equal", "BeCloseTo");
case nameof(AssertEqualAnalyzer.AssertObjectEqualSyntaxVisitor):
case nameof(AssertEqualAnalyzer.AssertStringEqualSyntaxVisitor):
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Equal", "Be");
default:
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
Expand Down