Skip to content

Commit 15a26e4

Browse files
committed
Added unit tests for new diagnostic
1 parent 21e0825 commit 15a26e4

File tree

1 file changed

+151
-5
lines changed

1 file changed

+151
-5
lines changed

UnitTests/UnitTests.SourceGenerators/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 151 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,31 +241,177 @@ public partial class SampleViewModel
241241
VerifyGeneratedDiagnostics<ICommandGenerator>(source, "MVVMTK0012");
242242
}
243243

244+
[TestCategory("Mvvm")]
245+
[TestMethod]
246+
public void UnsupportedCSharpLanguageVersion_FromINotifyPropertyChangedGenerator()
247+
{
248+
string source = @"
249+
using Microsoft.Toolkit.Mvvm.ComponentModel;
250+
251+
namespace MyApp
252+
{
253+
[INotifyPropertyChanged]
254+
public partial class SampleViewModel
255+
{
256+
}
257+
}";
258+
259+
VerifyGeneratedDiagnostics<INotifyPropertyChangedGenerator>(
260+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
261+
"MVVMTK0013");
262+
}
263+
264+
[TestCategory("Mvvm")]
265+
[TestMethod]
266+
public void UnsupportedCSharpLanguageVersion_FromObservableObjectGenerator()
267+
{
268+
string source = @"
269+
using Microsoft.Toolkit.Mvvm.ComponentModel;
270+
271+
namespace MyApp
272+
{
273+
[ObservableObject]
274+
public partial class SampleViewModel
275+
{
276+
}
277+
}";
278+
279+
VerifyGeneratedDiagnostics<ObservableObjectGenerator>(
280+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
281+
"MVVMTK0013");
282+
}
283+
284+
[TestCategory("Mvvm")]
285+
[TestMethod]
286+
public void UnsupportedCSharpLanguageVersion_FromObservablePropertyGenerator()
287+
{
288+
string source = @"
289+
using Microsoft.Toolkit.Mvvm.ComponentModel;
290+
291+
namespace MyApp
292+
{
293+
[INotifyPropertyChanged]
294+
public partial class SampleViewModel
295+
{
296+
[ObservableProperty]
297+
private string name;
298+
}
299+
}";
300+
301+
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(
302+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
303+
"MVVMTK0013");
304+
}
305+
306+
[TestCategory("Mvvm")]
307+
[TestMethod]
308+
public void UnsupportedCSharpLanguageVersion_FromObservableValidatorValidateAllPropertiesGenerator()
309+
{
310+
string source = @"
311+
using Microsoft.Toolkit.Mvvm.ComponentModel;
312+
313+
namespace MyApp
314+
{
315+
public partial class SampleViewModel : ObservableValidator
316+
{
317+
[Required]
318+
public string Name { get; set; }
319+
}
320+
}";
321+
322+
VerifyGeneratedDiagnostics<ObservableValidatorValidateAllPropertiesGenerator>(
323+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
324+
"MVVMTK0013");
325+
}
326+
327+
[TestCategory("Mvvm")]
328+
[TestMethod]
329+
public void UnsupportedCSharpLanguageVersion_FromICommandGenerator()
330+
{
331+
string source = @"
332+
using Microsoft.Toolkit.Mvvm.Input;
333+
334+
namespace MyApp
335+
{
336+
public partial class SampleViewModel
337+
{
338+
[ICommand]
339+
private void GreetUser(object value)
340+
{
341+
}
342+
}
343+
}";
344+
345+
VerifyGeneratedDiagnostics<ICommandGenerator>(
346+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
347+
"MVVMTK0013");
348+
}
349+
350+
[TestCategory("Mvvm")]
351+
[TestMethod]
352+
public void UnsupportedCSharpLanguageVersion_FromIMessengerRegisterAllGenerator()
353+
{
354+
string source = @"
355+
using Microsoft.Toolkit.Mvvm.Messaging;
356+
357+
namespace MyApp
358+
{
359+
public class MyMessage
360+
{
361+
}
362+
363+
public partial class SampleViewModel : IRecipient<MyMessage>
364+
{
365+
public void Receive(MyMessage message)
366+
{
367+
}
368+
}
369+
}";
370+
371+
VerifyGeneratedDiagnostics<IMessengerRegisterAllGenerator>(
372+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
373+
"MVVMTK0013");
374+
}
375+
244376
/// <summary>
245377
/// Verifies the output of a source generator.
246378
/// </summary>
247379
/// <typeparam name="TGenerator">The generator type to use.</typeparam>
248380
/// <param name="source">The input source to process.</param>
249381
/// <param name="diagnosticsIds">The diagnostic ids to expect for the input source code.</param>
250-
private void VerifyGeneratedDiagnostics<TGenerator>(string source, params string[] diagnosticsIds)
382+
private static void VerifyGeneratedDiagnostics<TGenerator>(string source, params string[] diagnosticsIds)
383+
where TGenerator : class, ISourceGenerator, new()
384+
{
385+
VerifyGeneratedDiagnostics<TGenerator>(CSharpSyntaxTree.ParseText(source), diagnosticsIds);
386+
}
387+
388+
/// <summary>
389+
/// Verifies the output of a source generator.
390+
/// </summary>
391+
/// <typeparam name="TGenerator">The generator type to use.</typeparam>
392+
/// <param name="syntaxTree">The input source tree to process.</param>
393+
/// <param name="diagnosticsIds">The diagnostic ids to expect for the input source code.</param>
394+
private static void VerifyGeneratedDiagnostics<TGenerator>(SyntaxTree syntaxTree, params string[] diagnosticsIds)
251395
where TGenerator : class, ISourceGenerator, new()
252396
{
253397
Type observableObjectType = typeof(ObservableObject);
254398
Type validationAttributeType = typeof(ValidationAttribute);
255399

256-
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
257-
258400
IEnumerable<MetadataReference> references =
259401
from assembly in AppDomain.CurrentDomain.GetAssemblies()
260402
where !assembly.IsDynamic
261403
let reference = MetadataReference.CreateFromFile(assembly.Location)
262404
select reference;
263405

264-
CSharpCompilation compilation = CSharpCompilation.Create("original", new SyntaxTree[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
406+
CSharpCompilation compilation = CSharpCompilation.Create(
407+
"original",
408+
new SyntaxTree[] { syntaxTree },
409+
references,
410+
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
265411

266412
ISourceGenerator generator = new TGenerator();
267413

268-
CSharpGeneratorDriver driver = CSharpGeneratorDriver.Create(generator);
414+
CSharpGeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { generator }, parseOptions: (CSharpParseOptions)syntaxTree.Options);
269415

270416
driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray<Diagnostic> diagnostics);
271417

0 commit comments

Comments
 (0)