Skip to content

Commit c730fb2

Browse files
authored
feat: add nunit collection asserts AllItemsAreNotNull & AllItemsAreUnique (#352)
* feat: add nunit collection asserts AllItemsAreNotNull & AllItemsAreUnique
1 parent 1503be5 commit c730fb2

File tree

6 files changed

+302
-5
lines changed

6 files changed

+302
-5
lines changed

docs/Nunit3Analyzer.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
2222
- [CollectionAssertDoesNotContain_WithCasting](#scenario-collectionassertdoesnotcontain_withcasting) - `collection.Should().NotContain((int)item);`
2323
- [CollectionAssertAllItemsAreInstancesOfType](#scenario-collectionassertallitemsareinstancesoftype) - `collection.Should().AllBeOfType<int>();`
2424
- [CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument](#scenario-collectionassertallitemsareinstancesoftype_withtypeargument) - `collection.Should().AllBeOfType(type);`
25+
- [CollectionAssertAllItemsAreNotNull](#scenario-collectionassertallitemsarenotnull) - `collection.Should().NotContainNulls();`
26+
- [CollectionAssertAllItemsAreUnique](#scenario-collectionassertallitemsareunique) - `collection.Should().OnlyHaveUniqueItems();`
2527

2628

2729
## Scenarios
@@ -598,4 +600,60 @@ CollectionAssert.AllItemsAreInstancesOfType(collection, type); /* fail message:
598600
collection.Should().AllBeOfType(type); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
599601
```
600602

603+
### scenario: CollectionAssertAllItemsAreNotNull
604+
605+
```cs
606+
// arrange
607+
var collection = new object[] { 1, "test", true };
608+
609+
// old assertion:
610+
CollectionAssert.AllItemsAreNotNull(collection);
611+
612+
// new assertion:
613+
collection.Should().NotContainNulls();
614+
```
615+
616+
#### Failure messages
617+
618+
```cs
619+
var collection = new object[] { 1, null, true };
620+
621+
// old assertion:
622+
CollectionAssert.AllItemsAreNotNull(collection); /* fail message: Expected: all items not null
623+
But was: < 1, null, True >
624+
First non-matching item at index [1]: null
625+
*/
626+
627+
// new assertion:
628+
collection.Should().NotContainNulls(); /* fail message: Expected collection not to contain <null>s, but found one at index 1. */
629+
```
630+
631+
### scenario: CollectionAssertAllItemsAreUnique
632+
633+
```cs
634+
// arrange
635+
var collection = new[] { 1, 2, 3 };
636+
637+
// old assertion:
638+
CollectionAssert.AllItemsAreUnique(collection);
639+
640+
// new assertion:
641+
collection.Should().OnlyHaveUniqueItems();
642+
```
643+
644+
#### Failure messages
645+
646+
```cs
647+
var collection = new[] { 1, 2, 1 };
648+
649+
// old assertion:
650+
CollectionAssert.AllItemsAreUnique(collection); /* fail message: Expected: all items unique
651+
But was: < 1, 2, 1 >
652+
Not unique items: < 1 >
653+
*/
654+
655+
// new assertion:
656+
collection.Should().OnlyHaveUniqueItems(); /* fail message: Expected collection to only have unique items, but item 1 is not unique. */
657+
```
658+
601659

docs/Nunit4Analyzer.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
2222
- [CollectionAssertDoesNotContain_WithCasting](#scenario-collectionassertdoesnotcontain_withcasting) - `collection.Should().NotContain((int)item);`
2323
- [CollectionAssertAllItemsAreInstancesOfType](#scenario-collectionassertallitemsareinstancesoftype) - `collection.Should().AllBeOfType<int>();`
2424
- [CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument](#scenario-collectionassertallitemsareinstancesoftype_withtypeargument) - `collection.Should().AllBeOfType(type);`
25+
- [CollectionAssertAllItemsAreNotNull](#scenario-collectionassertallitemsarenotnull) - `collection.Should().NotContainNulls();`
26+
- [CollectionAssertAllItemsAreUnique](#scenario-collectionassertallitemsareunique) - `collection.Should().OnlyHaveUniqueItems();`
2527

2628

2729
## Scenarios
@@ -392,7 +394,7 @@ ClassicAssert.AreNotSame(obj1, obj2); /* fail message: Assert.That(actual, Is.
392394
*/
393395

394396
// new assertion:
395-
obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to System.Object (HashCode=2766117). */
397+
obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to System.Object (HashCode=19989589). */
396398
```
397399

398400
### scenario: CollectionAssertAreEqual
@@ -633,4 +635,62 @@ CollectionAssert.AllItemsAreInstancesOfType(collection, type); /* fail message:
633635
collection.Should().AllBeOfType(type); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
634636
```
635637

638+
### scenario: CollectionAssertAllItemsAreNotNull
639+
640+
```cs
641+
// arrange
642+
var collection = new object[] { 1, "test", true };
643+
644+
// old assertion:
645+
CollectionAssert.AllItemsAreNotNull(collection);
646+
647+
// new assertion:
648+
collection.Should().NotContainNulls();
649+
```
650+
651+
#### Failure messages
652+
653+
```cs
654+
var collection = new object[] { 1, null, true };
655+
656+
// old assertion:
657+
CollectionAssert.AllItemsAreNotNull(collection); /* fail message: Assert.That(collection, Is.All.Not.Null)
658+
Expected: all items not null
659+
But was: < 1, null, True >
660+
First non-matching item at index [1]: null
661+
*/
662+
663+
// new assertion:
664+
collection.Should().NotContainNulls(); /* fail message: Expected collection not to contain <null>s, but found one at index 1. */
665+
```
666+
667+
### scenario: CollectionAssertAllItemsAreUnique
668+
669+
```cs
670+
// arrange
671+
var collection = new[] { 1, 2, 3 };
672+
673+
// old assertion:
674+
CollectionAssert.AllItemsAreUnique(collection);
675+
676+
// new assertion:
677+
collection.Should().OnlyHaveUniqueItems();
678+
```
679+
680+
#### Failure messages
681+
682+
```cs
683+
var collection = new[] { 1, 2, 1 };
684+
685+
// old assertion:
686+
CollectionAssert.AllItemsAreUnique(collection); /* fail message: Assert.That(collection, Is.Unique)
687+
Expected: all items unique
688+
But was: < 1, 2, 1 >
689+
Not unique items: < 1 >
690+
*/
691+
692+
// new assertion:
693+
collection.Should().OnlyHaveUniqueItems(); /* fail message: Expected collection to only have unique items, but item 1 is not unique. */
694+
```
695+
636696

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,4 +808,70 @@ public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_
808808
// new assertion:
809809
collection.Should().AllBeOfType(type);
810810
}
811+
812+
[Test]
813+
public void CollectionAssertAllItemsAreNotNull()
814+
{
815+
// arrange
816+
var collection = new object[] { 1, "test", true };
817+
818+
// old assertion:
819+
CollectionAssert.AllItemsAreNotNull(collection);
820+
821+
// new assertion:
822+
collection.Should().NotContainNulls();
823+
}
824+
825+
[Test, ExpectedAssertionException]
826+
public void CollectionAssertAllItemsAreNotNull_Failure_OldAssertion()
827+
{
828+
// arrange
829+
var collection = new object[] { 1, null, true };
830+
831+
// old assertion:
832+
CollectionAssert.AllItemsAreNotNull(collection);
833+
}
834+
835+
[Test, ExpectedAssertionException]
836+
public void CollectionAssertAllItemsAreNotNull_Failure_NewAssertion()
837+
{
838+
// arrange
839+
var collection = new object[] { 1, null, true };
840+
841+
// new assertion:
842+
collection.Should().NotContainNulls();
843+
}
844+
845+
[Test]
846+
public void CollectionAssertAllItemsAreUnique()
847+
{
848+
// arrange
849+
var collection = new[] { 1, 2, 3 };
850+
851+
// old assertion:
852+
CollectionAssert.AllItemsAreUnique(collection);
853+
854+
// new assertion:
855+
collection.Should().OnlyHaveUniqueItems();
856+
}
857+
858+
[Test, ExpectedAssertionException]
859+
public void CollectionAssertAllItemsAreUnique_Failure_OldAssertion()
860+
{
861+
// arrange
862+
var collection = new[] { 1, 2, 1 };
863+
864+
// old assertion:
865+
CollectionAssert.AllItemsAreUnique(collection);
866+
}
867+
868+
[Test, ExpectedAssertionException]
869+
public void CollectionAssertAllItemsAreUnique_Failure_NewAssertion()
870+
{
871+
// arrange
872+
var collection = new[] { 1, 2, 1 };
873+
874+
// new assertion:
875+
collection.Should().OnlyHaveUniqueItems();
876+
}
811877
}

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,4 +811,70 @@ public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_
811811
// new assertion:
812812
collection.Should().AllBeOfType(type);
813813
}
814+
815+
[TestMethod]
816+
public void CollectionAssertAllItemsAreNotNull()
817+
{
818+
// arrange
819+
var collection = new object[] { 1, "test", true };
820+
821+
// old assertion:
822+
CollectionAssert.AllItemsAreNotNull(collection);
823+
824+
// new assertion:
825+
collection.Should().NotContainNulls();
826+
}
827+
828+
[TestMethod, ExpectedTestFrameworkException]
829+
public void CollectionAssertAllItemsAreNotNull_Failure_OldAssertion()
830+
{
831+
// arrange
832+
var collection = new object[] { 1, null, true };
833+
834+
// old assertion:
835+
CollectionAssert.AllItemsAreNotNull(collection);
836+
}
837+
838+
[TestMethod, ExpectedTestFrameworkException]
839+
public void CollectionAssertAllItemsAreNotNull_Failure_NewAssertion()
840+
{
841+
// arrange
842+
var collection = new object[] { 1, null, true };
843+
844+
// new assertion:
845+
collection.Should().NotContainNulls();
846+
}
847+
848+
[TestMethod]
849+
public void CollectionAssertAllItemsAreUnique()
850+
{
851+
// arrange
852+
var collection = new[] { 1, 2, 3 };
853+
854+
// old assertion:
855+
CollectionAssert.AllItemsAreUnique(collection);
856+
857+
// new assertion:
858+
collection.Should().OnlyHaveUniqueItems();
859+
}
860+
861+
[TestMethod, ExpectedTestFrameworkException]
862+
public void CollectionAssertAllItemsAreUnique_Failure_OldAssertion()
863+
{
864+
// arrange
865+
var collection = new[] { 1, 2, 1 };
866+
867+
// old assertion:
868+
CollectionAssert.AllItemsAreUnique(collection);
869+
}
870+
871+
[TestMethod, ExpectedTestFrameworkException]
872+
public void CollectionAssertAllItemsAreUnique_Failure_NewAssertion()
873+
{
874+
// arrange
875+
var collection = new[] { 1, 2, 1 };
876+
877+
// new assertion:
878+
collection.Should().OnlyHaveUniqueItems();
879+
}
814880
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,53 @@ public void Nunit4_CollectionAssertDoesNotContain_WithCasting_TestCodeFix(string
16081608
[Implemented]
16091609
public void Nunit4_CollectionAssertAllItemsAreInstancesOfType_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("IEnumerable<string> actual, Type type", oldAssertion, newAssertion);
16101610

1611+
[DataTestMethod]
1612+
[AssertionDiagnostic("CollectionAssert.AllItemsAreNotNull(actual{0});")]
1613+
[Implemented]
1614+
public void Nunit3_CollectionAssertAllItemsAreNotNull_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("IEnumerable<string> actual", assertion);
1615+
1616+
[DataTestMethod]
1617+
[AssertionDiagnostic("CollectionAssert.AllItemsAreNotNull(actual{0});")]
1618+
[Implemented]
1619+
public void Nunit4_CollectionAssertAllItemsAreNotNull_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("IEnumerable<string> actual", assertion);
1620+
1621+
[DataTestMethod]
1622+
[AssertionCodeFix(
1623+
oldAssertion: "CollectionAssert.AllItemsAreNotNull(actual{0});",
1624+
newAssertion: "actual.Should().NotContainNulls({0});")]
1625+
[Implemented]
1626+
public void Nunit3_CollectionAssertAllItemsAreNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("IEnumerable<string> actual", oldAssertion, newAssertion);
1627+
1628+
[DataTestMethod]
1629+
[AssertionCodeFix(
1630+
oldAssertion: "CollectionAssert.AllItemsAreNotNull(actual{0});",
1631+
newAssertion: "actual.Should().NotContainNulls({0});")]
1632+
[Implemented]
1633+
public void Nunit4_CollectionAssertAllItemsAreNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("IEnumerable<string> actual", oldAssertion, newAssertion);
1634+
1635+
[DataTestMethod]
1636+
[AssertionDiagnostic("CollectionAssert.AllItemsAreUnique(actual{0});")]
1637+
[Implemented]
1638+
public void Nunit3_CollectionAssertAllItemsAreUnique_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("IEnumerable<string> actual", assertion);
1639+
1640+
[DataTestMethod]
1641+
[AssertionDiagnostic("CollectionAssert.AllItemsAreUnique(actual{0});")]
1642+
[Implemented]
1643+
public void Nunit4_CollectionAssertAllItemsAreUnique_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("IEnumerable<string> actual", assertion);
1644+
1645+
[DataTestMethod]
1646+
[AssertionCodeFix(
1647+
oldAssertion: "CollectionAssert.AllItemsAreUnique(actual{0});",
1648+
newAssertion: "actual.Should().OnlyHaveUniqueItems({0});")]
1649+
[Implemented]
1650+
public void Nunit3_CollectionAssertAllItemsAreUnique_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("IEnumerable<string> actual", oldAssertion, newAssertion);
1651+
1652+
[DataTestMethod]
1653+
[AssertionCodeFix(
1654+
oldAssertion: "CollectionAssert.AllItemsAreUnique(actual{0});",
1655+
newAssertion: "actual.Should().OnlyHaveUniqueItems({0});")]
1656+
[Implemented]
1657+
public void Nunit4_CollectionAssertAllItemsAreUnique_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("IEnumerable<string> actual", oldAssertion, newAssertion);
16111658

16121659
#endregion
16131660

src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper
209209
private CreateChangedDocument TryComputeFixForCollectionAssert(IInvocationOperation invocation, CodeFixContext context, NunitCodeFixContext t)
210210
{
211211
/**
212-
public static void AllItemsAreNotNull(IEnumerable collection)
213-
public static void AllItemsAreNotNull(IEnumerable collection, string message, params object[] args)
214-
public static void AllItemsAreUnique(IEnumerable collection)
215-
public static void AllItemsAreUnique(IEnumerable collection, string message, params object[] args)
216212
public static void AreEqual(IEnumerable expected, IEnumerable actual, IComparer comparer)
217213
public static void AreEqual(IEnumerable expected, IEnumerable actual, IComparer comparer, string message, params object[] args)
218214
public static void AreEquivalent(IEnumerable expected, IEnumerable actual)
@@ -269,6 +265,10 @@ public static void IsOrdered(IEnumerable collection, IComparer comparer)
269265

270266
return DocumentEditorUtils.RenameMethodToSubjectShouldGenericAssertion(invocation, ImmutableArray.Create(typeOf.TypeOperand), context, "AllBeOfType", subjectIndex: 0, argumentsToRemove: [1]);
271267
}
268+
case "AllItemsAreNotNull": // CollectionAssert.AllItemsAreNotNull(IEnumerable collection)
269+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotContainNulls", subjectIndex: 0, argumentsToRemove: []);
270+
case "AllItemsAreUnique": // CollectionAssert.AllItemsAreUnique(IEnumerable collection)
271+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "OnlyHaveUniqueItems", subjectIndex: 0, argumentsToRemove: []);
272272
}
273273
return null;
274274
}

0 commit comments

Comments
 (0)