Skip to content

Commit 37c27af

Browse files
authored
feat: add xunit Assert.Equivalent (#282)
1 parent d1e5142 commit 37c27af

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,28 @@ public void AssertInRange_TestCodeFix(string oldAssertion, string newAssertion)
672672
VerifyCSharpFix("long actual, long low, long high", oldAssertion, newAssertion);
673673
}
674674

675+
[DataTestMethod]
676+
[DataRow("object actual, object expected", "Assert.Equivalent(expected, actual);")]
677+
[DataRow("object actual, object expected", "Assert.Equivalent(expected, actual, false);")]
678+
[DataRow("DateTime actual, DateTime expected", "Assert.Equivalent(expected, actual);")]
679+
[DataRow("DateTime actual, DateTime expected", "Assert.Equivalent(expected, actual, false);")]
680+
[DataRow("int actual, int expected", "Assert.Equivalent(expected, actual);")]
681+
[DataRow("int actual, int expected", "Assert.Equivalent(expected, actual, false);")]
682+
[Implemented]
683+
public void AssertEquivalent_TestAnalyzer(string arguments, string assertion) =>
684+
VerifyCSharpDiagnostic(arguments, assertion);
685+
686+
[DataTestMethod]
687+
[DataRow(
688+
/* oldAssertion: */ "Assert.Equivalent(expected, actual);",
689+
/* newAssertion: */ "actual.Should().BeEquivalentTo(expected);")]
690+
[DataRow(
691+
/* oldAssertion: */ "Assert.Equivalent(expected, actual, false);",
692+
/* newAssertion: */ "actual.Should().BeEquivalentTo(expected);")]
693+
[Implemented]
694+
public void AssertEquivalent_TestCodeFix(string oldAssertion, string newAssertion)
695+
=> VerifyCSharpFix("object actual, object expected", oldAssertion, newAssertion);
696+
675697
private void VerifyCSharpDiagnostic(string methodArguments, string assertion)
676698
{
677699
var source = GenerateCode.XunitAssertion(methodArguments, assertion);

src/FluentAssertions.Analyzers/Tips/XunitCodeFixProvider.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Immutable;
22
using System.Composition;
3+
using System.Runtime.InteropServices.ComTypes;
34
using Microsoft.CodeAnalysis;
45
using Microsoft.CodeAnalysis.CodeFixes;
56
using Microsoft.CodeAnalysis.Operations;
@@ -26,6 +27,20 @@ protected override CreateChangedDocument TryComputeFix(IInvocationOperation invo
2627
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeSameAs", subjectIndex: 1, argumentsToRemove: []);
2728
case "NotSame" when ArgumentsAreTypeOf(invocation, t.Object, t.Object): // Assert.NotSame(object expected, object actual)
2829
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeSameAs", subjectIndex: 1, argumentsToRemove: []);
30+
case "Equivalent" when ArgumentsCount(invocation, 2): // Assert.Equivalent(object? expected, object? actual)
31+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeEquivalentTo", subjectIndex: 1, argumentsToRemove: []);
32+
case "Equivalent" when ArgumentsCount(invocation, 3): // Assert.Equivalent(object? expected, object? actual, bool strict = false)
33+
{
34+
if (invocation.Arguments[2].Value is ILiteralOperation literal)
35+
{
36+
return literal.ConstantValue.Value switch {
37+
false => DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeEquivalentTo", subjectIndex: 1, argumentsToRemove: literal.IsImplicit ? [] : [2]),
38+
_ => null
39+
};
40+
}
41+
42+
return null; // passing a reference for the "strict" argument
43+
}
2944
case "Equal" when ArgumentsAreTypeOf(invocation, t.Float, t.Float, t.Float): // Assert.Equal(float expected, float actual, float tolerance)
3045
case "Equal" when ArgumentsAreTypeOf(invocation, t.Double, t.Double, t.Double): // Assert.Equal(double expected, double actual, double tolerance)
3146
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeApproximately", subjectIndex: 1, argumentsToRemove: []);

0 commit comments

Comments
 (0)