25
25
using System.Reflection;
26
26
using System.Runtime.Serialization;
27
using NUnit.Framework;
27
using NUnit.Framework.Api;
28
using NUnit.Framework.Internal;
28
29
using NUnit.TestUtilities;
29
30
using NUnit.TestData.ExpectedExceptionData;
31
namespace NUnit.Core.Tests
32
namespace NUnit.Framework.Attributes
146
147
Type fixtureType = typeof(BaseException);
147
148
Test test = TestBuilder.MakeTestCase( fixtureType, "BaseExceptionTest" );
148
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
149
Assert.IsTrue(result.IsFailure, "BaseExceptionTest should have failed");
149
TestResult result = test.Run(TestListener.NULL);
150
Assert.IsTrue(result.ResultState == ResultState.Failure, "BaseExceptionTest should have failed");
150
151
StringAssert.StartsWith(
151
152
"An unexpected exception type was thrown" + Environment.NewLine +
152
153
"Expected: System.ArgumentException" + Environment.NewLine +
160
161
Type fixtureType = typeof(DerivedException);
161
162
Test test = TestBuilder.MakeTestCase( fixtureType, "DerivedExceptionTest" );
162
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
163
Assert.IsTrue(result.IsFailure, "DerivedExceptionTest should have failed");
163
TestResult result = test.Run(TestListener.NULL);
164
Assert.IsTrue(result.ResultState == ResultState.Failure, "DerivedExceptionTest should have failed");
164
165
StringAssert.StartsWith(
165
166
"An unexpected exception type was thrown" + Environment.NewLine +
166
167
"Expected: System.Exception" + Environment.NewLine +
174
175
Type fixtureType = typeof(MismatchedException);
175
176
Test test = TestBuilder.MakeTestCase(fixtureType, "MismatchedExceptionType");
176
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
177
Assert.IsTrue(result.IsFailure, "MismatchedExceptionType should have failed");
177
TestResult result = test.Run(TestListener.NULL);
178
Assert.IsTrue(result.ResultState == ResultState.Failure, "MismatchedExceptionType should have failed");
178
179
StringAssert.StartsWith(
179
180
"An unexpected exception type was thrown" + Environment.NewLine +
180
181
"Expected: System.ArgumentException" + Environment.NewLine +
188
189
Type fixtureType = typeof(MismatchedException);
189
190
Test test = TestBuilder.MakeTestCase(fixtureType, "MismatchedExceptionTypeAsNamedParameter");
190
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
191
Assert.IsTrue(result.IsFailure, "MismatchedExceptionType should have failed");
191
TestResult result = test.Run(TestListener.NULL);
192
Assert.IsTrue(result.ResultState == ResultState.Failure, "MismatchedExceptionType should have failed");
192
193
StringAssert.StartsWith(
193
194
"An unexpected exception type was thrown" + Environment.NewLine +
194
195
"Expected: System.ArgumentException" + Environment.NewLine +
202
203
Type fixtureType = typeof(MismatchedException);
203
204
Test test = TestBuilder.MakeTestCase( fixtureType, "MismatchedExceptionTypeWithUserMessage" );
204
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
205
Assert.IsTrue(result.IsFailure, "Test method should have failed");
205
TestResult result = test.Run(TestListener.NULL);
206
Assert.IsTrue(result.ResultState == ResultState.Failure, "Test method should have failed");
206
207
StringAssert.StartsWith(
207
208
"custom message" + Environment.NewLine +
208
209
"An unexpected exception type was thrown" + Environment.NewLine +
217
218
Type fixtureType = typeof(MismatchedException);
218
219
Test test = TestBuilder.MakeTestCase( fixtureType, "MismatchedExceptionName" );
219
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
220
Assert.IsTrue(result.IsFailure, "MismatchedExceptionName should have failed");
220
TestResult result = test.Run(TestListener.NULL);
221
Assert.IsTrue(result.ResultState == ResultState.Failure, "MismatchedExceptionName should have failed");
221
222
StringAssert.StartsWith(
222
223
"An unexpected exception type was thrown" + Environment.NewLine +
223
224
"Expected: System.ArgumentException" + Environment.NewLine +
231
232
Type fixtureType = typeof(MismatchedException);
232
233
Test test = TestBuilder.MakeTestCase( fixtureType, "MismatchedExceptionNameWithUserMessage" );
233
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
234
Assert.IsTrue(result.IsFailure, "Test method should have failed");
234
TestResult result = test.Run(TestListener.NULL);
235
Assert.IsTrue(result.ResultState == ResultState.Failure, "Test method should have failed");
235
236
StringAssert.StartsWith(
236
237
"custom message" + Environment.NewLine +
237
238
"An unexpected exception type was thrown" + Environment.NewLine +
246
247
Type fixtureType = typeof(TestThrowsExceptionWithWrongMessage);
247
248
Test test = TestBuilder.MakeTestCase( fixtureType, "TestThrow" );
248
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
249
Assert.IsTrue(result.IsFailure, "TestThrow should have failed");
249
TestResult result = test.Run(TestListener.NULL);
250
Assert.IsTrue(result.ResultState == ResultState.Failure, "TestThrow should have failed");
251
252
"The exception message text was incorrect" + Environment.NewLine +
252
253
"Expected: not the message" + Environment.NewLine +
260
261
Type fixtureType = typeof(TestThrowsExceptionWithWrongMessage);
261
262
Test test = TestBuilder.MakeTestCase( fixtureType, "TestThrowWithUserMessage" );
262
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
263
Assert.IsTrue(result.IsFailure, "TestThrow should have failed");
263
TestResult result = test.Run(TestListener.NULL);
264
Assert.IsTrue(result.ResultState == ResultState.Failure, "TestThrow should have failed");
265
266
"custom message" + Environment.NewLine +
266
267
"The exception message text was incorrect" + Environment.NewLine +
275
276
Type fixtureType = typeof(TestDoesNotThrowExceptionFixture);
276
277
Test test = TestBuilder.MakeTestCase( fixtureType, "TestDoesNotThrowUnspecifiedException" );
277
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
278
Assert.IsTrue(result.IsFailure, "Test method should have failed");
278
TestResult result = test.Run(TestListener.NULL);
279
Assert.IsTrue(result.ResultState == ResultState.Failure, "Test method should have failed");
279
280
Assert.AreEqual("An Exception was expected", result.Message);
285
286
Type fixtureType = typeof(TestDoesNotThrowExceptionFixture);
286
287
Test test = TestBuilder.MakeTestCase( fixtureType, "TestDoesNotThrowUnspecifiedExceptionWithUserMessage" );
287
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
288
Assert.IsTrue(result.IsFailure, "Test method should have failed");
288
TestResult result = test.Run(TestListener.NULL);
289
Assert.IsTrue(result.ResultState == ResultState.Failure, "Test method should have failed");
289
290
Assert.AreEqual("custom message" + Environment.NewLine + "An Exception was expected", result.Message);
295
296
Type fixtureType = typeof(TestDoesNotThrowExceptionFixture);
296
297
Test test = TestBuilder.MakeTestCase( fixtureType, "TestDoesNotThrowExceptionType" );
297
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
298
Assert.IsTrue(result.IsFailure, "Test method should have failed");
298
TestResult result = test.Run(TestListener.NULL);
299
Assert.IsTrue(result.ResultState == ResultState.Failure, "Test method should have failed");
299
300
Assert.AreEqual("System.ArgumentException was expected", result.Message);
305
306
Type fixtureType = typeof(TestDoesNotThrowExceptionFixture);
306
307
Test test = TestBuilder.MakeTestCase( fixtureType, "TestDoesNotThrowExceptionTypeWithUserMessage" );
307
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
308
Assert.IsTrue(result.IsFailure, "Test method should have failed");
308
TestResult result = test.Run(TestListener.NULL);
309
Assert.IsTrue(result.ResultState == ResultState.Failure, "Test method should have failed");
309
310
Assert.AreEqual("custom message" + Environment.NewLine + "System.ArgumentException was expected", result.Message);
315
316
Type fixtureType = typeof(TestDoesNotThrowExceptionFixture);
316
317
Test test = TestBuilder.MakeTestCase( fixtureType, "TestDoesNotThrowExceptionName" );
317
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
318
Assert.IsTrue(result.IsFailure, "Test method should have failed");
318
TestResult result = test.Run(TestListener.NULL);
319
Assert.IsTrue(result.ResultState == ResultState.Failure, "Test method should have failed");
319
320
Assert.AreEqual("System.ArgumentException was expected", result.Message);
325
326
Type fixtureType = typeof(TestDoesNotThrowExceptionFixture);
326
327
Test test = TestBuilder.MakeTestCase( fixtureType, "TestDoesNotThrowExceptionNameWithUserMessage" );
327
TestResult result = test.Run(TestListener.NULL, TestFilter.Empty);
328
Assert.IsTrue(result.IsFailure, "Test method should have failed");
328
TestResult result = test.Run(TestListener.NULL);
329
Assert.IsTrue(result.ResultState == ResultState.Failure, "Test method should have failed");
329
330
Assert.AreEqual("custom message" + Environment.NewLine + "System.ArgumentException was expected", result.Message);
333
334
public void MethodThrowsException()
335
336
TestResult result = TestBuilder.RunTestFixture( typeof( TestThrowsExceptionFixture ) );
336
Assert.AreEqual(true, result.IsFailure);
337
Assert.AreEqual(true, result.ResultState == ResultState.Failure);
340
341
public void MethodThrowsRightExceptionMessage()
342
343
TestResult result = TestBuilder.RunTestFixture( typeof( TestThrowsExceptionWithRightMessage ) );
343
Assert.AreEqual(true, result.IsSuccess);
344
Assert.AreEqual(true, result.ResultState == ResultState.Success);
347
348
public void MethodThrowsArgumentOutOfRange()
349
350
TestResult result = TestBuilder.RunTestFixture( typeof( TestThrowsArgumentOutOfRangeException ) );
350
Assert.AreEqual(true, result.IsSuccess );
351
Assert.AreEqual(true, result.ResultState == ResultState.Success);
354
355
public void MethodThrowsWrongExceptionMessage()
356
357
TestResult result = TestBuilder.RunTestFixture( typeof( TestThrowsExceptionWithWrongMessage ) );
357
Assert.AreEqual(true, result.IsFailure);
358
Assert.AreEqual(true, result.ResultState == ResultState.Failure);
361
362
public void SetUpThrowsSameException()
363
364
TestResult result = TestBuilder.RunTestFixture( typeof( SetUpExceptionTests ) );
364
Assert.AreEqual(true, result.IsFailure);
365
Assert.AreEqual(true, result.ResultState == ResultState.Failure);
368
369
public void TearDownThrowsSameException()
370
371
TestResult result = TestBuilder.RunTestFixture( typeof( TearDownExceptionTests ) );
371
Assert.AreEqual(true, result.IsFailure);
372
Assert.AreEqual(true, result.ResultState == ResultState.Failure);
421
422
ExceptionHandlerCalledClass fixture = new ExceptionHandlerCalledClass();
422
423
Test test = TestBuilder.MakeTestCase( fixture, "ThrowsArgumentException_AlternateHandler" );
423
test.Run(TestListener.NULL, TestFilter.Empty);
424
test.Run(TestListener.NULL);
424
425
Assert.IsFalse(fixture.HandlerCalled, "Base Handler should not be called" );
425
426
Assert.IsTrue(fixture.AlternateHandlerCalled, "Alternate Handler should be called" );
431
432
ExceptionHandlerCalledClass fixture = new ExceptionHandlerCalledClass();
432
433
Test test = TestBuilder.MakeTestCase( fixture, "ThrowsArgumentException" );
433
test.Run(TestListener.NULL, TestFilter.Empty);
434
test.Run(TestListener.NULL);
434
435
Assert.IsTrue(fixture.HandlerCalled, "Base Handler should be called");
435
436
Assert.IsFalse(fixture.AlternateHandlerCalled, "Alternate Handler should not be called");
441
442
ExceptionHandlerCalledClass fixture = new ExceptionHandlerCalledClass();
442
443
Test test = TestBuilder.MakeTestCase( fixture, "ThrowsApplicationException" );
443
test.Run(TestListener.NULL, TestFilter.Empty);
444
test.Run(TestListener.NULL);
444
445
Assert.IsFalse( fixture.HandlerCalled, "Base Handler should not be called" );
445
446
Assert.IsFalse( fixture.AlternateHandlerCalled, "Alternate Handler should not be called" );
451
452
ExceptionHandlerCalledClass fixture = new ExceptionHandlerCalledClass();
452
453
Test test = TestBuilder.MakeTestCase( fixture, "ThrowsApplicationException_AlternateHandler" );
453
test.Run(TestListener.NULL, TestFilter.Empty);
454
test.Run(TestListener.NULL);
454
455
Assert.IsFalse(fixture.HandlerCalled, "Base Handler should not be called");
455
456
Assert.IsFalse(fixture.AlternateHandlerCalled, "Alternate Handler should not be called");