Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 15 additions & 2 deletions TUnit.Core.SourceGenerator/Generators/AotConverterGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public class AotConverterGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var enabledProvider = context.AnalyzerConfigOptionsProvider
.Select((options, _) =>
{
options.GlobalOptions.TryGetValue("build_property.EnableTUnitSourceGeneration", out var value);
return !string.Equals(value, "false", StringComparison.OrdinalIgnoreCase);
});

var allTypes = context.CompilationProvider
.Select((compilation, ct) =>
{
Expand All @@ -26,10 +33,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var stackTrace = ex.StackTrace ?? "No stack trace";
throw new InvalidOperationException($"NullReferenceException in ScanTestParameters: {ex.Message}\nStack: {stackTrace}", ex);
}
});
})
.Combine(enabledProvider);

context.RegisterSourceOutput(allTypes, (spc, source) =>
context.RegisterSourceOutput(allTypes, (spc, data) =>
{
var (source, isEnabled) = data;
if (!isEnabled)
{
return;
}
try
{
GenerateConverters(spc, source);
Expand Down
47 changes: 39 additions & 8 deletions TUnit.Core.SourceGenerator/Generators/HookMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,92 @@ public class HookMetadataGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var enabledProvider = context.AnalyzerConfigOptionsProvider
.Select((options, _) =>
{
options.GlobalOptions.TryGetValue("build_property.EnableTUnitSourceGeneration", out var value);
return !string.Equals(value, "false", StringComparison.OrdinalIgnoreCase);
});

var beforeHooks = context.SyntaxProvider
.ForAttributeWithMetadataName(
"TUnit.Core.BeforeAttribute",
predicate: static (node, _) => node is MethodDeclarationSyntax,
transform: static (ctx, _) => GetHookMethodMetadata(ctx, "Before"))
.Where(static m => m is not null);
.Where(static m => m is not null)
.Combine(enabledProvider);

var afterHooks = context.SyntaxProvider
.ForAttributeWithMetadataName(
"TUnit.Core.AfterAttribute",
predicate: static (node, _) => node is MethodDeclarationSyntax,
transform: static (ctx, _) => GetHookMethodMetadata(ctx, "After"))
.Where(static m => m is not null);
.Where(static m => m is not null)
.Combine(enabledProvider);

var beforeEveryHooks = context.SyntaxProvider
.ForAttributeWithMetadataName(
"TUnit.Core.BeforeEveryAttribute",
predicate: static (node, _) => node is MethodDeclarationSyntax,
transform: static (ctx, _) => GetHookMethodMetadata(ctx, "BeforeEvery"))
.Where(static m => m is not null);
.Where(static m => m is not null)
.Combine(enabledProvider);

var afterEveryHooks = context.SyntaxProvider
.ForAttributeWithMetadataName(
"TUnit.Core.AfterEveryAttribute",
predicate: static (node, _) => node is MethodDeclarationSyntax,
transform: static (ctx, _) => GetHookMethodMetadata(ctx, "AfterEvery"))
.Where(static m => m is not null);
.Where(static m => m is not null)
.Combine(enabledProvider);

// Generate individual files for each hook instead of collecting them
context.RegisterSourceOutput(beforeHooks, (sourceProductionContext, hook) =>
context.RegisterSourceOutput(beforeHooks, (sourceProductionContext, data) =>
{
var (hook, isEnabled) = data;
if (!isEnabled)
{
return;
}
if (hook != null)
{
GenerateIndividualHookFile(sourceProductionContext, hook);
}
});

context.RegisterSourceOutput(afterHooks, (sourceProductionContext, hook) =>
context.RegisterSourceOutput(afterHooks, (sourceProductionContext, data) =>
{
var (hook, isEnabled) = data;
if (!isEnabled)
{
return;
}
if (hook != null)
{
GenerateIndividualHookFile(sourceProductionContext, hook);
}
});

context.RegisterSourceOutput(beforeEveryHooks, (sourceProductionContext, hook) =>
context.RegisterSourceOutput(beforeEveryHooks, (sourceProductionContext, data) =>
{
var (hook, isEnabled) = data;
if (!isEnabled)
{
return;
}
if (hook != null)
{
GenerateIndividualHookFile(sourceProductionContext, hook);
}
});

context.RegisterSourceOutput(afterEveryHooks, (sourceProductionContext, hook) =>
context.RegisterSourceOutput(afterEveryHooks, (sourceProductionContext, data) =>
{
var (hook, isEnabled) = data;
if (!isEnabled)
{
return;
}
if (hook != null)
{
GenerateIndividualHookFile(sourceProductionContext, hook);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,33 @@ public sealed class PropertyInjectionSourceGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var enabledProvider = context.AnalyzerConfigOptionsProvider
.Select((options, _) =>
{
options.GlobalOptions.TryGetValue("build_property.EnableTUnitSourceGeneration", out var value);
return !string.Equals(value, "false", StringComparison.OrdinalIgnoreCase);
});

var classesWithPropertyInjection = context.SyntaxProvider
.CreateSyntaxProvider(
predicate: (node, _) => IsClassWithDataSourceProperties(node),
transform: (ctx, _) => GetClassWithDataSourceProperties(ctx))
.Where(x => x != null)
.Select((x, _) => x!)
.Collect()
.SelectMany((classes, _) => classes.DistinctBy(c => c.ClassSymbol, SymbolEqualityComparer.Default));
.SelectMany((classes, _) => classes.DistinctBy(c => c.ClassSymbol, SymbolEqualityComparer.Default))
.Combine(enabledProvider);

// Generate individual files for each unique class
context.RegisterSourceOutput(classesWithPropertyInjection, GenerateIndividualPropertyInjectionSource);
context.RegisterSourceOutput(classesWithPropertyInjection, (context, data) =>
{
var (classData, isEnabled) = data;
if (!isEnabled)
{
return;
}
GenerateIndividualPropertyInjectionSource(context, classData);
});
}

private static bool IsClassWithDataSourceProperties(SyntaxNode node)
Expand Down
33 changes: 29 additions & 4 deletions TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,50 @@ public sealed class TestMetadataGenerator : IIncrementalGenerator

public void Initialize(IncrementalGeneratorInitializationContext context)
{
var enabledProvider = context.AnalyzerConfigOptionsProvider
.Select((options, _) =>
{
options.GlobalOptions.TryGetValue("build_property.EnableTUnitSourceGeneration", out var value);
return !string.Equals(value, "false", StringComparison.OrdinalIgnoreCase);
});

var testMethodsProvider = context.SyntaxProvider
.ForAttributeWithMetadataName(
"TUnit.Core.TestAttribute",
predicate: static (node, _) => node is MethodDeclarationSyntax,
transform: static (ctx, _) => GetTestMethodMetadata(ctx))
.Where(static m => m is not null);
.Where(static m => m is not null)
.Combine(enabledProvider);

var inheritsTestsClassesProvider = context.SyntaxProvider
.ForAttributeWithMetadataName(
"TUnit.Core.InheritsTestsAttribute",
predicate: static (node, _) => node is ClassDeclarationSyntax,
transform: static (ctx, _) => GetInheritsTestsClassMetadata(ctx))
.Where(static m => m is not null);
.Where(static m => m is not null)
.Combine(enabledProvider);

context.RegisterSourceOutput(testMethodsProvider,
static (context, testMethod) => GenerateTestMethodSource(context, testMethod));
static (context, data) =>
{
var (testMethod, isEnabled) = data;
if (!isEnabled)
{
return;
}
GenerateTestMethodSource(context, testMethod);
});

context.RegisterSourceOutput(inheritsTestsClassesProvider,
static (context, classInfo) => GenerateInheritedTestSources(context, classInfo));
static (context, data) =>
{
var (classInfo, isEnabled) = data;
if (!isEnabled)
{
return;
}
GenerateInheritedTestSources(context, classInfo);
});
}

private static InheritsTestsClassMetadata? GetInheritsTestsClassMetadata(GeneratorAttributeSyntaxContext context)
Expand Down
Loading