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 7 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 elements that are 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);

/// <summary>
/// Asserts that the current collection does not contain any elements that are assignable to the given type.
/// </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
Expand Up @@ -500,6 +500,8 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> NotContainInConsecutiveOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(params T[] unexpected) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo(System.Type type, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo<TExpectation>(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls<TKey>(System.Linq.Expressions.Expression<System.Func<T, TKey>> predicate, string because = "", params object[] becauseArgs)
where TKey : class { }
Expand Down Expand Up @@ -2799,4 +2801,4 @@ namespace FluentAssertions.Xml
public bool CanHandle(object value) { }
public void Format(object value, FluentAssertions.Formatting.FormattedObjectGraph formattedGraph, FluentAssertions.Formatting.FormattingContext context, FluentAssertions.Formatting.FormatChild formatChild) { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> NotContainInConsecutiveOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(params T[] unexpected) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo(System.Type type, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo<TExpectation>(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls<TKey>(System.Linq.Expressions.Expression<System.Func<T, TKey>> predicate, string because = "", params object[] becauseArgs)
where TKey : class { }
Expand Down Expand Up @@ -2929,4 +2931,4 @@ namespace FluentAssertions.Xml
public bool CanHandle(object value) { }
public void Format(object value, FluentAssertions.Formatting.FormattedObjectGraph formattedGraph, FluentAssertions.Formatting.FormattingContext context, FluentAssertions.Formatting.FormatChild formatChild) { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> NotContainInConsecutiveOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(params T[] unexpected) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo(System.Type type, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo<TExpectation>(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls<TKey>(System.Linq.Expressions.Expression<System.Func<T, TKey>> predicate, string because = "", params object[] becauseArgs)
where TKey : class { }
Expand Down Expand Up @@ -2801,4 +2803,4 @@ namespace FluentAssertions.Xml
public bool CanHandle(object value) { }
public void Format(object value, FluentAssertions.Formatting.FormattedObjectGraph formattedGraph, FluentAssertions.Formatting.FormattingContext context, FluentAssertions.Formatting.FormatChild formatChild) { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> NotContainInConsecutiveOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(params T[] unexpected) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo(System.Type type, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo<TExpectation>(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls<TKey>(System.Linq.Expressions.Expression<System.Func<T, TKey>> predicate, string because = "", params object[] becauseArgs)
where TKey : class { }
Expand Down Expand Up @@ -2801,4 +2803,4 @@ namespace FluentAssertions.Xml
public bool CanHandle(object value) { }
public void Format(object value, FluentAssertions.Formatting.FormattedObjectGraph formattedGraph, FluentAssertions.Formatting.FormattingContext context, FluentAssertions.Formatting.FormatChild formatChild) { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> NotContainInConsecutiveOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(params T[] unexpected) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo(System.Type type, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo<TExpectation>(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls<TKey>(System.Linq.Expressions.Expression<System.Func<T, TKey>> predicate, string because = "", params object[] becauseArgs)
where TKey : class { }
Expand Down Expand Up @@ -2750,4 +2752,4 @@ namespace FluentAssertions.Xml
public bool CanHandle(object value) { }
public void Format(object value, FluentAssertions.Formatting.FormattedObjectGraph formattedGraph, FluentAssertions.Formatting.FormattingContext context, FluentAssertions.Formatting.FormatChild formatChild) { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ namespace FluentAssertions.Collections
public FluentAssertions.AndConstraint<TAssertions> NotContainInConsecutiveOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(params T[] unexpected) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainInOrder(System.Collections.Generic.IEnumerable<T> unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo(System.Type type, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainItemsAssignableTo<TExpectation>(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotContainNulls<TKey>(System.Linq.Expressions.Expression<System.Func<T, TKey>> predicate, string because = "", params object[] becauseArgs)
where TKey : class { }
Expand Down Expand Up @@ -2801,4 +2803,4 @@ namespace FluentAssertions.Xml
public bool CanHandle(object value) { }
public void Format(object value, FluentAssertions.Formatting.FormattedObjectGraph formattedGraph, FluentAssertions.Formatting.FormattingContext context, FluentAssertions.Formatting.FormatChild formatChild) { }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using Xunit;
using Xunit.Sdk;

namespace FluentAssertions.Specs.Collections;

public partial class CollectionAssertionSpecs
{
public class NotContainItemsAssignableTo
{
[Fact]
public void Succeeds_when_the_collection_does_not_contain_items_of_the_unexpected_type()
{
// Arrange
var collection = new[] { "1", "2", "3" };

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

[Fact]
public void Throws_when_the_collection_contains_an_item_of_the_unexpected_type()
{
// Arrange
var collection = new object[] { 1, "2", "3" };

// Act
var act = () => collection
.Should()
.NotContainItemsAssignableTo<int>(
"because we want test that collection does not contain object of {0} type", typeof(int).FullName);

// Assert
act.Should()
.Throw<XunitException>()
.WithMessage(
"Expected collection to not contain any elements assignable to type \"System.Int32\" " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙃 I'm not quite fond of verifying the literal text and prefer to only assert some of the relevant parts match.

"because we want test that collection does not contain object of System.Int32 type, " +
"but found {System.Int32, System.String, System.String}.");
}

[Fact]
public void Succeeds_when_collection_is_empty()
{
// Arrange
var collection = Array.Empty<int>();

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

[Fact]
public void Throws_when_the_passed_type_argument_is_null()
{
// Arrange
var collection = new[] { 1, 2, 3 };

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

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

[Fact]
public void Throws_when_the_collection_is_null()
{
// 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 a collection does not contain any items 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