~a-schlapsi/nunit-3.0/linux-makefile

« back to all changes in this revision

Viewing changes to src/framework/Internal/ExpectedExceptionProcessor.cs

  • Committer: Andreas Schlapsi
  • Date: 2010-01-23 23:14:05 UTC
  • mfrom: (18.1.137 work)
  • Revision ID: a.schlapsi@gmx.at-20100123231405-17deqoh18nfnbq1j
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
using System.Reflection;
26
26
using System.Text.RegularExpressions;
27
27
using NUnit.Framework;
 
28
using NUnit.Framework.Api;
28
29
 
29
 
namespace NUnit.Core
 
30
namespace NUnit.Framework.Internal
30
31
{
 
32
    /// <summary>
 
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.
 
37
    /// </summary>
31
38
    public class ExpectedExceptionProcessor
32
39
    {
33
40
        #region Fields
67
74
        internal string userMessage;
68
75
        #endregion
69
76
 
70
 
        #region Constructor
71
 
        public ExpectedExceptionProcessor( TestMethod testMethod )
72
 
        {
73
 
            this.testMethod = testMethod;
74
 
        }
 
77
        #region Constructors
75
78
 
 
79
        /// <summary>
 
80
        /// Initializes a new instance of the <see cref="ExpectedExceptionProcessor"/> class
 
81
        /// based on an ExpectedExceptionAttribute.
 
82
        /// </summary>
 
83
        /// <param name="testMethod">The test method.</param>
 
84
        /// <param name="source">The ExpectedExceptionAttribute.</param>
76
85
        public ExpectedExceptionProcessor(TestMethod testMethod, ExpectedExceptionAttribute source)
77
86
        {
78
87
            this.testMethod = testMethod;
100
109
            }
101
110
        }
102
111
 
103
 
        public ExpectedExceptionProcessor(TestMethod testMethod, Extensibility.ParameterSet source)
 
112
#if !NUNITLITE
 
113
        /// <summary>
 
114
        /// Initializes a new instance of the <see cref="ExpectedExceptionProcessor"/> class
 
115
        /// based on a ParameterSet.
 
116
        /// </summary>
 
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)
104
120
        {
105
121
            this.testMethod = testMethod;
106
122
 
111
127
 
112
128
            this.exceptionHandler = GetDefaultExceptionHandler(testMethod.FixtureType);
113
129
        }
 
130
#endif
 
131
 
114
132
        #endregion
115
133
 
116
134
        #region Public Methods
 
135
        /// <summary>
 
136
        /// Handles processing when no exception was thrown.
 
137
        /// </summary>
 
138
        /// <param name="testResult">The test result.</param>
117
139
        public void ProcessNoException(TestResult testResult)
118
140
        {
119
 
            testResult.Failure(NoExceptionMessage(), null);
 
141
            testResult.SetResult(ResultState.Failure, NoExceptionMessage());
120
142
        }
121
143
 
 
144
        /// <summary>
 
145
        /// Handles processing when an exception was thrown.
 
146
        /// </summary>
 
147
        /// <param name="exception">The exception.</param>
 
148
        /// <param name="testResult">The test result.</param>
122
149
        public void ProcessException(Exception exception, TestResult testResult)
123
150
        {
124
151
            if (exception is NUnitException)
131
158
                    if (exceptionHandler != null)
132
159
                        Reflect.InvokeMethod(exceptionHandler, testMethod.Fixture, exception);
133
160
 
134
 
                    testResult.Success();
 
161
                    testResult.SetResult(ResultState.Success);
135
162
                }
136
163
                else
137
164
                {
138
 
                    testResult.Failure(WrongTextMessage(exception), GetStackTrace(exception));
 
165
#if NETCF_1_0
 
166
                    testResult.SetResult(ResultState.Failure, WrongTextMessage(exception));
 
167
#else
 
168
                    testResult.SetResult(ResultState.Failure, WrongTextMessage(exception), GetStackTrace(exception));
 
169
#endif
139
170
                }
140
171
            }
141
172
            else
142
173
            {
143
 
                switch (NUnitFramework.GetResultState(exception))
144
 
                {
145
 
                    case ResultState.Failure:
146
 
                        testResult.Failure(exception.Message, exception.StackTrace);
147
 
                        break;
148
 
                    case ResultState.Ignored:
149
 
                        testResult.Ignore(exception);
150
 
                        break;
151
 
                    case ResultState.Inconclusive:
152
 
                        testResult.SetResult(ResultState.Inconclusive, exception);
153
 
                        break;
154
 
                    case ResultState.Success:
155
 
                        testResult.Success(exception.Message);
156
 
                        break;
157
 
                    default:
158
 
                        testResult.Failure(WrongTypeMessage(exception), GetStackTrace(exception));
159
 
                        break;
160
 
                }
 
174
                testResult.RecordException(exception);
 
175
 
 
176
                // If it shows as an error, change it to a failure due to the wrong type
 
177
                if (testResult.ResultState == ResultState.Error)
 
178
#if NETCF_1_0
 
179
                    testResult.SetResult(ResultState.Failure, WrongTypeMessage(exception));
 
180
#else
 
181
                    testResult.SetResult(ResultState.Failure, WrongTypeMessage(exception), GetStackTrace(exception));
 
182
#endif
161
183
            }
162
184
                }
163
185
        #endregion
197
219
        private string WrongTypeMessage(Exception exception)
198
220
        {
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);
203
225
        }
204
226
 
223
245
            }
224
246
 
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);
229
251
        }
230
252
 
232
254
        {
233
255
            if (userMessage == null)
234
256
                return message;
235
 
            return userMessage + Environment.NewLine + message;
 
257
            return userMessage + Env.NewLine + message;
236
258
        }
237
259
 
 
260
#if !NETCF_1_0
238
261
        private string GetStackTrace(Exception exception)
239
262
        {
240
263
            try
246
269
                return "No stack trace available";
247
270
            }
248
271
        }
 
272
#endif
249
273
 
250
274
        private static MethodInfo GetDefaultExceptionHandler(Type fixtureType)
251
275
        {