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
36 changes: 12 additions & 24 deletions TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ private static void GenerateTestMethodSource(SourceProductionContext context, Te
return;
}

// Get compilation from semantic model instead of parameter
var compilation = testMethod.Context.Value.SemanticModel.Compilation;

var writer = new CodeWriter();
GenerateFileHeader(writer);
GenerateTestMetadata(writer, testMethod);
Expand Down Expand Up @@ -274,10 +271,7 @@ private static void GenerateFileHeader(CodeWriter writer)

private static void GenerateTestMetadata(CodeWriter writer, TestMethodMetadata testMethod)
{
var compilation = testMethod.Context!.Value.SemanticModel.Compilation;

var className = testMethod.TypeSymbol.GloballyQualified();
var methodName = testMethod.MethodSymbol.Name;

// Generate unique class name using same pattern as filename (without .g.cs extension)
var uniqueClassName = FileNameHelper.GetDeterministicFileNameForMethod(testMethod.TypeSymbol, testMethod.MethodSymbol)
Expand Down Expand Up @@ -319,7 +313,7 @@ private static void GenerateTestMetadata(CodeWriter writer, TestMethodMetadata t

var hasMethodDataSourceForGenericType = testMethod is { IsGenericType: true, IsGenericMethod: false } && testMethod.MethodAttributes
.Any(a => a.AttributeClass?.Name == "MethodDataSourceAttribute" &&
InferClassTypesFromMethodDataSource(compilation, testMethod, a) != null);
InferClassTypesFromMethodDataSource(testMethod, a) != null);

// Check for class-level data sources that could help resolve generic type arguments
var hasClassDataSources = testMethod.IsGenericType && testMethod.TypeSymbol.GetAttributesIncludingBaseTypes()
Expand Down Expand Up @@ -363,7 +357,6 @@ private static void GenerateSpecificGenericInstantiation(
string combinationGuid,
ImmutableArray<ITypeSymbol> typeArguments)
{
var compilation = testMethod.Context!.Value.SemanticModel.Compilation;
var methodName = testMethod.MethodSymbol.Name;
var typeArgsString = string.Join(", ", typeArguments.Select(t => t.GloballyQualified()));
var instantiatedMethodName = $"{methodName}<{typeArgsString}>";
Expand Down Expand Up @@ -585,8 +578,7 @@ private static void GenerateMetadata(CodeWriter writer, TestMethodMetadata testM
var compilation = testMethod.Context!.Value.SemanticModel.Compilation;
var methodSymbol = testMethod.MethodSymbol;


GenerateDependencies(writer, compilation, methodSymbol);
GenerateDependencies(writer, methodSymbol);

writer.AppendLine("AttributeFactory = static () =>");
writer.AppendLine("[");
Expand Down Expand Up @@ -631,8 +623,7 @@ private static void GenerateMetadataForConcreteInstantiation(CodeWriter writer,
var compilation = testMethod.Context!.Value.SemanticModel.Compilation;
var methodSymbol = testMethod.MethodSymbol;


GenerateDependencies(writer, compilation, methodSymbol);
GenerateDependencies(writer, methodSymbol);

writer.AppendLine("AttributeFactory = static () =>");
writer.AppendLine("[");
Expand Down Expand Up @@ -674,10 +665,8 @@ private static void GenerateMetadataForConcreteInstantiation(CodeWriter writer,
// Method metadata
writer.Append("MethodMetadata = ");
SourceInformationWriter.GenerateMethodInformation(writer, compilation, testMethod.TypeSymbol, testMethod.MethodSymbol, null, ',');

}


private static void GenerateDataSources(CodeWriter writer, TestMethodMetadata testMethod)
{
var compilation = testMethod.Context!.Value.SemanticModel.Compilation;
Expand Down Expand Up @@ -2475,7 +2464,7 @@ private static void GenerateReturnHandling(
}
}

private static void GenerateDependencies(CodeWriter writer, Compilation compilation, IMethodSymbol methodSymbol)
private static void GenerateDependencies(CodeWriter writer, IMethodSymbol methodSymbol)
{
var dependsOnAttributes = methodSymbol.GetAttributes()
.Concat(methodSymbol.ContainingType.GetAttributes())
Expand Down Expand Up @@ -2538,7 +2527,7 @@ private static void GenerateTestDependency(CodeWriter writer, AttributeData attr
if (arg.Type?.Name == "String")
{
var testName = arg.Value?.ToString() ?? "";

if (genericTypeArgument != null)
{
// DependsOnAttribute<T>(string testName) - dependency on specific test in class T
Expand Down Expand Up @@ -2575,7 +2564,7 @@ private static void GenerateTestDependency(CodeWriter writer, AttributeData attr
if (firstArg.Type?.Name == "String" && secondArg.Type is IArrayTypeSymbol)
{
var testName = firstArg.Value?.ToString() ?? "";

if (genericTypeArgument != null)
{
// DependsOnAttribute<T>(string testName, Type[] parameterTypes) - dependency on specific test with parameters in class T
Expand Down Expand Up @@ -3488,7 +3477,7 @@ private static void GenerateGenericTestWithConcreteTypes(
foreach (var mdsAttr in methodDataSourceAttributes)
{
// Try to infer types from the method data source
var inferredTypes = InferClassTypesFromMethodDataSource(compilation, testMethod, mdsAttr);
var inferredTypes = InferClassTypesFromMethodDataSource(testMethod, mdsAttr);
if (inferredTypes is { Length: > 0 })
{
var typeKey = BuildTypeKey(inferredTypes);
Expand Down Expand Up @@ -3542,7 +3531,7 @@ private static void GenerateGenericTestWithConcreteTypes(
foreach (var mdsAttr in methodDataSourceAttributes)
{
// Try to infer types from the method data source
var inferredTypes = InferTypesFromMethodDataSource(compilation, testMethod, mdsAttr);
var inferredTypes = InferTypesFromMethodDataSource(testMethod, mdsAttr);
if (inferredTypes is { Length: > 0 })
{
var typeKey = BuildTypeKey(inferredTypes);
Expand Down Expand Up @@ -4490,7 +4479,7 @@ private static void MapGenericTypeArguments(ITypeSymbol paramType, ITypeSymbol a
return null;
}

private static ITypeSymbol[]? InferTypesFromMethodDataSource(Compilation compilation, TestMethodMetadata testMethod, AttributeData mdsAttr)
private static ITypeSymbol[]? InferTypesFromMethodDataSource(TestMethodMetadata testMethod, AttributeData mdsAttr)
{
if (mdsAttr.ConstructorArguments.Length == 0)
{
Expand Down Expand Up @@ -4569,7 +4558,7 @@ private static void MapGenericTypeArguments(ITypeSymbol paramType, ITypeSymbol a
return inferredTypes;
}

private static ITypeSymbol[]? InferClassTypesFromMethodDataSource(Compilation compilation, TestMethodMetadata testMethod, AttributeData mdsAttr)
private static ITypeSymbol[]? InferClassTypesFromMethodDataSource(TestMethodMetadata testMethod, AttributeData mdsAttr)
{
if (mdsAttr.ConstructorArguments.Length == 0)
{
Expand Down Expand Up @@ -4788,7 +4777,6 @@ private static void GenerateConcreteTestMetadata(
ITypeSymbol[] typeArguments,
AttributeData? specificArgumentsAttribute = null)
{
var compilation = testMethod.Context!.Value.SemanticModel.Compilation;
var methodName = testMethod.MethodSymbol.Name;

// Separate class type arguments from method type arguments
Expand Down Expand Up @@ -5025,7 +5013,7 @@ private static void GenerateConcreteMetadataWithFilteredDataSources(
}
}

GenerateDependencies(writer, compilation, methodSymbol);
GenerateDependencies(writer, methodSymbol);

// Generate attribute factory with filtered attributes
var filteredAttributes = new List<AttributeData>();
Expand Down Expand Up @@ -5372,7 +5360,7 @@ private static void GenerateConcreteTestMetadataForNonGeneric(

// Generate metadata

GenerateDependencies(writer, compilation, testMethod.MethodSymbol);
GenerateDependencies(writer, testMethod.MethodSymbol);

// Generate attribute factory
writer.AppendLine("AttributeFactory = static () =>");
Expand Down
Loading