@@ -166,7 +166,7 @@ public static void Expect<T>(string expected, Func<string[], T> func, T expected
166166
167167 if ( ! expectedReturn . Equals ( @return ) )
168168 {
169- throw new Exception ( $ "The value returned from { nameof ( func ) } ({ @return } ) was not the { nameof ( expectedReturn ) } ({ expectedReturn } ) value.") ;
169+ throw new ConsoleAssertException ( $ "The value returned from { nameof ( func ) } ({ @return } ) was not the { nameof ( expectedReturn ) } ({ expectedReturn } ) value.") ;
170170 }
171171 }
172172
@@ -468,7 +468,7 @@ private static void AssertExpectation(string expectedOutput, string output, Func
468468 bool failTest = ! areEquivalentOperator ( expectedOutput , output ) ;
469469 if ( failTest )
470470 {
471- throw new Exception ( GetMessageText ( expectedOutput , output , equivalentOperatorErrorMessage ) ) ;
471+ throw new ConsoleAssertException ( GetMessageText ( expectedOutput , output , equivalentOperatorErrorMessage ) ) ;
472472 }
473473 }
474474
@@ -739,16 +739,21 @@ public static TException ExpectThrows<TException>(string expected,
739739 NormalizeOptions normalizeOptions = NormalizeOptions . Default )
740740 where TException : Exception
741741 {
742- try
743- {
744- Expect ( expected , action , normalizeOptions ) ;
745- }
746- catch ( TException ex )
742+ ( string input , string expectedOutput ) = Parse ( expected ) ;
743+ TException caughtException = null ;
744+
745+ string output = Execute ( input , ( ) =>
747746 {
748- return ex ;
749- }
747+ try { action ( ) ; }
748+ catch ( TException ex ) { caughtException = ex ; }
749+ } ) ;
750+
751+ if ( caughtException is null )
752+ throw new ConsoleAssertException ( $ "Expected exception of type { typeof ( TException ) . Name } was not thrown.") ;
753+
754+ CompareOutput ( output , expectedOutput , normalizeOptions , ( l , r ) => l == r , "Values are not equal" ) ;
750755
751- throw new Exception ( $ "Expected exception of type { typeof ( TException ) . Name } was not thrown." ) ;
756+ return caughtException ;
752757 }
753758
754759 /// <summary>
@@ -768,15 +773,20 @@ public static async Task<TException> ExpectThrowsAsync<TException>(string expect
768773 NormalizeOptions normalizeOptions = NormalizeOptions . Default )
769774 where TException : Exception
770775 {
771- try
772- {
773- await ExpectAsync ( expected , action , normalizeOptions ) ;
774- }
775- catch ( TException ex )
776+ ( string input , string expectedOutput ) = Parse ( expected ) ;
777+ TException caughtException = null ;
778+
779+ string output = await ExecuteAsync ( input , async ( ) =>
776780 {
777- return ex ;
778- }
781+ try { await action ( ) ; }
782+ catch ( TException ex ) { caughtException = ex ; }
783+ } ) ;
784+
785+ if ( caughtException is null )
786+ throw new ConsoleAssertException ( $ "Expected exception of type { typeof ( TException ) . Name } was not thrown.") ;
787+
788+ CompareOutput ( output , expectedOutput , normalizeOptions , ( l , r ) => l == r , "Values are not equal" ) ;
779789
780- throw new Exception ( $ "Expected exception of type { typeof ( TException ) . Name } was not thrown." ) ;
790+ return caughtException ;
781791 }
782- }
792+ }
0 commit comments