Skip to content

Commit

Permalink
feat: add xunit Assert.Equivalent (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 authored Jan 6, 2024
1 parent d1e5142 commit 37c27af
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,28 @@ public void AssertInRange_TestCodeFix(string oldAssertion, string newAssertion)
VerifyCSharpFix("long actual, long low, long high", oldAssertion, newAssertion);
}

[DataTestMethod]
[DataRow("object actual, object expected", "Assert.Equivalent(expected, actual);")]
[DataRow("object actual, object expected", "Assert.Equivalent(expected, actual, false);")]
[DataRow("DateTime actual, DateTime expected", "Assert.Equivalent(expected, actual);")]
[DataRow("DateTime actual, DateTime expected", "Assert.Equivalent(expected, actual, false);")]
[DataRow("int actual, int expected", "Assert.Equivalent(expected, actual);")]
[DataRow("int actual, int expected", "Assert.Equivalent(expected, actual, false);")]
[Implemented]
public void AssertEquivalent_TestAnalyzer(string arguments, string assertion) =>
VerifyCSharpDiagnostic(arguments, assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Equivalent(expected, actual);",
/* newAssertion: */ "actual.Should().BeEquivalentTo(expected);")]
[DataRow(
/* oldAssertion: */ "Assert.Equivalent(expected, actual, false);",
/* newAssertion: */ "actual.Should().BeEquivalentTo(expected);")]
[Implemented]
public void AssertEquivalent_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix("object actual, object expected", oldAssertion, newAssertion);

private void VerifyCSharpDiagnostic(string methodArguments, string assertion)
{
var source = GenerateCode.XunitAssertion(methodArguments, assertion);
Expand Down
15 changes: 15 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/XunitCodeFixProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Composition;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Operations;
Expand All @@ -26,6 +27,20 @@ protected override CreateChangedDocument TryComputeFix(IInvocationOperation invo
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeSameAs", subjectIndex: 1, argumentsToRemove: []);
case "NotSame" when ArgumentsAreTypeOf(invocation, t.Object, t.Object): // Assert.NotSame(object expected, object actual)
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeSameAs", subjectIndex: 1, argumentsToRemove: []);
case "Equivalent" when ArgumentsCount(invocation, 2): // Assert.Equivalent(object? expected, object? actual)
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeEquivalentTo", subjectIndex: 1, argumentsToRemove: []);
case "Equivalent" when ArgumentsCount(invocation, 3): // Assert.Equivalent(object? expected, object? actual, bool strict = false)
{
if (invocation.Arguments[2].Value is ILiteralOperation literal)
{
return literal.ConstantValue.Value switch {
false => DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeEquivalentTo", subjectIndex: 1, argumentsToRemove: literal.IsImplicit ? [] : [2]),
_ => null
};
}

return null; // passing a reference for the "strict" argument
}
case "Equal" when ArgumentsAreTypeOf(invocation, t.Float, t.Float, t.Float): // Assert.Equal(float expected, float actual, float tolerance)
case "Equal" when ArgumentsAreTypeOf(invocation, t.Double, t.Double, t.Double): // Assert.Equal(double expected, double actual, double tolerance)
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeApproximately", subjectIndex: 1, argumentsToRemove: []);
Expand Down

0 comments on commit 37c27af

Please sign in to comment.