2009-09-10 Marek Habersack <mhabersack@novell.com>
[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 Throws<ET> (AssertThrowsDelegate code, string message)
13                 {
14                         Throws (typeof (ET), code, message);
15                 }
16
17                 public static void Throws (Type exceptionType, AssertThrowsDelegate code, string message)
18                 {
19                         if (code == null)
20                                 Assert.Fail ("No code provided for the test.");
21
22                         Exception exception = null;
23                         try {
24                                 code ();
25                         } catch (Exception ex) {
26                                 exception = ex;
27                         }
28
29                         if (exceptionType == null) {
30                                 if (exception == null)
31                                         Assert.Fail ("{0}{1}Expected: any exception thrown{1}But was: no exception thrown{1}",
32                                                 message, Environment.NewLine);
33                                 return;
34                         }
35
36                         if (exception == null || exception.GetType () != exceptionType)
37                                 Assert.Fail ("{0}{1}Expected: {2}{1}But was: {3}{1}{4}{1}",
38                                     message,
39                                     Environment.NewLine,
40                                     exceptionType,
41                                     exception == null ? "no exception" : exception.GetType ().ToString (),
42                                     exception == null ? "no exception" : exception.ToString ());
43                 }
44
45                 public static void Throws (AssertThrowsDelegate code, string message)
46                 {
47                         Throws (null, code, message);
48                 }
49         }
50 }