Skip to content

Commit 03beb37

Browse files
authored
feat: add nunit collection Assert.That Is.EqualTo (#360)
1 parent 6b4f4d2 commit 03beb37

File tree

6 files changed

+87
-16
lines changed

6 files changed

+87
-16
lines changed

docs/Nunit3Analyzer.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ var expected = new[] { 1, 2, 3 };
523523

524524
// old assertion:
525525
CollectionAssert.AreEqual(expected, collection);
526+
Assert.That(collection, Is.EqualTo(expected));
526527

527528
// new assertion:
528529
collection.Should().Equal(expected);
@@ -540,6 +541,11 @@ CollectionAssert.AreEqual(expected, collection); /* fail message: Expected and
540541
Expected: 4
541542
But was: 3
542543
*/
544+
Assert.That(collection, Is.EqualTo(expected)); /* fail message: Expected and actual are both <System.Int32[3]>
545+
Values differ at index [2]
546+
Expected: 4
547+
But was: 3
548+
*/
543549

544550
// new assertion:
545551
collection.Should().Equal(expected); /* fail message: Expected collection to be equal to {1, 2, 4}, but {1, 2, 3} differs at index 2. */
@@ -554,6 +560,7 @@ var expected = new[] { 1, 2, 4 };
554560

555561
// old assertion:
556562
CollectionAssert.AreNotEqual(expected, collection);
563+
Assert.That(collection, Is.Not.EqualTo(expected));
557564

558565
// new assertion:
559566
collection.Should().NotEqual(expected);
@@ -569,6 +576,9 @@ var expected = new[] { 1, 2, 3 };
569576
CollectionAssert.AreNotEqual(expected, collection); /* fail message: Expected: not equal to < 1, 2, 3 >
570577
But was: < 1, 2, 3 >
571578
*/
579+
Assert.That(collection, Is.Not.EqualTo(expected)); /* fail message: Expected: not equal to < 1, 2, 3 >
580+
But was: < 1, 2, 3 >
581+
*/
572582

573583
// new assertion:
574584
collection.Should().NotEqual(expected); /* fail message: Did not expect collections {1, 2, 3} and {1, 2, 3} to be equal. */

docs/Nunit4Analyzer.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ var expected = new[] { 1, 2, 3 };
562562

563563
// old assertion:
564564
CollectionAssert.AreEqual(expected, collection);
565+
Assert.That(collection, Is.EqualTo(expected));
565566

566567
// new assertion:
567568
collection.Should().Equal(expected);
@@ -580,6 +581,12 @@ CollectionAssert.AreEqual(expected, collection); /* fail message: Assert.That(
580581
Expected: 4
581582
But was: 3
582583
*/
584+
Assert.That(collection, Is.EqualTo(expected)); /* fail message: Assert.That(collection, Is.EqualTo(expected))
585+
Expected and actual are both <System.Int32[3]>
586+
Values differ at index [2]
587+
Expected: 4
588+
But was: 3
589+
*/
583590

584591
// new assertion:
585592
collection.Should().Equal(expected); /* fail message: Expected collection to be equal to {1, 2, 4}, but {1, 2, 3} differs at index 2. */
@@ -594,6 +601,7 @@ var expected = new[] { 1, 2, 4 };
594601

595602
// old assertion:
596603
CollectionAssert.AreNotEqual(expected, collection);
604+
Assert.That(collection, Is.Not.EqualTo(expected));
597605

598606
// new assertion:
599607
collection.Should().NotEqual(expected);
@@ -610,6 +618,10 @@ CollectionAssert.AreNotEqual(expected, collection); /* fail message: Assert.Th
610618
Expected: not equal to < 1, 2, 3 >
611619
But was: < 1, 2, 3 >
612620
*/
621+
Assert.That(collection, Is.Not.EqualTo(expected)); /* fail message: Assert.That(collection, Is.Not.EqualTo(expected))
622+
Expected: not equal to < 1, 2, 3 >
623+
But was: < 1, 2, 3 >
624+
*/
613625

614626
// new assertion:
615627
collection.Should().NotEqual(expected); /* fail message: Did not expect collections {1, 2, 3} and {1, 2, 3} to be equal. */

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit3/Nunit3AnalyzerTests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,13 +759,14 @@ public void CollectionAssertAreEqual()
759759

760760
// old assertion:
761761
CollectionAssert.AreEqual(expected, collection);
762+
Assert.That(collection, Is.EqualTo(expected));
762763

763764
// new assertion:
764765
collection.Should().Equal(expected);
765766
}
766767

