Skip to content

Commit 849cf14

Browse files
authored
feat: add nunit equality assertions (#295)
1 parent 9fd07fb commit 849cf14

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace FluentAssertions.Analyzers.Tests.Tips;
88
[TestClass]
99
public class NunitTests
1010
{
11+
#region Assert.Conditions.cs
1112

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

197+
#endregion
198+
199+
#region Assert.Comparisons.cs
200+
196201
[DataTestMethod]
197202
[AssertionDiagnostic("Assert.Greater(arg1, arg2{0});")]
198203
[Implemented]
@@ -387,6 +392,124 @@ public void Nunit4_AssertLessOrEqual_TestCodeFix(string oldAssertion, string new
387392

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

395+
#endregion
396+
397+
#region Assert.Equality.cs
398+
399+
[DataTestMethod]
400+
[AssertionDiagnostic("Assert.AreEqual(expected, actual, delta{0});")]
401+
[Implemented]
402+
public void Nunit3_AssertEqualDouble_TestAnalyzer(string assertion)
403+
=> Nunit3VerifyDiagnostic("double expected, double actual, double delta", assertion);
404+
405+
[DataTestMethod]
406+
[AssertionDiagnostic("ClassicAssert.AreEqual(expected, actual, delta{0});")]
407+
[Implemented]
408+
public void Nunit4_AssertEqualDouble_TestAnalyzer(string assertion)
409+
=> Nunit4VerifyDiagnostic("double expected, double actual, double delta", assertion);
410+
411+
[DataTestMethod]
412+
[AssertionCodeFix(
413+
oldAssertion: "Assert.AreEqual(expected, actual, delta{0});",
414+
newAssertion: "actual.Should().BeApproximately(expected, delta{0});")]
415+
[Implemented]
416+
public void Nunit3_AssertEqualDouble_TestCodeFix(string oldAssertion, string newAssertion)
417+
=> Nunit3VerifyFix("double expected, double actual, double delta", oldAssertion, newAssertion);
418+
419+
[DataTestMethod]
420+
[AssertionCodeFix(
421+
oldAssertion: "ClassicAssert.AreEqual(expected, actual, delta{0});",
422+
newAssertion: "actual.Should().BeApproximately(expected, delta{0});")]
423+
[Implemented]
424+
public void Nunit4_AssertEqualDouble_TestCodeFix(string oldAssertion, string newAssertion)
425+
=> Nunit4VerifyFix("double expected, double actual, double delta", oldAssertion, newAssertion);
426+
427+
[DataTestMethod]
428+
[AssertionDiagnostic("Assert.AreEqual(expected, actual{0});")]
429+
[Implemented]
430+
public void Nunit3_AssertEqualObject_TestAnalyzer(string assertion)
431+
=> Nunit3VerifyDiagnostic("object expected, object actual", assertion);
432+
433+
[DataTestMethod]
434+
[AssertionDiagnostic("ClassicAssert.AreEqual(expected, actual{0});")]
435+
[Implemented]
436+
public void Nunit4_AssertEqualObject_TestAnalyzer(string assertion)
437+
=> Nunit4VerifyDiagnostic("object expected, object actual", assertion);
438+
439+
[DataTestMethod]
440+
[AssertionCodeFix(
441+
oldAssertion: "Assert.AreEqual(expected, actual{0});",
442+
newAssertion: "actual.Should().Be(expected{0});")]
443+
[Implemented]
444+
public void Nunit3_AssertEqualObject_TestCodeFix(string oldAssertion, string newAssertion)
445+
=> Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion);
446+
447+
[DataTestMethod]
448+
[AssertionCodeFix(
449+
oldAssertion: "ClassicAssert.AreEqual(expected, actual{0});",
450+
newAssertion: "actual.Should().Be(expected{0});")]
451+
[Implemented]
452+
public void Nunit4_AssertEqualObject_TestCodeFix(string oldAssertion, string newAssertion)
453+
=> Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion);
454+
455+
[DataTestMethod]
456+
[AssertionDiagnostic("Assert.AreNotEqual(expected, actual{0});")]
457+
[Implemented]
458+
public void Nunit3_AssertNotEqualObject_TestAnalyzer(string assertion)
459+
=> Nunit3VerifyDiagnostic("object expected, object actual", assertion);
460+
461+
[DataTestMethod]
462+
[AssertionDiagnostic("ClassicAssert.AreNotEqual(expected, actual{0});")]
463+
[Implemented]
464+
public void Nunit4_AssertNotEqualObject_TestAnalyzer(string assertion)
465+
=> Nunit4VerifyDiagnostic("object expected, object actual", assertion);
466+
467+
[DataTestMethod]
468+
[AssertionCodeFix(
469+
oldAssertion: "Assert.AreNotEqual(expected, actual{0});",
470+
newAssertion: "actual.Should().NotBe(expected{0});")]
471+
[Implemented]
472+
public void Nunit3_AssertNotEqualObject_TestCodeFix(string oldAssertion, string newAssertion)
473+
=> Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion);
474+
475+
[DataTestMethod]
476+
[AssertionCodeFix(
477+
oldAssertion: "ClassicAssert.AreNotEqual(expected, actual{0});",
478+
newAssertion: "actual.Should().NotBe(expected{0});")]
479+
[Implemented]
480+
public void Nunit4_AssertNotEqualObject_TestCodeFix(string oldAssertion, string newAssertion)
481+
=> Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion);
482+
483+
[DataTestMethod]
484+
[AssertionDiagnostic("Assert.AreSame(expected, actual{0});")]
485+
[Implemented]
486+
public void Nunit3_AssertSame_TestAnalyzer(string assertion)
487+
=> Nunit3VerifyDiagnostic("object expected, object actual", assertion);
488+
489+
[DataTestMethod]
490+
[AssertionDiagnostic("ClassicAssert.AreSame(expected, actual{0});")]
491+
[Implemented]
492+
public void Nunit4_AssertSame_TestAnalyzer(string assertion)
493+
=> Nunit4VerifyDiagnostic("object expected, object actual", assertion);
494+
495+
[DataTestMethod]
496+
[AssertionCodeFix(
497+
oldAssertion: "Assert.AreSame(expected, actual{0});",
498+
newAssertion: "actual.Should().BeSameAs(expected{0});")]
499+
[Implemented]
500+
public void Nunit3_AssertSame_TestCodeFix(string oldAssertion, string newAssertion)
501+
=> Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion);
502+
503+
[DataTestMethod]
504+
[AssertionCodeFix(
505+
oldAssertion: "ClassicAssert.AreSame(expected, actual{0});",
506+
newAssertion: "actual.Should().BeSameAs(expected{0});")]
507+
[Implemented]
508+
public void Nunit4_AssertSame_TestCodeFix(string oldAssertion, string newAssertion)
509+
=> Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion);
510+
511+
#endregion
512+
390513
private void Nunit3VerifyDiagnostic(string methodArguments, string assertion)
391514
=> VerifyDiagnostic(GenerateCode.Nunit3Assertion(methodArguments, assertion), PackageReference.Nunit_3_14_0);
392515
private void Nunit3VerifyFix(string methodArguments, string oldAssertion, string newAssertion)

