@@ -210,6 +210,7 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func<string, Strea
210
210
public void Close ( )
211
211
{
212
212
Stream ? stream = null ;
213
+ Stream ? testStream = null ;
213
214
214
215
var methodClassOutputBuilders = new Dictionary < string , IOutputBuilder > ( ) ;
215
216
var methodClassTestOutputBuilders = new Dictionary < string , IOutputBuilder > ( ) ;
@@ -237,11 +238,16 @@ public void Close()
237
238
{
238
239
var outputPath = _config . OutputLocation ;
239
240
stream = _outputStreamFactory ( outputPath ) ;
241
+
242
+ var testOutputPath = _config . TestOutputLocation ;
243
+ testStream = _outputStreamFactory ( testOutputPath ) ;
244
+
240
245
leaveStreamOpen = true ;
241
246
242
247
var usingDirectives = new SortedSet < string > ( StringComparer . Ordinal ) ;
243
248
var staticUsingDirectives = new SortedSet < string > ( StringComparer . Ordinal ) ;
244
249
var hasAnyContents = false ;
250
+ var testHasAnyContents = false ;
245
251
246
252
foreach ( var outputBuilder in _outputBuilderFactory . OutputBuilders )
247
253
{
@@ -257,11 +263,19 @@ public void Close()
257
263
_ = staticUsingDirectives . Add ( staticUsingDirective ) ;
258
264
}
259
265
260
- hasAnyContents = csharpOutputBuilder . Contents . Any ( ) ;
266
+ if ( csharpOutputBuilder . IsTestOutput )
267
+ {
268
+ testHasAnyContents |= csharpOutputBuilder . Contents . Any ( ) ;
269
+ }
270
+ else
271
+ {
272
+ hasAnyContents |= csharpOutputBuilder . Contents . Any ( ) ;
273
+ }
261
274
}
262
275
else if ( outputBuilder is XmlOutputBuilder xmlOutputBuilder )
263
276
{
264
- hasAnyContents = xmlOutputBuilder . Contents . Any ( ) ;
277
+ Debug . Assert ( ! xmlOutputBuilder . IsTestOutput ) ;
278
+ hasAnyContents |= xmlOutputBuilder . Contents . Any ( ) ;
265
279
}
266
280
}
267
281
@@ -316,6 +330,36 @@ public void Close()
316
330
}
317
331
}
318
332
}
333
+
334
+ if ( testHasAnyContents )
335
+ {
336
+ using var sw = new StreamWriter ( testStream , s_defaultStreamWriterEncoding , DefaultStreamWriterBufferSize , leaveStreamOpen ) ;
337
+ sw . NewLine = "\n " ;
338
+
339
+ if ( _config . OutputMode == PInvokeGeneratorOutputMode . CSharp )
340
+ {
341
+ if ( ! string . IsNullOrEmpty ( _config . HeaderText ) )
342
+ {
343
+ sw . WriteLine ( _config . HeaderText ) ;
344
+ }
345
+
346
+ if ( usingDirectives . Count != 0 )
347
+ {
348
+ foreach ( var usingDirective in usingDirectives )
349
+ {
350
+ sw . Write ( "using " ) ;
351
+ sw . Write ( usingDirective ) ;
352
+ sw . WriteLine ( ';' ) ;
353
+ }
354
+
355
+ sw . WriteLine ( ) ;
356
+ }
357
+ }
358
+ }
359
+ else
360
+ {
361
+ testStream = null ;
362
+ }
319
363
}
320
364
321
365
foreach ( var outputBuilder in _outputBuilderFactory . OutputBuilders )
@@ -359,11 +403,18 @@ public void Close()
359
403
360
404
Debug . Assert ( stream is not null ) ;
361
405
CloseOutputBuilder ( stream , outputBuilder , isMethodClass , leaveStreamOpen , emitNamespaceDeclaration ) ;
406
+
407
+ if ( testStream is not null )
408
+ {
409
+ CloseOutputBuilder ( testStream , outputBuilder , isMethodClass , leaveStreamOpen , emitNamespaceDeclaration ) ;
410
+ }
411
+
362
412
emitNamespaceDeclaration = false ;
363
413
364
414
if ( _config . GenerateMultipleFiles )
365
415
{
366
416
stream = null ;
417
+ Debug . Assert ( testStream is null ) ;
367
418
}
368
419
}
369
420
@@ -400,7 +451,7 @@ public void Close()
400
451
401
452
foreach ( var entry in methodClassTestOutputBuilders )
402
453
{
403
- CloseOutputBuilder ( stream , entry . Value , isMethodClass : true , leaveStreamOpen , emitNamespaceDeclaration ) ;
454
+ CloseOutputBuilder ( testStream ?? stream , entry . Value , isMethodClass : true , leaveStreamOpen , emitNamespaceDeclaration ) ;
404
455
}
405
456
406
457
using var sw = new StreamWriter ( stream , s_defaultStreamWriterEncoding , DefaultStreamWriterBufferSize , leaveStreamOpen ) ;
@@ -415,6 +466,17 @@ public void Close()
415
466
sw . WriteLine ( " </namespace>" ) ;
416
467
sw . WriteLine ( "</bindings>" ) ;
417
468
}
469
+
470
+ if ( testStream is not null )
471
+ {
472
+ using var tsw = new StreamWriter ( testStream , s_defaultStreamWriterEncoding , DefaultStreamWriterBufferSize , leaveStreamOpen ) ;
473
+ tsw . NewLine = "\n " ;
474
+
475
+ if ( _config . OutputMode == PInvokeGeneratorOutputMode . CSharp )
476
+ {
477
+ tsw . WriteLine ( '}' ) ;
478
+ }
479
+ }
418
480
}
419
481
420
482
_context . Clear ( ) ;
0 commit comments