@@ -95,11 +95,19 @@ bool ValidateOutput()
95
95
96
96
var oldOut = Console . Out ;
97
97
List < string > actualLines ;
98
+ Exception ? actualException = null ;
98
99
try
99
100
{
100
101
var builder = new StringBuilder ( ) ;
101
102
Console . SetOut ( new StringWriter ( builder ) ) ;
102
- method . Invoke ( null , arguments ) ;
103
+ try
104
+ {
105
+ method . Invoke ( null , arguments ) ;
106
+ }
107
+ catch ( TargetInvocationException outer )
108
+ {
109
+ actualException = outer . InnerException ?? throw new InvalidOperationException ( "TargetInvocationException had no nested exception" ) ;
110
+ }
103
111
// Skip blank lines, to avoid unnecessary trailing empties.
104
112
actualLines = builder . ToString ( ) . Replace ( "\r \n " , "\n " ) . Split ( '\n ' ) . Where ( line => line != "" ) . ToList ( ) ;
105
113
}
@@ -108,7 +116,31 @@ bool ValidateOutput()
108
116
Console . SetOut ( oldOut ) ;
109
117
}
110
118
var expectedLines = Metadata . ExpectedOutput ?? new List < string > ( ) ;
111
- return ValidateExpectedAgainstActual ( "output" , expectedLines , actualLines ) ;
119
+ return ValidateException ( actualException , Metadata . ExpectedException ) &&
120
+ ValidateExpectedAgainstActual ( "output" , expectedLines , actualLines ) ;
121
+ }
122
+
123
+ bool ValidateException ( Exception ? actualException , string ? expectedExceptionName )
124
+ {
125
+ return ( actualException , expectedExceptionName ) switch
126
+ {
127
+ ( null , null ) => true ,
128
+ ( Exception ex , string name ) =>
129
+ MaybeReportError ( ex . GetType ( ) . Name == name , $ " Mismatched exception type: Expected { name } ; Was { ex . GetType ( ) . Name } ") ,
130
+ ( null , string name ) =>
131
+ MaybeReportError ( false , $ " Expected exception type { name } ; no exception was thrown") ,
132
+ ( Exception ex , null ) =>
133
+ MaybeReportError ( false , $ " Exception type { ex . GetType ( ) . Name } was thrown unexpectedly; Message: { ex . Message } ")
134
+ } ;
135
+
136
+ bool MaybeReportError ( bool result , string message )
137
+ {
138
+ if ( ! result )
139
+ {
140
+ Console . WriteLine ( message ) ;
141
+ }
142
+ return result ;
143
+ }
112
144
}
113
145
114
146
bool ValidateExpectedAgainstActual ( string type , List < string > expected , List < string > actual )
0 commit comments