25
25
using System.Reflection;
26
26
using System.Text.RegularExpressions;
27
27
using NUnit.Framework;
28
using NUnit.Framework.Api;
30
namespace NUnit.Framework.Internal
33
/// ExceptedException processor is used internally to handle the
34
/// evaluation of exceptions when a method is annotated with the
35
/// ExpectedExceptionAttribute or a ParameterSet specifies an
36
/// expected exception.
31
38
public class ExpectedExceptionProcessor
67
74
internal string userMessage;
71
public ExpectedExceptionProcessor( TestMethod testMethod )
73
this.testMethod = testMethod;
80
/// Initializes a new instance of the <see cref="ExpectedExceptionProcessor"/> class
81
/// based on an ExpectedExceptionAttribute.
83
/// <param name="testMethod">The test method.</param>
84
/// <param name="source">The ExpectedExceptionAttribute.</param>
76
85
public ExpectedExceptionProcessor(TestMethod testMethod, ExpectedExceptionAttribute source)
78
87
this.testMethod = testMethod;
103
public ExpectedExceptionProcessor(TestMethod testMethod, Extensibility.ParameterSet source)
114
/// Initializes a new instance of the <see cref="ExpectedExceptionProcessor"/> class
115
/// based on a ParameterSet.
117
/// <param name="testMethod">The test method.</param>
118
/// <param name="source">The ParameterSet.</param>
119
public ExpectedExceptionProcessor(TestMethod testMethod, NUnit.Core.Extensibility.ParameterSet source)
105
121
this.testMethod = testMethod;
112
128
this.exceptionHandler = GetDefaultExceptionHandler(testMethod.FixtureType);
116
134
#region Public Methods
136
/// Handles processing when no exception was thrown.
138
/// <param name="testResult">The test result.</param>
117
139
public void ProcessNoException(TestResult testResult)
119
testResult.Failure(NoExceptionMessage(), null);
141
testResult.SetResult(ResultState.Failure, NoExceptionMessage());
145
/// Handles processing when an exception was thrown.
147
/// <param name="exception">The exception.</param>
148
/// <param name="testResult">The test result.</param>
122
149
public void ProcessException(Exception exception, TestResult testResult)
124
151
if (exception is NUnitException)
131
158
if (exceptionHandler != null)
132
159
Reflect.InvokeMethod(exceptionHandler, testMethod.Fixture, exception);
134
testResult.Success();
161
testResult.SetResult(ResultState.Success);
138
testResult.Failure(WrongTextMessage(exception), GetStackTrace(exception));
166
testResult.SetResult(ResultState.Failure, WrongTextMessage(exception));
168
testResult.SetResult(ResultState.Failure, WrongTextMessage(exception), GetStackTrace(exception));
143
switch (NUnitFramework.GetResultState(exception))
145
case ResultState.Failure:
146
testResult.Failure(exception.Message, exception.StackTrace);
148
case ResultState.Ignored:
149
testResult.Ignore(exception);
151
case ResultState.Inconclusive:
152
testResult.SetResult(ResultState.Inconclusive, exception);
154
case ResultState.Success:
155
testResult.Success(exception.Message);
158
testResult.Failure(WrongTypeMessage(exception), GetStackTrace(exception));
174
testResult.RecordException(exception);
176
// If it shows as an error, change it to a failure due to the wrong type
177
if (testResult.ResultState == ResultState.Error)
179
testResult.SetResult(ResultState.Failure, WrongTypeMessage(exception));
181
testResult.SetResult(ResultState.Failure, WrongTypeMessage(exception), GetStackTrace(exception));
197
219
private string WrongTypeMessage(Exception exception)
199
221
return CombineWithUserMessage(
200
"An unexpected exception type was thrown" + Environment.NewLine +
201
"Expected: " + expectedExceptionName + Environment.NewLine +
222
"An unexpected exception type was thrown" + Env.NewLine +
223
"Expected: " + expectedExceptionName + Env.NewLine +
202
224
" but was: " + exception.GetType().FullName + " : " + exception.Message);
225
247
return CombineWithUserMessage(
226
"The exception message text was incorrect" + Environment.NewLine +
227
expectedText + expectedMessage + Environment.NewLine +
248
"The exception message text was incorrect" + Env.NewLine +
249
expectedText + expectedMessage + Env.NewLine +
228
250
" but was: " + exception.Message);