forked from fluentassertions/fluentassertions
/
CollectionAssertionSpecs.AllBeAssignableTo.cs
170 lines (140 loc) · 6.15 KB
/
CollectionAssertionSpecs.AllBeAssignableTo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using FluentAssertions.Execution;
using Xunit;
using Xunit.Sdk;
namespace FluentAssertions.Specs.Collections
{
/// <content>
/// The AllBeAssignableTo specs.
/// </content>
public partial class CollectionAssertionSpecs
{
public class AllBeAssignableTo
{
[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);
// 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;
// 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>();
}
[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);
}
[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>();
}
[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");
// 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 };
// 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.");
}
[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>();
}
[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");
// 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) };
// 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_is_null_then_all_be_assignable_toOfT_should_fail()
{
// 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>.");
}
}
}
}