-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathToolkitSampleMetadataTests.Buttons.cs
More file actions
258 lines (213 loc) · 9.71 KB
/
ToolkitSampleMetadataTests.Buttons.cs
File metadata and controls
258 lines (213 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.Tooling.SampleGen.Diagnostics;
using CommunityToolkit.Tooling.SampleGen.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Immutable;
using System.ComponentModel.DataAnnotations;
namespace CommunityToolkit.Tooling.SampleGen.Tests;
public partial class ToolkitSampleMetadataTests
{
[TestMethod]
public void SampleButtonAttributeOnNonSample()
{
var source = """
using System.ComponentModel;
using CommunityToolkit.Tooling.SampleGen;
using CommunityToolkit.Tooling.SampleGen.Attributes;
namespace MyApp
{
public partial class Sample : Windows.UI.Xaml.Controls.UserControl
{
[ToolkitSampleButton(Title = "Click Me")]
private void OnButtonClick()
{
}
}
}
namespace Windows.UI.Xaml.Controls
{
public class UserControl { }
}
""";
var result = source.RunSourceGenerator<ToolkitSampleMetadataGenerator>(SAMPLE_ASM_NAME);
result.AssertDiagnosticsAre(DiagnosticDescriptors.SampleButtonAttributeOnNonSample);
result.AssertNoCompilationErrors();
}
[TestMethod]
public void SampleButtonAttributeValid()
{
var source = """
using System.ComponentModel;
using CommunityToolkit.Tooling.SampleGen;
using CommunityToolkit.Tooling.SampleGen.Attributes;
namespace MyApp
{
[ToolkitSample(id: nameof(Sample), "Test Sample", description: "")]
public partial class Sample : Windows.UI.Xaml.Controls.UserControl
{
[ToolkitSampleButton(Title = "Click Me")]
private void OnButtonClick()
{
}
}
}
namespace Windows.UI.Xaml.Controls
{
public class UserControl { }
}
""";
var result = source.RunSourceGenerator<ToolkitSampleMetadataGenerator>(SAMPLE_ASM_NAME);
result.AssertDiagnosticsAre(DiagnosticDescriptors.SampleNotReferencedInMarkdown);
result.AssertNoCompilationErrors();
}
[TestMethod]
public void SampleButtonAttributeMultipleButtons()
{
var source = """
using System.ComponentModel;
using CommunityToolkit.Tooling.SampleGen;
using CommunityToolkit.Tooling.SampleGen.Attributes;
namespace MyApp
{
[ToolkitSample(id: nameof(Sample), "Test Sample", description: "")]
public partial class Sample : Windows.UI.Xaml.Controls.UserControl
{
[ToolkitSampleButton(Title = "Add Item")]
private void AddItemClick()
{
}
[ToolkitSampleButton(Title = "Clear Items")]
private void ClearItemsClick()
{
}
}
}
namespace Windows.UI.Xaml.Controls
{
public class UserControl { }
}
""";
var result = source.RunSourceGenerator<ToolkitSampleMetadataGenerator>(SAMPLE_ASM_NAME);
result.AssertDiagnosticsAre(DiagnosticDescriptors.SampleNotReferencedInMarkdown);
result.AssertNoCompilationErrors();
}
[TestMethod]
public void SampleButtonCommand_GeneratedRegistryExecutesMethod()
{
// The sample registry is designed to be declared in the sample project,
// and generated in the project head. To test end-to-end execution of the
// generated button commands, we replicate this setup, verify the generated
// registry source, then execute the same command against a matching instance.
var sampleSource = """
using System.ComponentModel;
using CommunityToolkit.Tooling.SampleGen;
using CommunityToolkit.Tooling.SampleGen.Attributes;
namespace MyApp
{
[ToolkitSample(id: nameof(Sample), "Test Sample", description: "")]
public partial class Sample : Windows.UI.Xaml.Controls.UserControl
{
public int Counter { get; set; }
public Sample()
{
}
[ToolkitSampleButton(Title = "Increment")]
private void IncrementCounter()
{
Counter++;
}
}
}
namespace Windows.UI.Xaml.Controls
{
public class UserControl { }
}
""";
// Compile sample project as a metadata reference for the generator
var sampleProjectAssembly = sampleSource.ToSyntaxTree()
.CreateCompilation("MyApp.Samples")
.ToMetadataReference();
// Create application head that references the sample project
var headCompilation = string.Empty
.ToSyntaxTree()
.CreateCompilation("MyApp.Head")
.AddReferences(sampleProjectAssembly);
// Run source generator to produce the registry
var result = headCompilation.RunSourceGenerator<ToolkitSampleMetadataGenerator>();
result.AssertDiagnosticsAre();
result.AssertNoCompilationErrors();
// Verify the generated registry contains the expected button command
var registrySource = result.Compilation.GetFileContentsByName("ToolkitSampleRegistry.g.cs");
StringAssert.Contains(registrySource, @"new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleButtonCommand(""Increment"", ""IncrementCounter"")");
// Now verify the generated command mechanism works end-to-end
// by creating the same command the registry would and binding it to an instance
var testInstance = new SampleButtonCommandTestTarget();
Assert.AreEqual(0, testInstance.Counter);
var button = new Metadata.ToolkitSampleButtonCommand("Increment", "IncrementCounter");
button.BindToInstance(testInstance);
Assert.AreEqual("Increment", button.Title);
Assert.AreEqual("IncrementCounter", button.MethodName);
Assert.IsTrue(button.CanExecute(null!));
// Execute and verify the counter was incremented
button.Execute(null!);
Assert.AreEqual(1, testInstance.Counter);
button.Execute(null!);
Assert.AreEqual(2, testInstance.Counter);
}
[TestMethod]
public void SampleButtonCommand_AssemblyAttributeBridge()
{
// Verifies that the sample project emits assembly-level ToolkitSampleButtonDataAttribute
// and that the head project can read them to populate button commands in the registry.
// This tests the mechanism that bridges private method visibility across PE references.
var sampleSource = """
using System.ComponentModel;
using CommunityToolkit.Tooling.SampleGen;
using CommunityToolkit.Tooling.SampleGen.Attributes;
namespace MyApp
{
[ToolkitSample(id: nameof(Sample), "Test Sample", description: "")]
public partial class Sample : Windows.UI.Xaml.Controls.UserControl
{
[ToolkitSampleButton(Title = "Increment")]
private void IncrementCounter()
{
}
}
}
namespace Windows.UI.Xaml.Controls
{
public class UserControl { }
}
""";
// Step 1: Run the generator on the sample project to produce assembly-level button attributes
var sampleResult = sampleSource.RunSourceGenerator<ToolkitSampleMetadataGenerator>(SAMPLE_ASM_NAME);
sampleResult.AssertNoCompilationErrors();
// Verify the sample project generated the assembly-level button metadata
var buttonMetadataSource = sampleResult.Compilation.GetFileContentsByName("ToolkitSampleButtonMetadata.g.cs");
StringAssert.Contains(buttonMetadataSource, @"ToolkitSampleButtonDataAttribute(""MyApp.Sample"", ""IncrementCounter"", ""Increment"")");
// Step 2: Compile the sample project WITH the generated source and create a reference
var sampleWithGenerated = sampleResult.Compilation.ToMetadataReference();
// Step 3: Run the generator on the head project
var headCompilation = string.Empty
.ToSyntaxTree()
.CreateCompilation("MyApp.Head")
.AddReferences(sampleWithGenerated);
var headResult = headCompilation.RunSourceGenerator<ToolkitSampleMetadataGenerator>();
headResult.AssertDiagnosticsAre();
headResult.AssertNoCompilationErrors();
// Verify the head project's registry includes the button command
var registrySource = headResult.Compilation.GetFileContentsByName("ToolkitSampleRegistry.g.cs");
StringAssert.Contains(registrySource, @"new CommunityToolkit.Tooling.SampleGen.Metadata.ToolkitSampleButtonCommand(""Increment"", ""IncrementCounter"")");
}
}
/// <summary>
/// Mirrors the generated sample class structure for testing <see cref="Metadata.ToolkitSampleButtonCommand"/> execution.
/// </summary>
internal class SampleButtonCommandTestTarget
{
public int Counter { get; set; }
private void IncrementCounter() => Counter++;
}