-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathTestBuilder.cs
More file actions
1635 lines (1429 loc) · 74.3 KB
/
Copy pathTestBuilder.cs
File metadata and controls
1635 lines (1429 loc) · 74.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Requests;
using TUnit.Core;
using TUnit.Core.Enums;
using TUnit.Core.Exceptions;
using TUnit.Core.Helpers;
using TUnit.Core.Interfaces;
using TUnit.Core.Services;
using TUnit.Engine.Building.Interfaces;
using TUnit.Engine.Extensions;
using TUnit.Engine.Helpers;
using TUnit.Engine.Services;
using TUnit.Engine.Utilities;
namespace TUnit.Engine.Building;
internal sealed class TestBuilder : ITestBuilder
{
private readonly string _sessionId;
private readonly EventReceiverOrchestrator _eventReceiverOrchestrator;
private readonly IContextProvider _contextProvider;
private readonly ObjectLifecycleService _objectLifecycleService;
private readonly Discovery.IHookDiscoveryService _hookDiscoveryService;
private readonly TestArgumentRegistrationService _testArgumentRegistrationService;
private readonly IMetadataFilterMatcher _filterMatcher;
public TestBuilder(
string sessionId,
EventReceiverOrchestrator eventReceiverOrchestrator,
IContextProvider contextProvider,
ObjectLifecycleService objectLifecycleService,
Discovery.IHookDiscoveryService hookDiscoveryService,
TestArgumentRegistrationService testArgumentRegistrationService,
IMetadataFilterMatcher filterMatcher)
{
_sessionId = sessionId;
_hookDiscoveryService = hookDiscoveryService;
_eventReceiverOrchestrator = eventReceiverOrchestrator;
_contextProvider = contextProvider;
_objectLifecycleService = objectLifecycleService;
_testArgumentRegistrationService = testArgumentRegistrationService;
_filterMatcher = filterMatcher ?? throw new ArgumentNullException(nameof(filterMatcher));
}
/// <summary>
/// Initializes any IAsyncInitializer objects in class data that were deferred during registration.
/// </summary>
private async Task InitializeDeferredClassDataAsync(object?[] classData)
{
if (classData == null || classData.Length == 0)
{
return;
}
foreach (var data in classData)
{
if (data is IAsyncInitializer asyncInitializer && data is not IDataSourceAttribute)
{
if (!ObjectInitializer.IsInitialized(data))
{
await ObjectInitializer.InitializeAsync(data);
}
}
}
}
private async Task<object> CreateInstance(TestMetadata metadata, Type[] resolvedClassGenericArgs, object?[] classData, TestBuilderContext builderContext)
{
// Initialize any deferred IAsyncInitializer objects in class data
await InitializeDeferredClassDataAsync(classData);
// First try to create instance with ClassConstructor attribute
// Use attributes from context if available
var attributes = builderContext.InitializedAttributes ?? metadata.AttributeFactory();
var instance = await ClassConstructorHelper.TryCreateInstanceWithClassConstructor(
attributes,
metadata.TestClassType,
builderContext,
metadata.TestSessionId);
if (instance != null)
{
return instance;
}
// Fall back to InstanceFactory if no ClassConstructor or it returned null
if (metadata.InstanceFactory == null)
{
throw new InvalidOperationException($"No instance factory or class constructor available for {metadata.TestClassType.FullName}.");
}
try
{
instance = metadata.InstanceFactory(resolvedClassGenericArgs, classData);
if (instance is null)
{
throw new InvalidOperationException($"Error creating test class instance for {metadata.TestClassType.FullName}.");
}
return instance;
}
catch (NotSupportedException ex)
{
// This can happen when:
// 1. ClassConstructor is present but wasn't found or returned null
// 2. AOT scenarios where generic type instantiation fails
if (ex.Message.Contains("missing native code or metadata"))
{
throw new InvalidOperationException($"Failed to create instance of {metadata.TestClassType.FullName} in AOT scenario. Ensure types are properly annotated for AOT compatibility.", ex);
}
throw new InvalidOperationException($"Failed to create instance of {metadata.TestClassType.FullName}. This may be due to a ClassConstructor attribute issue or AOT incompatibility.", ex);
}
}
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Test building in reflection mode uses generic type resolution which requires unreferenced code")]
#endif
public async Task<IEnumerable<AbstractExecutableTest>> BuildTestsFromMetadataAsync(TestMetadata metadata, TestBuildingContext buildingContext)
{
// OPTIMIZATION: Pre-filter in execution mode to skip building tests that cannot match the filter
if (buildingContext.IsForExecution && buildingContext.Filter != null)
{
if (!CouldTestMatchFilter(buildingContext.Filter, metadata))
{
// This test class cannot match the filter - skip all expensive work!
return [];
}
}
var tests = new List<AbstractExecutableTest>();
try
{
// Create a context for capturing output during test building
using var buildContext = new TestBuildContext();
TestBuildContext.Current = buildContext;
// Handle GenericTestMetadata with ConcreteInstantiations
if (metadata is GenericTestMetadata { ConcreteInstantiations.Count: > 0 } genericMetadata)
{
// Build tests from each concrete instantiation
foreach (var concreteMetadata in genericMetadata.ConcreteInstantiations.Values)
{
var concreteTests = await BuildTestsFromMetadataAsync(concreteMetadata, buildingContext);
tests.AddRange(concreteTests);
}
return tests;
}
// Use pre-extracted repeat count from metadata (avoids instantiating attributes)
var repeatCount = metadata.RepeatCount ?? 0;
// Create and initialize attributes ONCE
var attributes = await InitializeAttributesAsync(metadata.AttributeFactory.Invoke());
if (metadata.ClassDataSources.Any(ds => ds is IAccessesInstanceData))
{
var failedTest = await CreateFailedTestForClassDataSourceCircularDependency(metadata);
tests.Add(failedTest);
return tests;
}
// Create a single context accessor that we'll reuse, updating its Current property for each test
var testBuilderContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
Events = new TestContextEvents(),
StateBag = new ConcurrentDictionary<string, object?>(),
InitializedAttributes = attributes // Store the initialized attributes
};
// Check for ClassConstructor attribute and set it early if present (reuse already created attributes)
var classConstructorAttribute = attributes.OfType<ClassConstructorAttribute>().FirstOrDefault();
if (classConstructorAttribute != null)
{
testBuilderContext.ClassConstructor = (IClassConstructor)Activator.CreateInstance(classConstructorAttribute.ClassConstructorType)!;
}
var contextAccessor = new TestBuilderContextAccessor(testBuilderContext);
var classDataAttributeIndex = 0;
foreach (var classDataSource in await GetDataSourcesAsync(metadata.ClassDataSources))
{
classDataAttributeIndex++;
var classDataLoopIndex = 0;
var hasAnyClassData = false;
await foreach (var classDataFactory in GetInitializedDataRowsAsync(
classDataSource,
DataGeneratorMetadataCreator.CreateDataGeneratorMetadata
(
testMetadata: metadata,
testSessionId: _sessionId,
generatorType: DataGeneratorType.ClassParameters,
testClassInstance: null, // Never pass instance for class data sources (circular dependency)
classInstanceArguments: null,
contextAccessor
)))
{
hasAnyClassData = true;
classDataLoopIndex++;
var classDataResult = await classDataFactory() ?? [];
var classData = DataUnwrapper.Unwrap(classDataResult);
var needsInstanceForMethodDataSources = metadata.DataSources.Any(ds => ds is IAccessesInstanceData);
object? instanceForMethodDataSources = null;
if (needsInstanceForMethodDataSources)
{
try
{
// Try to resolve class generic types using class data for early instance creation
if (metadata.TestClassType.IsGenericTypeDefinition)
{
var tempTestData = new TestData
{
TestClassInstanceFactory = () => Task.FromResult<object>(null!),
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = classDataLoopIndex,
ClassData = classData,
MethodDataSourceAttributeIndex = 0,
MethodDataLoopIndex = 0,
MethodData = [],
RepeatIndex = 0,
InheritanceDepth = metadata.InheritanceDepth
};
try
{
var resolution = TestGenericTypeResolver.Resolve(metadata, tempTestData);
instanceForMethodDataSources = metadata.InstanceFactory(resolution.ResolvedClassGenericArguments, classData);
}
catch (GenericTypeResolutionException) when (classData.Length == 0)
{
// If we can't resolve from constructor args, try to infer from data sources
var resolvedTypes = TryInferClassGenericsFromDataSources(metadata);
instanceForMethodDataSources = metadata.InstanceFactory(resolvedTypes, classData);
}
}
else
{
// Non-generic class
instanceForMethodDataSources = metadata.InstanceFactory([], classData);
}
// Initialize property data sources on the early instance so that
// method data sources can access fully-initialized properties.
// This is critical for scenarios like:
// [ClassDataSource<ErrFixture<T>>] public required ErrFixture<T> Fixture { get; init; }
// public IEnumerable<Func<T>> TestExecutions => [() => Fixture.Value];
// [MethodDataSource("TestExecutions")] [Test] public void MyTest(T value) { }
if (instanceForMethodDataSources != null)
{
var tempObjectBag = new ConcurrentDictionary<string, object?>();
var tempEvents = new TestContextEvents();
await _objectLifecycleService.RegisterObjectAsync(
instanceForMethodDataSources,
tempObjectBag,
metadata.MethodMetadata,
tempEvents);
}
}
catch (Exception ex)
{
var failedTest = await CreateFailedTestForInstanceDataSourceError(metadata, ex);
tests.Add(failedTest);
continue;
}
}
var methodDataAttributeIndex = 0;
foreach (var methodDataSource in await GetDataSourcesAsync(metadata.DataSources))
{
methodDataAttributeIndex++;
var methodDataLoopIndex = 0;
var hasAnyMethodData = false;
await foreach (var methodDataFactory in GetInitializedDataRowsAsync(
methodDataSource,
DataGeneratorMetadataCreator.CreateDataGeneratorMetadata
(
testMetadata: metadata,
testSessionId: _sessionId,
generatorType: DataGeneratorType.TestParameters,
testClassInstance: methodDataSource is IAccessesInstanceData ? instanceForMethodDataSources : null,
classInstanceArguments: classData,
contextAccessor
)))
{
hasAnyMethodData = true;
methodDataLoopIndex++;
for (var i = 0; i < repeatCount + 1; i++)
{
// Update context BEFORE calling data factories so they track objects in the right context
contextAccessor.Current = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
Events = new TestContextEvents(),
StateBag = new ConcurrentDictionary<string, object?>(),
DataSourceAttribute = methodDataSource,
InitializedAttributes = testBuilderContext.InitializedAttributes, // Preserve attributes from parent context
ClassConstructor = testBuilderContext.ClassConstructor // Preserve ClassConstructor for instance creation
};
classData = DataUnwrapper.Unwrap(await classDataFactory() ?? []);
var methodData = DataUnwrapper.UnwrapWithTypes(await methodDataFactory() ?? [], metadata.MethodMetadata.Parameters);
// For concrete generic instantiations, check if the data is compatible with the expected types
if (metadata.GenericMethodTypeArguments is { Length: > 0 })
{
if (!IsDataCompatibleWithExpectedTypes(metadata, methodData))
{
// Skip this data source as it's not compatible with the expected types
continue;
}
}
var tempTestData = new TestData
{
TestClassInstanceFactory = () => Task.FromResult<object>(null!), // Temporary placeholder
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = classDataLoopIndex,
ClassData = classData,
MethodDataSourceAttributeIndex = methodDataAttributeIndex,
MethodDataLoopIndex = methodDataLoopIndex,
MethodData = methodData,
RepeatIndex = i,
InheritanceDepth = metadata.InheritanceDepth
};
Type[] resolvedClassGenericArgs;
Type[] resolvedMethodGenericArgs;
try
{
var resolution = TestGenericTypeResolver.Resolve(metadata, tempTestData);
resolvedClassGenericArgs = resolution.ResolvedClassGenericArguments;
resolvedMethodGenericArgs = resolution.ResolvedMethodGenericArguments;
}
catch (GenericTypeResolutionException) when (
metadata.TestClassType.IsGenericTypeDefinition &&
classData.Length == 0 &&
methodData.Length > 0)
{
// Special handling for generic classes with no constructor arguments
// but with method parameters that can help infer the generic types
try
{
resolvedClassGenericArgs = TryInferClassGenericsFromMethodData(
metadata, methodData);
resolvedMethodGenericArgs = Type.EmptyTypes; // No method generics in this case
}
catch (Exception innerEx)
{
// If we still can't resolve, create a failed test
var failedTest = await CreateFailedTestForDataGenerationError(metadata, innerEx);
tests.Add(failedTest);
continue;
}
}
catch (Exception ex)
{
// If generic resolution fails, create a failed test
var failedTest = await CreateFailedTestForDataGenerationError(metadata, ex);
tests.Add(failedTest);
continue;
}
if (metadata.TestClassType.IsGenericTypeDefinition && resolvedClassGenericArgs.Length == 0)
{
throw new InvalidOperationException($"Cannot create instance of generic type {metadata.TestClassType.Name} with empty type arguments");
}
var basicSkipReason = GetBasicSkipReason(metadata, attributes);
Func<Task<object>> instanceFactory;
if (basicSkipReason is { Length: > 0 })
{
instanceFactory = () => Task.FromResult<object>(SkippedTestInstance.Instance);
}
else
{
var capturedMetadata = metadata;
var capturedClassGenericArgs = resolvedClassGenericArgs;
var capturedClassData = classData;
var capturedContext = contextAccessor.Current;
instanceFactory = () => CreateInstance(capturedMetadata, capturedClassGenericArgs, capturedClassData, capturedContext);
}
var testData = new TestData
{
TestClassInstanceFactory = instanceFactory,
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = classDataLoopIndex,
ClassData = classData,
MethodDataSourceAttributeIndex = methodDataAttributeIndex,
MethodDataLoopIndex = methodDataLoopIndex,
MethodData = methodData,
RepeatIndex = i,
InheritanceDepth = metadata.InheritanceDepth,
ResolvedClassGenericArguments = resolvedClassGenericArgs,
ResolvedMethodGenericArguments = resolvedMethodGenericArgs
};
var testSpecificContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
Events = new TestContextEvents(),
StateBag = contextAccessor.Current.StateBag,
ClassConstructor = testBuilderContext.ClassConstructor,
DataSourceAttribute = contextAccessor.Current.DataSourceAttribute,
InitializedAttributes = attributes
};
var test = await BuildTestAsync(metadata, testData, testSpecificContext);
// If we have a basic skip reason, set it immediately
if (!string.IsNullOrEmpty(basicSkipReason))
{
test.Context.SkipReason = basicSkipReason;
}
tests.Add(test);
// Context already updated at the beginning of the loop before calling factories
}
}
// If no data was yielded and SkipIfEmpty is true, create a skipped test
if (!hasAnyMethodData && methodDataSource.SkipIfEmpty)
{
const string skipReason = "Data source returned no data";
Type[] resolvedClassGenericArgs;
try
{
resolvedClassGenericArgs = metadata.TestClassType.IsGenericTypeDefinition
? TryInferClassGenericsFromDataSources(metadata)
: Type.EmptyTypes;
}
catch
{
resolvedClassGenericArgs = Type.EmptyTypes;
}
var testData = new TestData
{
TestClassInstanceFactory = () => Task.FromResult<object>(SkippedTestInstance.Instance),
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = classDataLoopIndex,
ClassData = classData,
MethodDataSourceAttributeIndex = methodDataAttributeIndex,
MethodDataLoopIndex = 1, // Use 1 since we're creating a single skipped test
MethodData = [],
RepeatIndex = 0,
InheritanceDepth = metadata.InheritanceDepth,
ResolvedClassGenericArguments = resolvedClassGenericArgs,
ResolvedMethodGenericArguments = Type.EmptyTypes
};
var testSpecificContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
Events = new TestContextEvents(),
StateBag = new ConcurrentDictionary<string, object?>(),
ClassConstructor = testBuilderContext.ClassConstructor,
DataSourceAttribute = methodDataSource,
InitializedAttributes = attributes
};
var test = await BuildTestAsync(metadata, testData, testSpecificContext);
test.Context.SkipReason = skipReason;
tests.Add(test);
}
}
}
// If no class data was yielded and SkipIfEmpty is true, create a skipped test
if (!hasAnyClassData && classDataSource.SkipIfEmpty)
{
const string skipReason = "Data source returned no data";
Type[] resolvedClassGenericArgs;
try
{
resolvedClassGenericArgs = metadata.TestClassType.IsGenericTypeDefinition
? TryInferClassGenericsFromDataSources(metadata)
: Type.EmptyTypes;
}
catch
{
resolvedClassGenericArgs = Type.EmptyTypes;
}
var testData = new TestData
{
TestClassInstanceFactory = () => Task.FromResult<object>(SkippedTestInstance.Instance),
ClassDataSourceAttributeIndex = classDataAttributeIndex,
ClassDataLoopIndex = 1, // Use 1 since we're creating a single skipped test
ClassData = [],
MethodDataSourceAttributeIndex = 0,
MethodDataLoopIndex = 0,
MethodData = [],
RepeatIndex = 0,
InheritanceDepth = metadata.InheritanceDepth,
ResolvedClassGenericArguments = resolvedClassGenericArgs,
ResolvedMethodGenericArguments = Type.EmptyTypes
};
var testSpecificContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
Events = new TestContextEvents(),
StateBag = new ConcurrentDictionary<string, object?>(),
ClassConstructor = testBuilderContext.ClassConstructor,
DataSourceAttribute = classDataSource,
InitializedAttributes = attributes
};
var test = await BuildTestAsync(metadata, testData, testSpecificContext);
test.Context.SkipReason = skipReason;
tests.Add(test);
}
}
// Transfer captured build-time output to all test contexts
var capturedOutput = buildContext.GetCapturedOutput();
var capturedErrorOutput = buildContext.GetCapturedErrorOutput();
if (!string.IsNullOrEmpty(capturedOutput) || !string.IsNullOrEmpty(capturedErrorOutput))
{
foreach (var test in tests)
{
test.Context.SetBuildTimeOutput(capturedOutput, capturedErrorOutput);
}
}
}
catch (Exception ex)
{
var failedTest = await CreateFailedTestForDataGenerationError(metadata, ex);
tests.Add(failedTest);
return tests;
}
return tests;
}
private static Type[] TryInferClassGenericsFromMethodData(TestMetadata metadata, object?[] methodData)
{
var genericClassType = metadata.TestClassType;
var genericParameters = genericClassType.GetGenericArguments();
var typeMapping = new Dictionary<Type, Type>();
// Try to match method parameter types with actual data types
var methodParameters = metadata.MethodMetadata.Parameters;
for (var i = 0; i < Math.Min(methodParameters.Length, methodData.Length); i++)
{
var methodParam = methodParameters[i];
var paramType = methodParam.Type;
var argValue = methodData[i];
if (argValue != null)
{
var argType = argValue.GetType();
// If the parameter type is object (placeholder for generic parameter in source-gen)
// and we have data, we can infer the type
if (paramType == typeof(object))
{
// Check if this corresponds to a class generic parameter
// by looking at the method metadata
if (methodParam.TypeInfo is GenericParameter { IsMethodParameter: false } gp)
{
var genericParamName = gp.Name;
// Find the matching generic parameter in the class
var matchingClassParam = genericParameters.FirstOrDefault(p => p.Name == genericParamName);
if (matchingClassParam != null)
{
typeMapping[matchingClassParam] = argType;
}
}
}
}
}
var resolvedTypes = new Type[genericParameters.Length];
for (var i = 0; i < genericParameters.Length; i++)
{
var genericParam = genericParameters[i];
if (!typeMapping.TryGetValue(genericParam, out var resolvedType))
{
throw new InvalidOperationException(
$"Could not resolve type for generic parameter '{genericParam.Name}' of type '{genericClassType.Name}' from method data");
}
resolvedTypes[i] = resolvedType;
}
return resolvedTypes;
}
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Generic type inference uses reflection on data sources and parameters")]
#endif
private static Type[] TryInferClassGenericsFromDataSources(TestMetadata metadata)
{
var genericClassType = metadata.TestClassType;
var genericParameters = genericClassType.GetGenericArguments();
var typeMapping = new Dictionary<Type, Type>();
// First, check if we have typed data sources that can help infer the generic type
foreach (var dataSource in metadata.DataSources)
{
var dataSourceType = dataSource.GetType();
// Check if this data source inherits from a generic base type
var baseType = dataSourceType.BaseType;
while (baseType != null)
{
if (baseType.IsGenericType)
{
var genericDef = baseType.GetGenericTypeDefinition();
var genericDefName = genericDef.FullName ?? genericDef.Name;
// Check if it's a typed data source attribute
if (genericDefName.Contains("DataSourceGeneratorAttribute`") ||
genericDefName.Contains("AsyncDataSourceGeneratorAttribute`"))
{
// Get the type argument (e.g., int from AsyncDataSourceGeneratorAttribute<int>)
var typeArgs = baseType.GetGenericArguments();
if (typeArgs.Length > 0 && genericParameters.Length > 0)
{
// For now, assume the first generic parameter maps to the data source type
// This handles simple cases like GenericClass<T> with IntDataSource
typeMapping[genericParameters[0]] = typeArgs[0];
}
break;
}
}
baseType = baseType.BaseType;
}
}
if (metadata.DataSources.Any(ds => ds is IAccessesInstanceData))
{
// Look at the test method parameters to find attributes that can help with generic type inference
foreach (var param in metadata.MethodMetadata.Parameters)
{
// Get the actual parameter type from reflection
var actualParamType = param.Type;
// Check if the actual parameter type is a generic parameter of the class
if (actualParamType.IsGenericParameter &&
genericParameters.Contains(actualParamType))
{
// Check for Matrix attributes using reflection
var attrs = param.ReflectionInfo.GetCustomAttributes(false);
foreach (var attr in attrs)
{
var attrType = attr.GetType();
// Check if it's a generic Matrix attribute
if (attrType.IsGenericType &&
attrType.GetGenericTypeDefinition().Name.StartsWith("Matrix"))
{
var matrixTypeArg = attrType.GetGenericArguments()[0];
typeMapping[actualParamType] = matrixTypeArg;
break;
}
}
}
}
}
// Look for instance method data sources
foreach (var dataSource in metadata.DataSources)
{
if (dataSource is InstanceMethodDataSourceAttribute instanceMethodDataSource)
{
// Get the method info
var methodName = instanceMethodDataSource.MethodNameProvidingDataSource;
var method = genericClassType.GetMethod(methodName,
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
if (method != null)
{
var returnType = method.ReturnType;
// Check if return type is IEnumerable<T> or similar
if (returnType.IsGenericType)
{
var genericDef = returnType.GetGenericTypeDefinition();
if (genericDef == typeof(IEnumerable<>) ||
genericDef == typeof(IAsyncEnumerable<>) ||
genericDef == typeof(List<>) ||
genericDef == typeof(Task<>))
{
var elementType = returnType.GetGenericArguments()[0];
// If Task<T>, unwrap to get the actual type
if (genericDef == typeof(Task<>) && elementType.IsGenericType)
{
var innerGenericDef = elementType.GetGenericTypeDefinition();
if (innerGenericDef == typeof(IEnumerable<>) ||
innerGenericDef == typeof(IAsyncEnumerable<>) ||
innerGenericDef == typeof(List<>))
{
elementType = elementType.GetGenericArguments()[0];
}
}
// Now try to match this element type with method parameters
// that use the class generic parameter
for (var i = 0; i < metadata.MethodMetadata.Parameters.Length; i++)
{
var paramType = metadata.MethodMetadata.Parameters[i].Type;
if (paramType == typeof(object)) // Placeholder for generic parameter
{
var methodParam = metadata.MethodMetadata.Parameters[i];
if (methodParam.TypeInfo is GenericParameter { IsMethodParameter: false } gp2)
{
var genericParamName = gp2.Name;
var matchingClassParam = genericParameters.FirstOrDefault(p => p.Name == genericParamName);
if (matchingClassParam != null)
{
typeMapping[matchingClassParam] = elementType;
}
}
}
}
}
}
}
}
}
var resolvedTypes = new Type[genericParameters.Length];
for (var i = 0; i < genericParameters.Length; i++)
{
var genericParam = genericParameters[i];
if (!typeMapping.TryGetValue(genericParam, out var resolvedType))
{
throw new InvalidOperationException(
$"Could not resolve type for generic parameter '{genericParam.Name}' of type '{genericClassType.Name}' from data sources");
}
resolvedTypes[i] = resolvedType;
}
return resolvedTypes;
}
private async Task<IDataSourceAttribute[]> GetDataSourcesAsync(IDataSourceAttribute[] dataSources)
{
if (dataSources.Length == 0)
{
return [NoDataSource.Instance];
}
// Initialize all data sources to ensure properties are injected
foreach (var dataSource in dataSources)
{
await _objectLifecycleService.EnsureInitializedAsync(dataSource);
}
return dataSources;
}
/// <summary>
/// Ensures a data source is initialized before use and returns data rows.
/// This centralizes the initialization logic for all data source usage.
/// </summary>
private async IAsyncEnumerable<Func<Task<object?[]?>>> GetInitializedDataRowsAsync(
IDataSourceAttribute dataSource,
DataGeneratorMetadata dataGeneratorMetadata)
{
// Ensure the data source is fully initialized before getting data rows
// This includes property injection and IAsyncInitializer.InitializeAsync
var initializedDataSource = await _objectLifecycleService.EnsureInitializedAsync(
dataSource,
dataGeneratorMetadata.TestBuilderContext.Current.StateBag,
dataGeneratorMetadata.TestInformation,
dataGeneratorMetadata.TestBuilderContext.Current.Events);
// Now get data rows from the initialized data source
await foreach (var dataRow in initializedDataSource.GetDataRowsAsync(dataGeneratorMetadata))
{
yield return dataRow;
}
}
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Hook discovery service handles mode-specific logic; reflection calls suppressed in AOT mode")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Hook discovery service handles mode-specific logic; dynamic code suppressed in AOT mode")]
public async Task<AbstractExecutableTest> BuildTestAsync(TestMetadata metadata, TestData testData, TestBuilderContext testBuilderContext)
{
// Discover instance hooks for closed generic types (no-op in source gen mode)
if (metadata.TestClassType is { IsGenericType: true, IsGenericTypeDefinition: false })
{
_hookDiscoveryService.DiscoverInstanceHooksForType(metadata.TestClassType);
}
var testId = TestIdentifierService.GenerateTestId(metadata, testData);
var context = await CreateTestContextAsync(testId, metadata, testData, testBuilderContext);
context.Metadata.TestDetails.ClassInstance = PlaceholderInstance.Instance;
// Arguments will be tracked by TestArgumentTrackingService during TestRegistered event
// This ensures proper reference counting for shared instances
// Create the test object BEFORE invoking event receivers
// This ensures context.InternalExecutableTest is set for error handling in registration
var creationContext = new ExecutableTestCreationContext
{
TestId = testId,
DisplayName = context.GetDisplayName(),
Arguments = testData.MethodData,
ClassArguments = testData.ClassData,
Context = context,
TestClassInstanceFactory = testData.TestClassInstanceFactory,
ResolvedMethodGenericArguments = testData.ResolvedMethodGenericArguments,
ResolvedClassGenericArguments = testData.ResolvedClassGenericArguments
};
var test = metadata.CreateExecutableTestFactory(creationContext, metadata);
// Set InternalExecutableTest so it's available during registration for error handling
context.InternalExecutableTest = test;
// Invoke test registered event receivers BEFORE discovery event receivers
// This is critical for allowing attributes to set custom hook executors
try
{
await InvokeTestRegisteredEventReceiversAsync(context);
}
catch (Exception ex)
{
// Property registration or other registration logic failed
// Mark the test as failed immediately, as the old code did
test.SetResult(TestState.Failed, ex);
}
await InvokeDiscoveryEventReceiversAsync(context);
// Clear the cached display name after discovery events
// This ensures that ArgumentDisplayFormatterAttribute and similar attributes
// have a chance to register their formatters before the display name is finalized
context.InvalidateDisplayNameCache();
return test;
}
/// <summary>
/// Checks if a test has basic SkipAttribute instances that can be evaluated at discovery time.
/// Returns null if no skip attributes, a skip reason if basic skip attributes are found,
/// or empty string if custom skip attributes requiring runtime evaluation are found.
/// </summary>
private static string? GetBasicSkipReason(TestMetadata metadata, Attribute[]? cachedAttributes = null)
{
var attributes = cachedAttributes ?? metadata.AttributeFactory();
var skipAttributes = attributes.OfType<SkipAttribute>();
SkipAttribute? firstSkipAttribute = null;
// Check if all skip attributes are basic (non-derived) SkipAttribute instances
foreach (var skipAttribute in skipAttributes)
{
firstSkipAttribute ??= skipAttribute;
var attributeType = skipAttribute.GetType();
if (attributeType != typeof(SkipAttribute))
{
// This is a derived skip attribute that might have custom ShouldSkip logic
return string.Empty; // Indicates custom skip attributes that need runtime evaluation
}
}
// All skip attributes are basic SkipAttribute instances
// Return the first reason (they all should skip), or null if no skip attributes
return firstSkipAttribute?.Reason;
}
private async ValueTask<TestContext> CreateTestContextAsync(string testId, TestMetadata metadata, TestData testData, TestBuilderContext testBuilderContext)
{
// Use attributes from context if available, or create new ones
var attributes = testBuilderContext.InitializedAttributes ?? await InitializeAttributesAsync(metadata.AttributeFactory.Invoke());
if (testBuilderContext.DataSourceAttribute != null && testBuilderContext.DataSourceAttribute is not NoDataSource)
{
attributes = [..attributes, (Attribute)testBuilderContext.DataSourceAttribute];
}
var testDetails = new TestDetails
{
TestId = testId,
TestName = metadata.TestName,
ClassType = metadata.TestClassType,
MethodName = metadata.TestMethodName,
ClassInstance = PlaceholderInstance.Instance,
TestMethodArguments = testData.MethodData,
TestClassArguments = testData.ClassData,
TestFilePath = metadata.FilePath,
TestLineNumber = metadata.LineNumber,
ReturnType = metadata.MethodMetadata.ReturnType ?? typeof(void),
MethodMetadata = metadata.MethodMetadata,
AttributesByType = attributes.ToAttributeDictionary(),
MethodGenericArguments = testData.ResolvedMethodGenericArguments,
ClassGenericArguments = testData.ResolvedClassGenericArguments,
Timeout = TimeSpan.FromMinutes(30) // Default 30-minute timeout (can be overridden by TimeoutAttribute)
// Don't set RetryLimit here - let discovery event receivers set it
};
var context = _contextProvider.CreateTestContext(
metadata.TestName,
metadata.TestClassType,
testBuilderContext,
CancellationToken.None);
context.Metadata.TestDetails = testDetails;
return context;
}
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Type comes from runtime objects that cannot be annotated")]
#endif
private async Task InvokeTestRegisteredEventReceiversAsync(TestContext context)
{
var discoveredTest = new DiscoveredTest<object>
{
TestContext = context
};
var registeredContext = new TestRegisteredContext(context)
{
DiscoveredTest = discoveredTest
};
context.InternalDiscoveredTest = discoveredTest;
// First, invoke the global test argument registration service to register shared instances
await _testArgumentRegistrationService.RegisterTestArgumentsAsync(context);
var eventObjects = context.GetEligibleEventObjects();
foreach (var receiver in eventObjects.OfType<ITestRegisteredEventReceiver>())
{
await receiver.OnTestRegistered(registeredContext);
}
}
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Scoped attribute filtering uses Type.GetInterfaces and reflection")]
#endif
private async Task InvokeDiscoveryEventReceiversAsync(TestContext context)
{
var discoveredContext = new DiscoveredTestContext(
context.Metadata.TestDetails.TestName,
context);
{
await _eventReceiverOrchestrator.InvokeTestDiscoveryEventReceiversAsync(context, discoveredContext, CancellationToken.None);
}
}
private async Task<AbstractExecutableTest> CreateFailedTestForDataGenerationError(TestMetadata metadata, Exception exception)
{
return await CreateFailedTestForDataGenerationError(metadata, exception, new TestDataCombination());
}
private async Task<AbstractExecutableTest> CreateFailedTestForDataGenerationError(TestMetadata metadata, Exception exception, TestDataCombination combination)
{
var testId = TestIdentifierService.GenerateFailedTestId(metadata, combination);
var testDetails = await CreateFailedTestDetails(metadata, testId);
var context = CreateFailedTestContext(metadata, testDetails);
return new FailedExecutableTest(exception)
{
TestId = testId,
Metadata = metadata,
Arguments = [],
ClassArguments = [],
Context = context
};
}
private async Task<TestDetails> CreateFailedTestDetails(TestMetadata metadata, string testId)
{
return new TestDetails