767768
[Test, ExpectedAssertionException]
768-
public void CollectionAssertAreEqual_Failure_OldAssertion()
769+
public void CollectionAssertAreEqual_Failure_OldAssertion_0()
769770
{
770771
// arrange
771772
var collection = new[] { 1, 2, 3 };
@@ -775,6 +776,17 @@ public void CollectionAssertAreEqual_Failure_OldAssertion()
775776
CollectionAssert.AreEqual(expected, collection);
776777
}
777778

779+
[Test, ExpectedAssertionException]
780+
public void CollectionAssertAreEqual_Failure_OldAssertion_1()
781+
{
782+
// arrange
783+
var collection = new[] { 1, 2, 3 };
784+
var expected = new[] { 1, 2, 4 };
785+
786+
// old assertion:
787+
Assert.That(collection, Is.EqualTo(expected));
788+
}
789+
778790
[Test, ExpectedAssertionException]
779791
public void CollectionAssertAreEqual_Failure_NewAssertion()
780792
{
@@ -795,13 +807,14 @@ public void CollectionAssertAreNotEqual()
795807

796808
// old assertion:
797809
CollectionAssert.AreNotEqual(expected, collection);
810+
Assert.That(collection, Is.Not.EqualTo(expected));
798811

799812
// new assertion:
800813
collection.Should().NotEqual(expected);
801814
}
802815

803816
[Test, ExpectedAssertionException]
804-
public void CollectionAssertAreNotEqual_Failure_OldAssertion()
817+
public void CollectionAssertAreNotEqual_Failure_OldAssertion_0()
805818
{
806819
// arrange
807820
var collection = new[] { 1, 2, 3 };
@@ -811,6 +824,17 @@ public void CollectionAssertAreNotEqual_Failure_OldAssertion()
811824
CollectionAssert.AreNotEqual(expected, collection);
812825
}
813826

827+
[Test, ExpectedAssertionException]
828+
public void CollectionAssertAreNotEqual_Failure_OldAssertion_1()
829+
{
830+
// arrange
831+
var collection = new[] { 1, 2, 3 };
832+
var expected = new[] { 1, 2, 3 };
833+
834+
// old assertion:
835+
Assert.That(collection, Is.Not.EqualTo(expected));
836+
}
837+
814838
[Test, ExpectedAssertionException]
815839
public void CollectionAssertAreNotEqual_Failure_NewAssertion()
816840
{

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,13 +770,14 @@ public void CollectionAssertAreEqual()
770770

771771
// old assertion:
772772
CollectionAssert.AreEqual(expected, collection);
773+
Assert.That(collection, Is.EqualTo(expected));
773774

774775
// new assertion:
775776
collection.Should().Equal(expected);
776777
}
777778

778779
[Test, ExpectedAssertionException]
779-
public void CollectionAssertAreEqual_Failure_OldAssertion()
780+
public void CollectionAssertAreEqual_Failure_OldAssertion_0()
780781
{
781782
// arrange
782783
var collection = new[] { 1, 2, 3 };
@@ -786,6 +787,17 @@ public void CollectionAssertAreEqual_Failure_OldAssertion()
786787
CollectionAssert.AreEqual(expected, collection);
787788
}
788789

790+
[Test, ExpectedAssertionException]
791+
public void CollectionAssertAreEqual_Failure_OldAssertion_1()
792+
{
793+
// arrange
794+
var collection = new[] { 1, 2, 3 };
795+
var expected = new[] { 1, 2, 4 };
796+
797+
// old assertion:
798+
Assert.That(collection, Is.EqualTo(expected));
799+
}
800+
789801
[Test, ExpectedAssertionException]
790802
public void CollectionAssertAreEqual_Failure_NewAssertion()
791803
{
@@ -806,13 +818,14 @@ public void CollectionAssertAreNotEqual()
806818

807819
// old assertion:
808820
CollectionAssert.AreNotEqual(expected, collection);
821+
Assert.That(collection, Is.Not.EqualTo(expected));
809822

810823
// new assertion:
811824
collection.Should().NotEqual(expected);
812825
}
813826

