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

feat: add xunit Assert.Equivalent #282

Merged
merged 1 commit into from
Jan 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading