|
8 | 8 | using Microsoft.CodeAnalysis.CSharp; |
9 | 9 | using Microsoft.CodeAnalysis.Text; |
10 | 10 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 11 | +using System.ComponentModel.DataAnnotations; |
11 | 12 |
|
12 | 13 | namespace CommunityToolkit.Labs.Core.SourceGenerators.Tests; |
13 | 14 |
|
14 | 15 | [TestClass] |
15 | 16 | public partial class ToolkitSampleMetadataTests |
16 | 17 | { |
17 | 18 | [TestMethod] |
18 | | - public void PaneOption_GeneratesProperty() |
| 19 | + public void PaneOption_GeneratesWithoutDiagnostics() |
19 | 20 | { |
20 | 21 | var source = $@" |
21 | 22 | using System.ComponentModel; |
@@ -46,6 +47,50 @@ public class UserControl {{ }} |
46 | 47 | VerifyGeneratedDiagnostics<ToolkitSampleOptionGenerator>(source, string.Empty); |
47 | 48 | } |
48 | 49 |
|
| 50 | + [TestMethod] |
| 51 | + public void PaneOption_GeneratesTitleProperty() |
| 52 | + { |
| 53 | + var source = """ |
| 54 | + using System.ComponentModel; |
| 55 | + using CommunityToolkit.Labs.Core.SourceGenerators; |
| 56 | + using CommunityToolkit.Labs.Core.SourceGenerators.Attributes; |
| 57 | +
|
| 58 | + namespace MyApp |
| 59 | + { |
| 60 | + [ToolkitSampleNumericOption("TextSize", 12, 8, 48, 2, false, Title = "FontSize")] |
| 61 | + [ToolkitSample(id: nameof(Sample), "Test Sample", description: "")] |
| 62 | + public partial class Sample : Windows.UI.Xaml.Controls.UserControl |
| 63 | + { |
| 64 | + public Sample() |
| 65 | + { |
| 66 | + var x = this.Test; |
| 67 | + var y = this.TextFontFamily; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +
|
| 72 | + namespace Windows.UI.Xaml.Controls |
| 73 | + { |
| 74 | + public class UserControl { } |
| 75 | + } |
| 76 | + """; |
| 77 | + |
| 78 | + var result = """ |
| 79 | + #nullable enable |
| 80 | + namespace CommunityToolkit.Labs.Core.SourceGenerators; |
| 81 | +
|
| 82 | + public static class ToolkitSampleRegistry |
| 83 | + { |
| 84 | + public static System.Collections.Generic.Dictionary<string, CommunityToolkit.Labs.Core.SourceGenerators.Metadata.ToolkitSampleMetadata> Listing |
| 85 | + { get; } = new() { |
| 86 | + ["Sample"] = new CommunityToolkit.Labs.Core.SourceGenerators.Metadata.ToolkitSampleMetadata("Sample", "Test Sample", "", typeof(MyApp.Sample), () => new MyApp.Sample(), null, null, new CommunityToolkit.Labs.Core.SourceGenerators.Metadata.IGeneratedToolkitSampleOptionViewModel[] { new CommunityToolkit.Labs.Core.SourceGenerators.Metadata.ToolkitSampleNumericOptionMetadataViewModel(name: "TextSize", initial: 12, min: 8, max: 48, step: 2, showAsNumberBox: false, title: "FontSize") }) |
| 87 | + }; |
| 88 | + } |
| 89 | + """; |
| 90 | + |
| 91 | + VerifyGenerateSources("MyApp.Tests", source, new[] { new ToolkitSampleMetadataGenerator() }, ignoreDiagnostics: true, ("ToolkitSampleRegistry.g.cs", result)); |
| 92 | + } |
| 93 | + |
49 | 94 | // https://github.com/CommunityToolkit/Labs-Windows/issues/175 |
50 | 95 | [TestMethod] |
51 | 96 | public void PaneOption_GeneratesProperty_DuplicatePropNamesAcrossSampleClasses() |
@@ -444,6 +489,55 @@ from assembly in AppDomain.CurrentDomain.GetAssemblies() |
444 | 489 | GC.KeepAlive(sampleAttributeType); |
445 | 490 | } |
446 | 491 |
|
| 492 | + //// See: https://github.com/CommunityToolkit/dotnet/blob/c2053562d1a4d4829fc04b1cb86d1564c2c4a03c/tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsCodegen.cs#L103 |
| 493 | + /// <summary> |
| 494 | + /// Generates the requested sources |
| 495 | + /// </summary> |
| 496 | + /// <param name="source">The input source to process.</param> |
| 497 | + /// <param name="generators">The generators to apply to the input syntax tree.</param> |
| 498 | + /// <param name="results">The source files to compare.</param> |
| 499 | + private static void VerifyGenerateSources(string assemblyName, string source, IIncrementalGenerator[] generators, bool ignoreDiagnostics = false, params (string Filename, string Text)[] results) |
| 500 | + { |
| 501 | + // Ensure our types are loaded |
| 502 | + Type sampleattributeObjectType = typeof(ToolkitSampleAttribute); |
| 503 | + |
| 504 | + // Get all assembly references for the loaded assemblies (easy way to pull in all necessary dependencies) |
| 505 | + IEnumerable<MetadataReference> references = |
| 506 | + from assembly in AppDomain.CurrentDomain.GetAssemblies() |
| 507 | + where !assembly.IsDynamic |
| 508 | + let reference = MetadataReference.CreateFromFile(assembly.Location) |
| 509 | + select reference; |
| 510 | + |
| 511 | + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp10)); |
| 512 | + |
| 513 | + // Create a syntax tree with the input source |
| 514 | + CSharpCompilation compilation = CSharpCompilation.Create( |
| 515 | + assemblyName, |
| 516 | + new SyntaxTree[] { syntaxTree }, |
| 517 | + references, |
| 518 | + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); |
| 519 | + |
| 520 | + GeneratorDriver driver = CSharpGeneratorDriver.Create(generators).WithUpdatedParseOptions((CSharpParseOptions)syntaxTree.Options); |
| 521 | + |
| 522 | + // Run all source generators on the input source code |
| 523 | + _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray<Diagnostic> diagnostics); |
| 524 | + |
| 525 | + // Ensure that no diagnostics were generated |
| 526 | + if (!ignoreDiagnostics) |
| 527 | + { |
| 528 | + CollectionAssert.AreEquivalent(Array.Empty<Diagnostic>(), diagnostics); |
| 529 | + } |
| 530 | + |
| 531 | + foreach ((string filename, string text) in results) |
| 532 | + { |
| 533 | + SyntaxTree generatedTree = outputCompilation.SyntaxTrees.Single(tree => Path.GetFileName(tree.FilePath) == filename); |
| 534 | + |
| 535 | + Assert.AreEqual(text, generatedTree.ToString()); |
| 536 | + } |
| 537 | + |
| 538 | + GC.KeepAlive(sampleattributeObjectType); |
| 539 | + } |
| 540 | + |
447 | 541 | // From: https://github.com/dotnet/roslyn/blob/main/src/Compilers/Test/Core/SourceGeneration/TestGenerators.cs |
448 | 542 | internal class InMemoryAdditionalText : AdditionalText |
449 | 543 | { |
|
0 commit comments