Skip to content

Commit

Permalink
feat: add nunit collection assert AllItemsAreInstancesOfType (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 authored May 20, 2024
1 parent 9d7eefb commit db3aeac
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 13 deletions.
60 changes: 60 additions & 0 deletions docs/Nunit3Analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
- [CollectionAssertContains_WithCasting](#scenario-collectionassertcontains_withcasting) - `collection.Should().Contain((int)item);`
- [CollectionAssertDoesNotContain](#scenario-collectionassertdoesnotcontain) - `collection.Should().NotContain(4);`
- [CollectionAssertDoesNotContain_WithCasting](#scenario-collectionassertdoesnotcontain_withcasting) - `collection.Should().NotContain((int)item);`
- [CollectionAssertAllItemsAreInstancesOfType](#scenario-collectionassertallitemsareinstancesoftype) - `collection.Should().AllBeOfType<int>();`
- [CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument](#scenario-collectionassertallitemsareinstancesoftype_withtypeargument) - `collection.Should().AllBeOfType(type);`


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

### scenario: CollectionAssertAllItemsAreInstancesOfType

```cs
// arrange
var collection = new object[] { 1, 2, 3 };

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));

// new assertion:
collection.Should().AllBeOfType<int>();
```

#### Failure messages

```cs
var collection = new object[] { 1, 2, "3" };

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int)); /* fail message: Expected: all items instance of <System.Int32>
But was: < 1, 2, "3" >
First non-matching item at index [2]: "3"
*/

// new assertion:
collection.Should().AllBeOfType<int>(); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
```

### scenario: CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument

```cs
// arrange
var collection = new object[] { 1, 2, 3 };
var type = typeof(int);

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, type);

// new assertion:
collection.Should().AllBeOfType(type);
```

#### Failure messages

```cs
var collection = new object[] { 1, 2, "3" };
var type = typeof(int);

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, type); /* fail message: Expected: all items instance of <System.Int32>
But was: < 1, 2, "3" >
First non-matching item at index [2]: "3"
*/

// new assertion:
collection.Should().AllBeOfType(type); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
```


62 changes: 62 additions & 0 deletions docs/Nunit4Analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser
- [CollectionAssertContains_WithCasting](#scenario-collectionassertcontains_withcasting) - `collection.Should().Contain((int)item);`
- [CollectionAssertDoesNotContain](#scenario-collectionassertdoesnotcontain) - `collection.Should().NotContain(4);`
- [CollectionAssertDoesNotContain_WithCasting](#scenario-collectionassertdoesnotcontain_withcasting) - `collection.Should().NotContain((int)item);`
- [CollectionAssertAllItemsAreInstancesOfType](#scenario-collectionassertallitemsareinstancesoftype) - `collection.Should().AllBeOfType<int>();`
- [CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument](#scenario-collectionassertallitemsareinstancesoftype_withtypeargument) - `collection.Should().AllBeOfType(type);`


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

### scenario: CollectionAssertAllItemsAreInstancesOfType

```cs
// arrange
var collection = new object[] { 1, 2, 3 };

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));

// new assertion:
collection.Should().AllBeOfType<int>();
```

#### Failure messages

```cs
var collection = new object[] { 1, 2, "3" };

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int)); /* fail message: Assert.That(collection, Is.All.InstanceOf(expectedType))
Expected: all items instance of <System.Int32>
But was: < 1, 2, "3" >
First non-matching item at index [2]: "3"
*/

// new assertion:
collection.Should().AllBeOfType<int>(); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
```

### scenario: CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument

```cs
// arrange
var collection = new object[] { 1, 2, 3 };
var type = typeof(int);

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, type);

// new assertion:
collection.Should().AllBeOfType(type);
```

#### Failure messages

```cs
var collection = new object[] { 1, 2, "3" };
var type = typeof(int);

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, type); /* fail message: Assert.That(collection, Is.All.InstanceOf(expectedType))
Expected: all items instance of <System.Int32>
But was: < 1, 2, "3" >
First non-matching item at index [2]: "3"
*/

// new assertion:
collection.Should().AllBeOfType(type); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */
```


Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,73 @@ public void CollectionAssertDoesNotContain_WithCasting_Failure_NewAssertion()
// new assertion:
collection.Should().NotContain((int)item);
}

[Test]
public void CollectionAssertAllItemsAreInstancesOfType()
{
// arrange
var collection = new object[] { 1, 2, 3 };

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));

// new assertion:
collection.Should().AllBeOfType<int>();
}

[Test, ExpectedAssertionException]
public void CollectionAssertAllItemsAreInstancesOfType_Failure_OldAssertion()
{
// arrange
var collection = new object[] { 1, 2, "3" };

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));
}

[Test, ExpectedAssertionException]
public void CollectionAssertAllItemsAreInstancesOfType_Failure_NewAssertion()
{
// arrange
var collection = new object[] { 1, 2, "3" };

// new assertion:
collection.Should().AllBeOfType<int>();
}

[Test]
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument()
{
// arrange
var collection = new object[] { 1, 2, 3 };
var type = typeof(int);

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, type);

// new assertion:
collection.Should().AllBeOfType(type);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_OldAssertion()
{
// arrange
var collection = new object[] { 1, 2, "3" };
var type = typeof(int);

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, type);
}

[Test, ExpectedAssertionException]
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_NewAssertion()
{
// arrange
var collection = new object[] { 1, 2, "3" };
var type = typeof(int);

// new assertion:
collection.Should().AllBeOfType(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,73 @@ public void CollectionAssertDoesNotContain_WithCasting_Failure_NewAssertion()
// new assertion:
collection.Should().NotContain((int)item);
}

[TestMethod]
public void CollectionAssertAllItemsAreInstancesOfType()
{
// arrange
var collection = new object[] { 1, 2, 3 };

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));

// new assertion:
collection.Should().AllBeOfType<int>();
}

[TestMethod, ExpectedTestFrameworkException]
public void CollectionAssertAllItemsAreInstancesOfType_Failure_OldAssertion()
{
// arrange
var collection = new object[] { 1, 2, "3" };

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, typeof(int));
}

[TestMethod, ExpectedTestFrameworkException]
public void CollectionAssertAllItemsAreInstancesOfType_Failure_NewAssertion()
{
// arrange
var collection = new object[] { 1, 2, "3" };

// new assertion:
collection.Should().AllBeOfType<int>();
}

[TestMethod]
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument()
{
// arrange
var collection = new object[] { 1, 2, 3 };
var type = typeof(int);

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, type);

// new assertion:
collection.Should().AllBeOfType(type);
}

[TestMethod, ExpectedTestFrameworkException]
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_OldAssertion()
{
// arrange
var collection = new object[] { 1, 2, "3" };
var type = typeof(int);

// old assertion:
CollectionAssert.AllItemsAreInstancesOfType(collection, type);
}

[TestMethod, ExpectedTestFrameworkException]
public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_NewAssertion()
{
// arrange
var collection = new object[] { 1, 2, "3" };
var type = typeof(int);

// new assertion:
collection.Should().AllBeOfType(type);
}
}
Loading

0 comments on commit db3aeac

Please sign in to comment.