forked from fluentassertions/fluentassertions
/
AssertionExtensions.cs
686 lines (620 loc) · 25.4 KB
/
AssertionExtensions.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml.Linq;
using FluentAssertions.Collections;
using FluentAssertions.Events;
using FluentAssertions.Numeric;
using FluentAssertions.Primitives;
using FluentAssertions.Reflection;
using FluentAssertions.Specialized;
using FluentAssertions.Types;
using FluentAssertions.Xml;
using JetBrains.Annotations;
namespace FluentAssertions
{
/// <summary>
/// Contains extension methods for custom assertions in unit tests.
/// </summary>
[DebuggerNonUserCode]
public static partial class AssertionExtensions
{
/// <summary>
/// Invokes the specified action on an subject so that you can chain it with any of the ShouldThrow or ShouldNotThrow
/// overloads.
/// </summary>
[Pure]
public static Action Invoking<T>(this T subject, Action<T> action)
{
return () => action(subject);
}
[Pure]
public static Func<Task> Awaiting<T>(this T subject, Func<T, Task> action)
{
return () => action(subject);
}
/// <summary>
/// Provides methods for asserting the execution time of a method or property.
/// </summary>
/// <param name="subject">The object that exposes the method or property.</param>
/// <param name="action">A reference to the method or property to measure the execution time of.</param>
/// <returns>
/// Returns an object for asserting that the execution time matches certain conditions.
/// </returns>
[MustUseReturnValue /* do not use Pure because this method executes the action before returning to the caller */]
public static MemberExecutionTime<T> ExecutionTimeOf<T>(this T subject, Expression<Action<T>> action)
{
return new MemberExecutionTime<T>(subject, action);
}
/// <summary>
/// Provides methods for asserting the execution time of an action.
/// </summary>
/// <param name="action">An action to measure the execution time of.</param>
/// <returns>
/// Returns an object for asserting that the execution time matches certain conditions.
/// </returns>
[MustUseReturnValue /* do not use Pure because this method executes the action before returning to the caller */]
public static ExecutionTime ExecutionTime(this Action action)
{
return new ExecutionTime(action);
}
/// <summary>
/// Provides methods for asserting the execution time of an async action.
/// </summary>
/// <param name="action">An async action to measure the execution time of.</param>
/// <returns>
/// Returns an object for asserting that the execution time matches certain conditions.
/// </returns>
[MustUseReturnValue /* do not use Pure because this method executes the action before returning to the caller */]
public static ExecutionTime ExecutionTime(this Func<Task> action)
{
return new ExecutionTime(action);
}
/// <summary>
/// Returns an <see cref="ExecutionTimeAssertions"/> object that can be used to assert the
/// current <see cref="ExecutionTime"/>.
/// </summary>
[Pure]
public static ExecutionTimeAssertions Should(this ExecutionTime executionTime)
{
return new ExecutionTimeAssertions(executionTime);
}
/// <summary>
/// Returns an <see cref="AssemblyAssertions"/> object that can be used to assert the
/// current <see cref="Assembly"/>.
/// </summary>
[Pure]
public static AssemblyAssertions Should(this Assembly assembly)
{
return new AssemblyAssertions(assembly);
}
/// <summary>
/// Returns an <see cref="XDocumentAssertions"/> object that can be used to assert the
/// current <see cref="XElement"/>.
/// </summary>
[Pure]
public static XDocumentAssertions Should(this XDocument actualValue)
{
return new XDocumentAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="XElementAssertions"/> object that can be used to assert the
/// current <see cref="XElement"/>.
/// </summary>
[Pure]
public static XElementAssertions Should(this XElement actualValue)
{
return new XElementAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="XAttributeAssertions"/> object that can be used to assert the
/// current <see cref="XAttribute"/>.
/// </summary>
[Pure]
public static XAttributeAssertions Should(this XAttribute actualValue)
{
return new XAttributeAssertions(actualValue);
}
/// <summary>
/// Forces enumerating a collection. Should be used to assert that a method that uses the
/// <c>yield</c> keyword throws a particular exception.
/// </summary>
[Pure]
public static Action Enumerating(this Func<IEnumerable> enumerable)
{
return () => ForceEnumeration(enumerable);
}
/// <summary>
/// Forces enumerating a collection. Should be used to assert that a method that uses the
/// <c>yield</c> keyword throws a particular exception.
/// </summary>
[Pure]
public static Action Enumerating<T>(this Func<IEnumerable<T>> enumerable)
{
return () => ForceEnumeration(enumerable);
}
private static void ForceEnumeration(Func<IEnumerable> enumerable)
{
foreach (object _ in enumerable())
{
// Do nothing
}
}
/// <summary>
/// Returns an <see cref="ObjectAssertions"/> object that can be used to assert the
/// current <see cref="object"/>.
/// </summary>
[Pure]
public static ObjectAssertions Should(this object actualValue)
{
return new ObjectAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="BooleanAssertions"/> object that can be used to assert the
/// current <see cref="bool"/>.
/// </summary>
[Pure]
public static BooleanAssertions Should(this bool actualValue)
{
return new BooleanAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableBooleanAssertions"/> object that can be used to assert the
/// current nullable <see cref="bool"/>.
/// </summary>
[Pure]
public static NullableBooleanAssertions Should(this bool? actualValue)
{
return new NullableBooleanAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="GuidAssertions"/> object that can be used to assert the
/// current <see cref="Guid"/>.
/// </summary>
[Pure]
public static GuidAssertions Should(this Guid actualValue)
{
return new GuidAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableGuidAssertions"/> object that can be used to assert the
/// current nullable <see cref="Guid"/>.
/// </summary>
[Pure]
public static NullableGuidAssertions Should(this Guid? actualValue)
{
return new NullableGuidAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="NonGenericCollectionAssertions"/> object that can be used to assert the
/// current <see cref="IEnumerable"/>.
/// </summary>
[Pure]
public static NonGenericCollectionAssertions Should(this IEnumerable actualValue)
{
return new NonGenericCollectionAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="GenericCollectionAssertions{T}"/> object that can be used to assert the
/// current <see cref="IEnumerable{T}"/>.
/// </summary>
[Pure]
public static GenericCollectionAssertions<T> Should<T>(this IEnumerable<T> actualValue)
{
return new GenericCollectionAssertions<T>(actualValue);
}
/// <summary>
/// Returns an <see cref="StringCollectionAssertions"/> object that can be used to assert the
/// current <see cref="IEnumerable{T}"/>.
/// </summary>
[Pure]
public static StringCollectionAssertions Should(this IEnumerable<string> @this)
{
return new StringCollectionAssertions(@this);
}
/// <summary>
/// Returns an <see cref="GenericDictionaryAssertions{TKey, TValue}"/> object that can be used to assert the
/// current <see cref="IDictionary{TKey, TValue}"/>.
/// </summary>
[Pure]
public static GenericDictionaryAssertions<TKey, TValue> Should<TKey, TValue>(this IDictionary<TKey, TValue> actualValue)
{
return new GenericDictionaryAssertions<TKey, TValue>(actualValue);
}
/// <summary>
/// Returns an <see cref="DateTimeAssertions"/> object that can be used to assert the
/// current <see cref="DateTime"/>.
/// </summary>
[Pure]
public static DateTimeAssertions Should(this DateTime actualValue)
{
return new DateTimeAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="DateTimeOffsetAssertions"/> object that can be used to assert the
/// current <see cref="DateTimeOffset"/>.
/// </summary>
[Pure]
public static DateTimeOffsetAssertions Should(this DateTimeOffset actualValue)
{
return new DateTimeOffsetAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableDateTimeAssertions"/> object that can be used to assert the
/// current nullable <see cref="DateTime"/>.
/// </summary>
[Pure]
public static NullableDateTimeAssertions Should(this DateTime? actualValue)
{
return new NullableDateTimeAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableDateTimeOffsetAssertions"/> object that can be used to assert the
/// current nullable <see cref="DateTimeOffset"/>.
/// </summary>
[Pure]
public static NullableDateTimeOffsetAssertions Should(this DateTimeOffset? actualValue)
{
return new NullableDateTimeOffsetAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="ComparableTypeAssertions{T}"/> object that can be used to assert the
/// current <see cref="IComparable{T}"/>.
/// </summary>
[Pure]
public static ComparableTypeAssertions<T> Should<T>(this IComparable<T> comparableValue)
{
return new ComparableTypeAssertions<T>(comparableValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="int"/>.
/// </summary>
[Pure]
public static NumericAssertions<int> Should(this int actualValue)
{
return new NumericAssertions<int>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="int"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<int> Should(this int? actualValue)
{
return new NullableNumericAssertions<int>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="uint"/>.
/// </summary>
[Pure]
public static NumericAssertions<uint> Should(this uint actualValue)
{
return new NumericAssertions<uint>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="uint"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<uint> Should(this uint? actualValue)
{
return new NullableNumericAssertions<uint>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="decimal"/>.
/// </summary>
[Pure]
public static NumericAssertions<decimal> Should(this decimal actualValue)
{
return new NumericAssertions<decimal>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="decimal"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<decimal> Should(this decimal? actualValue)
{
return new NullableNumericAssertions<decimal>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="byte"/>.
/// </summary>
[Pure]
public static NumericAssertions<byte> Should(this byte actualValue)
{
return new NumericAssertions<byte>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="byte"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<byte> Should(this byte? actualValue)
{
return new NullableNumericAssertions<byte>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="sbyte"/>.
/// </summary>
[Pure]
public static NumericAssertions<sbyte> Should(this sbyte actualValue)
{
return new NumericAssertions<sbyte>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="sbyte"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<sbyte> Should(this sbyte? actualValue)
{
return new NullableNumericAssertions<sbyte>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="short"/>.
/// </summary>
[Pure]
public static NumericAssertions<short> Should(this short actualValue)
{
return new NumericAssertions<short>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="short"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<short> Should(this short? actualValue)
{
return new NullableNumericAssertions<short>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="ushort"/>.
/// </summary>
[Pure]
public static NumericAssertions<ushort> Should(this ushort actualValue)
{
return new NumericAssertions<ushort>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="ushort"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<ushort> Should(this ushort? actualValue)
{
return new NullableNumericAssertions<ushort>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="long"/>.
/// </summary>
[Pure]
public static NumericAssertions<long> Should(this long actualValue)
{
return new NumericAssertions<long>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="long"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<long> Should(this long? actualValue)
{
return new NullableNumericAssertions<long>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="ulong"/>.
/// </summary>
[Pure]
public static NumericAssertions<ulong> Should(this ulong actualValue)
{
return new NumericAssertions<ulong>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="ulong"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<ulong> Should(this ulong? actualValue)
{
return new NullableNumericAssertions<ulong>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="float"/>.
/// </summary>
[Pure]
public static NumericAssertions<float> Should(this float actualValue)
{
return new NumericAssertions<float>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="float"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<float> Should(this float? actualValue)
{
return new NullableNumericAssertions<float>(actualValue);
}
/// <summary>
/// Returns an <see cref="NumericAssertions{T}"/> object that can be used to assert the
/// current <see cref="double"/>.
/// </summary>
[Pure]
public static NumericAssertions<double> Should(this double actualValue)
{
return new NumericAssertions<double>(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableNumericAssertions{T}"/> object that can be used to assert the
/// current nullable <see cref="double"/>.
/// </summary>
[Pure]
public static NullableNumericAssertions<double> Should(this double? actualValue)
{
return new NullableNumericAssertions<double>(actualValue);
}
/// <summary>
/// Returns an <see cref="StringAssertions"/> object that can be used to assert the
/// current <see cref="string"/>.
/// </summary>
[Pure]
public static StringAssertions Should(this string actualValue)
{
return new StringAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="SimpleTimeSpanAssertions"/> object that can be used to assert the
/// current <see cref="TimeSpan"/>.
/// </summary>
[Pure]
public static SimpleTimeSpanAssertions Should(this TimeSpan actualValue)
{
return new SimpleTimeSpanAssertions(actualValue);
}
/// <summary>
/// Returns an <see cref="NullableSimpleTimeSpanAssertions"/> object that can be used to assert the
/// current nullable <see cref="TimeSpan"/>.
/// </summary>
[Pure]
public static NullableSimpleTimeSpanAssertions Should(this TimeSpan? actualValue)
{
return new NullableSimpleTimeSpanAssertions(actualValue);
}
/// <summary>
/// Returns a <see cref="TypeAssertions"/> object that can be used to assert the
/// current <see cref="System.Type"/>.
/// </summary>
[Pure]
public static TypeAssertions Should(this Type subject)
{
return new TypeAssertions(subject);
}
/// <summary>
/// Returns a <see cref="TypeAssertions"/> object that can be used to assert the
/// current <see cref="System.Type"/>.
/// </summary>
[Pure]
public static TypeSelectorAssertions Should(this TypeSelector typeSelector)
{
return new TypeSelectorAssertions(typeSelector.ToArray());
}
/// <summary>
/// Returns a <see cref="FluentAssertions.Types.MethodBaseAssertions{TSubject, TAssertions}"/> object
/// that can be used to assert the current <see cref="MethodInfo"/>.
/// </summary>
/// <seealso cref="TypeAssertions"/>
[Pure]
public static ConstructorInfoAssertions Should(this ConstructorInfo constructorInfo)
{
return new ConstructorInfoAssertions(constructorInfo);
}
/// <summary>
/// Returns a <see cref="MethodInfoAssertions"/> object that can be used to assert the current <see cref="MethodInfo"/>.
/// </summary>
/// <seealso cref="TypeAssertions"/>
[Pure]
public static MethodInfoAssertions Should(this MethodInfo methodInfo)
{
return new MethodInfoAssertions(methodInfo);
}
/// <summary>
/// Returns a <see cref="MethodInfoSelectorAssertions"/> object that can be used to assert the methods returned by the
/// current <see cref="MethodInfoSelector"/>.
/// </summary>
/// <seealso cref="TypeAssertions"/>
[Pure]
public static MethodInfoSelectorAssertions Should(this MethodInfoSelector methodSelector)
{
return new MethodInfoSelectorAssertions(methodSelector.ToArray());
}
/// <summary>
/// Returns a <see cref="PropertyInfoAssertions"/> object that can be used to assert the
/// current <see cref="PropertyInfoSelector"/>.
/// </summary>
/// <seealso cref="TypeAssertions"/>
[Pure]
public static PropertyInfoAssertions Should(this PropertyInfo propertyInfo)
{
return new PropertyInfoAssertions(propertyInfo);
}
/// <summary>
/// Returns a <see cref="PropertyInfoSelectorAssertions"/> object that can be used to assert the properties returned by the
/// current <see cref="PropertyInfoSelector"/>.
/// </summary>
/// <seealso cref="TypeAssertions"/>
[Pure]
public static PropertyInfoSelectorAssertions Should(this PropertyInfoSelector propertyInfoSelector)
{
return new PropertyInfoSelectorAssertions(propertyInfoSelector.ToArray());
}
/// <summary>
/// Returns a <see cref="ActionAssertions"/> object that can be used to assert the
/// current <see cref="System.Action"/> .
/// </summary>
[Pure]
public static ActionAssertions Should(this Action action)
{
return new ActionAssertions(action, extractor);
}
/// <summary>
/// Returns a <see cref="AsyncFunctionAssertions"/> object that can be used to assert the
/// current <see cref="System.Func{Task}"/> .
/// </summary>
[Pure]
public static AsyncFunctionAssertions Should(this Func<Task> action)
{
return new AsyncFunctionAssertions(action, extractor);
}
/// <summary>
/// Returns a <see cref="FunctionAssertions{T}"/> object that can be used to assert the
/// current <see cref="System.Func{T}"/> .
/// </summary>
[Pure]
public static FunctionAssertions<T> Should<T>(this Func<T> func)
{
return new FunctionAssertions<T>(func, extractor);
}
#if NET45 || NET47 || NETCOREAPP2_0
/// <summary>
/// Starts monitoring <paramref name="eventSource"/> for its events.
/// </summary>
/// <param name="eventSource">The object for which to monitor the events.</param>
/// <param name="utcNow">
/// An optional delegate that returns the current date and time in UTC format.
/// Will revert to <see cref="DateTime.UtcNow"/> if no delegate was provided.
/// </param>
/// <exception cref = "ArgumentNullException">Thrown if <paramref name="eventSource"/> is Null.</exception>
public static IMonitor<T> Monitor<T>(this T eventSource, Func<DateTime> utcNow = null)
{
return new EventMonitor<T>(eventSource, utcNow ?? (() => DateTime.UtcNow));
}
#endif
/// <summary>
/// Safely casts the specified object to the type specified through <typeparamref name="TTo"/>.
/// </summary>
/// <remarks>
/// Has been introduced to allow casting objects without breaking the fluent API.
/// </remarks>
/// <typeparam name="TTo"></typeparam>
[Pure]
public static TTo As<TTo>(this object subject)
{
return subject is TTo ? (TTo)subject : default(TTo);
}
}
}