summaryrefslogtreecommitdiff
path: root/tests/src/Interop/common/Assertion.cs
blob: 30dd072a7581d2e7ae0252d9912c6c9086c6a490 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Note: Exception messages call ToString instead of Name to avoid MissingMetadataException when just outputting basic info

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CoreFXTestLibrary
{
    /// <summary>
    ///    A collection of helper classes to test various conditions within
    /// unit tests. If the condition being tested is not met, an exception
    /// is thrown.
    /// </summary>
    public static class Assert
    {
        /// <summary>
        ///     Asserts that the given delegate throws an <see cref="ArgumentNullException"/> with the given parameter name.
        /// </summary>
        /// <param name="action">
        ///     The delagate of type <see cref="Action"/> to execute.
        /// </param>
        /// <param name="message">
        ///     A <see cref="String"/> containing additional information for when the assertion fails.
        /// </param>
        /// <param name="parameterName">
        ///     A <see cref="String"/> containing the parameter of name to check, <see langword="null"/> to skip parameter validation.
        /// </param>
        /// <returns>
        ///     The thrown <see cref="ArgumentNullException"/>.
        /// </returns>
        /// <exception cref="AssertFailedException">
        ///     <see cref="Exception"/> of type <see cref="ArgumentNullException"/> was not thrown.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <see cref="ArgumentException.ParamName"/> is not equal to <paramref name="parameterName"/> .
        /// </exception>
        public static ArgumentNullException ThrowsArgumentNullException(string parameterName, Action action, string message = null)
        {
            return ThrowsArgumentException<ArgumentNullException>(parameterName, action, message);
        }

        /// <summary>
        ///     Asserts that the given delegate throws an <see cref="ArgumentException"/> with the given parameter name.
        /// </summary>
        /// <param name="action">
        ///     The delagate of type <see cref="Action"/> to execute.
        /// </param>
        /// <param name="message">
        ///     A <see cref="String"/> containing additional information for when the assertion fails.
        /// </param>
        /// <param name="parameterName">
        ///     A <see cref="String"/> containing the parameter of name to check, <see langword="null"/> to skip parameter validation.
        /// </param>
        /// <returns>
        ///     The thrown <see cref="ArgumentException"/>.
        /// </returns>
        /// <exception cref="AssertFailedException">
        ///     <see cref="Exception"/> of type <see cref="ArgumentException"/> was not thrown.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <see cref="ArgumentException.ParamName"/> is not equal to <paramref name="parameterName"/> .
        /// </exception>
        public static ArgumentException ThrowsArgumentException(string parameterName, Action action, string message = null)
        {
            return ThrowsArgumentException<ArgumentException>(parameterName, action, message);
        }

        /// <summary>
        ///     Asserts that the given delegate throws an <see cref="ArgumentException"/> of type <typeparamref name="T"/> with the given parameter name.
        /// </summary>
        /// <param name="action">
        ///     The delagate of type <see cref="Action"/> to execute.
        /// </param>
        /// <param name="message">
        ///     A <see cref="String"/> containing additional information for when the assertion fails.
        /// </param>
        /// <param name="parameterName">
        ///     A <see cref="String"/> containing the parameter of name to check, <see langword="null"/> to skip parameter validation.
        /// </param>
        /// <returns>
        ///     The thrown <see cref="Exception"/>.
        /// </returns>
        /// <exception cref="AssertFailedException">
        ///     <see cref="Exception"/> of type <typeparam name="T"/> was not thrown.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <see cref="ArgumentException.ParamName"/> is not equal to <paramref name="parameterName"/> .
        /// </exception>
        public static T ThrowsArgumentException<T>(string parameterName, Action action, string message = null)
            where T : ArgumentException
        {
            T exception = Throws<T>(action, message);

#if DEBUG
            // ParamName's not available on ret builds
            if (parameterName != null)
                Assert.AreEqual(parameterName, exception.ParamName, "Expected '{0}.ParamName' to be '{1}'. {2}", typeof(T), parameterName, message);
#endif

            return exception;
        }

        /// <summary>
        ///     Asserts that the given delegate throws an <see cref="AggregateException"/> with a base exception <see cref="Exception"/> of type <typeparam name="T" />.
        /// </summary>
        /// <param name="action">
        ///     The delagate of type <see cref="Action"/> to execute.
        /// </param>
        /// <param name="message">
        ///     A <see cref="String"/> containing additional information for when the assertion fails.
        /// </param>
        /// <returns>
        ///     The base <see cref="Exception"/> of the <see cref="AggregateException"/>.
        /// </returns>
        /// <exception cref="AssertFailedException">
        ///     <see cref="AggregateException"/> of was not thrown.
        ///         -or-
        ///     </para>
        ///     <see cref="AggregateException.GetBaseException()"/> is not of type <typeparam name="TBase"/>.
        /// </exception>
        public static TBase ThrowsAggregateException<TBase>(Action action, string message = "") where TBase : Exception
        {
            AggregateException exception = Throws<AggregateException>(action, message);

            Exception baseException = exception.GetBaseException();
            if (baseException == null)
                Assert.Fail("Expected 'AggregateException.GetBaseException()' to be '{0}', however it is null. {1}", typeof(TBase), message);

            if (baseException.GetType() != typeof(TBase))
                Assert.Fail("Expected 'AggregateException.GetBaseException()', to be '{0}', however, '{1}' is. {2}", typeof(TBase), baseException.GetType(), message);

            return (TBase)baseException;
        }

        /// <summary>
        ///     Asserts that the given delegate throws an <see cref="Exception"/> of type <typeparam name="T" />.
        /// </summary>
        /// <param name="action">
        ///     The delagate of type <see cref="Action"/> to execute.
        /// </param>
        /// <param name="format">
        ///     A <see cref="String"/> containing format information for when the assertion fails.
        /// </param>
        /// <param name="args">
        ///     An <see cref="Array"/> of arguments to be formatted.
        /// </param>
        /// <returns>
        ///     The thrown <see cref="Exception"/>.
        /// </returns>
        /// <exception cref="AssertFailedException">
        ///     <see cref="Exception"/> of type <typeparam name="T"/> was not thrown.
        /// </exception>
        public static T Throws<T>(Action action, string format, params Object[] args) where T : Exception
        {
            return Throws<T>(action, String.Format(format, args));
        }

        /// <summary>
        ///     Asserts that the given delegate throws an <see cref="Exception"/> of type <typeparam name="T" />.
        /// </summary>
        /// <param name="action">
        ///     The delagate of type <see cref="Action"/> to execute.
        /// </param>
        /// <param name="message">
        ///     A <see cref="String"/> containing additional information for when the assertion fails.
        /// </param>
        /// <param name="options">
        ///     Specifies whether <see cref="Assert.Throws{T}"/> should require an exact type match when comparing the expected exception type with the thrown exception. The default is <see cref="AssertThrowsOptions.None"/>.
        /// </param>
        /// <returns>
        ///     The thrown <see cref="Exception"/>.
        /// </returns>
        /// <exception cref="AssertFailedException">
        ///     <see cref="Exception"/> of type <typeparam name="T"/> was not thrown.
        /// </exception>
        public static T Throws<T>(Action action, string message = "", AssertThrowsOptions options = AssertThrowsOptions.None) where T : Exception
        {
            Exception exception = RunWithCatch(action);

            if (exception == null)
                Assert.Fail("Expected '{0}' to be thrown. {1}", typeof(T).ToString(), message);

            if (!IsOfExceptionType<T>(exception, options))
                Assert.Fail("Expected '{0}' to be thrown, however '{1}' was thrown. {2}", typeof(T), exception.GetType(), message);

            return (T)exception;
        }

        /// <summary>
        ///     Asserts that the given async delegate throws an <see cref="Exception"/> of type <typeparam name="T".
        /// </summary>
        /// <param name="action">
        ///     The delagate of type <see cref="Func{}"/> to execute.
        /// </param>
        /// <param name="message">
        ///     A <see cref="String"/> containing additional information for when the assertion fails.
        /// </param>
        /// <param name="options">
        ///     Specifies whether <see cref="Assert.Throws{T}"/> should require an exact type match when comparing the expected exception type with the thrown exception. The default is <see cref="AssertThrowsOptions.None"/>.
        /// </param>
        /// <returns>
        ///     The thrown <see cref="Exception"/>.
        /// </returns>
        /// <exception cref="AssertFailedException">
        ///     <see cref="Exception"/> of type <typeparam name="T"/> was not thrown.
        /// </exception>
        public static async Task<T> ThrowsAsync<T>(Func<Task> action, string message = "", AssertThrowsOptions options = AssertThrowsOptions.None) where T : Exception
        {
            Exception exception = await RunWithCatchAsync(action);

            if (exception == null)
                Assert.Fail("Expected '{0}' to be thrown. {1}", typeof(T).ToString(), message);

            if (!IsOfExceptionType<T>(exception, options))
                Assert.Fail("Expected '{0}' to be thrown, however '{1}' was thrown. {2}", typeof(T), exception.GetType(), message);

            return (T)exception;
        }

        /// <summary>
        ///     Asserts that the given async delegate throws an <see cref="Exception"/> of type <typeparam name="T" /> and <see cref="Exception.InnerException"/> 
        ///     returns an <see cref="Exception"/> of type <typeparam name="TInner" />.
        /// </summary>
        /// <param name="action">
        ///     The delagate of type <see cref="Action"/> to execute.
        /// </param>
        /// <param name="message">
        ///     A <see cref="String"/> containing additional information for when the assertion fails.
        /// </param>
        /// <param name="options">
        ///     Specifies whether <see cref="Assert.Throws{T}"/> should require an exact type match when comparing the expected exception type with the thrown exception. The default is <see cref="AssertThrowsOptions.None"/>.
        /// </param>
        /// <returns>
        ///     The thrown inner <see cref="Exception"/>.
        /// </returns>
        /// <exception cref="AssertFailedException">
        ///     <see cref="Exception"/> of type <typeparam name="T"/> was not thrown.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <see cref="Exception.InnerException"/> is not of type <typeparam name="TInner"/>.
        /// </exception>
        public static TInner Throws<T, TInner>(Action action, string message = "", AssertThrowsOptions options = AssertThrowsOptions.None)
            where T : Exception
            where TInner : Exception
        {
            T outerException = Throws<T>(action, message, options);

            if (outerException.InnerException == null)
                Assert.Fail("Expected '{0}.InnerException' to be '{1}', however it is null. {2}", typeof(T), typeof(TInner), message);

            if (!IsOfExceptionType<TInner>(outerException.InnerException, options))
                Assert.Fail("Expected '{0}.InnerException', to be '{1}', however, '{2}' is. {3}", typeof(T), typeof(TInner), outerException.InnerException.GetType(), message);

            return (TInner)outerException.InnerException;
        }


        /// <summary>
        /// Tests whether the specified condition is true and throws an exception
        /// if the condition is false.
        /// </summary>
        /// <param name="condition">The condition the test expects to be true.</param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="condition"/>
        /// is false. The message is shown in test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="condition"/> is false.
        /// </exception>
        public static void IsTrue(bool condition, string format, params Object[] args)
        {
            if (!condition)
            {
                Assert.HandleFail("Assert.IsTrue", String.Format(format, args));
            }
        }

        /// <summary>
        /// Tests whether the specified condition is true and throws an exception
        /// if the condition is false.
        /// </summary>
        /// <param name="condition">The condition the test expects to be true.</param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="condition"/>
        /// is false. The message is shown in test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="condition"/> is false.
        /// </exception>
        public static void IsTrue(bool condition, string message = "")
        {
            if (!condition)
            {
                Assert.HandleFail("Assert.IsTrue", message);
            }
        }

        /// <summary>
        /// Tests whether the specified condition is false and throws an exception
        /// if the condition is true.
        /// </summary>
        /// <param name="condition">The condition the test expects to be false.</param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="condition"/>
        /// is true. The message is shown in test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="condition"/> is true.
        /// </exception>
        public static void IsFalse(bool condition, string message = "")
        {
            if (condition)
            {
                Assert.HandleFail("Assert.IsFalse", message);
            }
        }

        /// <summary>
        /// Tests whether the specified condition is false and throws an exception
        /// if the condition is true.
        /// </summary>
        /// <param name="condition">The condition the test expects to be false.</param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="condition"/>
        /// is true. The message is shown in test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="condition"/> is true.
        /// </exception>
        public static void IsFalse(bool condition, string format, params Object[] args)
        {
            IsFalse(condition, String.Format(format, args));
        }

        /// <summary>
        /// Tests whether the specified object is null and throws an exception
        /// if it is not.
        /// </summary>
        /// <param name="value">The object the test expects to be null.</param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="value"/>
        /// is not null. The message is shown in test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="value"/> is not null.
        /// </exception>
        public static void IsNull(object value, string message = "")
        {
            if (value != null)
            {
                Assert.HandleFail("Assert.IsNull", message);
            }
        }

        /// <summary>
        /// Tests whether the specified object is null and throws an exception
        /// if it is not.
        /// </summary>
        /// <param name="value">The object the test expects to be null.</param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="value"/>
        /// is not null. The message is shown in test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="value"/> is not null.
        /// </exception>
        public static void IsNull(object value, string format, params Object[] args)
        {
            IsNull(value, String.Format(format, args));
        }

        /// <summary>
        /// Tests whether the specified object is non-null and throws an exception
        /// if it is null.
        /// </summary>
        /// <param name="value">The object the test expects not to be null.</param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="value"/>
        /// is null. The message is shown in test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="value"/> is null.
        /// </exception>
        public static void IsNotNull(object value, string message = "")
        {
            if (value == null)
            {
                Assert.HandleFail("Assert.IsNotNull", message);
            }
        }

        /// <summary>
        /// Tests whether the expected object is equal to the actual object  and 
        /// throws an exception if it is not.
        /// </summary>
        /// <param name="notExpected">Expected object.</param>
        /// <param name="actual">Actual object.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreEqual<T>(T expected, T actual, string message = "")
        {
            const string EXPECTED_MSG = @"Expected: [{1}]. Actual: [{2}]. {0}";

            if (!Object.Equals(expected, actual))
            {
                string finalMessage = String.Format(EXPECTED_MSG, message, (object)expected ?? "NULL", (object)actual ?? "NULL");
                Assert.HandleFail("Assert.AreEqual", finalMessage);
            }
        }

        /// <summary>
        /// Tests whether the expected object is equal to the actual object  and 
        /// throws an exception if it is not.
        /// </summary>
        /// <param name="notExpected">Expected object.</param>
        /// <param name="actual">Actual object.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreEqual<T>(T expected, T actual, string format, params Object[] args)
        {
            AreEqual<T>(expected, actual, String.Format(format, args));
        }

        /// <summary>
        /// Tests whether the expected object is equal to the actual object  and 
        /// throws an exception if it is not.
        /// </summary>
        /// <param name="notExpected">Expected object that we do not want it to be.</param>
        /// <param name="actual">Actual object.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreNotEqual<T>(T notExpected, T actual, string message = "")
        {
            if (Object.Equals(notExpected, actual))
            {
                String finalMessage =
                    String.Format(@"Expected any value except:[{1}]. Actual:[{2}]. {0}",
                    message, notExpected, actual);

                Assert.HandleFail("Assert.AreNotEqual", finalMessage);
            }
        }

        /// <summary>
        /// Tests whether the expected object is equal to the actual object  and 
        /// throws an exception if it is not.
        /// </summary>
        /// <param name="notExpected">Expected object that we do not want it to be.</param>
        /// <param name="actual">Actual object.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreNotEqual<T>(T notExpected, T actual, string format, params Object[] args)
        {
            AreNotEqual<T>(notExpected, actual, String.Format(format, args));
        }

        /// <summary>
        /// Tests whether the two lists are the same length and contain the same objects (using Object.Equals()) in the same order and
        /// throws an exception if it is not.
        /// </summary>
        /// <param name="expected">Expected list.</param>
        /// <param name="actual">Actual list.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreAllEqual<T>(T[] expected, T[] actual, string message = "")
        {
            Assert.AreEqual(expected.Length, actual.Length, message);

            for (int i = 0; i < expected.Length; i++)
                Assert.AreEqual<T>(expected[i], actual[i], message);
        }

        /// <summary>
        /// Tests whether the two lists are the same length and contain the same objects (using Object.Equals()) in the same order and
        /// throws an exception if it is not.
        /// </summary>
        /// <param name="expected">Expected list.</param>
        /// <param name="actual">Actual list.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreAllEqual<T>(T[] expected, T[] actual, string format, params Object[] args)
        {
            AreAllEqual<T>(expected, actual, String.Format(format, args));
        }

        /// <summary>
        /// Tests whether the two lists are the same length and contain the same objects (using Object.Equals()) (but not necessarily in the same order) and
        /// throws an exception if it is not.
        /// </summary>
        /// <param name="expected">Expected list.</param>
        /// <param name="actual">Actual list.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreAllEqualUnordered<T>(T[] expected, T[] actual)
        {
            Assert.AreEqual(expected.Length, actual.Length);

            int count = expected.Length;
            bool[] removedFromActual = new bool[count];
            for (int i = 0; i < count; i++)
            {
                T item1 = expected[i];
                bool foundMatch = false;
                for (int j = 0; j < count; j++)
                {
                    if (!removedFromActual[j])
                    {
                        T item2 = actual[j];
                        if ((item1 == null && item2 == null) || (item1 != null && item1.Equals(item2)))
                        {
                            foundMatch = true;
                            removedFromActual[j] = true;
                            break;
                        }
                    }
                }
                if (!foundMatch)
                    Assert.HandleFail("Assert.AreAllEqualUnordered", "First array has element not found in second array: " + item1);
            }
            return;
        }

        /// <summary>
        /// Tests whether the two enumerables are the same length and contain the same objects (using Object.Equals()) in the same order and
        /// throws an exception if it is not.
        /// </summary>
        /// <param name="expected">Expected enumerables.</param>
        /// <param name="actual">Actual enumerables.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreAllEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, string message = "")
        {
            AreAllEqual(CopyToArray(expected), CopyToArray(actual), message);
        }

        /// <summary>
        /// Tests whether the two enumerables are the same length and contain the same objects (using Object.Equals()) (but not necessarily 
        /// in the same order) and throws an exception if it is not.
        /// </summary>
        /// <param name="expected">Expected enumerable.</param>
        /// <param name="actual">Actual enumerable.</param>
        /// <param name="message">Message to display upon failure.</param>
        public static void AreAllEqualUnordered<T>(IEnumerable<T> expected, IEnumerable<T> actual, string message = "")
        {
            AreAllEqualUnordered(CopyToArray(expected), CopyToArray(actual), message);
        }

        /// <summary>
        /// Iterates through an IEnumerable to generate an array of elements. The rational for using this instead of 
        /// System.Linq.ToArray is that this will not require a dependency on System.Linq.dll
        /// </summary>
        private static T[] CopyToArray<T>(IEnumerable<T> source)
        {
            T[] items = new T[4];
            int count = 0;

            if (source == null)
                return null;

            foreach (var item in source)
            {
                if (items.Length == count)
                {
                    var newItems = new T[checked(count * 2)];
                    Array.Copy(items, 0, newItems, 0, count);
                    items = newItems;
                }

                items[count] = item;
                count++;
            }

            if (items.Length == count)
                return items;

            var finalItems = new T[count];
            Array.Copy(items, 0, finalItems, 0, count);
            return finalItems;
        }


        /// <summary>
        /// Tests whether the specified objects both refer to the same object and
        /// throws an exception if the two inputs do not refer to the same object.
        /// </summary>
        /// <param name="expected">
        /// The first object to compare. This is the value the test expects.
        /// </param>
        /// <param name="actual">
        /// The second object to compare. This is the value produced by the code under test.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="expected"/> does not refer to the same object
        /// as <paramref name="actual"/>.
        /// </exception>
        static public void AreSame(object expected, object actual)
        {
            Assert.AreSame(expected, actual, string.Empty);
        }

        /// <summary>
        /// Tests whether the specified objects both refer to the same object and
        /// throws an exception if the two inputs do not refer to the same object.
        /// </summary>
        /// <param name="expected">
        /// The first object to compare. This is the value the test expects.
        /// </param>
        /// <param name="actual">
        /// The second object to compare. This is the value produced by the code under test.
        /// </param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="actual"/>
        /// is not the same as <paramref name="expected"/>. The message is shown
        /// in test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="expected"/> does not refer to the same object
        /// as <paramref name="actual"/>.
        /// </exception>
        static public void AreSame(object expected, object actual, string message)
        {
            if (!Object.ReferenceEquals(expected, actual))
            {
                string finalMessage = message;

                ValueType valExpected = expected as ValueType;
                if (valExpected != null)
                {
                    ValueType valActual = actual as ValueType;
                    if (valActual != null)
                    {
                        finalMessage = message == null ? String.Empty : message;
                    }
                }

                Assert.HandleFail("Assert.AreSame", finalMessage);
            }
        }

        /// <summary>
        /// Tests whether the specified objects refer to different objects and
        /// throws an exception if the two inputs refer to the same object.
        /// </summary>
        /// <param name="notExpected">
        /// The first object to compare. This is the value the test expects not
        /// to match <paramref name="actual"/>.
        /// </param>
        /// <param name="actual">
        /// The second object to compare. This is the value produced by the code under test.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="notExpected"/> refers to the same object
        /// as <paramref name="actual"/>.
        /// </exception>
        static public void AreNotSame(object notExpected, object actual)
        {
            Assert.AreNotSame(notExpected, actual, string.Empty);
        }

        /// <summary>
        /// Tests whether the specified objects refer to different objects and
        /// throws an exception if the two inputs refer to the same object.
        /// </summary>
        /// <param name="notExpected">
        /// The first object to compare. This is the value the test expects not
        /// to match <paramref name="actual"/>.
        /// </param>
        /// <param name="actual">
        /// The second object to compare. This is the value produced by the code under test.
        /// </param>
        /// <param name="message">
        /// The message to include in the exception when <paramref name="actual"/>
        /// is the same as <paramref name="notExpected"/>. The message is shown in
        /// test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Thrown if <paramref name="notExpected"/> refers to the same object
        /// as <paramref name="actual"/>.
        /// </exception>
        static public void AreNotSame(object notExpected, object actual, string message)
        {
            if (Object.ReferenceEquals(notExpected, actual))
            {
                Assert.HandleFail("Assert.AreNotSame", message);
            }
        }

        /// <summary>
        /// Throws an AssertFailedException.
        /// </summary>
        /// <exception cref="AssertFailedException">
        /// Always thrown.
        /// </exception>
        public static void Fail()
        {
            Assert.HandleFail("Assert.Fail", "");
        }

        /// <summary>
        /// Throws an AssertFailedException.
        /// </summary>
        /// <param name="message">
        /// The message to include in the exception. The message is shown in
        /// test results.
        /// </param>
        /// <exception cref="AssertFailedException">
        /// Always thrown.
        /// </exception>
        public static void Fail(string message, params object[] args)
        {
            string exceptionMessage = args.Length == 0 ? message : string.Format(message, args);
            Assert.HandleFail("Assert.Fail", exceptionMessage);
        }

        /// <summary>
        /// Helper function that creates and throws an exception.
        /// </summary>
        /// <param name="assertionName">name of the assertion throwing an exception.</param>
        /// <param name="message">message describing conditions for assertion failure.</param>
        /// <param name="parameters">The parameters.</param>
        /// TODO: Modify HandleFail to take in parameters
        internal static void HandleFail(string assertionName, string message)
        {
            // change this to use AssertFailedException             
            throw new AssertTestException(assertionName + ": " + message);
        }


        [Obsolete("Did you mean to call Assert.AreEqual()")]
        public static new bool Equals(Object o1, Object o2)
        {
            Assert.Fail("Don\u2019t call this.");
            throw new Exception();
        }

        private static bool IsOfExceptionType<T>(Exception thrown, AssertThrowsOptions options)
        {
            if ((options & AssertThrowsOptions.AllowDerived) == AssertThrowsOptions.AllowDerived)
                return thrown is T;

            return thrown.GetType() == typeof(T);
        }

        private static Exception RunWithCatch(Action action)
        {
            try
            {
                action();
                return null;
            }
            catch (Exception ex)
            {
                return ex;
            }
        }

        private static async Task<Exception> RunWithCatchAsync(Func<Task> action)
        {
            try
            {
                await action();
                return null;
            }
            catch (Exception ex)
            {
                return ex;
            }
        }
    }

    /// <summary>
    /// Exception raised by the Assert on Fail
    /// </summary>
    public class AssertTestException : Exception
    {
        public AssertTestException(string message)
            : base(message)
        {
        }

        public AssertTestException()
            : base()
        {
        }
    }

    public static class ExceptionAssert
    {
        public static void Throws<T>(String message, Action a) where T : Exception
        {
            Assert.Throws<T>(a, message);
        }
    }

    /// <summary>
    ///     Specifies whether <see cref="Assert.Throws{T}"/> should require an exact type match when comparing the expected exception type with the thrown exception.
    /// </summary>
    [Flags]
    public enum AssertThrowsOptions
    {
        /// <summary>
        ///     Specifies that <see cref="Assert.Throws{T}"/> should require an exact type 
        ///     match when comparing the specified exception type with the throw exception.
        /// </summary>
        None = 0,

        /// <summary>
        ///     Specifies that <see cref="Assert.Throws{T}"/> should not require an exact type 
        ///     match when comparing the specified exception type with the thrown exception.
        /// </summary>
        AllowDerived = 1,
    }
}