-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
ReferenceTypeAssertions.cs
428 lines (381 loc) · 19.3 KB
/
ReferenceTypeAssertions.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using FluentAssertions.Common;
using FluentAssertions.Execution;
namespace FluentAssertions.Primitives;
#pragma warning disable CS0659, S1206 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CA1065 // Ignore throwing NotSupportedException from Equals
/// <summary>
/// Contains a number of methods to assert that a reference type object is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public abstract class ReferenceTypeAssertions<TSubject, TAssertions>
where TAssertions : ReferenceTypeAssertions<TSubject, TAssertions>
{
protected ReferenceTypeAssertions(TSubject subject)
{
Subject = subject;
}
/// <summary>
/// Gets the object which value is being asserted.
/// </summary>
public TSubject Subject { get; }
/// <summary>
/// Asserts that the current object has not been initialized yet.
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> BeNull(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject is null)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier(Identifier)
.FailWith("Expected {context} to be <null>{reason}, but found {0}.", Subject);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that the current object has been initialized.
/// </summary>
/// <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 <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotBeNull(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject is not null)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier(Identifier)
.FailWith("Expected {context} not to be <null>{reason}.");
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that an object reference refers to the exact same object as another object reference.
/// </summary>
/// <param name="expected">The expected object</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 <paramref name="because"/>.
/// </param>
public AndConstraint<TAssertions> BeSameAs(TSubject expected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.UsingLineBreaks
.ForCondition(ReferenceEquals(Subject, expected))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier(Identifier)
.FailWith("Expected {context} to refer to {0}{reason}, but found {1}.", expected, Subject);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that an object reference refers to a different object than another object reference refers to.
/// </summary>
/// <param name="unexpected">The unexpected object</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 <paramref name="because"/>.
/// </param>
public AndConstraint<TAssertions> NotBeSameAs(TSubject unexpected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.UsingLineBreaks
.ForCondition(!ReferenceEquals(Subject, unexpected))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier(Identifier)
.FailWith("Did not expect {context} to refer to {0}{reason}.", unexpected);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that the object is of the specified type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The expected type of the object.</typeparam>
/// <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 <paramref name="because" />.
/// </param>
public AndWhichConstraint<TAssertions, T> BeOfType<T>(string because = "", params object[] becauseArgs)
{
BeOfType(typeof(T), because, becauseArgs);
T typedSubject = Subject is T type
? type
: default;
return new AndWhichConstraint<TAssertions, T>((TAssertions)this, typedSubject);
}
/// <summary>
/// Asserts that the object is of the <paramref name="expectedType"/>.
/// </summary>
/// <param name="expectedType">
/// The type that the subject is supposed to be.
/// </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 <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expectedType"/> is <see langword="null"/>.</exception>
public AndConstraint<TAssertions> BeOfType(Type expectedType, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expectedType);
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} to be {0}{reason}, but found <null>.", expectedType);
if (success)
{
Type subjectType = Subject.GetType();
if (expectedType.IsGenericTypeDefinition && subjectType.IsGenericType)
{
subjectType.GetGenericTypeDefinition().Should().Be(expectedType, because, becauseArgs);
}
else
{
subjectType.Should().Be(expectedType, because, becauseArgs);
}
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that the object is not of the specified type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type that the subject is not supposed to be.</typeparam>
/// <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 <paramref name="because" />.
/// </param>
public AndConstraint<TAssertions> NotBeOfType<T>(string because = "", params object[] becauseArgs)
{
NotBeOfType(typeof(T), because, becauseArgs);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that the object is not the <paramref name="unexpectedType"/>.
/// </summary>
/// <param name="unexpectedType">
/// The type that the subject is not supposed to be.
/// </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 <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="unexpectedType"/> is <see langword="null"/>.</exception>
public AndConstraint<TAssertions> NotBeOfType(Type unexpectedType, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(unexpectedType);
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} not to be {0}{reason}, but found <null>.", unexpectedType);
if (success)
{
Type subjectType = Subject.GetType();
if (unexpectedType.IsGenericTypeDefinition && subjectType.IsGenericType)
{
subjectType.GetGenericTypeDefinition().Should().NotBe(unexpectedType, because, becauseArgs);
}
else
{
subjectType.Should().NotBe(unexpectedType, because, becauseArgs);
}
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that the object is assignable to a variable of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type to which the object should be assignable to.</typeparam>
/// <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 <paramref name="because" />.
/// </param>
/// <returns>An <see cref="AndWhichConstraint{TAssertions, T}"/> which can be used to chain assertions.</returns>
public AndWhichConstraint<TAssertions, T> BeAssignableTo<T>(string because = "", params object[] becauseArgs)
{
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} to be assignable to {0}{reason}, but found <null>.", typeof(T));
if (success)
{
Execute.Assertion
.ForCondition(Subject is T)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier(Identifier)
.FailWith("Expected {context} to be assignable to {0}{reason}, but {1} is not.", typeof(T), Subject.GetType());
}
T typedSubject = Subject is T type
? type
: default;
return new AndWhichConstraint<TAssertions, T>((TAssertions)this, typedSubject);
}
/// <summary>
/// Asserts that the object is assignable to a variable of given <paramref name="type"/>.
/// </summary>
/// <param name="type">The type to which the object should be assignable to.</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 <paramref name="because"/>.
/// </param>
/// <returns>An <see cref="AndConstraint{TAssertions}"/> which can be used to chain assertions.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type"/> is <see langword="null"/>.</exception>
public AndConstraint<TAssertions> BeAssignableTo(Type type, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(type);
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} to be assignable to {0}{reason}, but found <null>.", type);
if (success)
{
bool isAssignable = type.IsGenericTypeDefinition
? Subject.GetType().IsAssignableToOpenGeneric(type)
: type.IsAssignableFrom(Subject.GetType());
Execute.Assertion
.ForCondition(isAssignable)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier(Identifier)
.FailWith("Expected {context} to be assignable to {0}{reason}, but {1} is not.",
type,
Subject.GetType());
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that the object is not assignable to a variable of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type to which the object should not be assignable to.</typeparam>
/// <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 <paramref name="because" />.
/// </param>
/// <returns>An <see cref="AndConstraint{TAssertions}"/> which can be used to chain assertions.</returns>
public AndConstraint<TAssertions> NotBeAssignableTo<T>(string because = "", params object[] becauseArgs)
{
return NotBeAssignableTo(typeof(T), because, becauseArgs);
}
/// <summary>
/// Asserts that the object is not assignable to a variable of given <paramref name="type"/>.
/// </summary>
/// <param name="type">The type to which the object should not be assignable to.</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 <paramref name="because"/>.
/// </param>
/// <returns>An <see cref="AndConstraint{TAssertions}"/> which can be used to chain assertions.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type"/> is <see langword="null"/>.</exception>
public AndConstraint<TAssertions> NotBeAssignableTo(Type type, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(type);
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} to not be assignable to {0}{reason}, but found <null>.", type);
if (success)
{
bool isAssignable = type.IsGenericTypeDefinition
? Subject.GetType().IsAssignableToOpenGeneric(type)
: type.IsAssignableFrom(Subject.GetType());
Execute.Assertion
.ForCondition(!isAssignable)
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier(Identifier)
.FailWith("Expected {context} to not be assignable to {0}{reason}, but {1} is.", type, Subject.GetType());
}
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Asserts that the <paramref name="predicate" /> is satisfied.
/// </summary>
/// <param name="predicate">The predicate which must be satisfied by the <typeparamref name="TSubject" />.</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 <paramref name="because" />.
/// </param>
/// <returns>An <see cref="AndConstraint{T}" /> which can be used to chain assertions.</returns>
public AndConstraint<TAssertions> Match(Expression<Func<TSubject, bool>> predicate,
string because = "",
params object[] becauseArgs)
{
return Match<TSubject>(predicate, because, becauseArgs);
}
/// <summary>
/// Asserts that the <paramref name="predicate" /> is satisfied.
/// </summary>
/// <param name="predicate">The predicate which must be satisfied by the <typeparamref name="TSubject" />.</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 <paramref name="because" />.
/// </param>
/// <returns>An <see cref="AndConstraint{T}" /> which can be used to chain assertions.</returns>
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is <see langword="null"/>.</exception>
public AndConstraint<TAssertions> Match<T>(Expression<Func<T, bool>> predicate,
string because = "",
params object[] becauseArgs)
where T : TSubject
{
Guard.ThrowIfArgumentIsNull(predicate, nameof(predicate), "Cannot match an object against a <null> predicate.");
Execute.Assertion
.ForCondition(predicate.Compile()((T)Subject))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier(Identifier)
.FailWith("Expected {context:object} to match {1}{reason}, but found {0}.", Subject, predicate);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Returns the type of the subject the assertion applies on.
/// It should be a user-friendly name as it is included in the failure message.
/// </summary>
protected abstract string Identifier { get; }
/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean BeSameAs() instead?");
}