-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
AssertionExtensions.cs
1120 lines (1000 loc) · 43.8 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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml.Linq;
using FluentAssertions.Collections;
using FluentAssertions.Common;
using FluentAssertions.Execution;
using FluentAssertions.Numeric;
using FluentAssertions.Primitives;
using FluentAssertions.Specialized;
using FluentAssertions.Streams;
using FluentAssertions.Types;
using FluentAssertions.Xml;
using JetBrains.Annotations;
#if !NETSTANDARD2_0
using FluentAssertions.Events;
#endif
namespace FluentAssertions;
/// <summary>
/// Contains extension methods for custom assertions in unit tests.
/// </summary>
[DebuggerNonUserCode]
public static class AssertionExtensions
{
private static readonly AggregateExceptionExtractor Extractor = new();
static AssertionExtensions()
{
Services.EnsureInitialized();
}
/// <summary>
/// Invokes the specified action on a subject so that you can chain it
/// with any of the assertions from <see cref="ActionAssertions"/>
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="subject"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception>
[Pure]
public static Action Invoking<T>(this T subject, Action<T> action)
{
Guard.ThrowIfArgumentIsNull(subject);
Guard.ThrowIfArgumentIsNull(action);
return () => action(subject);
}
/// <summary>
/// Invokes the specified action on a subject so that you can chain it
/// with any of the assertions from <see cref="FunctionAssertions{T}"/>
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="subject"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception>
[Pure]
public static Func<TResult> Invoking<T, TResult>(this T subject, Func<T, TResult> action)
{
Guard.ThrowIfArgumentIsNull(subject);
Guard.ThrowIfArgumentIsNull(action);
return () => action(subject);
}
/// <summary>
/// Invokes the specified action on a subject so that you can chain it
/// with any of the assertions from <see cref="NonGenericAsyncFunctionAssertions"/>
/// </summary>
[Pure]
public static Func<Task> Awaiting<T>(this T subject, Func<T, Task> action)
{
return () => action(subject);
}
/// <summary>
/// Invokes the specified action on a subject so that you can chain it
/// with any of the assertions from <see cref="GenericAsyncFunctionAssertions{TResult}"/>
/// </summary>
[Pure]
public static Func<Task<TResult>> Awaiting<T, TResult>(this T subject, Func<T, Task<TResult>> action)
{
return () => action(subject);
}
/// <summary>
/// Invokes the specified action on a subject so that you can chain it
/// with any of the assertions from <see cref="NonGenericAsyncFunctionAssertions"/>
/// </summary>
[Pure]
public static Func<Task> Awaiting<T>(this T subject, Func<T, ValueTask> action)
{
return () => action(subject).AsTask();
}
/// <summary>
/// Invokes the specified action on a subject so that you can chain it
/// with any of the assertions from <see cref="GenericAsyncFunctionAssertions{TResult}"/>
/// </summary>
[Pure]
public static Func<Task<TResult>> Awaiting<T, TResult>(this T subject, Func<T, ValueTask<TResult>> action)
{
return () => action(subject).AsTask();
}
/// <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>
/// <exception cref="ArgumentNullException"><paramref name="subject"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception>
[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,
StartTimer createTimer = null)
{
Guard.ThrowIfArgumentIsNull(subject);
Guard.ThrowIfArgumentIsNull(action);
createTimer ??= () => new StopwatchTimer();
return new MemberExecutionTime<T>(subject, action, createTimer);
}
/// <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>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception>
[MustUseReturnValue /* do not use Pure because this method executes the action before returning to the caller */]
public static ExecutionTime ExecutionTime(this Action action, StartTimer createTimer = null)
{
createTimer ??= () => new StopwatchTimer();
return new ExecutionTime(action, createTimer);
}
/// <summary>
/// Provides methods for asserting the execution time of an async action.
/// </summary>
/// <returns>
/// Returns an object for asserting that the execution time matches certain conditions.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception>
[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, () => new StopwatchTimer());
}
/// <summary>
/// Returns an <see cref="ExecutionTimeAssertions"/> object that can be used to assert the
/// current <see cref="FluentAssertions.Specialized.ExecutionTime"/>.
/// </summary>
[Pure]
public static ExecutionTimeAssertions Should(this ExecutionTime executionTime)
{
return new ExecutionTimeAssertions(executionTime, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="StreamAssertions"/> object that can be used to assert the
/// current <see cref="Stream"/>.
/// </summary>
[Pure]
public static StreamAssertions Should(this Stream actualValue)
{
return new StreamAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="BufferedStreamAssertions"/> object that can be used to assert the
/// current <see cref="BufferedStream"/>.
/// </summary>
[Pure]
public static BufferedStreamAssertions Should(this BufferedStream actualValue)
{
return new BufferedStreamAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <summary>
/// Forces enumerating a collection. Should be used to assert that a method that uses the
/// <see langword="yield"/> 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
/// <see langword="yield"/> keyword throws a particular exception.
/// </summary>
[Pure]
public static Action Enumerating<T>(this Func<IEnumerable<T>> enumerable)
{
return () => ForceEnumeration(enumerable);
}
/// <summary>
/// Forces enumerating a collection of the provided <paramref name="subject"/>.
/// Should be used to assert that a method that uses the <see langword="yield"/> keyword throws a particular exception.
/// </summary>
/// <param name="subject">The object that exposes the method or property.</param>
/// <param name="enumerable">A reference to the method or property to force enumeration of.</param>
public static Action Enumerating<T, TResult>(this T subject, Func<T, IEnumerable<TResult>> enumerable)
{
return () => ForceEnumeration(subject, enumerable);
}
private static void ForceEnumeration(Func<IEnumerable> enumerable)
{
foreach (object _ in enumerable())
{
// Do nothing
}
}
private static void ForceEnumeration<T>(T subject, Func<T, IEnumerable> enumerable)
{
foreach (object _ in enumerable(subject))
{
// 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, AssertionChain.GetOrCreate());
}
public static ObjectAssertions Should(this WhichResult<object> tuple)
{
return new ObjectAssertions(tuple.MatchedElement, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="HttpResponseMessageAssertions"/> object that can be used to assert the
/// current <see cref="HttpResponseMessage"/>.
/// </summary>
[Pure]
public static HttpResponseMessageAssertions Should(this HttpResponseMessage actualValue)
{
return new HttpResponseMessageAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="GenericDictionaryAssertions{TCollection, TKey, TValue}"/> object that can be used to assert the
/// current <see cref="IDictionary{TKey, TValue}"/>.
/// </summary>
[Pure]
public static GenericDictionaryAssertions<IDictionary<TKey, TValue>, TKey, TValue> Should<TKey, TValue>(
this IDictionary<TKey, TValue> actualValue)
{
return new GenericDictionaryAssertions<IDictionary<TKey, TValue>, TKey, TValue>(actualValue, AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="GenericDictionaryAssertions{TCollection, TKey, TValue}"/> object that can be used to assert the
/// current <see cref="IEnumerable{T}"/> of <see cref="KeyValuePair{TKey, TValue}"/>.
/// </summary>
[Pure]
public static GenericDictionaryAssertions<IEnumerable<KeyValuePair<TKey, TValue>>, TKey, TValue> Should<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> actualValue)
{
return new GenericDictionaryAssertions<IEnumerable<KeyValuePair<TKey, TValue>>, TKey, TValue>(actualValue,
AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="GenericDictionaryAssertions{TCollection, TKey, TValue}"/> object that can be used to assert the
/// current <typeparamref name="TCollection"/>.
/// </summary>
[Pure]
public static GenericDictionaryAssertions<TCollection, TKey, TValue> Should<TCollection, TKey, TValue>(
this TCollection actualValue)
where TCollection : IEnumerable<KeyValuePair<TKey, TValue>>
{
return new GenericDictionaryAssertions<TCollection, TKey, TValue>(actualValue, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
/// <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, AssertionChain.GetOrCreate());
}
#if NET6_0_OR_GREATER
/// <summary>
/// Returns an <see cref="DateOnlyAssertions"/> object that can be used to assert the
/// current <see cref="DateOnly"/>.
/// </summary>
[Pure]
public static DateOnlyAssertions Should(this DateOnly actualValue)
{
return new DateOnlyAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="NullableDateOnlyAssertions"/> object that can be used to assert the
/// current nullable <see cref="DateOnly"/>.
/// </summary>
[Pure]
public static NullableDateOnlyAssertions Should(this DateOnly? actualValue)
{
return new NullableDateOnlyAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="TimeOnlyAssertions"/> object that can be used to assert the
/// current <see cref="TimeOnly"/>.
/// </summary>
[Pure]
public static TimeOnlyAssertions Should(this TimeOnly actualValue)
{
return new TimeOnlyAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <summary>
/// Returns an <see cref="NullableTimeOnlyAssertions"/> object that can be used to assert the
/// current nullable <see cref="TimeOnly"/>.
/// </summary>
[Pure]
public static NullableTimeOnlyAssertions Should(this TimeOnly? actualValue)
{
return new NullableTimeOnlyAssertions(actualValue, AssertionChain.GetOrCreate());
}
#endif
/// <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, AssertionChain.GetOrCreate());
}
/// <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 WhichResult<IComparable<T>> tuple)
{
return new ComparableTypeAssertions<T>(tuple.MatchedElement, AssertionChain.GetOrCreate());
}
/// <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 Int32Assertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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 WhichResult<int> actualValue)
{
return new Int32Assertions(actualValue.MatchedElement, AssertionChain.GetOrCreate());
}
/// <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 NullableInt32Assertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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 UInt32Assertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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 NullableUInt32Assertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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 DecimalAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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 NullableDecimalAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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 ByteAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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 NullableByteAssertions(actualValue, AssertionChain.GetOrCreate());
}
/// <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 SByteAssertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 NullableSByteAssertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 Int16Assertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 NullableInt16Assertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 UInt16Assertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 NullableUInt16Assertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 Int64Assertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 NullableInt64Assertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 UInt64Assertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 NullableUInt64Assertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 SingleAssertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 NullableSingleAssertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 DoubleAssertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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 NullableDoubleAssertions(actualValue, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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,
AssertionChain.GetOrCreate(() => AssertionScope.Current, () => AssertionScope.Current.GetIdentifier()));
}
/// <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, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <summary>
/// Returns a <see cref="TypeAssertions"/> object that can be used to assert the
/// current <see cref="Type"/>.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="typeSelector"/> is <see langword="null"/>.</exception>
[Pure]
public static TypeSelectorAssertions Should(this TypeSelector typeSelector)
{
Guard.ThrowIfArgumentIsNull(typeSelector);
return new TypeSelectorAssertions(AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()), 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, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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"/>
/// <exception cref="ArgumentNullException"><paramref name="methodSelector"/> is <see langword="null"/>.</exception>
[Pure]
public static MethodInfoSelectorAssertions Should(this MethodInfoSelector methodSelector)
{
Guard.ThrowIfArgumentIsNull(methodSelector);
return new MethodInfoSelectorAssertions(AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()), 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, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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"/>
/// <exception cref="ArgumentNullException"><paramref name="propertyInfoSelector"/> is <see langword="null"/>.</exception>
[Pure]
public static PropertyInfoSelectorAssertions Should(this PropertyInfoSelector propertyInfoSelector)
{
Guard.ThrowIfArgumentIsNull(propertyInfoSelector);
return new PropertyInfoSelectorAssertions(AssertionChain.GetOrCreate(
() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()), 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, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <summary>
/// Returns a <see cref="NonGenericAsyncFunctionAssertions"/> object that can be used to assert the
/// current <see cref="System.Func{Task}"/>.
/// </summary>
[Pure]
public static NonGenericAsyncFunctionAssertions Should(this Func<Task> action)
{
return new NonGenericAsyncFunctionAssertions(action, Extractor, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <summary>
/// Returns a <see cref="GenericAsyncFunctionAssertions{T}"/> object that can be used to assert the
/// current <see><cref>System.Func{Task{T}}</cref></see>.
/// </summary>
[Pure]
public static GenericAsyncFunctionAssertions<T> Should<T>(this Func<Task<T>> action)
{
return new GenericAsyncFunctionAssertions<T>(action, Extractor, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <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, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
/// <summary>
/// Returns a <see cref="TaskCompletionSourceAssertions{T}"/> object that can be used to assert the
/// current <see cref="TaskCompletionSource{T}"/>.
/// </summary>
[Pure]
public static TaskCompletionSourceAssertions<T> Should<T>(this TaskCompletionSource<T> tcs)
{
return new TaskCompletionSourceAssertions<T>(tcs, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
#if !NETSTANDARD2_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"><paramref name="eventSource"/> is <see langword="null"/>.</exception>
public static IMonitor<T> Monitor<T>(this T eventSource, Func<DateTime> utcNow = null)
{
return new EventMonitor<T>(eventSource, utcNow ?? (() => DateTime.UtcNow));
}
#endif
#if NET6_0_OR_GREATER
/// <summary>
/// Returns a <see cref="TaskCompletionSourceAssertions"/> object that can be used to assert the
/// current <see cref="TaskCompletionSource"/>.
/// </summary>
[Pure]
public static TaskCompletionSourceAssertions Should(this TaskCompletionSource tcs)
{
return new TaskCompletionSourceAssertions(tcs, AssertionChain.GetOrCreate(() => AssertionScope.Current,
() => AssertionScope.Current.GetIdentifier()));
}
#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">The <see cref="Type"/> to cast <paramref name="subject"/> to</typeparam>
[Pure]
public static TTo As<TTo>(this object subject)
{
return subject is TTo to ? to : default;
}
#region Prevent chaining on AndConstraint
/// <inheritdoc cref="Should(ExecutionTimeAssertions)" />
[Obsolete("You are asserting the 'AndConstraint' itself. Remove the 'Should()' method directly following 'And'", error: true)]
public static void Should<TSubject, TAssertions>(this ReferenceTypeAssertions<TSubject, TAssertions> _)
where TAssertions : ReferenceTypeAssertions<TSubject, TAssertions>
{
InvalidShouldCall();
}
/// <inheritdoc cref="Should(ExecutionTimeAssertions)" />
[Obsolete("You are asserting the 'AndConstraint' itself. Remove the 'Should()' method directly following 'And'", error: true)]
public static void Should<TAssertions>(this BooleanAssertions<TAssertions> _)
where TAssertions : BooleanAssertions<TAssertions>
{
InvalidShouldCall();
}
/// <inheritdoc cref="Should(ExecutionTimeAssertions)" />
[Obsolete("You are asserting the 'AndConstraint' itself. Remove the 'Should()' method directly following 'And'", error: true)]
public static void Should<TAssertions>(this DateTimeAssertions<TAssertions> _)
where TAssertions : DateTimeAssertions<TAssertions>
{
InvalidShouldCall();
}
/// <inheritdoc cref="Should(ExecutionTimeAssertions)" />
[Obsolete("You are asserting the 'AndConstraint' itself. Remove the 'Should()' method directly following 'And'", error: true)]
public static void Should<TAssertions>(this DateTimeOffsetAssertions<TAssertions> _)