Skip to content
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
24 changes: 24 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down