@@ -1357,20 +1357,7 @@ private void VisitRecordDecl(RecordDecl recordDecl)
1357
1357
1358
1358
var isTopLevelStruct = _config . WithTypes . TryGetValue ( name , out var withType ) && withType . Equals ( "struct" , StringComparison . Ordinal ) ;
1359
1359
var generateTestsClass = ! recordDecl . IsAnonymousStructOrUnion && recordDecl . DeclContext is not RecordDecl ;
1360
-
1361
- if ( ( _testOutputBuilder is not null ) && generateTestsClass && ! isTopLevelStruct )
1362
- {
1363
- _testOutputBuilder . WriteIndented ( "/// <summary>Provides validation of the <see cref=\" " ) ;
1364
- _testOutputBuilder . Write ( escapedName ) ;
1365
- _testOutputBuilder . WriteLine ( "\" /> struct.</summary>" ) ;
1366
-
1367
- WithAttributes ( recordDecl , onlySupportedOSPlatform : true , isTestOutput : true ) ;
1368
-
1369
- _testOutputBuilder . WriteIndented ( "public static unsafe partial class " ) ;
1370
- _testOutputBuilder . Write ( escapedName ) ;
1371
- _testOutputBuilder . WriteLine ( "Tests" ) ;
1372
- _testOutputBuilder . WriteBlockStart ( ) ;
1373
- }
1360
+ var testOutputStarted = false ;
1374
1361
1375
1362
var nullableUuid = ( Guid ? ) null ;
1376
1363
var uuidName = "" ;
@@ -1384,6 +1371,8 @@ private void VisitRecordDecl(RecordDecl recordDecl)
1384
1371
1385
1372
if ( ( _testOutputBuilder is not null ) && ( uuid != Guid . Empty ) )
1386
1373
{
1374
+ StartTestOutput ( ref testOutputStarted , generateTestsClass , isTopLevelStruct ) ;
1375
+
1387
1376
var className = GetClass ( uuidName ) ;
1388
1377
1389
1378
_testOutputBuilder . AddUsingDirective ( "System" ) ;
@@ -1676,6 +1665,8 @@ private void VisitRecordDecl(RecordDecl recordDecl)
1676
1665
1677
1666
if ( ( _testOutputBuilder is not null ) && generateTestsClass && ! _config . GenerateDisableRuntimeMarshalling )
1678
1667
{
1668
+ StartTestOutput ( ref testOutputStarted , generateTestsClass , isTopLevelStruct ) ;
1669
+
1679
1670
_testOutputBuilder . WriteIndented ( "/// <summary>Validates that the <see cref=\" " ) ;
1680
1671
_testOutputBuilder . Write ( escapedName ) ;
1681
1672
_testOutputBuilder . WriteLine ( "\" /> struct is blittable.</summary>" ) ;
@@ -1857,7 +1848,7 @@ private void VisitRecordDecl(RecordDecl recordDecl)
1857
1848
{
1858
1849
_outputBuilder . EndStruct ( in desc ) ;
1859
1850
1860
- if ( ( _testOutputBuilder is not null ) && generateTestsClass )
1851
+ if ( ( _testOutputBuilder is not null ) && generateTestsClass && testOutputStarted )
1861
1852
{
1862
1853
_testOutputBuilder . WriteBlockEnd ( ) ;
1863
1854
}
@@ -3125,6 +3116,25 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co
3125
3116
3126
3117
_outputBuilder . EndStruct ( in desc ) ;
3127
3118
}
3119
+
3120
+ void StartTestOutput ( ref bool testOutputStarted , bool generateTestsClass , bool isTopLevelStruct )
3121
+ {
3122
+ if ( ( _testOutputBuilder is not null ) && generateTestsClass && ! isTopLevelStruct && ! testOutputStarted )
3123
+ {
3124
+ _testOutputBuilder . WriteIndented ( "/// <summary>Provides validation of the <see cref=\" " ) ;
3125
+ _testOutputBuilder . Write ( escapedName ) ;
3126
+ _testOutputBuilder . WriteLine ( "\" /> struct.</summary>" ) ;
3127
+
3128
+ WithAttributes ( recordDecl , onlySupportedOSPlatform : true , isTestOutput : true ) ;
3129
+
3130
+ _testOutputBuilder . WriteIndented ( "public static unsafe partial class " ) ;
3131
+ _testOutputBuilder . Write ( escapedName ) ;
3132
+ _testOutputBuilder . WriteLine ( "Tests" ) ;
3133
+ _testOutputBuilder . WriteBlockStart ( ) ;
3134
+
3135
+ testOutputStarted = true ;
3136
+ }
3137
+ }
3128
3138
}
3129
3139
3130
3140
private void VisitTranslationUnitDecl ( TranslationUnitDecl translationUnitDecl )
0 commit comments