forked from fluentassertions/fluentassertions
/
NonGenericCollectionAssertions.cs
305 lines (269 loc) · 14.6 KB
/
NonGenericCollectionAssertions.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using FluentAssertions.Equivalency;
using FluentAssertions.Execution;
namespace FluentAssertions.Collections
{
/// <summary>
/// Contains a number of methods to assert that an <see cref="IEnumerable"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class NonGenericCollectionAssertions : CollectionAssertions<IEnumerable, NonGenericCollectionAssertions>
{
public NonGenericCollectionAssertions(IEnumerable collection)
{
if (collection != null)
{
Subject = collection;
}
}
/// <summary>
/// Asserts that the number of items in the collection matches the supplied <paramref name="expected" /> amount.
/// </summary>
/// <param name="expected">The expected number of items in the 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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs)
{
if (Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain {0} item(s){reason}, but found <null>.", expected);
}
int actualCount = GetMostLocalCount();
Execute.Assertion
.ForCondition(actualCount == expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain {0} item(s){reason}, but found {1}.", expected, actualCount);
return new AndConstraint<NonGenericCollectionAssertions>(this);
}
/// <summary>
/// Asserts that the number of items in the collection does not match the supplied <paramref name="unexpected" /> amount.
/// </summary>
/// <param name="unexpected">The unexpected number of items in the 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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> NotHaveCount(int unexpected, string because = "", params object[] becauseArgs)
{
if (Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to not contain {0} item(s){reason}, but found <null>.", unexpected);
}
int actualCount = GetMostLocalCount();
Execute.Assertion
.ForCondition(actualCount != unexpected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to not contain {0} item(s){reason}, but found {1}.", unexpected, actualCount);
return new AndConstraint<NonGenericCollectionAssertions>(this);
}
/// <summary>
/// Asserts that the number of items in the collection is greater than the supplied <paramref name="expected" /> amount.
/// </summary>
/// <param name="expected">The number to which the actual number items in the collection will be compared.</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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> HaveCountGreaterThan(int expected, string because = "", params object[] becauseArgs)
{
if (Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain more than {0} item(s){reason}, but found <null>.", expected);
}
int actualCount = GetMostLocalCount();
Execute.Assertion
.ForCondition(actualCount > expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain more than {0} item(s){reason}, but found {1}.", expected, actualCount);
return new AndConstraint<NonGenericCollectionAssertions>(this);
}
/// <summary>
/// Asserts that the number of items in the collection is greater or equal to the supplied <paramref name="expected" /> amount.
/// </summary>
/// <param name="expected">The number to which the actual number items in the collection will be compared.</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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> HaveCountGreaterOrEqualTo(int expected, string because = "", params object[] becauseArgs)
{
if (Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain at least {0} item(s){reason}, but found <null>.", expected);
}
int actualCount = GetMostLocalCount();
Execute.Assertion
.ForCondition(actualCount >= expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain at least {0} item(s){reason}, but found {1}.", expected, actualCount);
return new AndConstraint<NonGenericCollectionAssertions>(this);
}
/// <summary>
/// Asserts that the number of items in the collection is less than the supplied <paramref name="expected" /> amount.
/// </summary>
/// <param name="expected">The number to which the actual number items in the collection will be compared.</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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> HaveCountLessThan(int expected, string because = "", params object[] becauseArgs)
{
if (Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain fewer than {0} item(s){reason}, but found <null>.", expected);
}
int actualCount = GetMostLocalCount();
Execute.Assertion
.ForCondition(actualCount < expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain fewer than {0} item(s){reason}, but found {1}.", expected, actualCount);
return new AndConstraint<NonGenericCollectionAssertions>(this);
}
/// <summary>
/// Asserts that the number of items in the collection is less or equal to the supplied <paramref name="expected" /> amount.
/// </summary>
/// <param name="expected">The number to which the actual number items in the collection will be compared.</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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> HaveCountLessOrEqualTo(int expected, string because = "", params object[] becauseArgs)
{
if (Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain at most {0} item(s){reason}, but found <null>.", expected);
}
int actualCount = GetMostLocalCount();
Execute.Assertion
.ForCondition(actualCount <= expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain at most {0} item(s){reason}, but found {1}.", expected, actualCount);
return new AndConstraint<NonGenericCollectionAssertions>(this);
}
/// <summary>
/// Asserts that the number of items in the collection matches a condition stated by the <paramref name="countPredicate"/>.
/// </summary>
/// <param name="countPredicate">A predicate that yields the number of items that is expected to be in the 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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> HaveCount(Expression<Func<int, bool>> countPredicate, string because = "",
params object[] becauseArgs)
{
if (countPredicate == null)
{
throw new ArgumentNullException(nameof(countPredicate), "Cannot compare collection count against a <null> predicate.");
}
if (Subject is null)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} to contain {0} items{reason}, but found {1}.", countPredicate.Body, Subject);
}
Func<int, bool> compiledPredicate = countPredicate.Compile();
int actualCount = GetMostLocalCount();
if (!compiledPredicate(actualCount))
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:collection} {0} to have a count {1}{reason}, but count is {2}.",
Subject, countPredicate.Body, actualCount);
}
return new AndConstraint<NonGenericCollectionAssertions>(this);
}
private int GetMostLocalCount()
{
if (Subject is ICollection castSubject)
{
return castSubject.Count;
}
else
{
return Subject.Cast<object>().Count();
}
}
/// <summary>
/// Asserts that the current collection contains the specified <paramref name="expected"/> object. Elements are compared
/// using their <see cref="object.Equals(object)" /> implementation.
/// </summary>
/// <param name="expected">An object, or <see cref="IEnumerable"/> of objects that are expected to be in the 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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> Contain(object expected, string because = "",
params object[] becauseArgs)
{
if (expected is IEnumerable enumerable)
{
return base.Contain(enumerable, because, becauseArgs);
}
return base.Contain(new[] { expected }, because, becauseArgs);
}
/// <summary>
/// Asserts that the current collection does not contain the supplied <paramref name="unexpected" /> item.
/// Elements are compared using their <see cref="object.Equals(object)" /> implementation.
/// </summary>
/// <param name="unexpected">The element that is not expected to be in the 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 <see cref="because" />.
/// </param>
public AndConstraint<NonGenericCollectionAssertions> NotContain(object unexpected, string because = "",
params object[] becauseArgs)
{
if (unexpected is IEnumerable enumerable)
{
return base.NotContain(enumerable, because, becauseArgs);
}
return base.NotContain(new[] { unexpected }, because, becauseArgs);
}
}
}