-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorDiagnosticsTests.cs
More file actions
501 lines (392 loc) · 19.4 KB
/
GeneratorDiagnosticsTests.cs
File metadata and controls
501 lines (392 loc) · 19.4 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
namespace EasySourceGenerators.GeneratorTests;
[TestFixture]
public class GeneratorDiagnosticsTests
{
// -----------------------------------------------------------------------
// MSGH001 – Missing partial method
// -----------------------------------------------------------------------
[Test]
public void GeneratesMethod_WithNonExistingMethodName_EmitsMSGH001()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public partial class MyClass
{
public partial string ExistingMethod();
[MethodBodyGenerator("NonExistingMethod")]
private static string MyGenerator() => "hello";
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Diagnostic? msgh001 = diagnostics.FirstOrDefault(d => d.Id == "MSGH001");
Assert.That(msgh001, Is.Not.Null, "Expected MSGH001 diagnostic for non-existing method name");
Assert.That(msgh001!.GetMessage(), Does.Contain("NonExistingMethod"),
"Error message should mention the missing method name");
Assert.That(msgh001.GetMessage(), Does.Contain("MyClass"),
"Error message should mention the containing class name");
}
[Test]
public void GeneratesMethod_WithEmptyMethodName_DoesNotCrash()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public partial class MyClass
{
public partial string ExistingMethod();
[MethodBodyGenerator("")]
private static string MyGenerator() => "hello";
}
""";
// Should not throw – just silently skip (empty name is filtered before MSGH001)
Assert.DoesNotThrow(() => GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source));
}
// -----------------------------------------------------------------------
// MSGH002 – Generator method must be static
// -----------------------------------------------------------------------
[Test]
public void GeneratesMethod_WithNonStaticMethod_EmitsMSGH002()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public partial class MyClass
{
public partial string GetValue();
[MethodBodyGenerator(nameof(GetValue))]
private string NonStaticGenerator() => "hello";
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Diagnostic? msgh002 = diagnostics.FirstOrDefault(d => d.Id == "MSGH002");
Assert.That(msgh002, Is.Not.Null, "Expected MSGH002 diagnostic for non-static generator method");
Assert.That(msgh002!.GetMessage(), Does.Contain("NonStaticGenerator"),
"Error message should mention the non-static method name");
}
// -----------------------------------------------------------------------
// MSGH005 – Generator method has too many parameters (switch pattern)
// SwitchCase-based tests are commented out pending replacement with data-driven approach
// -----------------------------------------------------------------------
/*
[Test]
public void GeneratesMethod_SwitchCaseWithMoreThanOneParameter_EmitsMSGH005()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public static partial class MyClass
{
public static partial int GetValue(int key);
[MethodBodyGenerator(nameof(GetValue))]
[SwitchCase(arg1: 1)]
private static int GetValue_Generator(int key, string extraParam) => 42;
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Diagnostic? msgh005 = diagnostics.FirstOrDefault(d => d.Id == "MSGH005");
Assert.That(msgh005, Is.Not.Null, "Expected MSGH005 diagnostic for generator method with too many parameters");
Assert.That(msgh005!.GetMessage(), Does.Contain("GetValue_Generator"),
"Error message should mention the generator method name");
Assert.That(msgh005.GetMessage(), Does.Contain("2"),
"Error message should mention the number of parameters");
}
[Test]
public void GeneratesMethod_SwitchCaseWithOneParameter_DoesNotEmitMSGH005()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public static partial class MyClass
{
public static partial int GetValue(int key);
[MethodBodyGenerator(nameof(GetValue))]
[SwitchCase(arg1: 1)]
private static int GetValue_Generator(int key) => 42;
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Assert.That(diagnostics.Any(d => d.Id == "MSGH005"), Is.False,
"Should not emit MSGH005 for a generator method with exactly one parameter");
}
[Test]
public void GeneratesMethod_SwitchCaseWithZeroParameters_DoesNotEmitMSGH005()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public static partial class MyClass
{
public static partial int GetValue(int key);
[MethodBodyGenerator(nameof(GetValue))]
[SwitchCase(arg1: 1)]
private static int GetValue_Generator() => 42;
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Assert.That(diagnostics.Any(d => d.Id == "MSGH005"), Is.False,
"Should not emit MSGH005 for a generator method with zero parameters");
}
*/
// -----------------------------------------------------------------------
// MSGH004 – Generator method execution failed (unfinished fluent API)
// -----------------------------------------------------------------------
[Test]
public void GeneratesMethod_FluentUnfinished_EmitsMSGH004()
{
// The generator method returns IMethodBodyGenerator<int, int> directly
// (before calling GenerateSwitchBody / ForCases / ForDefaultCase), which the generator
// treats as a simple pattern and tries to execute it, resulting in a non-null
// IMethodBodyGenerator object whose ToString() is not meaningful.
// We expect either an MSGH004 or generated code with no meaningful content.
// Either way, no uncaught exception should escape the generator.
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public static partial class MyClass
{
public static partial int GetValue(int key);
[MethodBodyGenerator(nameof(GetValue))]
private static IMethodBodyGenerator GetValue_Generator() =>
Generate.Method().WithOneParameter<int>().WithReturnType<int>();
}
""";
Assert.DoesNotThrow(() => GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source),
"Generator should handle unfinished fluent construction without crashing");
}
// -----------------------------------------------------------------------
// Partial method called from fluent API / generator execution
// SwitchCase-based tests are commented out pending replacement
// -----------------------------------------------------------------------
/*
[Test]
public void GeneratesMethod_PartialMethodCalledInsideGenerator_EmitsMSGH004WithHelpfulMessage()
{
// The generator method calls a partial method that hasn't been implemented yet.
// The dummy implementation should throw PartialMethodCalledDuringGenerationException,
// which should be surfaced as MSGH004 with an informative message.
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public static partial class MyClass
{
public static partial int GetValue(int key);
public static partial string GetLabel(int key);
[MethodBodyGenerator(nameof(GetValue))]
[SwitchCase(arg1: 1)]
private static int GetValue_Generator(int key)
{
// Calling the other partial method from inside a generator — this should throw.
string label = GetLabel(key);
return label.Length;
}
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Diagnostic? msgh004 = diagnostics.FirstOrDefault(d => d.Id == "MSGH004");
Assert.That(msgh004, Is.Not.Null,
"Expected MSGH004 when a partial method is called inside a generator method");
Assert.That(msgh004!.GetMessage(), Does.Contain("GetValue_Generator"),
"Error message should mention the generator method name");
Assert.That(msgh004.GetMessage(), Does.Contain("PartialMethodCalledDuringGenerationException")
.Or.Contain("GetLabel")
.Or.Contain("partial method"),
"Error message should hint that a partial method was called during generation");
}
*/
// -----------------------------------------------------------------------
// MSGH006 – SwitchCase argument type mismatch
// SwitchCase-based tests are commented out pending replacement
// -----------------------------------------------------------------------
/*
[Test]
public void GeneratesMethod_SwitchCaseWithWrongArgumentType_EmitsMSGH006()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public static partial class MyClass
{
public static partial int GetValue(int key);
[MethodBodyGenerator(nameof(GetValue))]
[SwitchCase(arg1: "aaa")]
private static int GetValue_Generator(int key) => 42;
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Diagnostic? msgh006 = diagnostics.FirstOrDefault(d => d.Id == "MSGH006");
Assert.That(msgh006, Is.Not.Null, "Expected MSGH006 diagnostic for SwitchCase argument type mismatch");
Assert.That(msgh006!.GetMessage(), Does.Contain("string"),
"Error message should mention the provided type");
Assert.That(msgh006.GetMessage(), Does.Contain("int"),
"Error message should mention the expected parameter type");
}
[Test]
public void GeneratesMethod_SwitchCaseWithCorrectArgumentType_DoesNotEmitMSGH006()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public static partial class MyClass
{
public static partial int GetValue(int key);
[MethodBodyGenerator(nameof(GetValue))]
[SwitchCase(arg1: 1)]
private static int GetValue_Generator(int key) => 42;
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Assert.That(diagnostics.Any(d => d.Id == "MSGH006"), Is.False,
"Should not emit MSGH006 when SwitchCase argument type matches the parameter type");
}
*/
// -----------------------------------------------------------------------
// Type mismatch between generator return type and partial method return type
// -----------------------------------------------------------------------
[Test]
public void GeneratesMethod_SimplePattern_ReturnTypeMismatch_ProducesCompilationError()
{
// The generator method returns a string, but the partial method returns int.
// The generated code will contain `return "hello";` where int is expected.
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public partial class MyClass
{
public partial int GetValue();
[MethodBodyGenerator(nameof(GetValue))]
private static string GetValue_Generator() => "hello";
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetDiagnostics(source);
// The generator will produce code with a string literal where int is expected,
// yielding a compiler error in the output compilation.
Assert.That(diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), Is.True,
"Expected compilation error due to return type mismatch");
}
// -----------------------------------------------------------------------
// Correct configuration – should produce no generator errors
// -----------------------------------------------------------------------
[Test]
public void GeneratesMethod_SimplePattern_ValidConfiguration_ProducesNoDiagnosticErrors()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public partial class MyClass
{
public partial string GetValue();
[MethodBodyGenerator(nameof(GetValue))]
private static string GetValue_Generator() => "hello";
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Assert.That(diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error), Is.Empty,
"Valid generator configuration should produce no error diagnostics");
}
// SwitchCase-based tests are commented out pending replacement with data-driven approach
/*
[Test]
public void GeneratesMethod_SwitchCasePattern_ValidConfiguration_ProducesNoDiagnosticErrors()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public static partial class MyClass
{
public static partial int GetPiDigit(int index);
[MethodBodyGenerator(nameof(GetPiDigit))]
[SwitchCase(arg1: 0)]
[SwitchCase(arg1: 1)]
private static int GetPiDigit_Generator(int index) => index == 0 ? 3 : 1;
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Assert.That(diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error), Is.Empty,
"Valid switch-case generator configuration should produce no error diagnostics");
}
*/
// -----------------------------------------------------------------------
// CompilationReference scenario – simulates Rider's code inspector
// -----------------------------------------------------------------------
[Test]
public void GeneratesMethod_FluentPattern_WithCompilationReference_ProducesNoDiagnosticErrors()
{
// This test simulates the scenario that occurs in Rider's code inspector, where the
// abstractions project is provided as a CompilationReference (in-memory) instead of
// a PortableExecutableReference (file-based). This was the root cause of MSGH004 in Rider.
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public partial class MyClass
{
public partial string GetValue();
[MethodBodyGenerator(nameof(GetValue))]
static IMethodBodyGenerator GetValue_Generator() =>
Generate.Method().WithReturnType<string>().BodyReturningConstantValue(() => "hello");
}
""";
ImmutableArray<Diagnostic>? diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnosticsWithCompilationReference(source);
if (diagnostics == null)
{
Assert.Ignore("Could not locate abstractions source files on disk; skipping CompilationReference test.");
return;
}
Assert.That(diagnostics.Value.Where(d => d.Severity == DiagnosticSeverity.Error), Is.Empty,
"Fluent generator with a CompilationReference for abstractions should produce no error diagnostics");
}
// -----------------------------------------------------------------------
// MSGH001 – targeting an existing but non-partial method
// -----------------------------------------------------------------------
[Test]
public void GeneratesMethod_TargetingNonPartialMethod_EmitsMSGH001()
{
string source = """
using EasySourceGenerators.Abstractions;
namespace TestNamespace;
public partial class MyClass
{
public string NonPartialMethod() => "hello";
[MethodBodyGenerator("NonPartialMethod")]
private static string MyGenerator() => "world";
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Diagnostic? msgh001 = diagnostics.FirstOrDefault(d => d.Id == "MSGH001");
Assert.That(msgh001, Is.Not.Null,
"Expected MSGH001 when targeting an existing but non-partial method");
Assert.That(msgh001!.GetMessage(), Does.Contain("NonPartialMethod"),
"Error message should mention the non-partial method name");
}
// -----------------------------------------------------------------------
// Bool switch parameter – valid configuration
// SwitchCase-based tests are commented out pending replacement
// -----------------------------------------------------------------------
/*
[Test]
public void GeneratesMethod_SwitchCaseWithBoolParameter_ValidConfiguration_ProducesNoDiagnosticErrors()
{
string source = """
using EasySourceGenerators.Abstractions;
using System;
namespace TestNamespace;
public static partial class MyClass
{
public static partial string GetLabel(bool flag);
[MethodBodyGenerator(nameof(GetLabel))]
[SwitchCase(arg1: true)]
[SwitchCase(arg1: false)]
private static string GetLabel_Generator(bool flag) => flag ? "Yes" : "No";
[MethodBodyGenerator(nameof(GetLabel))]
[SwitchDefault]
private static Func<bool, string> GetLabel_Default() => _ => "Unknown";
}
""";
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
Assert.That(diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error), Is.Empty,
"Valid bool switch case generator should produce no error diagnostics");
}
*/
}