src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper
106106
case "LessOrEqual" when ArgumentsAreTypeOf(invocation, t.Double, t.Double, t.String, t.ObjectArray): // Assert.LessOrEqual(double arg1, double arg2, string message, params object[] parms)
107107
case "LessOrEqual" when ArgumentsAreTypeOf(invocation, t.Decimal, t.Decimal, t.String, t.ObjectArray): // Assert.LessOrEqual(decimal arg1, decimal arg2, string message, params object[] parms)
108108
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeLessOrEqualTo", subjectIndex: 0, argumentsToRemove: []);
109+
case "AreEqual" when ArgumentsAreTypeOf(invocation, t.Double, t.Double, t.Double): // Assert.AreEqual(double expected, double actual, double delta)
110+
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)
111+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeApproximately", subjectIndex: 1, argumentsToRemove: []);
112+
case "AreEqual" when ArgumentsAreTypeOf(invocation, t.Object, t.Object): // Assert.AreEqual(object expected, object actual)
113+
case "AreEqual" when ArgumentsAreTypeOf(invocation, t.Object, t.Object, t.String, t.ObjectArray): // Assert.AreEqual(object expected, object actual, string message, params object[] parms)
114+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "Be", subjectIndex: 1, argumentsToRemove: []);
115+
case "AreNotEqual" when ArgumentsAreTypeOf(invocation, t.Object, t.Object): // Assert.AreNotEqual(object expected, object actual)
116+
case "AreNotEqual" when ArgumentsAreTypeOf(invocation, t.Object, t.Object, t.String, t.ObjectArray): // Assert.AreNotEqual(object expected, object actual, string message, params object[] parms)
117+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBe", subjectIndex: 1, argumentsToRemove: []);
118+
case "AreSame": // Assert.AreSame(object expected, object actual)
119+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeSameAs", subjectIndex: 1, argumentsToRemove: []);
120+
case "AreNotSame": // Assert.AreNotSame(object expected, object actual)
121+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeSameAs", subjectIndex: 1, argumentsToRemove: []);
109122
}
110123
return null;
111124
}

0 commit comments

Comments
 (0)