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

Structure assertions with nested classes instead of regions (Part 4) #1908

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
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
}
}