@@ -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,32 @@ 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
+ }
319
359
}
320
360
321
361
foreach ( var outputBuilder in _outputBuilderFactory . OutputBuilders )
@@ -359,11 +399,18 @@ public void Close()
359
399
360
400
Debug . Assert ( stream is not null ) ;
361
401
CloseOutputBuilder ( stream , outputBuilder , isMethodClass , leaveStreamOpen , emitNamespaceDeclaration ) ;
402
+
403
+ if ( testStream is not null )
404
+ {
405
+ CloseOutputBuilder ( testStream , outputBuilder , isMethodClass , leaveStreamOpen , emitNamespaceDeclaration ) ;
406
+ }
407
+
362
408
emitNamespaceDeclaration = false ;
363
409
364
410
if ( _config . GenerateMultipleFiles )
365
411
{
366
412
stream = null ;
413
+ Debug . Assert ( testStream is null ) ;
367
414
}
368
415
}
369
416
@@ -400,7 +447,7 @@ public void Close()
400
447
401
448
foreach ( var entry in methodClassTestOutputBuilders )
402
449
{
403
- CloseOutputBuilder ( stream , entry . Value , isMethodClass : true , leaveStreamOpen , emitNamespaceDeclaration ) ;
450
+ CloseOutputBuilder ( testStream ?? stream , entry . Value , isMethodClass : true , leaveStreamOpen , emitNamespaceDeclaration ) ;
404
451
}
405
452
406
453
using var sw = new StreamWriter ( stream , s_defaultStreamWriterEncoding , DefaultStreamWriterBufferSize , leaveStreamOpen ) ;
@@ -415,6 +462,17 @@ public void Close()
415
462
sw . WriteLine ( " </namespace>" ) ;
416
463
sw . WriteLine ( "</bindings>" ) ;
417
464
}
465
+
466
+ if ( testStream is not null )
467
+ {
468
+ using var tsw = new StreamWriter ( testStream , s_defaultStreamWriterEncoding , DefaultStreamWriterBufferSize , leaveStreamOpen ) ;
469
+ tsw . NewLine = "\n " ;
470
+
471
+ if ( _config . OutputMode == PInvokeGeneratorOutputMode . CSharp )
472
+ {
473
+ tsw . WriteLine ( '}' ) ;
474
+ }
475
+ }
418
476
}
419
477
420
478
_context . Clear ( ) ;
0 commit comments