Skip to content

feat: add nunit equality assertions #295

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 17, 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
123 changes: 123 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace FluentAssertions.Analyzers.Tests.Tips;
[TestClass]
public class NunitTests
{
#region Assert.Conditions.cs

[DataTestMethod]
[AssertionDiagnostic("Assert.True(actual{0});")]
Expand Down Expand Up @@ -193,6 +194,10 @@ public class NunitTests
[Implemented]
public void Nunit4_AssertNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object actual", oldAssertion, newAssertion);

#endregion

#region Assert.Comparisons.cs

[DataTestMethod]
[AssertionDiagnostic("Assert.Greater(arg1, arg2{0});")]
[Implemented]
Expand Down Expand Up @@ -387,6 +392,124 @@ public void Nunit4_AssertLessOrEqual_TestCodeFix(string oldAssertion, string new

private static readonly string[] ComparableArguments = Array.ConvertAll(new string[] { "int", "uint", "long", "ulong", "float", "double", "decimal" }, x => $"{x} arg1, {x} arg2");

#endregion

#region Assert.Equality.cs

[DataTestMethod]
[AssertionDiagnostic("Assert.AreEqual(expected, actual, 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});")]
[Implemented]
public void Nunit4_AssertEqualDouble_TestAnalyzer(string assertion)
=> Nunit4VerifyDiagnostic("double expected, double actual, double delta", assertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual, delta{0});",
newAssertion: "actual.Should().BeApproximately(expected, delta{0});")]
[Implemented]
public void Nunit3_AssertEqualDouble_TestCodeFix(string oldAssertion, string newAssertion)
=> Nunit3VerifyFix("double expected, double actual, double delta", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.AreEqual(expected, actual, delta{0});",
newAssertion: "actual.Should().BeApproximately(expected, 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});")]
[Implemented]
public void Nunit3_AssertEqualObject_TestAnalyzer(string assertion)
=> Nunit3VerifyDiagnostic("object expected, object actual", assertion);

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.AreEqual(expected, actual{0});")]
[Implemented]
public void Nunit4_AssertEqualObject_TestAnalyzer(string assertion)
=> Nunit4VerifyDiagnostic("object expected, object actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.AreEqual(expected, actual{0});",
newAssertion: "actual.Should().Be(expected{0});")]
[Implemented]
public void Nunit3_AssertEqualObject_TestCodeFix(string oldAssertion, string newAssertion)
=> Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.AreEqual(expected, actual{0});",
newAssertion: "actual.Should().Be(expected{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});")]
[Implemented]
public void Nunit3_AssertNotEqualObject_TestAnalyzer(string assertion)
=> Nunit3VerifyDiagnostic("object expected, object actual", assertion);

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.AreNotEqual(expected, actual{0});")]
[Implemented]
public void Nunit4_AssertNotEqualObject_TestAnalyzer(string assertion)
=> Nunit4VerifyDiagnostic("object expected, object actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.AreNotEqual(expected, actual{0});",
newAssertion: "actual.Should().NotBe(expected{0});")]
[Implemented]
public void Nunit3_AssertNotEqualObject_TestCodeFix(string oldAssertion, string newAssertion)
=> Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.AreNotEqual(expected, actual{0});",
newAssertion: "actual.Should().NotBe(expected{0});")]
[Implemented]
public void Nunit4_AssertNotEqualObject_TestCodeFix(string oldAssertion, string newAssertion)
=> Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.AreSame(expected, actual{0});")]
[Implemented]
public void Nunit3_AssertSame_TestAnalyzer(string assertion)
=> Nunit3VerifyDiagnostic("object expected, object actual", assertion);

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.AreSame(expected, actual{0});")]
[Implemented]
public void Nunit4_AssertSame_TestAnalyzer(string assertion)
=> Nunit4VerifyDiagnostic("object expected, object actual", assertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.AreSame(expected, actual{0});",
newAssertion: "actual.Should().BeSameAs(expected{0});")]
[Implemented]
public void Nunit3_AssertSame_TestCodeFix(string oldAssertion, string newAssertion)
=> Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.AreSame(expected, actual{0});",
newAssertion: "actual.Should().BeSameAs(expected{0});")]
[Implemented]
public void Nunit4_AssertSame_TestCodeFix(string oldAssertion, string newAssertion)
=> Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion);

#endregion

private void Nunit3VerifyDiagnostic(string methodArguments, string assertion)
=> VerifyDiagnostic(GenerateCode.Nunit3Assertion(methodArguments, assertion), PackageReference.Nunit_3_14_0);
private void Nunit3VerifyFix(string methodArguments, string oldAssertion, string newAssertion)
Expand Down
13 changes: 13 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper
case "LessOrEqual" when ArgumentsAreTypeOf(invocation, t.Double, t.Double, t.String, t.ObjectArray): // Assert.LessOrEqual(double arg1, double arg2, string message, params object[] parms)
case "LessOrEqual" when ArgumentsAreTypeOf(invocation, t.Decimal, t.Decimal, t.String, t.ObjectArray): // Assert.LessOrEqual(decimal arg1, decimal arg2, string message, params object[] parms)
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: []);
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: []);
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: []);
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)
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeSameAs", subjectIndex: 1, argumentsToRemove: []);
}
return null;
}
Expand Down