From ae6037eb4aa796d6d8a25e12cb790f790bc7ec9c Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Sat, 6 Jan 2024 22:02:08 +0200 Subject: [PATCH] feat: add xunit Assert.Equivalent --- .../Tips/XunitTests.cs | 22 +++++++++++++++++++ .../Tips/XunitCodeFixProvider.cs | 15 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs index 75892b19..7c3719d9 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs @@ -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); diff --git a/src/FluentAssertions.Analyzers/Tips/XunitCodeFixProvider.cs b/src/FluentAssertions.Analyzers/Tips/XunitCodeFixProvider.cs index 67efe7b0..6daf1b28 100644 --- a/src/FluentAssertions.Analyzers/Tips/XunitCodeFixProvider.cs +++ b/src/FluentAssertions.Analyzers/Tips/XunitCodeFixProvider.cs @@ -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; @@ -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: []);