Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NotContainItemsAssignableTo #2266

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions Src/FluentAssertions/Collections/GenericCollectionAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,52 @@ public AndConstraint<TAssertions> ContainItemsAssignableTo<TExpectation>(string
return new AndConstraint<TAssertions>((TAssertions)this);
}

/// <summary>
/// Asserts that the current collection does not contain any element that is assignable to the type <typeparamref name="TExpectation" />.
/// </summary>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotContainItemsAssignableTo<TExpectation>(string because = "", params object[] becauseArgs) =>
NotContainItemsAssignableTo(typeof(TExpectation), because, becauseArgs);
Leo506 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Asserts that the current collection does not contain any element that is assignable to the given type.
Leo506 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="type">
/// Object type that should not be in collection
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotContainItemsAssignableTo(Type type, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(type);

Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:collection} to not contain any elements assignable to type {0}{reason}, ",
type.FullName)
.ForCondition(Subject is not null)
.FailWith("but found <null>.")
.Then
.Given(() => Subject.ConvertOrCastToCollection())
.ForCondition(subject => subject.All(x => !type.IsAssignableFrom(GetType(x))))
.FailWith("but found {0}.", subject => subject.Select(x => GetType(x)))
.Then
.ClearExpectation();

return new AndConstraint<TAssertions>((TAssertions)this);
}

/// <summary>
/// Expects the current collection to contain only a single item.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using Xunit;
using Xunit.Sdk;

namespace FluentAssertions.Specs.Collections;

public partial class CollectionAssertionSpecs
{
public class NotContainItemsAssignableTo
{
[Fact]
public void Should_succeed_when_asserting_collection_not_contains_items_assignable_to_type()
Leo506 marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
var collection = new[] { "1", "2", "3" };

// Act / Assert
collection.Should().NotContainItemsAssignableTo<int>();
}

[Fact]
public void Should_throw_when_asserting_collection_contains_item_assignable_to_type()
Leo506 marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
var collection = new object[] { 1, "2", "3" };

// Act
var act = () => collection.Should().NotContainItemsAssignableTo<int>();

// Assert
act.Should()
.Throw<XunitException>()
.WithMessage(
"Expected collection to not contain any elements assignable to type \"System.Int32\", but found {System.Int32, System.String, System.String}.");
Leo506 marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void Should_throw_when_passed_type_argument_is_null()
Leo506 marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
var collection = new[] { 1, 2, 3 };

// Act
var act = () => collection.Should().NotContainItemsAssignableTo(null);

// Assert
act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void Should_throw_when_collection_is_null()
Leo506 marked this conversation as resolved.
Show resolved Hide resolved
Leo506 marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
int[] collection = null;

// Act
var act = () => collection.Should().NotContainItemsAssignableTo<int>();

// Assert
act.Should()
.Throw<XunitException>()
.WithMessage(
"Expected collection to not contain any elements assignable to type \"System.Int32\", but found <null>.");
}
}
}
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sidebar:
### What's new
* Added `Be`, `NotBe` and `BeOneOf` for object comparisons with custom comparer - [#2111](https://github.com/fluentassertions/fluentassertions/pull/2111)
* Added `BeSignedWithPublicKey()` and `BeUnsigned()` for assertions on `Assembly` - [#2207](https://github.com/fluentassertions/fluentassertions/pull/2207)
* Added `NotContainItemsAssignableTo` for asserting that collection does not contain any item assignable to specific type - [#2266](https://github.com/fluentassertions/fluentassertions/pull/2266)
Leo506 marked this conversation as resolved.
Show resolved Hide resolved

### Fixes
* `because` and `becauseArgs` were not included in the error message when collections of enums were not equivalent - [#2214](https://github.com/fluentassertions/fluentassertions/pull/2214)
Expand Down