diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs index 54431d1..5b6f9a0 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs @@ -919,12 +919,14 @@ public void Nunit4_AssertLessOrEqual_TestCodeFix(string oldAssertion, string new [DataTestMethod] [AssertionDiagnostic("Assert.AreEqual(expected, actual, delta{0});")] + [AssertionDiagnostic("Assert.AreEqual(actual, 4.2d, delta{0});")] [Implemented] public void Nunit3_AssertEqualDouble_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("double expected, double actual, double delta", assertion); [DataTestMethod] [AssertionDiagnostic("ClassicAssert.AreEqual(expected, actual, delta{0});")] + [AssertionDiagnostic("ClassicAssert.AreEqual(actual, 4.2d, delta{0});")] [Implemented] public void Nunit4_AssertEqualDouble_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("double expected, double actual, double delta", assertion); @@ -933,6 +935,9 @@ public void Nunit4_AssertEqualDouble_TestAnalyzer(string assertion) [AssertionCodeFix( oldAssertion: "Assert.AreEqual(expected, actual, delta{0});", newAssertion: "actual.Should().BeApproximately(expected, delta{0});")] + [AssertionCodeFix( + oldAssertion: "Assert.AreEqual(actual, 4.2d, delta{0});", + newAssertion: "actual.Should().BeApproximately(4.2d, delta{0});")] [Implemented] public void Nunit3_AssertEqualDouble_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("double expected, double actual, double delta", oldAssertion, newAssertion); @@ -941,18 +946,23 @@ public void Nunit3_AssertEqualDouble_TestCodeFix(string oldAssertion, string new [AssertionCodeFix( oldAssertion: "ClassicAssert.AreEqual(expected, actual, delta{0});", newAssertion: "actual.Should().BeApproximately(expected, delta{0});")] + [AssertionCodeFix( + oldAssertion: "ClassicAssert.AreEqual(actual, 4.2d, delta{0});", + newAssertion: "actual.Should().BeApproximately(4.2d, delta{0});")] [Implemented] public void Nunit4_AssertEqualDouble_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("double expected, double actual, double delta", oldAssertion, newAssertion); [DataTestMethod] [AssertionDiagnostic("Assert.AreEqual(expected, actual{0});")] + [AssertionDiagnostic("Assert.AreEqual(actual, \"foo\"{0});")] [Implemented] public void Nunit3_AssertEqualObject_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("object expected, object actual", assertion); [DataTestMethod] [AssertionDiagnostic("ClassicAssert.AreEqual(expected, actual{0});")] + [AssertionDiagnostic("ClassicAssert.AreEqual(actual, \"foo\"{0});")] [Implemented] public void Nunit4_AssertEqualObject_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("object expected, object actual", assertion); @@ -961,6 +971,9 @@ public void Nunit4_AssertEqualObject_TestAnalyzer(string assertion) [AssertionCodeFix( oldAssertion: "Assert.AreEqual(expected, actual{0});", newAssertion: "actual.Should().Be(expected{0});")] + [AssertionCodeFix( + oldAssertion: "Assert.AreEqual(actual, \"foo\"{0});", + newAssertion: "actual.Should().Be(\"foo\"{0});")] [Implemented] public void Nunit3_AssertEqualObject_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion); @@ -969,18 +982,23 @@ public void Nunit3_AssertEqualObject_TestCodeFix(string oldAssertion, string new [AssertionCodeFix( oldAssertion: "ClassicAssert.AreEqual(expected, actual{0});", newAssertion: "actual.Should().Be(expected{0});")] + [AssertionCodeFix( + oldAssertion: "ClassicAssert.AreEqual(actual, \"foo\"{0});", + newAssertion: "actual.Should().Be(\"foo\"{0});")] [Implemented] public void Nunit4_AssertEqualObject_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion); [DataTestMethod] [AssertionDiagnostic("Assert.AreNotEqual(expected, actual{0});")] + [AssertionDiagnostic("Assert.AreNotEqual(actual, \"foo\"{0});")] [Implemented] public void Nunit3_AssertNotEqualObject_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("object expected, object actual", assertion); [DataTestMethod] [AssertionDiagnostic("ClassicAssert.AreNotEqual(expected, actual{0});")] + [AssertionDiagnostic("ClassicAssert.AreNotEqual(actual, \"foo\"{0});")] [Implemented] public void Nunit4_AssertNotEqualObject_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("object expected, object actual", assertion); @@ -989,6 +1007,9 @@ public void Nunit4_AssertNotEqualObject_TestAnalyzer(string assertion) [AssertionCodeFix( oldAssertion: "Assert.AreNotEqual(expected, actual{0});", newAssertion: "actual.Should().NotBe(expected{0});")] + [AssertionCodeFix( + oldAssertion: "Assert.AreNotEqual(actual, \"foo\"{0});", + newAssertion: "actual.Should().NotBe(\"foo\"{0});")] [Implemented] public void Nunit3_AssertNotEqualObject_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion); @@ -997,6 +1018,9 @@ public void Nunit3_AssertNotEqualObject_TestCodeFix(string oldAssertion, string [AssertionCodeFix( oldAssertion: "ClassicAssert.AreNotEqual(expected, actual{0});", newAssertion: "actual.Should().NotBe(expected{0});")] + [AssertionCodeFix( + oldAssertion: "ClassicAssert.AreNotEqual(actual, \"foo\"{0});", + newAssertion: "actual.Should().NotBe(\"foo\"{0});")] [Implemented] public void Nunit4_AssertNotEqualObject_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion); diff --git a/src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs b/src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs index 34040c6..7f2a3f7 100644 --- a/src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs +++ b/src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs @@ -116,6 +116,8 @@ public static void ByVal(object? actual, IResolveConstraint expression) public static void ByVal(object? actual, IResolveConstraint expression, string? message, params object?[]? args) */ + var actualSubjectIndex = invocation.Arguments.Length is 1 ? 0 + : invocation.Arguments[1].IsLiteralValue() ? 0 : 1; switch (invocation.TargetMethod.Name) { case "True": // Assert.True(bool condition) @@ -212,13 +214,13 @@ public static void ByVal(object? actual, IResolveConstraint expression, string? return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeLessOrEqualTo", subjectIndex: 0, argumentsToRemove: []); case "AreEqual" when ArgumentsAreTypeOf(invocation, t.Double, t.Double, t.Double): // Assert.AreEqual(double expected, double actual, double delta) case "AreEqual" when ArgumentsAreTypeOf(invocation, t.Double, t.Double, t.Double, t.String, t.ObjectArray): // Assert.AreEqual(double expected, double actual, double delta, string message, params object[] parms) - return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeApproximately", subjectIndex: 1, argumentsToRemove: []); + return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeApproximately", actualSubjectIndex, argumentsToRemove: []); case "AreEqual" when ArgumentsAreTypeOf(invocation, t.Object, t.Object): // Assert.AreEqual(object expected, object actual) case "AreEqual" when ArgumentsAreTypeOf(invocation, t.Object, t.Object, t.String, t.ObjectArray): // Assert.AreEqual(object expected, object actual, string message, params object[] parms) - return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "Be", subjectIndex: 1, argumentsToRemove: []); + return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "Be", actualSubjectIndex, argumentsToRemove: []); case "AreNotEqual" when ArgumentsAreTypeOf(invocation, t.Object, t.Object): // Assert.AreNotEqual(object expected, object actual) case "AreNotEqual" when ArgumentsAreTypeOf(invocation, t.Object, t.Object, t.String, t.ObjectArray): // Assert.AreNotEqual(object expected, object actual, string message, params object[] parms) - return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBe", subjectIndex: 1, argumentsToRemove: []); + return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBe", actualSubjectIndex, argumentsToRemove: []); case "AreSame": // Assert.AreSame(object expected, object actual) return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeSameAs", subjectIndex: 1, argumentsToRemove: []); case "AreNotSame": // Assert.AreNotSame(object expected, object actual)