|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Threading.Tasks; |
| 6 | +using CommunityToolkit.Mvvm.Input; |
| 7 | +using Microsoft.CodeAnalysis.Testing; |
| 8 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | +using CSharpCodeFixTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest< |
| 10 | + CommunityToolkit.Mvvm.SourceGenerators.AsyncVoidReturningRelayCommandMethodAnalyzer, |
| 11 | + CommunityToolkit.Mvvm.CodeFixers.AsyncVoidReturningRelayCommandMethodCodeFixer, |
| 12 | + Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier>; |
| 13 | +using CSharpCodeFixVerifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier< |
| 14 | + CommunityToolkit.Mvvm.SourceGenerators.AsyncVoidReturningRelayCommandMethodAnalyzer, |
| 15 | + CommunityToolkit.Mvvm.CodeFixers.AsyncVoidReturningRelayCommandMethodCodeFixer, |
| 16 | + Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier>; |
| 17 | + |
| 18 | +namespace CommunityToolkit.Mvvm.SourceGenerators.Roslyn401.UnitTests; |
| 19 | + |
| 20 | +[TestClass] |
| 21 | +public class Test_AsyncVoidReturningRelayCommandMethodCodeFixer |
| 22 | +{ |
| 23 | + [TestMethod] |
| 24 | + public async Task AsyncVoidMethod_FileContainsSystemThreadingTasksUsingDirective() |
| 25 | + { |
| 26 | + string original = """ |
| 27 | + using System.Threading.Tasks; |
| 28 | + using CommunityToolkit.Mvvm.Input; |
| 29 | +
|
| 30 | + partial class C |
| 31 | + { |
| 32 | + [RelayCommand] |
| 33 | + private async void Foo() |
| 34 | + { |
| 35 | + } |
| 36 | + } |
| 37 | + """; |
| 38 | + |
| 39 | + string @fixed = """ |
| 40 | + using System.Threading.Tasks; |
| 41 | + using CommunityToolkit.Mvvm.Input; |
| 42 | +
|
| 43 | + partial class C |
| 44 | + { |
| 45 | + [RelayCommand] |
| 46 | + private async Task Foo() |
| 47 | + { |
| 48 | + } |
| 49 | + } |
| 50 | + """; |
| 51 | + |
| 52 | + CSharpCodeFixTest test = new() |
| 53 | + { |
| 54 | + TestCode = original, |
| 55 | + FixedCode = @fixed, |
| 56 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 |
| 57 | + }; |
| 58 | + |
| 59 | + test.TestState.AdditionalReferences.Add(typeof(RelayCommand).Assembly); |
| 60 | + test.ExpectedDiagnostics.AddRange(new[] |
| 61 | + { |
| 62 | + // /0/Test0.cs(7,24): error MVVMTK0039: The method C.Foo() annotated with [RelayCommand] is async void (make sure to return a Task type instead) |
| 63 | + CSharpCodeFixVerifier.Diagnostic().WithSpan(7, 24, 7, 27).WithArguments("C.Foo()") |
| 64 | + }); |
| 65 | + |
| 66 | + await test.RunAsync(); |
| 67 | + } |
| 68 | + |
| 69 | + [TestMethod] |
| 70 | + public async Task AsyncVoidMethod_FileDoesNotContainSystemThreadingTasksUsingDirective() |
| 71 | + { |
| 72 | + string original = """ |
| 73 | + using CommunityToolkit.Mvvm.Input; |
| 74 | +
|
| 75 | + partial class C |
| 76 | + { |
| 77 | + [RelayCommand] |
| 78 | + private async void Foo() |
| 79 | + { |
| 80 | + } |
| 81 | + } |
| 82 | + """; |
| 83 | + |
| 84 | + string @fixed = """ |
| 85 | + using System.Threading.Tasks; |
| 86 | + using CommunityToolkit.Mvvm.Input; |
| 87 | +
|
| 88 | + partial class C |
| 89 | + { |
| 90 | + [RelayCommand] |
| 91 | + private async Task Foo() |
| 92 | + { |
| 93 | + } |
| 94 | + } |
| 95 | + """; |
| 96 | + |
| 97 | + CSharpCodeFixTest test = new() |
| 98 | + { |
| 99 | + TestCode = original, |
| 100 | + FixedCode = @fixed, |
| 101 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 |
| 102 | + }; |
| 103 | + |
| 104 | + test.TestState.AdditionalReferences.Add(typeof(RelayCommand).Assembly); |
| 105 | + test.ExpectedDiagnostics.AddRange(new[] |
| 106 | + { |
| 107 | + // /0/Test0.cs(7,24): error MVVMTK0039: The method C.Foo() annotated with [RelayCommand] is async void (make sure to return a Task type instead) |
| 108 | + CSharpCodeFixVerifier.Diagnostic().WithSpan(6, 24, 6, 27).WithArguments("C.Foo()") |
| 109 | + }); |
| 110 | + |
| 111 | + await test.RunAsync(); |
| 112 | + } |
| 113 | +} |
0 commit comments