From db0b3cbae937720f721d3f2db61cc98cd0af3cc4 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Wed, 5 Jun 2024 15:17:06 -0700 Subject: [PATCH] Inline SetupShouldNotIncludeAsyncResult tests --- ...ncludeAsyncResult.TestBadForGenericTask.cs | 20 ----- ...cResult.TestOkForGenericTaskProperSetup.cs | 21 ----- ...ouldNotIncludeAsyncResult.TestOkForTask.cs | 20 ----- .../Moq.Analyzers.Test.csproj | 7 +- ...houldNotIncludeAsyncResultAnalyzerTests.cs | 79 ++++++++++++++++++- 5 files changed, 77 insertions(+), 70 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestBadForGenericTask.cs delete mode 100644 Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestOkForGenericTaskProperSetup.cs delete mode 100644 Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestOkForTask.cs diff --git a/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestBadForGenericTask.cs b/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestBadForGenericTask.cs deleted file mode 100644 index 16e21b9d..00000000 --- a/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestBadForGenericTask.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Threading.Tasks; -using Moq; - -namespace SetupShouldNotIncludeAsyncResult.TestBadForGenericTask; - -public class AsyncClient -{ - public virtual Task TaskAsync() => Task.CompletedTask; - - public virtual Task GenericTaskAsync() => Task.FromResult(string.Empty); -} - -internal class MyUnitTests -{ - private void TestBadForGenericTask() - { - var mock = new Mock(); - mock.Setup(c => c.GenericTaskAsync().Result); - } -} diff --git a/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestOkForGenericTaskProperSetup.cs b/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestOkForGenericTaskProperSetup.cs deleted file mode 100644 index bf740b7a..00000000 --- a/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestOkForGenericTaskProperSetup.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; -using Moq; - -namespace SetupShouldNotIncludeAsyncResult.TestOkForGenericTaskProperSetup; - -public class AsyncClient -{ - public virtual Task TaskAsync() => Task.CompletedTask; - - public virtual Task GenericTaskAsync() => Task.FromResult(string.Empty); -} - -internal class MyUnitTests -{ - private void TestOkForGenericTaskProperSetup() - { - var mock = new Mock(); - mock.Setup(c => c.GenericTaskAsync()) - .ReturnsAsync(string.Empty); - } -} diff --git a/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestOkForTask.cs b/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestOkForTask.cs deleted file mode 100644 index 6f991b24..00000000 --- a/Source/Moq.Analyzers.Test/Data/SetupShouldNotIncludeAsyncResult.TestOkForTask.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Threading.Tasks; -using Moq; - -namespace SetupShouldNotIncludeAsyncResult.TestOkForTask; - -public class AsyncClient -{ - public virtual Task TaskAsync() => Task.CompletedTask; - - public virtual Task GenericTaskAsync() => Task.FromResult(string.Empty); -} - -internal class MyUnitTests -{ - private void TestOkForTask() - { - var mock = new Mock(); - mock.Setup(c => c.TaskAsync()); - } -} diff --git a/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj b/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj index 941099cd..b4e3a49d 100644 --- a/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj +++ b/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -41,10 +41,5 @@ - - - PreserveNewest - - \ No newline at end of file diff --git a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs index 2252fa91..47819bc8 100644 --- a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs @@ -11,19 +11,92 @@ public class SetupShouldNotIncludeAsyncResultAnalyzerTests : DiagnosticVerifier [Fact] public Task ShouldPassWhenSetupWithoutReturn() { - return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/SetupShouldNotIncludeAsyncResult.TestOkForTask.cs"))); + return Verify(VerifyCSharpDiagnostic( + [ + """ + using System.Threading.Tasks; + using Moq; + + namespace SetupShouldNotIncludeAsyncResult.TestOkForTask; + + public class AsyncClient + { + public virtual Task TaskAsync() => Task.CompletedTask; + + public virtual Task GenericTaskAsync() => Task.FromResult(string.Empty); + } + + internal class MyUnitTests + { + private void TestOkForTask() + { + var mock = new Mock(); + mock.Setup(c => c.TaskAsync()); + } + } + """ + ])); } [Fact] public Task ShouldPassWhenSetupWithReturnsAsync() { - return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/SetupShouldNotIncludeAsyncResult.TestOkForGenericTaskProperSetup.cs"))); + return Verify(VerifyCSharpDiagnostic( + [ + """ + using System.Threading.Tasks; + using Moq; + + namespace SetupShouldNotIncludeAsyncResult.TestOkForGenericTaskProperSetup; + + public class AsyncClient + { + public virtual Task TaskAsync() => Task.CompletedTask; + + public virtual Task GenericTaskAsync() => Task.FromResult(string.Empty); + } + + internal class MyUnitTests + { + private void TestOkForGenericTaskProperSetup() + { + var mock = new Mock(); + mock.Setup(c => c.GenericTaskAsync()) + .ReturnsAsync(string.Empty); + } + } + """ + ])); } [Fact] public Task ShouldFailWhenSetupWithTaskResult() { - return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/SetupShouldNotIncludeAsyncResult.TestBadForGenericTask.cs"))); + return Verify(VerifyCSharpDiagnostic( + [ + """ + using System.Threading.Tasks; + using Moq; + + namespace SetupShouldNotIncludeAsyncResult.TestBadForGenericTask; + + public class AsyncClient + { + public virtual Task TaskAsync() => Task.CompletedTask; + + public virtual Task GenericTaskAsync() => Task.FromResult(string.Empty); + } + + internal class MyUnitTests + { + private void TestBadForGenericTask() + { + var mock = new Mock(); + mock.Setup(c => c.GenericTaskAsync().Result); + } + } + """ + ])); } protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()