@@ -241,31 +241,177 @@ public partial class SampleViewModel
241
241
VerifyGeneratedDiagnostics < ICommandGenerator > ( source , "MVVMTK0012" ) ;
242
242
}
243
243
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
+
244
376
/// <summary>
245
377
/// Verifies the output of a source generator.
246
378
/// </summary>
247
379
/// <typeparam name="TGenerator">The generator type to use.</typeparam>
248
380
/// <param name="source">The input source to process.</param>
249
381
/// <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 )
251
395
where TGenerator : class , ISourceGenerator , new ( )
252
396
{
253
397
Type observableObjectType = typeof ( ObservableObject ) ;
254
398
Type validationAttributeType = typeof ( ValidationAttribute ) ;
255
399
256
- SyntaxTree syntaxTree = CSharpSyntaxTree . ParseText ( source ) ;
257
-
258
400
IEnumerable < MetadataReference > references =
259
401
from assembly in AppDomain . CurrentDomain . GetAssemblies ( )
260
402
where ! assembly . IsDynamic
261
403
let reference = MetadataReference . CreateFromFile ( assembly . Location )
262
404
select reference ;
263
405
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 ) ) ;
265
411
266
412
ISourceGenerator generator = new TGenerator ( ) ;
267
413
268
- CSharpGeneratorDriver driver = CSharpGeneratorDriver . Create ( generator ) ;
414
+ CSharpGeneratorDriver driver = CSharpGeneratorDriver . Create ( new [ ] { generator } , parseOptions : ( CSharpParseOptions ) syntaxTree . Options ) ;
269
415
270
416
driver . RunGeneratorsAndUpdateCompilation ( compilation , out Compilation outputCompilation , out ImmutableArray < Diagnostic > diagnostics ) ;
271
417
0 commit comments