/
ReferenceTypeAssertions.cs
364 lines (328 loc) · 17.5 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
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using FluentAssertions.Common;
using FluentAssertions.Execution;
namespace FluentAssertions.Primitives
{
/// <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>
{
/// <summary>
/// Gets the object which value is being asserted.
/// </summary>
public TSubject Subject { get; protected set; }
/// <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 <see cref="because" />.
/// </param>
public AndConstraint<TAssertions> BeNull(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(ReferenceEquals(Subject, 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 <see cref="because" />.
/// </param>
public AndConstraint<TAssertions> NotBeNull(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!ReferenceEquals(Subject, 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 explaining why the assertion should be satisfied. If the phrase does not
/// start with the word <i>because</i>, it is prepended to the message.
/// </param>
/// <param name="becauseArgs">
/// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
/// </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 explaining why the assertion should be satisfied. If the phrase does not
/// start with the word <i>because</i>, it is prepended to the message.
/// </param>
/// <param name="becauseArgs">
/// Zero or more values to use for filling in any <see cref="string.Format(string,object[])" /> compatible placeholders.
/// </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 <see cref="because" />.
/// </param>
public AndWhichConstraint<TAssertions, T> BeOfType<T>(string because = "", params object[] becauseArgs)
{
BeOfType(typeof(T), because, becauseArgs);
return new AndWhichConstraint<TAssertions, T>((TAssertions)this, (T)(object)Subject);
}
/// <summary>
/// Asserts that the object is of the specified type <paramref name="expectedType"/>.
/// </summary>
/// <param name="expectedType">
/// The type that the subject is supposed to be of.
/// </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<TAssertions> BeOfType(Type expectedType, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!ReferenceEquals(Subject, null))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} to be {0}{reason}, but found <null>.", expectedType);
Type subjectType = Subject.GetType();
if (expectedType.GetTypeInfo().IsGenericTypeDefinition && subjectType.GetTypeInfo().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 of.</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 <see cref="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 of the specified type <paramref name="expectedType"/>.
/// </summary>
/// <param name="expectedType">
/// The type that the subject is not supposed to be of.
/// </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<TAssertions> NotBeOfType(Type expectedType, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!ReferenceEquals(Subject, null))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} not to be {0}{reason}, but found <null>.", expectedType);
Type subjectType = Subject.GetType();
if (expectedType.GetTypeInfo().IsGenericTypeDefinition && subjectType.GetTypeInfo().IsGenericType)
{
subjectType.GetGenericTypeDefinition().Should().NotBe(expectedType, because, becauseArgs);
}
else
{
subjectType.Should().NotBe(expectedType, 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.</typeparam>
/// <param name="because">The reason why the object should be assignable to the type.</param>
/// <param name="becauseArgs">The parameters used when formatting the <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)
{
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());
return new AndWhichConstraint<TAssertions, T>((TAssertions)this, (T)((object)Subject));
}
/// <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.</param>
/// <param name="because">The parameters used when formatting the <paramref name="because"/>.</param>
/// <param name="becauseArgs"></param>
/// <returns>An <see cref="AndConstraint{TAssertions}"/> which can be used to chain assertions.</returns>
public AndConstraint<TAssertions> BeAssignableTo(Type type, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!ReferenceEquals(Subject, null))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} not to be {0}{reason}, but found <null>.", type);
bool isAssignable;
if (type.GetTypeInfo().IsGenericTypeDefinition)
{
isAssignable = Subject.GetType().IsAssignableToOpenGeneric(type);
}
else
{
isAssignable = 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.</typeparam>
/// <param name="because">The reason why the object should not be assignable to the type.</param>
/// <param name="becauseArgs">The parameters used when formatting the <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.</param>
/// <param name="because">The parameters used when formatting the <paramref name="because"/>.</param>
/// <param name="becauseArgs"></param>
/// <returns>An <see cref="AndConstraint{TAssertions}"/> which can be used to chain assertions.</returns>
public AndConstraint<TAssertions> NotBeAssignableTo(Type type, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!ReferenceEquals(Subject, null))
.BecauseOf(because, becauseArgs)
.WithDefaultIdentifier("type")
.FailWith("Expected {context} not to be {0}{reason}, but found <null>.", type);
bool isAssignable;
if (type.GetTypeInfo().IsGenericTypeDefinition)
{
isAssignable = Subject.GetType().IsAssignableToOpenGeneric(type);
}
else
{
isAssignable = 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">The reason why the predicate should be satisfied.</param>
/// <param name="becauseArgs">The parameters used when formatting the <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">The reason why the predicate should be satisfied.</param>
/// <param name="becauseArgs">The parameters used when formatting the <paramref name="because" />.</param>
/// <returns>An <see cref="AndConstraint{T}" /> which can be used to chain assertions.</returns>
public AndConstraint<TAssertions> Match<T>(Expression<Func<T, bool>> predicate,
string because = "",
params object[] becauseArgs)
where T : TSubject
{
if (predicate == null)
{
throw new ArgumentNullException(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.Body);
return new AndConstraint<TAssertions>((TAssertions)this);
}
/// <summary>
/// Returns the type of the subject the assertion applies on.
/// </summary>
protected abstract string Identifier { get; }
}
}