PageRenderTime 16ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Attributes/ExpectedExceptionAttribute.cs

#
C# | 123 lines | 67 code | 12 blank | 44 comment | 0 complexity | ad744a1b043c3dd758d5b24642b195f8 MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You may
  3// obtain a copy of the license at http://nunit.org.
  4// ****************************************************************
  5
  6namespace NUnit.Framework
  7{
  8	using System;
  9
 10	/// <summary>
 11	/// Enumeration indicating how the expected message parameter is to be used
 12	/// </summary>
 13	public enum MessageMatch
 14	{
 15		/// Expect an exact match
 16		Exact,	
 17		/// Expect a message containing the parameter string
 18		Contains,
 19		/// Match the regular expression provided as a parameter
 20		Regex,
 21        /// Expect a message that starts with the parameter string
 22        StartsWith
 23	}
 24
 25	/// <summary>
 26	/// ExpectedExceptionAttribute
 27	/// </summary>
 28	/// 
 29	[AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
 30	public class ExpectedExceptionAttribute : Attribute
 31	{
 32		private Type expectedException;
 33		private string expectedExceptionName;
 34		private string expectedMessage;
 35		private MessageMatch matchType;
 36		private string userMessage;
 37		private string handler;
 38
 39		/// <summary>
 40		/// Constructor for a non-specific exception
 41		/// </summary>
 42		public ExpectedExceptionAttribute()
 43		{
 44		}
 45
 46		/// <summary>
 47		/// Constructor for a given type of exception
 48		/// </summary>
 49		/// <param name="exceptionType">The type of the expected exception</param>
 50		public ExpectedExceptionAttribute(Type exceptionType)
 51		{
 52			this.expectedException = exceptionType;
 53			this.expectedExceptionName = exceptionType.FullName;
 54		}
 55
 56		/// <summary>
 57		/// Constructor for a given exception name
 58		/// </summary>
 59		/// <param name="exceptionName">The full name of the expected exception</param>
 60		public ExpectedExceptionAttribute(string exceptionName)
 61		{
 62			this.expectedExceptionName = exceptionName;
 63		}
 64
 65		/// <summary>
 66		/// Gets or sets the expected exception type
 67		/// </summary>
 68		public Type ExpectedException
 69		{
 70			get{ return expectedException; }
 71			set
 72            { 
 73                expectedException = value;
 74                expectedExceptionName = expectedException.FullName;
 75            }
 76		}
 77
 78		/// <summary>
 79		/// Gets or sets the full Type name of the expected exception
 80		/// </summary>
 81		public string ExpectedExceptionName
 82		{
 83			get{ return expectedExceptionName; }
 84			set{ expectedExceptionName = value; }
 85		}
 86
 87		/// <summary>
 88		/// Gets or sets the expected message text
 89		/// </summary>
 90		public string ExpectedMessage 
 91		{
 92			get { return expectedMessage; }
 93			set { expectedMessage = value; }
 94		}
 95
 96		/// <summary>
 97		/// Gets or sets the user message displayed in case of failure
 98		/// </summary>
 99		public string UserMessage
100		{
101			get { return userMessage; }
102			set { userMessage = value; }
103		}
104
105		/// <summary>
106		///  Gets or sets the type of match to be performed on the expected message
107		/// </summary>
108		public MessageMatch MatchType
109		{
110			get { return matchType; }
111			set { matchType = value; }
112		}
113
114		/// <summary>
115		///  Gets the name of a method to be used as an exception handler
116		/// </summary>
117		public string Handler
118		{
119			get { return handler; }
120			set { handler = value; }
121		}
122	}
123}