Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/TUnit.Engine/Discovery/ReflectionTestDataCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,15 +1307,17 @@ private static List<TestMetadata> BuildTestsFromMethodRows(Type genericTypeDefin
}

var parameters = genericMethod.GetParameters();
var methodDataSources = ReflectionAttributeExtractor.ExtractDataSources(genericMethod);
// A typed source closes the class even when no parameter uses its type parameters, as in the generator
// (Run(int value) with ClassDataSource<int>, #7904 review).
// (Run(int value) with ClassDataSource<int>, #7904 review). Decided from attribute metadata, so a method
// this skips never constructs its data sources: a throwing one would replace a constraint failure (#7671).
if (!Array.Exists(parameters, static p => p.ParameterType.ContainsGenericParameters)
&& !Array.Exists(methodDataSources, source => InferClassTypesFromTypedMethodSource(source, classGenericParameters.Length) is not null))
&& !DeclaresTypedClassSource(genericMethod, classGenericParameters.Length))
{
continue;
}

var methodDataSources = ReflectionAttributeExtractor.ExtractDataSources(genericMethod);

var typeArgumentGroups = new List<Type[]>();

// The group whose type arguments each data source's row inferred, or -1 when it inferred none
Expand Down Expand Up @@ -1452,13 +1454,14 @@ private static List<TestMetadata> BuildTestsFromMethodRows(Type genericTypeDefin

var parameters = testMethod.GetParameters();
classGenericParameters ??= genericTypeDefinition.GetGenericArguments();
var methodDataSources = ReflectionAttributeExtractor.ExtractDataSources(testMethod);
if (!Array.Exists(parameters, static p => p.ParameterType.ContainsGenericParameters)
&& !(inferMethodSources && Array.Exists(methodDataSources, source => InferClassTypesFromTypedMethodSource(source, classGenericParameters.Length) is not null)))
&& !(inferMethodSources && DeclaresTypedClassSource(testMethod, classGenericParameters.Length)))
{
continue;
}

var methodDataSources = ReflectionAttributeExtractor.ExtractDataSources(testMethod);

// Beside type-free class rows, a parameter attribute implementing IInfersType<T> supplies the class types for
// every other source of the method, as the generator's InferTypesFromTypedDataSourceForClass (#7390).
var parameterInferredTypes = inferMethodSources ? InferClassTypesFromTypeInferringAttributes(parameters, classGenericParameters) : null;
Expand Down Expand Up @@ -1505,8 +1508,31 @@ _ when inferMethodSources && testMethod.IsGenericMethodDefinition
/// <c>InferTypesFromDataSourceAttribute</c> for class inference (#7390).
/// </summary>
internal static Type[]? InferClassTypesFromTypedMethodSource(IDataSourceAttribute dataSource, int classTypeParameterCount)
=> InferClassTypesFromTypedMethodSourceType(dataSource.GetType(), classTypeParameterCount);

/// <summary>
/// Whether a data source the method declares is typed and closes the class (see
/// <see cref="InferClassTypesFromTypedMethodSource"/>). Reads attribute types from metadata, with the same
/// <c>inherit: false</c> as <see cref="ReflectionAttributeExtractor.ExtractDataSources"/> for methods, so no
/// attribute is constructed (#7671).
/// </summary>
private static bool DeclaresTypedClassSource(MethodInfo method, int classTypeParameterCount)
{
for (var baseType = dataSource.GetType().BaseType; baseType is not null; baseType = baseType.BaseType)
foreach (var attributeData in method.GetCustomAttributesData())
{
if (typeof(IDataSourceAttribute).IsAssignableFrom(attributeData.AttributeType)
&& InferClassTypesFromTypedMethodSourceType(attributeData.AttributeType, classTypeParameterCount) is not null)
{
return true;
}
}

return false;
}

private static Type[]? InferClassTypesFromTypedMethodSourceType(Type dataSourceType, int classTypeParameterCount)
{
for (var baseType = dataSourceType.BaseType; baseType is not null; baseType = baseType.BaseType)
{
if (!baseType.IsGenericType)
{
Expand Down
Loading