Skip to content

Commit b8481d1

Browse files
committed
Updated tooling and refactored SampleGen to remove special code path for when executing in tests
1 parent 0e7616d commit b8481d1

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

CommunityToolkit.Tooling.SampleGen.Tests/Helpers/TestHelpers.Compilation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ internal static CSharpCompilation CreateCompilation(this IEnumerable<SyntaxTree>
3333
return CSharpCompilation.Create(assemblyName, syntaxTree, references ?? GetAllReferencedAssemblies(), new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
3434
}
3535

36-
internal static GeneratorDriver CreateSourceGeneratorDriver(this SyntaxTree syntaxTree, IIncrementalGenerator generator)
36+
internal static GeneratorDriver CreateSourceGeneratorDriver(this SyntaxTree syntaxTree, params IIncrementalGenerator[] generators)
3737
{
38-
return CSharpGeneratorDriver.Create(generator).WithUpdatedParseOptions((CSharpParseOptions)syntaxTree.Options);
38+
return CSharpGeneratorDriver.Create(generators).WithUpdatedParseOptions((CSharpParseOptions)syntaxTree.Options);
3939
}
4040

41-
internal static GeneratorDriver CreateSourceGeneratorDriver(this SyntaxTree syntaxTree, params IIncrementalGenerator[] generators)
41+
internal static GeneratorDriver CreateSourceGeneratorDriver(this Compilation compilation, params IIncrementalGenerator[] generators)
4242
{
43-
return CSharpGeneratorDriver.Create(generators).WithUpdatedParseOptions((CSharpParseOptions)syntaxTree.Options);
43+
return CSharpGeneratorDriver.Create(generators).WithUpdatedParseOptions((CSharpParseOptions)compilation.SyntaxTrees.First().Options);
4444
}
4545

4646
internal static GeneratorDriver WithMarkdown(this GeneratorDriver driver, params string[] markdownFilesToCreate)

CommunityToolkit.Tooling.SampleGen.Tests/Helpers/TestHelpers.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@ namespace CommunityToolkit.Tooling.SampleGen.Tests.Helpers;
66
public static partial class TestHelpers
77
{
88
internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this string source, string assemblyName, string markdown = "") where TGenerator : class, IIncrementalGenerator, new() => RunSourceGenerator<TGenerator>(source.ToSyntaxTree(), assemblyName, markdown);
9-
9+
1010
internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this SyntaxTree syntaxTree, string assemblyName, string markdown = "")
1111
where TGenerator : class, IIncrementalGenerator, new()
1212
{
13-
var references = GetAllReferencedAssemblies();
14-
var compilation = syntaxTree.CreateCompilation(assemblyName, references); // assembly name should always be supplied in param
15-
return RunSourceGenerator<TGenerator>(syntaxTree, compilation, markdown);
13+
var compilation = syntaxTree.CreateCompilation(assemblyName); // assembly name should always be supplied in param
14+
return RunSourceGenerator<TGenerator>(compilation, markdown);
1615
}
1716