814827
[Test, ExpectedAssertionException]
815-
public void CollectionAssertAreNotEqual_Failure_OldAssertion()
828+
public void CollectionAssertAreNotEqual_Failure_OldAssertion_0()
816829
{
817830
// arrange
818831
var collection = new[] { 1, 2, 3 };
@@ -822,6 +835,17 @@ public void CollectionAssertAreNotEqual_Failure_OldAssertion()
822835
CollectionAssert.AreNotEqual(expected, collection);
823836
}
824837

838+
[Test, ExpectedAssertionException]
839+
public void CollectionAssertAreNotEqual_Failure_OldAssertion_1()
840+
{
841+
// arrange
842+
var collection = new[] { 1, 2, 3 };
843+
var expected = new[] { 1, 2, 3 };
844+
845+
// old assertion:
846+
Assert.That(collection, Is.Not.EqualTo(expected));
847+
}
848+
825849
[Test, ExpectedAssertionException]
826850
public void CollectionAssertAreNotEqual_Failure_NewAssertion()
827851
{

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ public void Nunit4_AssertContains_WithCasting_TestCodeFix(string methodArguments
13051305
[DataTestMethod]
13061306
[AssertionDiagnostic("CollectionAssert.AreEqual(expected, actual{0});")]
13071307
[AssertionDiagnostic("CollectionAssert.AreEqual(expected, actual, comparer{0});")]
1308-
[AssertionDiagnostic("Assert.That(actual, Is.EqualTo(expected){0});", ignore: true)]
1308+
[AssertionDiagnostic("Assert.That(actual, Is.EqualTo(expected){0});")]
13091309
[Implemented]
13101310
public void Nunit3_CollectionAssertAreEqual_TestAnalyzer(string assertion)
13111311
{
@@ -1317,7 +1317,7 @@ public void Nunit3_CollectionAssertAreEqual_TestAnalyzer(string assertion)
13171317
[DataTestMethod]
13181318
[AssertionDiagnostic("CollectionAssert.AreEqual(expected, actual{0});")]
13191319
[AssertionDiagnostic("CollectionAssert.AreEqual(expected, actual, comparer{0});")]
1320-
[AssertionDiagnostic("Assert.That(actual, Is.EqualTo(expected));", ignore: true)]
1320+
[AssertionDiagnostic("Assert.That(actual, Is.EqualTo(expected));")]
13211321
[Implemented]
13221322
public void Nunit4_CollectionAssertAreEqual_TestAnalyzer(string assertion)
13231323
{
@@ -1332,8 +1332,7 @@ public void Nunit4_CollectionAssertAreEqual_TestAnalyzer(string assertion)
13321332
newAssertion: "actual.Should().Equal(expected{0});")]
13331333
[AssertionCodeFix(
13341334
oldAssertion: "Assert.That(actual, Is.EqualTo(expected){0});",
1335-
newAssertion: "actual.Should().Equal(expected{0});",
1336-
ignore: true)]
1335+
newAssertion: "actual.Should().Equal(expected{0});")]
13371336
[Implemented]
13381337
public void Nunit3_CollectionAssertAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
13391338
{
@@ -1364,8 +1363,7 @@ public void Nunit3_CollectionAssertAreEqual_NoFix_IComparer_TestCodeFix(string a
13641363
newAssertion: "actual.Should().Equal(expected{0});")]
13651364
[AssertionCodeFix(
13661365
oldAssertion: "Assert.That(actual, Is.EqualTo(expected));",
1367-
newAssertion: "actual.Should().Equal(expected);",
1368-
ignore: true)]
1366+
newAssertion: "actual.Should().Equal(expected);")]
13691367
[Implemented]
13701368
public void Nunit4_CollectionAssertAreEqual_TestCodeFix(string oldAssertion, string newAssertion)
13711369
{
@@ -1393,7 +1391,7 @@ public void Nunit4_CollectionAssertAreEqual_NoFix_IComparer_TestCodeFix(string a
13931391

13941392
[DataTestMethod]
13951393
[AssertionDiagnostic("CollectionAssert.AreNotEqual(expected, actual{0});")]
1396-
[AssertionDiagnostic("Assert.That(actual, Is.Not.EqualTo(expected){0});", ignore: true)]
1394+
[AssertionDiagnostic("Assert.That(actual, Is.Not.EqualTo(expected){0});")]
13971395
[Implemented]
13981396
public void Nunit3_CollectionAssertAreNotEqual_TestAnalyzer(string assertion)
13991397
{
@@ -1404,7 +1402,7 @@ public void Nunit3_CollectionAssertAreNotEqual_TestAnalyzer(string assertion)
14041402

14051403
[DataTestMethod]
14061404
[AssertionDiagnostic("CollectionAssert.AreNotEqual(expected, actual{0});")]
1407-
[AssertionDiagnostic("Assert.That(actual, Is.Not.EqualTo(expected));", ignore: true)]
1405+
[AssertionDiagnostic("Assert.That(actual, Is.Not.EqualTo(expected));")]
14081406
[Implemented]
14091407
public void Nunit4_CollectionAssertAreNotEqual_TestAnalyzer(string assertion)
14101408
{
@@ -1419,8 +1417,7 @@ public void Nunit4_CollectionAssertAreNotEqual_TestAnalyzer(string assertion)
14191417
newAssertion: "actual.Should().NotEqual(expected{0});")]
14201418
[AssertionCodeFix(
14211419
oldAssertion: "Assert.That(actual, Is.Not.EqualTo(expected){0});",
1422-
newAssertion: "actual.Should().NotEqual(expected{0});",
1423-
ignore: true)]
1420+
newAssertion: "actual.Should().NotEqual(expected{0});")]
14241421
[Implemented]
14251422
public void Nunit3_CollectionAssertAreNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
14261423
{
@@ -1435,8 +1432,7 @@ public void Nunit3_CollectionAssertAreNotEqual_TestCodeFix(string oldAssertion,
14351432
newAssertion: "actual.Should().NotEqual(expected{0});")]
14361433
[AssertionCodeFix(
14371434
oldAssertion: "Assert.That(actual, Is.Not.EqualTo(expected));",
1438-
newAssertion: "actual.Should().NotEqual(expected);",
1439-
ignore: true)]
1435+
newAssertion: "actual.Should().NotEqual(expected);")]
14401436
[Implemented]
14411437
public void Nunit4_CollectionAssertAreNotEqual_TestCodeFix(string oldAssertion, string newAssertion)
14421438
{

src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ private CreateChangedDocument TryComputeFixForNunitThat(IInvocationOperation inv
468468
else
469469
return rewriter.Should("HaveCountGreaterThan", argument);
470470
}
471+
else if (matcher.Is(Method("EqualTo"), out argument))
472+
return rewriter.Should("Equal", argument);
473+
else if (matcher.Is("Not", Method("EqualTo"), out argument))
474+
return rewriter.Should("NotEqual", argument);
471475
}
472476
if (matcher.Is("Zero"))
473477
return rewriter.Should("Be", g => g.LiteralExpression(0));
@@ -557,6 +561,7 @@ public class NunitCodeFixContext(Compilation compilation) : TestingFrameworkCode
557561
private class AssertThatMatcher(IOperation constraint, NunitCodeFixContext t)
558562
{
559563
public bool Is(MethodInvocationMatcher methodMatcher, out IArgumentOperation argument) => Matches(t.Is, methodMatcher, out argument);
564+
public bool Is(string propertyMatcher, MethodInvocationMatcher methodMatcher, out IArgumentOperation argument) => Matches(t.Is, [Property(propertyMatcher)], methodMatcher, out argument);
560565
public bool Is(params string[] matchers) => Matches(t.Is, matchers);
561566
public bool Has(params string[] matchers) => Matches(t.Has, matchers);
562567
public bool Has(IOperationMatcher[] matchers, MethodInvocationMatcher methodMatcher, out IArgumentOperation argument) => Matches(t.Has, matchers, methodMatcher, out argument);

0 commit comments

Comments
 (0)