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

New tests for better code coverage on collection assertions #2035

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 7 additions & 6 deletions Src/FluentAssertions/Collections/GenericCollectionAssertions.cs
Expand Up @@ -263,7 +263,7 @@ public AndConstraint<TAssertions> AllBeOfType(Type expectedType, string because
.FailWith("but found a null element.")
.Then
.ForCondition(subject => subject.All(x => expectedType == GetType(x)))
.FailWith("but found {0}.", subject => $"but found [{string.Join(", ", subject.Select(x => GetType(x).FullName))}].")
.FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]")
.Then
.ClearExpectation();

Expand Down Expand Up @@ -758,7 +758,7 @@ public AndConstraint<TAssertions> Contain(IEnumerable<T> expected, string becaus
bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith("Expected {context:collection} to contain {0}{reason}, but found <null>.", expected);
.FailWith("Expected {context:collection} to contain {0}{reason}, but found <null>.", expectedObjects);

if (success)
{
Expand All @@ -770,13 +770,14 @@ public AndConstraint<TAssertions> Contain(IEnumerable<T> expected, string becaus
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} {0} to contain {1}{reason}, but could not find {2}.",
Subject, expected, missingItems);
Subject, expectedObjects, missingItems);
}
else
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} {0} to contain {1}{reason}.", Subject, expected.First());
.FailWith("Expected {context:collection} {0} to contain {1}{reason}.",
Subject, expectedObjects.Single());
}
}
}
Expand Down Expand Up @@ -2166,8 +2167,8 @@ public AndConstraint<TAssertions> NotContain(IEnumerable<T> unexpected, string b
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} {0} to not contain element {1}{reason}.", Subject,
unexpectedObjects.First());
.FailWith("Expected {context:collection} {0} to not contain {1}{reason}.",
Subject, unexpectedObjects.First());
}
}
}
Expand Down
Expand Up @@ -17,7 +17,7 @@ public class AllBeAssignableTo
public void When_the_types_in_a_collection_is_matched_against_a_null_type_it_should_throw()
{
// Arrange
var collection = new int[0];
var collection = Array.Empty<int>();

// Act
Action act = () => collection.Should().AllBeAssignableTo(null);
Expand Down Expand Up @@ -49,7 +49,17 @@ public void When_collection_is_null_then_all_be_assignable_to_should_fail()
public void When_all_of_the_types_in_a_collection_match_expected_type_it_should_succeed()
{
// Arrange
var collection = new int[] { 1, 2, 3 };
var collection = new[] { 1, 2, 3 };

// Act / Assert
collection.Should().AllBeAssignableTo(typeof(int));
}

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

// Act / Assert
collection.Should().AllBeAssignableTo<int>();
Expand All @@ -59,7 +69,7 @@ public void When_all_of_the_types_in_a_collection_match_expected_type_it_should_
public void When_matching_a_collection_against_a_type_it_should_return_the_casted_items()
{
// Arrange
var collection = new int[] { 1, 2, 3 };
var collection = new[] { 1, 2, 3 };

// Act / Assert
collection.Should().AllBeAssignableTo<int>()
Expand All @@ -70,7 +80,7 @@ public void When_matching_a_collection_against_a_type_it_should_return_the_caste
public void When_all_of_the_types_in_a_collection_match_the_type_or_subtype_it_should_succeed()
{
// Arrange
var collection = new object[] { new Exception(), new ArgumentException() };
var collection = new object[] { new Exception(), new ArgumentException("foo") };

// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
Expand All @@ -82,6 +92,20 @@ public void When_one_of_the_types_does_not_match_it_should_throw_with_a_clear_ex
// Arrange
var collection = new object[] { 1, "2", 3 };

// Act
Action act = () => collection.Should().AllBeAssignableTo(typeof(int), "because they are of different type");

// Assert
act.Should().Throw<XunitException>().WithMessage(
"Expected type to be \"System.Int32\" because they are of different type, but found \"[System.Int32, System.String, System.Int32]\".");
}

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

// Act
Action act = () => collection.Should().AllBeAssignableTo<int>("because they are of different type");

Expand All @@ -108,7 +132,7 @@ public void When_one_of_the_elements_is_null_it_should_throw_with_a_clear_explan
public void When_collection_is_of_matching_types_it_should_succeed()
{
// Arrange
var collection = new Type[] { typeof(Exception), typeof(ArgumentException) };
var collection = new[] { typeof(Exception), typeof(ArgumentException) };

// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
Expand All @@ -118,7 +142,7 @@ public void When_collection_is_of_matching_types_it_should_succeed()
public void When_collection_of_types_contains_one_type_that_does_not_match_it_should_throw_with_a_clear_explanation()
{
// Arrange
var collection = new Type[] { typeof(int), typeof(string), typeof(int) };
var collection = new[] { typeof(int), typeof(string), typeof(int) };

// Act
Action act = () => collection.Should().AllBeAssignableTo<int>("because they are of different type");
Expand All @@ -142,7 +166,7 @@ public void When_collection_of_types_and_objects_are_all_of_matching_types_it_sh
public void When_collection_of_different_types_and_objects_are_all_assignable_to_type_it_should_succeed()
{
// Arrange
var collection = new object[] { typeof(Exception), new ArgumentException() };
var collection = new object[] { typeof(Exception), new ArgumentException("foo") };

// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
Expand Down
Expand Up @@ -17,7 +17,7 @@ public class AllBeOfType
public void When_the_types_in_a_collection_is_matched_against_a_null_type_exactly_it_should_throw()
{
// Arrange
var collection = new int[0];
var collection = Array.Empty<int>();

// Act
Action act = () => collection.Should().AllBeOfType(null);
Expand Down Expand Up @@ -49,7 +49,17 @@ public void When_collection_is_null_then_all_be_of_type_should_fail()
public void When_all_of_the_types_in_a_collection_match_expected_type_exactly_it_should_succeed()
{
// Arrange
var collection = new int[] { 1, 2, 3 };
var collection = new[] { 1, 2, 3 };

// Act / Assert
collection.Should().AllBeOfType(typeof(int));
}

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

// Act / Assert
collection.Should().AllBeOfType<int>();
Expand All @@ -59,7 +69,7 @@ public void When_all_of_the_types_in_a_collection_match_expected_type_exactly_it
public void When_matching_a_collection_against_an_exact_type_it_should_return_the_casted_items()
{
// Arrange
var collection = new int[] { 1, 2, 3 };
var collection = new[] { 1, 2, 3 };

// Act / Assert
collection.Should().AllBeOfType<int>()
Expand All @@ -70,7 +80,21 @@ public void When_matching_a_collection_against_an_exact_type_it_should_return_th
public void When_one_of_the_types_does_not_match_exactly_it_should_throw_with_a_clear_explanation()
{
// Arrange
var collection = new object[] { new Exception(), new ArgumentException() };
var collection = new object[] { new Exception(), new ArgumentException("foo") };

// Act
Action act = () => collection.Should().AllBeOfType(typeof(Exception), "because they are of different type");

// Assert
act.Should().Throw<XunitException>().WithMessage(
"Expected type to be \"System.Exception\" because they are of different type, but found \"[System.Exception, System.ArgumentException]\".");
}

[Fact]
public void When_one_of_the_types_does_not_match_exactly_the_generic_type_it_should_throw_with_a_clear_explanation()
{
// Arrange
var collection = new object[] { new Exception(), new ArgumentException("foo") };

// Act
Action act = () => collection.Should().AllBeOfType<Exception>("because they are of different type");
Expand Down Expand Up @@ -98,7 +122,7 @@ public void When_one_of_the_elements_is_null_for_an_exact_match_it_should_throw_
public void When_collection_of_types_match_expected_type_exactly_it_should_succeed()
{
// Arrange
var collection = new Type[] { typeof(int), typeof(int), typeof(int) };
var collection = new[] { typeof(int), typeof(int), typeof(int) };

// Act / Assert
collection.Should().AllBeOfType<int>();
Expand All @@ -108,7 +132,7 @@ public void When_collection_of_types_match_expected_type_exactly_it_should_succe
public void When_collection_of_types_and_objects_match_type_exactly_it_should_succeed()
{
// Arrange
var collection = new object[] { typeof(ArgumentException), new ArgumentException() };
var collection = new object[] { typeof(ArgumentException), new ArgumentException("foo") };

// Act / Assert
collection.Should().AllBeOfType<ArgumentException>();
Expand All @@ -118,7 +142,7 @@ public void When_collection_of_types_and_objects_match_type_exactly_it_should_su
public void When_collection_of_types_and_objects_do_not_match_type_exactly_it_should_throw()
{
// Arrange
var collection = new object[] { typeof(Exception), new ArgumentException() };
var collection = new object[] { typeof(Exception), new ArgumentException("foo") };

// Act
Action act = () => collection.Should().AllBeOfType<ArgumentException>();
Expand Down
Expand Up @@ -79,6 +79,38 @@ public void When_a_collection_does_not_contain_another_collection_it_should_thro
"Expected collection {1, 2, 3} to contain {3, 4, 5} because we do, but could not find {4, 5}.");
}

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

// Act
Action act = () => collection.Should().Contain(new[] { 4 }, "because {0}", "we do");

// Assert
act.Should().Throw<XunitException>().WithMessage(
"Expected collection {1, 2, 3} to contain 4 because we do.");
}

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

// Act
Action act = () =>
{
using var _ = new AssertionScope();
collection.Should().Contain(new[] { 4 }).And.Contain(new[] { 5, 6 });
};

// Assert
act.Should().Throw<XunitException>().WithMessage(
"*to contain 4*to contain {5, 6}*");
}

[Fact]
public void When_the_contents_of_a_collection_are_checked_against_an_empty_collection_it_should_throw_clear_explanation()
{
Expand Down Expand Up @@ -331,6 +363,21 @@ public void When_asserting_collection_does_not_contain_item_against_null_collect
"Expected collection to not contain 1 because we want to test the behaviour with a null subject, but found <null>.");
}

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

// Act
Action act = () => collection.Should()
.NotContain(new[] { 2 }, "because we {0} like them", "don't");

// Assert
act.Should().Throw<XunitException>().WithMessage(
"Expected collection {1, 2, 3} to not contain 2 because we don't like them.");
}

[Fact]
public void When_collection_contains_unexpected_items_it_should_throw()
{
Expand All @@ -346,6 +393,37 @@ public void When_collection_contains_unexpected_items_it_should_throw()
"Expected collection {1, 2, 3} to not contain {1, 2, 4} because we don't like them, but found {1, 2}.");
}

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

// Act
Action act = () =>
{
using var _ = new AssertionScope();
collection.Should().NotContain(new[] { 1, 2 }).And.NotContain(new[] { 3 });
};

// Assert
act.Should().Throw<XunitException>().WithMessage(
"*to not contain {1, 2}*to not contain 3*");
}

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

// Act
Action act = () => collection.Should().NotContain(Array.Empty<int>());

// Assert
act.Should().Throw<ArgumentException>().WithMessage("Cannot verify*");
}

[Fact]
public void When_asserting_collection_does_not_contain_predicate_item_against_null_collection_it_should_fail()
{
Expand Down
Expand Up @@ -89,6 +89,21 @@ public void When_a_collection_does_not_contain_an_ordered_item_it_should_throw_w
"but 4 (index 0) did not appear (in the right order).");
}

[Fact]
public void When_a_collection_does_not_contain_items_with_assertion_scope_all_items_are_reported()
{
// Act
Action act = () =>
{
using var _ = new AssertionScope();
new[] { 1, 2, 3 }.Should().ContainInOrder(4).And.ContainInOrder(5);
};

// Assert
act.Should().Throw<XunitException>().WithMessage(
"*but 4 (index 0)*but 5 (index 0)*");
}

[Fact]
public void When_passing_in_null_while_checking_for_ordered_containment_it_should_throw_with_a_clear_explanation()
{
Expand Down
Expand Up @@ -59,6 +59,24 @@ public void When_collection_has_a_count_larger_than_the_minimum_it_should_not_th
collection.Should().HaveCount(c => c >= 3);
}

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

// Act
Action act = () =>
{
using var _ = new AssertionScope();
collection.Should().HaveCount(c => c > 3).And.HaveCount(c => c < 3);
};

// Assert
act.Should().Throw<XunitException>().WithMessage(
"*to have a count (c > 3)*to have a count (c < 3)*");
}

[Fact]
public void When_collection_has_a_count_that_not_matches_the_predicate_it_should_throw()
{
Expand Down