public void Invoke(Function function) { bool bMissingExpectedException = false; try { function.Invoke(); if(function.HasCustomAttribute("NUnit.Framework.ExpectedExceptionAttribute")) { bMissingExpectedException = true; } } catch (System.Exception ex) { if (!ConsumeExpectedException(function, ex)) { System.Exception nunitEx = GetNUnitException(ex); if (nunitEx != null) { throw nunitEx; } else { throw; } } } if (bMissingExpectedException) { System.Exception e = new System.Exception("Test failed to throw exception of type " + GetExpectedExceptionName(function) + " when it was expected to."); throw e; } }
private Type GetExpectedExceptionType(Function function) { if (function.HasCustomAttribute("NUnit.Framework.ExpectedExceptionAttribute")) { System.Attribute pAttributeExpectedException = function.GetAttribute("NUnit.Framework.ExpectedExceptionAttribute"); if (pAttributeExpectedException != null) { System.Type pExpectedExceptionType = GetExpectedExceptionType(function, pAttributeExpectedException); return pExpectedExceptionType; } } return null; }
private bool ConsumeExpectedException(Function function, System.Exception pEx) { if (function.HasCustomAttribute("NUnit.Framework.ExpectedExceptionAttribute")) { System.Attribute pAttributeExpectedException = function.GetAttribute("NUnit.Framework.ExpectedExceptionAttribute"); if (pAttributeExpectedException != null) { System.Type pExpectedExceptionType = GetExpectedExceptionType(function, pAttributeExpectedException); if (pExpectedExceptionType != null) { string pExpectedExceptionName = (System.String)function.GetProperty(pExpectedExceptionType, "FullName"); string pExpectedMessage = (System.String)function.GetProperty(pAttributeExpectedException, "ExpectedMessage"); if (pExpectedExceptionName != null) { System.Exception pTestEx = pEx; while (pTestEx != null) { if (pTestEx.ToString().StartsWith(pExpectedExceptionName)) { if ((pExpectedMessage == null) || (pTestEx.Message.CompareTo(pExpectedMessage) == 0)) { return true; } } pTestEx = pTestEx.InnerException; } } } } } return false; }