Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / AssertExtensions.cs
1 using System;
2 using System.Collections.Generic;
3
4 using NUnit.Framework;
5
6 namespace MonoTests.Common
7 {
8         delegate void AssertThrowsDelegate();
9
10         static class AssertExtensions
11         {
12                 public static void AreEqual (byte[] expected, byte[] data, string message)
13                 {
14                         if (expected == null) {
15                                 if (data == null)
16                                         return;
17                                 Assert.Fail ("{0}{1}Expected: null{1}Got: byte array with {2} elements and of rank {3}{1}",
18                                         message, Environment.NewLine, data.Length, data.Rank);
19                         }
20
21                         if (data == null)
22                                 Assert.Fail ("{0}{1}Expected: byte array with {2} elements and rank {3}{1}Got: null{1}",
23                                         message, Environment.NewLine, expected.Length, expected.Rank);
24
25                         if (expected.Rank > 1)
26                                 Assert.Fail ("Only single-dimensional arrays are supported.");
27
28                         if (expected.Rank != data.Rank || expected.Length != data.Length)
29                                 Assert.Fail ("{0}{1}Expected: byte array with {2} elements and rank {3}{1}Got: byte array with {4} elements and rank {5}{1}",
30                                         message, Environment.NewLine, expected.Length, expected.Rank, data.Length, data.Rank);
31
32                         int max = expected.Length;
33                         for (int i = 0; i < max; i++) {
34                                 if (expected[i] != data[i])
35                                         Assert.Fail ("{0}{1}Arrays differ at index {2}.{1}Expected 0x{3:X} got 0x{4:X}{1}",
36                                                 message, Environment.NewLine, i, expected[i], data[i]);
37                         }
38                 }
39
40                 public static void Throws<ET> (AssertThrowsDelegate code, string message)
41                 {
42                         Throws(typeof(ET), code, message);
43                 }
44
45                 public static void Throws(Type exceptionType, AssertThrowsDelegate code, string message)
46                 {
47                         if (code == null)
48                                 Assert.Fail("No code provided for the test.");
49
50                         Exception exception = null;
51                         try
52                         {
53                                 code();
54                         }
55                         catch (Exception ex)
56                         {
57                                 exception = ex;
58                         }
59
60                         if (exceptionType == null)
61                         {
62                                 if (exception == null)
63                                         Assert.Fail("{0}{1}Expected: any exception thrown{1}But was: no exception thrown{1}",
64                                                 message, Environment.NewLine);
65                                 return;
66                         }
67
68                         if (exception == null || exception.GetType() != exceptionType)
69                                 Assert.Fail("{0}{1}Expected: {2}{1}But was: {3}{1}{4}{1}",
70                                     message,
71                                     Environment.NewLine,
72                                     exceptionType,
73                                     exception == null ? "no exception" : exception.GetType().ToString(),
74                                     exception == null ? "no exception" : exception.ToString());
75                 }
76
77                 public static void Throws(AssertThrowsDelegate code, string message)
78                 {
79                         Throws(null, code, message);
80                 }
81         }
82 }