18-
internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this SyntaxTree syntaxTree, Compilation compilation, string markdown = "")
17+
internal static SourceGeneratorRunResult RunSourceGenerator<TGenerator>(this Compilation compilation, string markdown = "")
1918
where TGenerator : class, IIncrementalGenerator, new()
2019
{
2120
// Create a driver for the source generator
22-
var driver = syntaxTree
21+
var driver = compilation
2322
.CreateSourceGeneratorDriver(new TGenerator())
2423
.WithMarkdown(markdown);
2524

CommunityToolkit.Tooling.SampleGen.Tests/ToolkitSampleGeneratedPaneTests.cs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
using CommunityToolkit.Tooling.SampleGen.Diagnostics;
66
using CommunityToolkit.Tooling.SampleGen.Tests.Helpers;
7+
using Microsoft.CodeAnalysis;
78
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using System.Linq;
810

911
namespace CommunityToolkit.Tooling.SampleGen.Tests;
1012

@@ -51,7 +53,9 @@ public class UserControl {{ }}
5153
[TestMethod]
5254
public void PaneOption_GeneratesTitleProperty()
5355
{
54-
var syntaxTree = """
56+
// The sample registry is designed to be declared in the sample project, and generated in the project head where its displayed in the UI as data.
57+
// To test the contents of the generated sample registry, we must replicate this setup.
58+
var sampleProjectAssembly = """
5559
using System.ComponentModel;
5660
using CommunityToolkit.Tooling.SampleGen;
5761
using CommunityToolkit.Tooling.SampleGen.Attributes;
@@ -72,13 +76,16 @@ namespace Windows.UI.Xaml.Controls
7276
{
7377
public class UserControl { }
7478
}
75-
""".ToSyntaxTree();
79+
""".ToSyntaxTree()
80+
.CreateCompilation("MyApp.Samples")
81+
.ToMetadataReference();
7682

77-
// Create compilation builder with custom assembly name
78-
var compilation = syntaxTree.CreateCompilation("MyApp.Tests");
83+
// Get all current referenced assemblies + our generated sample project.
84+
var headReferences = TestHelpers.GetAllReferencedAssemblies().Concat(new[] { sampleProjectAssembly });
85+
var headCompilation = string.Empty.ToSyntaxTree().CreateCompilation("MyApp.Head", headReferences);
7986

8087
// Run source generator
81-
var result = syntaxTree.RunSourceGenerator<ToolkitSampleMetadataGenerator>(compilation);
88+
var result = headCompilation.RunSourceGenerator<ToolkitSampleMetadataGenerator>();
8289

8390
result.AssertDiagnosticsAre();
8491
result.AssertNoCompilationErrors();
@@ -378,7 +385,7 @@ public class UserControl {{ }}
378385
[TestMethod]
379386
public void GeneratedPaneOption_ButtonAction()
380387
{
381-
var source = $@"
388+
var syntaxTree = $@"
382389
using System.ComponentModel;
383390
using CommunityToolkit.Tooling.SampleGen;
384391
using CommunityToolkit.Tooling.SampleGen.Attributes;
@@ -398,11 +405,29 @@ private void RaiseNotification()
398405
namespace Windows.UI.Xaml.Controls
399406
{{
400407
public class UserControl {{ }}
401-
}}";
408+
}}".ToSyntaxTree();
402409

403410

404-
var result = source.RunSourceGenerator<ToolkitSampleMetadataGenerator>(SAMPLE_ASM_NAME);
411+
// Create compilation builder with custom assembly name
412+
var compilation = syntaxTree.CreateCompilation("MyApp.Tests");
405413

406-
result.AssertDiagnosticsAre(DiagnosticDescriptors.SampleNotReferencedInMarkdown);
414+
// Run source generator
415+
var result = compilation.RunSourceGenerator<ToolkitSampleMetadataGenerator>();
416+
417+
result.AssertDiagnosticsAre();
418+
result.AssertNoCompilationErrors();
419+
420+
result.AssertSourceGenerated(filename: "ToolkitSampleRegistry.g.cs", expectedContents: """
421+
#nullable enable
422+
namespace CommunityToolkit.Tooling.SampleGen;
423+
424+
public static class ToolkitSampleRegistry
425+
{
426+
public static System.Collections.Generic.Dictionary<string, CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleMetadata> Listing
427+
{ get; } = new() {
428+
["Sample"] = new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleMetadata("Sample", "Test Sample", "", typeof(MyApp.Sample), () => new MyApp.Sample(), null, null, new CommunityToolkit.Tooling.SampleGen.Metadata.IGeneratedToolkitSampleOptionViewModel[] { new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleNumericOptionMetadataViewModel(name: "TextSize", initial: 12, min: 8, max: 48, step: 2, showAsNumberBox: false, title: "FontSize") })
429+
};
430+
}
431+
""");
407432
}
408433
}

CommunityToolkit.Tooling.SampleGen/ToolkitSampleMetadataGenerator.Sample.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ void Execute(IncrementalValuesProvider<ISymbol> types, bool skipDiagnostics = fa
125125
var currentAssembly = data.Right;
126126

127127
var isExecutingInSampleProject = currentAssembly?.EndsWith(".Samples") ?? false;
128-
var isExecutingInTestProject = currentAssembly?.EndsWith(".Tests") ?? false;
129128

130129
// Reconstruct sample metadata from attributes
131130
var sampleMetadata = toolkitSampleAttributeData
@@ -151,9 +150,7 @@ void Execute(IncrementalValuesProvider<ISymbol> types, bool skipDiagnostics = fa
151150
ReportDocumentDiagnostics(ctx, sampleMetadata, markdownFileData, toolkitSampleAttributeData, docFrontMatter);
152151
}
153152

154-
// For tests we need one pass to do diagnostics and registry as we're in a contrived environment that'll have both our scenarios. Though we check if we have anything to write, as we will hit both executes.
155-
if ((!isExecutingInSampleProject && !skipRegistry) ||
156-
(isExecutingInTestProject && (docFrontMatter.Any() || sampleMetadata.Any())))
153+
if (!isExecutingInSampleProject && !skipRegistry)
157154
{
158155
CreateDocumentRegistry(ctx, docFrontMatter);
159156
CreateSampleRegistry(ctx, sampleMetadata);

0 commit comments

Comments
 (0)