Skip to content

Commit db3aeac

Browse files
authored
feat: add nunit collection assert AllItemsAreInstancesOfType (#349)
1 parent 9d7eefb commit db3aeac

File tree

7 files changed

+392
-13
lines changed

7 files changed

+392
-13
lines changed

docs/Nunit3Analyzer.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
1818
- [CollectionAssertContains_WithCasting](#scenario-collectionassertcontains_withcasting) - `collection.Should().Contain((int)item);`
1919
- [CollectionAssertDoesNotContain](#scenario-collectionassertdoesnotcontain) - `collection.Should().NotContain(4);`
2020
- [CollectionAssertDoesNotContain_WithCasting](#scenario-collectionassertdoesnotcontain_withcasting) - `collection.Should().NotContain((int)item);`
21+
- [CollectionAssertAllItemsAreInstancesOfType](#scenario-collectionassertallitemsareinstancesoftype) - `collection.Should().AllBeOfType<int>();`
22+
- [CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument](#scenario-collectionassertallitemsareinstancesoftype_withtypeargument) - `collection.Should().AllBeOfType(type);`
2123

2224

2325
## Scenarios
@@ -478,4 +480,62 @@ CollectionAssert.DoesNotContain(collection, item); /* fail message: Expected:
478480
collection.Should().NotContain((int)item); /* fail message: Expected collection {1, 2, 3} to not contain 2. */
479481
```
480482

483+
### scenario: CollectionAssertAllItemsAreInstancesOfType
484+
485+
```cs
486+
// arrange
487+
var collection = new object[] { 1, 2, 3 };
488+
489+
// old assertion:
490+
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));
491+
492+
// new assertion:
493+
collection.Should().AllBeOfType<int>();
494+
```
495+
496+
#### Failure messages
497+
498+
```cs
499+
var collection = new object[] { 1, 2, "3" };
500+
501+
// old assertion:
502+
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int)); /* fail message: Expected: all items instance of <System.Int32>
503+
But was: < 1, 2, "3" >
504+
First non-matching item at index [2]: "3"
505+
*/
506+
507+
// new assertion:
508+
collection.Should().AllBeOfType<int>(); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
509+
```
510+
511+
### scenario: CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument
512+
513+
```cs
514+
// arrange
515+
var collection = new object[] { 1, 2, 3 };
516+
var type = typeof(int);
517+
518+
// old assertion:
519+
CollectionAssert.AllItemsAreInstancesOfType(collection, type);
520+
521+
// new assertion:
522+
collection.Should().AllBeOfType(type);
523+
```
524+
525+
#### Failure messages
526+
527+
```cs
528+
var collection = new object[] { 1, 2, "3" };
529+
var type = typeof(int);
530+
531+
// old assertion:
532+
CollectionAssert.AllItemsAreInstancesOfType(collection, type); /* fail message: Expected: all items instance of <System.Int32>
533+
But was: < 1, 2, "3" >
534+
First non-matching item at index [2]: "3"
535+
*/
536+
537+
// new assertion:
538+
collection.Should().AllBeOfType(type); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
539+
```
540+
481541

docs/Nunit4Analyzer.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
1818
- [CollectionAssertContains_WithCasting](#scenario-collectionassertcontains_withcasting) - `collection.Should().Contain((int)item);`
1919
- [CollectionAssertDoesNotContain](#scenario-collectionassertdoesnotcontain) - `collection.Should().NotContain(4);`
2020
- [CollectionAssertDoesNotContain_WithCasting](#scenario-collectionassertdoesnotcontain_withcasting) - `collection.Should().NotContain((int)item);`
21+
- [CollectionAssertAllItemsAreInstancesOfType](#scenario-collectionassertallitemsareinstancesoftype) - `collection.Should().AllBeOfType<int>();`
22+
- [CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument](#scenario-collectionassertallitemsareinstancesoftype_withtypeargument) - `collection.Should().AllBeOfType(type);`
2123

2224

2325
## Scenarios
@@ -509,4 +511,64 @@ CollectionAssert.DoesNotContain(collection, item); /* fail message: Assert.Tha
509511
collection.Should().NotContain((int)item); /* fail message: Expected collection {1, 2, 3} to not contain 2. */
510512
```
511513

514+
### scenario: CollectionAssertAllItemsAreInstancesOfType
515+
516+
```cs
517+
// arrange
518+
var collection = new object[] { 1, 2, 3 };
519+
520+
// old assertion:
521+
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));
522+
523+
// new assertion:
524+
collection.Should().AllBeOfType<int>();
525+
```
526+
527+
#### Failure messages
528+
529+
```cs
530+
var collection = new object[] { 1, 2, "3" };
531+
532+
// old assertion:
533+
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int)); /* fail message: Assert.That(collection, Is.All.InstanceOf(expectedType))
534+
Expected: all items instance of <System.Int32>
535+
But was: < 1, 2, "3" >
536+
First non-matching item at index [2]: "3"
537+
*/
538+
539+
// new assertion:
540+
collection.Should().AllBeOfType<int>(); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
541+
```
542+
543+
### scenario: CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument
544+
545+
```cs
546+
// arrange
547+
var collection = new object[] { 1, 2, 3 };
548+
var type = typeof(int);
549+
550+
// old assertion:
551+
CollectionAssert.AllItemsAreInstancesOfType(collection, type);
552+
553+
// new assertion:
554+
collection.Should().AllBeOfType(type);
555+
```
556+
557+
#### Failure messages
558+
559+
```cs
560+
var collection = new object[] { 1, 2, "3" };
561+
var type = typeof(int);
562+
563+
// old assertion:
564+
CollectionAssert.AllItemsAreInstancesOfType(collection, type); /* fail message: Assert.That(collection, Is.All.InstanceOf(expectedType))
565+
Expected: all items instance of <System.Int32>
566+
But was: < 1, 2, "3" >
567+
First non-matching item at index [2]: "3"
568+
*/
569+
570+
// new assertion:
571+
collection.Should().AllBeOfType(type); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
572+
```
573+
512574

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,4 +667,73 @@ public void CollectionAssertDoesNotContain_WithCasting_Failure_NewAssertion()
667667
// new assertion:
668668
collection.Should().NotContain((int)item);
669669
}
670+
671+
[Test]
672+
public void CollectionAssertAllItemsAreInstancesOfType()
673+
{
674+
// arrange
675+
var collection = new object[] { 1, 2, 3 };
676+
677+
// old assertion:
678+
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));
679+
680+
// new assertion:
681+
collection.Should().AllBeOfType<int>();
682+
}
683+
684+
[Test, ExpectedAssertionException]
685+
public void CollectionAssertAllItemsAreInstancesOfType_Failure_OldAssertion()
686+
{
687+
// arrange
688+
var collection = new object[] { 1, 2, "3" };
689+
690+
// old assertion:
691+
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));
692+
}
693+
694+
[Test, ExpectedAssertionException]
695+
public void CollectionAssertAllItemsAreInstancesOfType_Failure_NewAssertion()
696+
{
697+
// arrange
698+
var collection = new object[] { 1, 2, "3" };
699+
700+
// new assertion:
701+
collection.Should().AllBeOfType<int>();
702+
}
703+
704+
[Test]
705+
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument()
706+
{
707+
// arrange
708+
var collection = new object[] { 1, 2, 3 };
709+
var type = typeof(int);
710+
711+
// old assertion:
712+
CollectionAssert.AllItemsAreInstancesOfType(collection, type);
713+
714+
// new assertion:
715+
collection.Should().AllBeOfType(type);
716+
}
717+
718+
[Test, ExpectedAssertionException]
719+
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_OldAssertion()
720+
{
721+
// arrange
722+
var collection = new object[] { 1, 2, "3" };
723+
var type = typeof(int);
724+
725+
// old assertion:
726+
CollectionAssert.AllItemsAreInstancesOfType(collection, type);
727+
}
728+
729+
[Test, ExpectedAssertionException]
730+
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_NewAssertion()
731+
{
732+
// arrange
733+
var collection = new object[] { 1, 2, "3" };
734+
var type = typeof(int);
735+
736+
// new assertion:
737+
collection.Should().AllBeOfType(type);
738+
}
670739
}

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,4 +670,73 @@ public void CollectionAssertDoesNotContain_WithCasting_Failure_NewAssertion()
670670
// new assertion:
671671
collection.Should().NotContain((int)item);
672672
}
673+
674+
[TestMethod]
675+
public void CollectionAssertAllItemsAreInstancesOfType()
676+
{
677+
// arrange
678+
var collection = new object[] { 1, 2, 3 };
679+
680+
// old assertion:
681+
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));
682+
683+
// new assertion:
684+
collection.Should().AllBeOfType<int>();
685+
}
686+
687+
[TestMethod, ExpectedTestFrameworkException]
688+
public void CollectionAssertAllItemsAreInstancesOfType_Failure_OldAssertion()
689+
{
690+
// arrange
691+
var collection = new object[] { 1, 2, "3" };
692+
693+
// old assertion:
694+
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));
695+
}
696+
697+
[TestMethod, ExpectedTestFrameworkException]
698+
public void CollectionAssertAllItemsAreInstancesOfType_Failure_NewAssertion()
699+
{
700+
// arrange
701+
var collection = new object[] { 1, 2, "3" };
702+
703+
// new assertion:
704+
collection.Should().AllBeOfType<int>();
705+
}
706+
707+
[TestMethod]
708+
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument()
709+
{
710+
// arrange
711+
var collection = new object[] { 1, 2, 3 };
712+
var type = typeof(int);
713+
714+
// old assertion:
715+
CollectionAssert.AllItemsAreInstancesOfType(collection, type);
716+
717+
// new assertion:
718+
collection.Should().AllBeOfType(type);
719+
}
720+
721+
[TestMethod, ExpectedTestFrameworkException]
722+
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_OldAssertion()
723+
{
724+
// arrange
725+
var collection = new object[] { 1, 2, "3" };
726+
var type = typeof(int);
727+
728+
// old assertion:
729+
CollectionAssert.AllItemsAreInstancesOfType(collection, type);
730+
}
731+
732+
[TestMethod, ExpectedTestFrameworkException]
733+
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_NewAssertion()
734+
{
735+
// arrange
736+
var collection = new object[] { 1, 2, "3" };
737+
var type = typeof(int);
738+
739+
// new assertion:
740+
collection.Should().AllBeOfType(type);
741+
}
673742
}

0 commit comments

Comments
 (0)