44
55using CommunityToolkit . Tooling . SampleGen . Diagnostics ;
66using CommunityToolkit . Tooling . SampleGen . Tests . Helpers ;
7+ using Microsoft . CodeAnalysis ;
78using Microsoft . VisualStudio . TestTools . UnitTesting ;
9+ using System . Linq ;
810
911namespace 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}
0 commit comments