4
4
5
5
using CommunityToolkit . Tooling . SampleGen . Diagnostics ;
6
6
using CommunityToolkit . Tooling . SampleGen . Tests . Helpers ;
7
+ using Microsoft . CodeAnalysis ;
7
8
using Microsoft . VisualStudio . TestTools . UnitTesting ;
9
+ using System . Linq ;
8
10
9
11
namespace CommunityToolkit . Tooling . SampleGen . Tests ;
10
12
@@ -51,7 +53,9 @@ public class UserControl {{ }}
51
53
[ TestMethod ]
52
54
public void PaneOption_GeneratesTitleProperty ( )
53
55
{
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 = """
55
59
using System.ComponentModel;
56
60
using CommunityToolkit.Tooling.SampleGen;
57
61
using CommunityToolkit.Tooling.SampleGen.Attributes;
@@ -72,13 +76,16 @@ namespace Windows.UI.Xaml.Controls
72
76
{
73
77
public class UserControl { }
74
78
}
75
- """ . ToSyntaxTree ( ) ;
79
+ """ . ToSyntaxTree ( )
80
+ . CreateCompilation ( "MyApp.Samples" )
81
+ . ToMetadataReference ( ) ;
76
82
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 ) ;
79
86
80
87
// Run source generator
81
- var result = syntaxTree . RunSourceGenerator < ToolkitSampleMetadataGenerator > ( compilation ) ;
88
+ var result = headCompilation . RunSourceGenerator < ToolkitSampleMetadataGenerator > ( ) ;
82
89
83
90
result . AssertDiagnosticsAre ( ) ;
84
91
result . AssertNoCompilationErrors ( ) ;
@@ -378,7 +385,7 @@ public class UserControl {{ }}
378
385
[ TestMethod ]
379
386
public void GeneratedPaneOption_ButtonAction ( )
380
387
{
381
- var source = $@ "
388
+ var syntaxTree = $@ "
382
389
using System.ComponentModel;
383
390
using CommunityToolkit.Tooling.SampleGen;
384
391
using CommunityToolkit.Tooling.SampleGen.Attributes;
@@ -398,11 +405,29 @@ private void RaiseNotification()
398
405
namespace Windows.UI.Xaml.Controls
399
406
{{
400
407
public class UserControl {{ }}
401
- }}" ;
408
+ }}" . ToSyntaxTree ( ) ;
402
409
403
410
404
- var result = source . RunSourceGenerator < ToolkitSampleMetadataGenerator > ( SAMPLE_ASM_NAME ) ;
411
+ // Create compilation builder with custom assembly name
412
+ var compilation = syntaxTree . CreateCompilation ( "MyApp.Tests" ) ;
405
413
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
+ """ ) ;
407
432
}
408
433
}
0 commit comments