1 // ****************************************************************
2 // This is free software licensed under the NUnit license. You
3 // may obtain a copy of the license as well as information regarding
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
5 // ****************************************************************
6 
7 namespace NUnit.Framework
8 {
9 	using System;
10 
11 	/// <summary>
12 	/// Enumeration indicating how the expected message parameter is to be used
13 	/// </summary>
14 	public enum MessageMatch
15 	{
16 		/// Expect an exact match
17 		Exact,
18 		/// Expect a message containing the parameter string
19 		Contains,
20 		/// Match the regular expression provided as a parameter
21 		Regex
22 	}
23 
24 	/// <summary>
25 	/// ExpectedExceptionAttribute
26 	/// </summary>
27 	///
28 	[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
29 	public class ExpectedExceptionAttribute : Attribute
30 	{
31 		private Type expectedException;
32 		private string expectedExceptionName;
33 		private string expectedMessage;
34 		private MessageMatch matchType;
35 		private string userMessage;
36 		private string handler;
37 
38 		/// <summary>
39 		/// Constructor for a non-specific exception
40 		/// </summary>
ExpectedExceptionAttribute()41 		public ExpectedExceptionAttribute()
42 		{
43 		}
44 
45 		/// <summary>
46 		/// Constructor for a given type of exception
47 		/// </summary>
48 		/// <param name="exceptionType">The type of the expected exception</param>
ExpectedExceptionAttribute(Type exceptionType)49 		public ExpectedExceptionAttribute(Type exceptionType)
50 		{
51 			this.expectedException = exceptionType;
52 			this.expectedExceptionName = exceptionType.FullName;
53 		}
54 
55 		/// <summary>
56 		/// Constructor for a given exception name
57 		/// </summary>
58 		/// <param name="exceptionName">The full name of the expected exception</param>
ExpectedExceptionAttribute(string exceptionName)59 		public ExpectedExceptionAttribute(string exceptionName)
60 		{
61 			this.expectedExceptionName = exceptionName;
62 		}
63 
64 		/// <summary>
65 		/// Constructor for a given type of exception and expected message text
66 		/// </summary>
67 		/// <param name="exceptionType">The type of the expected exception</param>
68 		/// <param name="expectedMessage">The expected message text</param>
69         [Obsolete("Use named parameter format 'ExpectedMessage=...'", false)]
ExpectedExceptionAttribute(Type exceptionType, string expectedMessage)70         public ExpectedExceptionAttribute(Type exceptionType, string expectedMessage)
71             : this(exceptionType)
72         {
73             this.expectedMessage = expectedMessage;
74             this.matchType = MessageMatch.Exact;
75         }
76 
77 		/// <summary>
78 		/// Constructor for a given exception name and expected message text
79 		/// </summary>
80 		/// <param name="exceptionName">The full name of the expected exception</param>
81 		/// <param name="expectedMessage">The expected messge text</param>
82         [Obsolete("Use named parameter format 'ExpectedMessage=...'", false)]
ExpectedExceptionAttribute(string exceptionName, string expectedMessage)83         public ExpectedExceptionAttribute(string exceptionName, string expectedMessage)
84             : this(exceptionName)
85         {
86             this.expectedMessage = expectedMessage;
87             this.matchType = MessageMatch.Exact;
88         }
89 
90 		/// <summary>
91 		/// Gets or sets the expected exception type
92 		/// </summary>
93 		public Type ExceptionType
94 		{
95 			get{ return expectedException; }
96 			set{ expectedException = value; }
97 		}
98 
99 		/// <summary>
100 		/// Gets or sets the full Type name of the expected exception
101 		/// </summary>
102 		public string ExceptionName
103 		{
104 			get{ return expectedExceptionName; }
105 			set{ expectedExceptionName = value; }
106 		}
107 
108 		/// <summary>
109 		/// Gets or sets the expected message text
110 		/// </summary>
111 		public string ExpectedMessage
112 		{
113 			get { return expectedMessage; }
114 			set { expectedMessage = value; }
115 		}
116 
117 		/// <summary>
118 		/// Gets or sets the user message displayed in case of failure
119 		/// </summary>
120 		public string UserMessage
121 		{
122 			get { return userMessage; }
123 			set { userMessage = value; }
124 		}
125 
126 		/// <summary>
127 		///  Gets or sets the type of match to be performed on the expected message
128 		/// </summary>
129 		public MessageMatch MatchType
130 		{
131 			get { return matchType; }
132 			set { matchType = value; }
133 		}
134 
135 		/// <summary>
136 		///  Gets the name of a method to be used as an exception handler
137 		/// </summary>
138 		public string Handler
139 		{
140 			get { return handler; }
141 			set { handler = value; }
142 		}
143 	}
144 }
145