forked from FritzAndFriends/BlazorWebFormsComponents
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAllAnalyzersIntegrationTests.cs
More file actions
374 lines (314 loc) · 11.9 KB
/
AllAnalyzersIntegrationTests.cs
File metadata and controls
374 lines (314 loc) · 11.9 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Reflection;
namespace BlazorWebFormsComponents.Analyzers.Test;
/// <summary>
/// Cross-analyzer integration tests. Verifies that all BWFC analyzers
/// are registered, have unique IDs, and fire correctly in combination.
/// </summary>
public class AllAnalyzersIntegrationTests
{
/// <summary>
/// All analyzer types in the assembly, discovered via reflection.
/// </summary>
private static readonly Type[] AllAnalyzerTypes = typeof(MissingParameterAttributeAnalyzer)
.Assembly
.GetTypes()
.Where(t => !t.IsAbstract && typeof(DiagnosticAnalyzer).IsAssignableFrom(t))
.OrderBy(t => t.Name)
.ToArray();
/// <summary>
/// Expected diagnostic IDs. BWFC011 is pending from Cyclops — add it here when delivered.
/// </summary>
private static readonly string[] ExpectedIds = new[]
{
"BWFC001", // MissingParameterAttributeAnalyzer
"BWFC002", // ViewStateUsageAnalyzer
"BWFC003", // IsPostBackUsageAnalyzer
"BWFC004", // ResponseRedirectAnalyzer
"BWFC005", // SessionUsageAnalyzer
"BWFC010", // RequiredAttributeAnalyzer
"BWFC011", // EventHandlerSignatureAnalyzer
"BWFC012", // RunatServerAnalyzer
"BWFC013", // ResponseObjectUsageAnalyzer
"BWFC014", // RequestObjectUsageAnalyzer
"BWFC020", // ViewStatePropertyPatternAnalyzer
"BWFC021", // FindControlUsageAnalyzer
"BWFC022", // PageClientScriptUsageAnalyzer
"BWFC023", // IPostBackEventHandlerUsageAnalyzer
"BWFC025", // NonSerializableViewStateAnalyzer
};
#region ID Registration and Uniqueness
[Fact]
public void AllAnalyzerTypes_AreDiscoverable()
{
// Sanity check: we should find at least the 10 analyzers we know exist
Assert.True(AllAnalyzerTypes.Length >= 10,
$"Expected at least 10 analyzer types but found {AllAnalyzerTypes.Length}: " +
string.Join(", ", AllAnalyzerTypes.Select(t => t.Name)));
}
[Fact]
public void AllDiagnosticIds_AreUnique()
{
var ids = new List<(string Id, string AnalyzerName)>();
foreach (var analyzerType in AllAnalyzerTypes)
{
var analyzer = CreateAnalyzer(analyzerType);
foreach (var descriptor in analyzer.SupportedDiagnostics)
{
ids.Add((descriptor.Id, analyzerType.Name));
}
}
var duplicates = ids
.GroupBy(x => x.Id)
.Where(g => g.Count() > 1)
.Select(g => $"{g.Key} is claimed by: {string.Join(", ", g.Select(x => x.AnalyzerName))}")
.ToList();
Assert.True(duplicates.Count == 0,
"Duplicate diagnostic IDs found:\n" + string.Join("\n", duplicates));
}
[Fact]
public void AllExpectedIds_AreRegistered()
{
var registeredIds = new HashSet<string>();
foreach (var analyzerType in AllAnalyzerTypes)
{
var analyzer = CreateAnalyzer(analyzerType);
foreach (var descriptor in analyzer.SupportedDiagnostics)
{
registeredIds.Add(descriptor.Id);
}
}
var missingIds = ExpectedIds.Where(id => !registeredIds.Contains(id)).ToList();
Assert.True(missingIds.Count == 0,
"Expected diagnostic IDs not found in any analyzer: " + string.Join(", ", missingIds) +
"\nRegistered IDs: " + string.Join(", ", registeredIds.OrderBy(x => x)));
}
[Fact]
public void AllAnalyzers_HaveValidCategory()
{
var validCategories = new[] { "Usage", "Migration" };
foreach (var analyzerType in AllAnalyzerTypes)
{
var analyzer = CreateAnalyzer(analyzerType);
foreach (var descriptor in analyzer.SupportedDiagnostics)
{
Assert.Contains(descriptor.Category, validCategories);
}
}
}
[Fact]
public void AllAnalyzers_AreEnabledByDefault()
{
foreach (var analyzerType in AllAnalyzerTypes)
{
var analyzer = CreateAnalyzer(analyzerType);
foreach (var descriptor in analyzer.SupportedDiagnostics)
{
Assert.True(descriptor.IsEnabledByDefault,
$"{descriptor.Id} ({analyzerType.Name}) is not enabled by default");
}
}
}
#endregion
#region Multi-Analyzer Integration
/// <summary>
/// A WebControl subclass with multiple migration issues should trigger
/// BWFC001 (missing [Parameter]), BWFC002 (ViewState), and BWFC003 (IsPostBack) simultaneously.
/// </summary>
[Fact]
public async Task ComplexWebControl_TriggersMultipleDiagnostics()
{
var source = @"
using System.Collections.Generic;
namespace BlazorWebFormsComponents.CustomControls
{
public class WebControl
{
public string ID { get; set; }
public Dictionary<string, object> ViewState { get; } = new Dictionary<string, object>();
public bool IsPostBack { get; set; }
public WebControl Page => this;
}
}
namespace MyApp
{
using BlazorWebFormsComponents.CustomControls;
public class LegacyGrid : WebControl
{
// BWFC001: public property without [Parameter]
public string DataSourceID { get; set; }
// BWFC001: another missing [Parameter]
public bool AllowPaging { get; set; }
public void Page_Load()
{
// BWFC003: IsPostBack check
if (!IsPostBack)
{
// BWFC002: ViewState usage
ViewState[""initialized""] = true;
}
}
}
}";
var analyzers = ImmutableArray.Create<DiagnosticAnalyzer>(
new MissingParameterAttributeAnalyzer(),
new ViewStateUsageAnalyzer(),
new IsPostBackUsageAnalyzer());
var diagnostics = await GetDiagnosticsAsync(source, analyzers);
var diagnosticIds = diagnostics.Select(d => d.Id).ToList();
Assert.Contains("BWFC001", diagnosticIds);
Assert.Contains("BWFC002", diagnosticIds);
Assert.Contains("BWFC003", diagnosticIds);
// BWFC001 should fire twice (DataSourceID + AllowPaging)
var bwfc001Count = diagnosticIds.Count(id => id == "BWFC001");
Assert.Equal(2, bwfc001Count);
// BWFC002 should fire once (ViewState["initialized"])
var bwfc002Count = diagnosticIds.Count(id => id == "BWFC002");
Assert.Equal(1, bwfc002Count);
// BWFC003 should fire once (IsPostBack)
var bwfc003Count = diagnosticIds.Count(id => id == "BWFC003");
Assert.Equal(1, bwfc003Count);
}
/// <summary>
/// A single file with issues spanning 5+ analyzers: missing [Parameter],
/// ViewState, IsPostBack, Session, runat="server", and event handler signature.
/// </summary>
[Fact]
public async Task KitchenSink_FiveAnalyzersFire()
{
var source = @"
using System;
using System.Collections.Generic;
namespace BlazorWebFormsComponents.CustomControls
{
public class WebControl
{
public string ID { get; set; }
public Dictionary<string, object> ViewState { get; } = new Dictionary<string, object>();
public bool IsPostBack { get; set; }
public WebControl Page => this;
public Dictionary<string, object> Session { get; } = new Dictionary<string, object>();
}
}
namespace MyApp
{
using BlazorWebFormsComponents.CustomControls;
public class MegaPage : WebControl
{
// BWFC001: missing [Parameter]
public string Title { get; set; }
public void Page_Load()
{
// BWFC003: IsPostBack
if (!IsPostBack)
{
// BWFC002: ViewState
ViewState[""loaded""] = true;
}
// BWFC005: Session usage
Session[""user""] = ""admin"";
// BWFC012: runat=""server"" in string
var legacy = ""<asp:Label runat=\""server\"" Text=\""Hello\"" />"";
}
// BWFC011: Web Forms event handler signature
protected void Button_Click(object sender, EventArgs e)
{
}
}
}";
var analyzers = ImmutableArray.Create<DiagnosticAnalyzer>(
new MissingParameterAttributeAnalyzer(),
new ViewStateUsageAnalyzer(),
new IsPostBackUsageAnalyzer(),
new SessionUsageAnalyzer(),
new RunatServerAnalyzer(),
new EventHandlerSignatureAnalyzer());
var diagnostics = await GetDiagnosticsAsync(source, analyzers);
var diagnosticIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(id => id).ToList();
Assert.Contains("BWFC001", diagnosticIds);
Assert.Contains("BWFC002", diagnosticIds);
Assert.Contains("BWFC003", diagnosticIds);
Assert.Contains("BWFC005", diagnosticIds);
Assert.Contains("BWFC011", diagnosticIds);
Assert.Contains("BWFC012", diagnosticIds);
}
/// <summary>
/// Clean code that follows Blazor patterns should produce NO diagnostics
/// from any analyzer in the suite.
/// </summary>
[Fact]
public async Task CleanBlazorComponent_NoDiagnostics()
{
var source = @"
namespace BlazorWebFormsComponents.CustomControls
{
public class WebControl
{
public string ID { get; set; }
}
}
namespace Microsoft.AspNetCore.Components
{
[System.AttributeUsage(System.AttributeTargets.Property)]
public class ParameterAttribute : System.Attribute { }
}
namespace MyApp
{
using BlazorWebFormsComponents.CustomControls;
using Microsoft.AspNetCore.Components;
public class CleanComponent : WebControl
{
[Parameter]
public string Title { get; set; }
[Parameter]
public int PageSize { get; set; }
public void OnInitialized()
{
var message = ""Hello, Blazor!"";
}
}
}";
var analyzers = ImmutableArray.Create<DiagnosticAnalyzer>(
new MissingParameterAttributeAnalyzer(),
new ViewStateUsageAnalyzer(),
new IsPostBackUsageAnalyzer(),
new ResponseRedirectAnalyzer(),
new SessionUsageAnalyzer(),
new RunatServerAnalyzer());
var diagnostics = await GetDiagnosticsAsync(source, analyzers);
Assert.Empty(diagnostics);
}
#endregion
#region Helpers
private static DiagnosticAnalyzer CreateAnalyzer(Type analyzerType) =>
(DiagnosticAnalyzer)(Activator.CreateInstance(analyzerType)
?? throw new InvalidOperationException($"Could not create instance of {analyzerType.Name}"));
private static async Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(
string source,
ImmutableArray<DiagnosticAnalyzer> analyzers)
{
var syntaxTree = CSharpSyntaxTree.ParseText(source);
var references = new[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
MetadataReference.CreateFromFile(Assembly.Load("System.Runtime").Location),
MetadataReference.CreateFromFile(Assembly.Load("System.Collections").Location),
};
var compilation = CSharpCompilation.Create(
"IntegrationTest",
syntaxTrees: new[] { syntaxTree },
references: references,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var compilationWithAnalyzers = compilation.WithAnalyzers(analyzers);
var allDiagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync();
// Filter to only our BWFC diagnostics
return allDiagnostics
.Where(d => d.Id.StartsWith("BWFC"))
.ToImmutableArray();
}
#endregion
}