Skip to content

Commit

Permalink
Merge pull request #1908 from IT-VBFK/structure-assertions-with-neste…
Browse files Browse the repository at this point in the history
…d-classes-instead-of-regions-4

Structure assertions with nested classes instead of regions (Part 4)
  • Loading branch information
jnyrup committed Apr 23, 2022
2 parents 3634163 + 59fd88a commit 9cf8638
Show file tree
Hide file tree
Showing 38 changed files with 8,678 additions and 8,703 deletions.
Expand Up @@ -11,161 +11,160 @@ namespace FluentAssertions.Specs.Collections
/// </content>
public partial class CollectionAssertionSpecs
{
#region All Be Assignable To

[Fact]
public void When_the_types_in_a_collection_is_matched_against_a_null_type_it_should_throw()
public class AllBeAssignableTo
{
// Arrange
var collection = new int[0];
[Fact]
public void When_the_types_in_a_collection_is_matched_against_a_null_type_it_should_throw()
{
// Arrange
var collection = new int[0];

// Act
Action act = () => collection.Should().AllBeAssignableTo(null);
// Act
Action act = () => collection.Should().AllBeAssignableTo(null);

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

[Fact]
public void When_collection_is_null_then_all_be_assignable_to_should_fail()
{
// Arrange
IEnumerable<object> collection = null;
// Assert
act.Should().Throw<ArgumentNullException>()
.WithParameterName("expectedType");
}

// Act
Action act = () =>
[Fact]
public void When_collection_is_null_then_all_be_assignable_to_should_fail()
{
using var _ = new AssertionScope();
collection.Should().AllBeAssignableTo(typeof(object), "we want to test the failure {0}", "message");
};

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected type to be \"*.Object\" *failure message*, but found collection is <null>.");
}

[Fact]
public void When_all_of_the_types_in_a_collection_match_expected_type_it_should_succeed()
{
// Arrange
var collection = new int[] { 1, 2, 3 };
// Arrange
IEnumerable<object> collection = null;

// Act
Action act = () =>
{
using var _ = new AssertionScope();
collection.Should().AllBeAssignableTo(typeof(object), "we want to test the failure {0}", "message");
};

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected type to be \"*.Object\" *failure message*, but found collection is <null>.");
}

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

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

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

// Act / Assert
collection.Should().AllBeAssignableTo<int>()
.Which.Should().Equal(1, 2, 3);
}
// Act / Assert
collection.Should().AllBeAssignableTo<int>()
.Which.Should().Equal(1, 2, 3);
}

[Fact]
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() };
[Fact]
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() };

// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
}
// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
}

[Fact]
public void When_one_of_the_types_does_not_match_it_should_throw_with_a_clear_explanation()
{
// Arrange
var collection = new object[] { 1, "2", 3 };
[Fact]
public void When_one_of_the_types_does_not_match_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");
// Act
Action act = () => collection.Should().AllBeAssignableTo<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]\".");
}
// 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_elements_is_null_it_should_throw_with_a_clear_explanation()
{
// Arrange
var collection = new object[] { 1, null, 3 };
[Fact]
public void When_one_of_the_elements_is_null_it_should_throw_with_a_clear_explanation()
{
// Arrange
var collection = new object[] { 1, null, 3 };

// Act
Action act = () => collection.Should().AllBeAssignableTo<int>("because they are of different type");
// Act
Action act = () => collection.Should().AllBeAssignableTo<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 a null element.");
}
// Assert
act.Should().Throw<XunitException>().WithMessage(
"Expected type to be \"System.Int32\" because they are of different type, but found a null element.");
}

[Fact]
public void When_collection_is_of_matching_types_it_should_succeed()
{
// Arrange
var collection = new Type[] { typeof(Exception), typeof(ArgumentException) };
[Fact]
public void When_collection_is_of_matching_types_it_should_succeed()
{
// Arrange
var collection = new Type[] { typeof(Exception), typeof(ArgumentException) };

// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
}
// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
}

[Fact]
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) };
[Fact]
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) };

// Act
Action act = () => collection.Should().AllBeAssignableTo<int>("because they are of different type");
// Act
Action act = () => collection.Should().AllBeAssignableTo<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]\".");
}
// 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_collection_of_types_and_objects_are_all_of_matching_types_it_should_succeed()
{
// Arrange
var collection = new object[] { typeof(int), 2, typeof(int) };
[Fact]
public void When_collection_of_types_and_objects_are_all_of_matching_types_it_should_succeed()
{
// Arrange
var collection = new object[] { typeof(int), 2, typeof(int) };

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

[Fact]
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() };

// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
}
[Fact]
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() };

[Fact]
public void When_collection_is_null_then_all_be_assignable_toOfT_should_fail()
{
// Arrange
IEnumerable<object> collection = null;
// Act / Assert
collection.Should().AllBeAssignableTo<Exception>();
}

// Act
Action act = () =>
[Fact]
public void When_collection_is_null_then_all_be_assignable_toOfT_should_fail()
{
using var _ = new AssertionScope();
collection.Should().AllBeAssignableTo<object>("we want to test the failure {0}", "message");
};

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected type to be \"*.Object\" *failure message*, but found collection is <null>.");
// Arrange
IEnumerable<object> collection = null;

// Act
Action act = () =>
{
using var _ = new AssertionScope();
collection.Should().AllBeAssignableTo<object>("we want to test the failure {0}", "message");
};

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected type to be \"*.Object\" *failure message*, but found collection is <null>.");
}
}

#endregion
}
}

0 comments on commit 9cf8638

Please sign in to comment.