// ****************************************************************
// This is free software licensed under the NUnit license. You
// may obtain a copy of the license as well as information regarding
// copyright ownership at http://nunit.org/?p=license&r=2.4.
// ****************************************************************
namespace NUnit.Framework
{
using System;
///
/// Enumeration indicating how the expected message parameter is to be used
///
public enum MessageMatch
{
/// Expect an exact match
Exact,
/// Expect a message containing the parameter string
Contains,
/// Match the regular expression provided as a parameter
Regex
}
///
/// ExpectedExceptionAttribute
///
///
[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
public class ExpectedExceptionAttribute : Attribute
{
private Type expectedException;
private string expectedExceptionName;
private string expectedMessage;
private MessageMatch matchType;
private string userMessage;
private string handler;
///
/// Constructor for a non-specific exception
///
public ExpectedExceptionAttribute()
{
}
///
/// Constructor for a given type of exception
///
/// The type of the expected exception
public ExpectedExceptionAttribute(Type exceptionType)
{
this.expectedException = exceptionType;
this.expectedExceptionName = exceptionType.FullName;
}
///
/// Constructor for a given exception name
///
/// The full name of the expected exception
public ExpectedExceptionAttribute(string exceptionName)
{
this.expectedExceptionName = exceptionName;
}
///
/// Constructor for a given type of exception and expected message text
///
/// The type of the expected exception
/// The expected message text
[Obsolete("Use named parameter format 'ExpectedMessage=...'", false)]
public ExpectedExceptionAttribute(Type exceptionType, string expectedMessage)
: this(exceptionType)
{
this.expectedMessage = expectedMessage;
this.matchType = MessageMatch.Exact;
}
///
/// Constructor for a given exception name and expected message text
///
/// The full name of the expected exception
/// The expected messge text
[Obsolete("Use named parameter format 'ExpectedMessage=...'", false)]
public ExpectedExceptionAttribute(string exceptionName, string expectedMessage)
: this(exceptionName)
{
this.expectedMessage = expectedMessage;
this.matchType = MessageMatch.Exact;
}
///
/// Gets or sets the expected exception type
///
public Type ExceptionType
{
get{ return expectedException; }
set{ expectedException = value; }
}
///
/// Gets or sets the full Type name of the expected exception
///
public string ExceptionName
{
get{ return expectedExceptionName; }
set{ expectedExceptionName = value; }
}
///
/// Gets or sets the expected message text
///
public string ExpectedMessage
{
get { return expectedMessage; }
set { expectedMessage = value; }
}
///
/// Gets or sets the user message displayed in case of failure
///
public string UserMessage
{
get { return userMessage; }
set { userMessage = value; }
}
///
/// Gets or sets the type of match to be performed on the expected message
///
public MessageMatch MatchType
{
get { return matchType; }
set { matchType = value; }
}
///
/// Gets the name of a method to be used as an exception handler
///
public string Handler
{
get { return handler; }
set { handler = value; }
}
}
}