Skip to content

Commit ecb6693

Browse files
Add failing test-case for missing Attribute Property Title
1 parent fc82791 commit ecb6693

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

common/CommunityToolkit.Labs.Core.SourceGenerators.Tests/CommunityToolkit.Labs.Core.SourceGenerators.Tests/ToolkitSampleMetadataTests.cs

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.Text;
1010
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
using System.ComponentModel.DataAnnotations;
1112

1213
namespace CommunityToolkit.Labs.Core.SourceGenerators.Tests;
1314

1415
[TestClass]
1516
public partial class ToolkitSampleMetadataTests
1617
{
1718
[TestMethod]
18-
public void PaneOption_GeneratesProperty()
19+
public void PaneOption_GeneratesWithoutDiagnostics()
1920
{
2021
var source = $@"
2122
using System.ComponentModel;
@@ -46,6 +47,50 @@ public class UserControl {{ }}
4647
VerifyGeneratedDiagnostics<ToolkitSampleOptionGenerator>(source, string.Empty);
4748
}
4849

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+
4994
// https://github.com/CommunityToolkit/Labs-Windows/issues/175
5095
[TestMethod]
5196
public void PaneOption_GeneratesProperty_DuplicatePropNamesAcrossSampleClasses()
@@ -444,6 +489,55 @@ from assembly in AppDomain.CurrentDomain.GetAssemblies()
444489
GC.KeepAlive(sampleAttributeType);
445490
}
446491

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+
447541
// From: https://github.com/dotnet/roslyn/blob/main/src/Compilers/Test/Core/SourceGeneration/TestGenerators.cs
448542
internal class InMemoryAdditionalText : AdditionalText
449543
{

0 commit comments

Comments
 (0)