Skip to content

Commit f923eae

Browse files
authored
add support for xunit StringAsserts Equals (#229)
1 parent 9dd2e5d commit f923eae

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ public void AssertObjectEqualWithComparer_TestCodeFix(string oldAssertion, strin
214214
public void AssertObjectEqual_TestCodeFix(string oldAssertion, string newAssertion)
215215
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("object actual, object expected", oldAssertion, newAssertion);
216216

217+
[DataTestMethod]
218+
[DataRow("Assert.Equal(expected, actual);")]
219+
[Implemented]
220+
public void AssertStringEqual_TestAnalyzer(string assertion) =>
221+
VerifyCSharpDiagnostic<AssertEqualAnalyzer>("string actual, string expected", assertion);
222+
223+
[DataTestMethod]
224+
[DataRow(
225+
/* oldAssertion: */ "Assert.Equal(expected, actual);",
226+
/* newAssertion: */ "actual.Should().Be(expected);")]
227+
[Implemented]
228+
public void AssertStringEqual_TestCodeFix(string oldAssertion, string newAssertion)
229+
=> VerifyCSharpFix<AssertEqualCodeFix, AssertEqualAnalyzer>("string actual, string expected", oldAssertion, newAssertion);
230+
217231
[DataTestMethod]
218232
[DataRow("Assert.StrictEqual(expected, actual);")]
219233
[Implemented]

src/FluentAssertions.Analyzers/Tips/Xunit/AssertEqual.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class AssertEqualAnalyzer : XunitAnalyzer
2222
protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
2323
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[]
2424
{
25+
new AssertStringEqualSyntaxVisitor(),
2526
new AssertFloatEqualWithToleranceSyntaxVisitor(),
2627
new AssertDoubleEqualWithToleranceSyntaxVisitor(),
2728
new AssertDoubleEqualWithPrecisionSyntaxVisitor(),
@@ -30,7 +31,18 @@ public class AssertEqualAnalyzer : XunitAnalyzer
3031
new AssertObjectEqualWithComparerSyntaxVisitor(),
3132
new AssertObjectEqualSyntaxVisitor()
3233
};
33-
34+
35+
// public static void Equal(string? expected, string? actual)
36+
public class AssertStringEqualSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
37+
{
38+
public AssertStringEqualSyntaxVisitor() : base(
39+
MemberValidator.ArgumentsMatch("Equal",
40+
ArgumentValidator.IsType(TypeSelector.GetStringType),
41+
ArgumentValidator.IsType(TypeSelector.GetStringType)))
42+
{
43+
}
44+
}
45+
3446
// public static void Equal(float expected, float actual, float tolerance)
3547
public class AssertFloatEqualWithToleranceSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
3648
{
@@ -137,6 +149,7 @@ protected override ExpressionSyntax GetNewExpression(
137149
case nameof(AssertEqualAnalyzer.AssertDateTimeEqualSyntaxVisitor):
138150
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Equal", "BeCloseTo");
139151
case nameof(AssertEqualAnalyzer.AssertObjectEqualSyntaxVisitor):
152+
case nameof(AssertEqualAnalyzer.AssertStringEqualSyntaxVisitor):
140153
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Equal", "Be");
141154
default:
142155
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");

0 commit comments

Comments
 (0)