You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#define MsTestUnitTests
#if MsTestUnitTestsnamespaceMicroshaoft{usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingSystem;//using static Microsoft.QualityTools.Testing.Fakes.FakesDelegates;publicstaticclassAssertHelper{privateconststring_beginSpliterLineOfMessageBlock="<<<<<<<<<<<<<<<<<<<<<<<<<<";privateconststring_endSpliterLineOfMessageBlock=">>>>>>>>>>>>>>>>>>>>>>>>>>";privatestaticExceptiondrillDownInnerException(thisException@this,Func<Exception,bool>onProcessFunc=null!,boolneedDrillDownInnerException=true){varresult=@this;while(result!=null){if(onProcessFunc!=null){bool@return=onProcessFunc(result);if(@return){break;}}if(!needDrillDownInnerException){break;}result=result.InnerException;}returnresult!;}privatestaticvoidprocessExpectedExceptionMessage(Exceptionexception,stringexpectedExceptionMessage=null!){if(!string.IsNullOrEmpty(expectedExceptionMessage)){Assert.IsTrue(string.Compare(expectedExceptionMessage,exception.Message,true)==0,$@"Expected exception of type {exception.GetType()} with a message of ""{expectedExceptionMessage}"" but expected exception with actual message of ""{exception.Message}"" was thrown instead.The caught actual ""{exception.GetType()}"" as below:{_beginSpliterLineOfMessageBlock}{exception}{_endSpliterLineOfMessageBlock}");}}publicstaticvoidThrows<TExpectedException>(thisAssert@this,Actionaction,stringexpectedExceptionMessage=null!,Action<TExpectedException>onProcessAction=null!,boolneedDrillDownInnerExceptions=true)whereTExpectedException:Exception{Action<Exception>actionExceptionProcess=null!;if(onProcessAction!=null){actionExceptionProcess=(e)=>{onProcessAction((TExpectedException)e);};}Throws(@this,action,typeof(TExpectedException),expectedExceptionMessage,actionExceptionProcess,needDrillDownInnerExceptions);}publicstaticvoidThrows(thisAssert@this,Actionaction,TypeexpectedExceptionType,stringexpectedExceptionMessage=null!,Action<Exception>onProcessAction=null!,boolneedDrillDownInnerExceptions=true){ExceptioncaughtException=null!;ExceptioncaughtExpectedException=null!;ExceptioncaughtExpectedExceptionWithMessage=null!;varfoundCaughtExpectedExceptionWithMessage=false;voiddrillDownInnerExceptionProcess(Exceptione){caughtExpectedExceptionWithMessage=e.drillDownInnerException((ee)=>{if(//ee.GetType() == expectedExceptionTypeexpectedExceptionType.IsAssignableFrom(ee.GetType())){caughtExpectedException=ee;foundCaughtExpectedExceptionWithMessage=(string.Compare(expectedExceptionMessage,ee.Message,true)==0||string.IsNullOrEmpty(expectedExceptionMessage));}returnfoundCaughtExpectedExceptionWithMessage;},needDrillDownInnerExceptions);}try{action();}catch(AggregateExceptionaggregateException){caughtException=aggregateException;if(caughtException.GetType()==expectedExceptionType){if(string.Compare(expectedExceptionMessage,caughtException.Message,true)==0||string.IsNullOrEmpty(expectedExceptionMessage)){caughtExpectedExceptionWithMessage=caughtException;caughtExpectedException=caughtException;foundCaughtExpectedExceptionWithMessage=true;}}if(!foundCaughtExpectedExceptionWithMessage){if(needDrillDownInnerExceptions){if(caughtException.InnerException!=null){drillDownInnerExceptionProcess(caughtException.InnerException);if(!foundCaughtExpectedExceptionWithMessage){caughtExpectedExceptionWithMessage=null!;}}}}if(!foundCaughtExpectedExceptionWithMessage){varinnerExceptions=aggregateException.Flatten().InnerExceptions;if(needDrillDownInnerExceptions){foreach(vareininnerExceptions){drillDownInnerExceptionProcess(e);if(foundCaughtExpectedExceptionWithMessage){break;}}}if(!foundCaughtExpectedExceptionWithMessage){caughtExpectedExceptionWithMessage=null!;}}}//catch (TExpectedException expectedException)//{// caughtException = expectedException;// caughtExpectedException = expectedException;// drillDownInnerExceptionProcess(caughtExpectedException);// if (!foundCaughtExpectedException)// {// caughtExpectedException = null!;// }//}catch(Exceptionexception){caughtException=exception;caughtExpectedException=exception;drillDownInnerExceptionProcess(caughtExpectedException);if(!foundCaughtExpectedExceptionWithMessage){caughtExpectedExceptionWithMessage=null!;}}if(caughtException==null){Assert.Fail($@"Expected exception of type ""{expectedExceptionType}"" but no exception was thrown.");}else{if(!foundCaughtExpectedExceptionWithMessage&&caughtExpectedException!=null){processExpectedExceptionMessage(caughtExpectedException,expectedExceptionMessage);}Assert.IsTrue(foundCaughtExpectedExceptionWithMessage,$@"Expected exception of type ""{expectedExceptionType}"" but actual type of ""{caughtException.GetType()}"" was thrown instead.The caught actual ""{caughtException.GetType()}"" as below:{_beginSpliterLineOfMessageBlock}{caughtException}{_endSpliterLineOfMessageBlock}");onProcessAction?.Invoke(caughtExpectedExceptionWithMessage);}}}}
#endif
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: