Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add AsyncVoidReturningRelayCommandMethodCodeFixer tests
  • Loading branch information
Sergio0694 committed Jun 8, 2023
commit ce23265ddc49303e5f9f45f5383c9860f33d81b5
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CSharpCodeFixTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest<
CommunityToolkit.Mvvm.SourceGenerators.AsyncVoidReturningRelayCommandMethodAnalyzer,
CommunityToolkit.Mvvm.CodeFixers.AsyncVoidReturningRelayCommandMethodCodeFixer,
Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier>;
using CSharpCodeFixVerifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier<
CommunityToolkit.Mvvm.SourceGenerators.AsyncVoidReturningRelayCommandMethodAnalyzer,
CommunityToolkit.Mvvm.CodeFixers.AsyncVoidReturningRelayCommandMethodCodeFixer,
Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier>;

namespace CommunityToolkit.Mvvm.SourceGenerators.Roslyn401.UnitTests;

[TestClass]
public class Test_AsyncVoidReturningRelayCommandMethodCodeFixer
{
[TestMethod]
public async Task AsyncVoidMethod_FileContainsSystemThreadingTasksUsingDirective()
{
string original = """
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;

partial class C
{
[RelayCommand]
private async void Foo()
{
}
}
""";

string @fixed = """
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;

partial class C
{
[RelayCommand]
private async Task Foo()
{
}
}
""";

CSharpCodeFixTest test = new()
{
TestCode = original,
FixedCode = @fixed,
ReferenceAssemblies = ReferenceAssemblies.Net.Net60
};

test.TestState.AdditionalReferences.Add(typeof(RelayCommand).Assembly);
test.ExpectedDiagnostics.AddRange(new[]
{
// /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)
CSharpCodeFixVerifier.Diagnostic().WithSpan(7, 24, 7, 27).WithArguments("C.Foo()")
});

await test.RunAsync();
}

[TestMethod]
public async Task AsyncVoidMethod_FileDoesNotContainSystemThreadingTasksUsingDirective()
{
string original = """
using CommunityToolkit.Mvvm.Input;

partial class C
{
[RelayCommand]
private async void Foo()
{
}
}
""";

string @fixed = """
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;

partial class C
{
[RelayCommand]
private async Task Foo()
{
}
}
""";

CSharpCodeFixTest test = new()
{
TestCode = original,
FixedCode = @fixed,
ReferenceAssemblies = ReferenceAssemblies.Net.Net60
};

test.TestState.AdditionalReferences.Add(typeof(RelayCommand).Assembly);
test.ExpectedDiagnostics.AddRange(new[]
{
// /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)
CSharpCodeFixVerifier.Diagnostic().WithSpan(6, 24, 6, 27).WithArguments("C.Foo()")
});

await test.RunAsync();
}
}