From 02ce110d216df00432c32089760ca30c5ae9de34 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Wed, 5 Jun 2024 16:46:23 -0700 Subject: [PATCH 01/16] Comment out analyzer tests to prevent build failures --- Source/Moq.Analyzers.Test/AbstractClassTests.cs | 12 ++++++------ .../AsAcceptOnlyInterfaceAnalyzerTests.cs | 8 ++++---- ...natureShouldMatchMockedMethodAnalyzerTests.cs | 10 +++++----- ...gnatureShouldMatchMockedMethodCodeFixTests.cs | 10 +++++----- ...nstructorArgumentsShouldMatchAnalyzerTests.cs | 6 +++--- ...ctorArgumentsForInterfaceMockAnalyzerTests.cs | 12 ++++++------ .../NoMethodsInPropertySetupAnalyzerTests.cs | 4 ++-- .../NoSealedClassMocksAnalyzerTests.cs | 4 ++-- ...UsedOnlyForOverridableMembersAnalyzerTests.cs | 16 ++++++++-------- ...upShouldNotIncludeAsyncResultAnalyzerTests.cs | 6 +++--- 10 files changed, 44 insertions(+), 44 deletions(-) diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.cs b/Source/Moq.Analyzers.Test/AbstractClassTests.cs index 0c60b243..fd3fd448 100644 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.cs +++ b/Source/Moq.Analyzers.Test/AbstractClassTests.cs @@ -8,7 +8,7 @@ namespace Moq.Analyzers.Test; public class AbstractClassTests : DiagnosticVerifier { // TODO: Review use of `.As<>()` in the test cases. It is not clear what purpose it serves. - [Fact] + // [Fact] public Task ShouldFailOnGenericTypesWithMismatchArgs() { return Verify(VerifyCSharpDiagnostic( @@ -52,7 +52,7 @@ private void TestBadWithGeneric() ])); } - [Fact] + // [Fact] public Task ShouldPassOnGenericTypesWithNoArgs() { return Verify(VerifyCSharpDiagnostic( @@ -83,7 +83,7 @@ private void TestForBaseGenericNoArgs() ])); } - [Fact] + // [Fact] public Task ShouldFailOnMismatchArgs() { return Verify(VerifyCSharpDiagnostic( @@ -127,7 +127,7 @@ private void TestBad() ])); } - [Fact] + // [Fact] public Task ShouldPassWithNoArgs() { return Verify(VerifyCSharpDiagnostic( @@ -155,7 +155,7 @@ private void TestForBaseNoArgs() ])); } - [Fact(Skip = "I think this _should_ fail, but currently passes. Tracked by #55.")] + // [Fact(Skip = "I think this _should_ fail, but currently passes. Tracked by #55.")] public Task ShouldFailWithArgsNonePassed() { return Verify(VerifyCSharpDiagnostic( @@ -189,7 +189,7 @@ private void TestForBaseWithArgsNonePassed() ])); } - [Fact] + // [Fact] public Task ShouldPassWithArgsPassed() { return Verify(VerifyCSharpDiagnostic( diff --git a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs index b1295ffc..e3c20687 100644 --- a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs @@ -8,7 +8,7 @@ namespace Moq.Analyzers.Test; public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier { - [Fact] + // [Fact] public Task ShouldFailWhenUsingAsWithAbstractClass() { return Verify(VerifyCSharpDiagnostic( @@ -35,7 +35,7 @@ private void TestBadAsForAbstractClass() ])); } - [Fact] + // [Fact] public Task ShouldFailWhenUsingAsWithConcreteClass() { return Verify(VerifyCSharpDiagnostic( @@ -73,7 +73,7 @@ private void TestBadAsForNonAbstractClass() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenUsingAsWithInterface() { return Verify(VerifyCSharpDiagnostic( @@ -105,7 +105,7 @@ private void TestOkAsForInterface() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenUsingAsWithInterfaceWithSetup() { return Verify(VerifyCSharpDiagnostic( diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs index cfe825e8..fc619c22 100644 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs @@ -6,31 +6,31 @@ namespace Moq.Analyzers.Test; public class CallbackSignatureShouldMatchMockedMethodAnalyzerTests : CallbackSignatureShouldMatchMockedMethodBase { - [Fact] + // [Fact] public Task ShouldPassWhenCorrectSetupAndReturns() { return Verify(VerifyCSharpDiagnostic(GoodSetupAndCallback)); } - [Fact] + // [Fact] public Task ShouldFailWhenIncorrectCallbacks() { return Verify(VerifyCSharpDiagnostic(BadCallbacks)); } - [Fact] + // [Fact] public Task ShouldPassWhenCorrectSetupAndCallbacks() { return Verify(VerifyCSharpDiagnostic(GoodSetupAndCallback)); } - [Fact] + // [Fact] public Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() { return Verify(VerifyCSharpDiagnostic(GoodSetupAndParameterlessCallback)); } - [Fact] + // [Fact] public Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() { return Verify(VerifyCSharpDiagnostic(GoodSetupAndReturnsAndCallback)); diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs index 49221e4d..6e6253ab 100644 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs +++ b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs @@ -9,31 +9,31 @@ namespace Moq.Analyzers.Test; public class CallbackSignatureShouldMatchMockedMethodCodeFixTests : CallbackSignatureShouldMatchMockedMethodBase { - [Fact] + // [Fact] public Task ShouldPassWhenCorrectSetupAndReturns() { return Verify(VerifyCSharpFix(GoodSetupAndReturns)); } - [Fact] + // [Fact] public Task ShouldSuggestQuickFixWhenIncorrectCallbacks() { return Verify(VerifyCSharpFix(BadCallbacks)); } - [Fact] + // [Fact] public Task ShouldPassWhenCorrectSetupAndCallbacks() { return Verify(VerifyCSharpFix(GoodSetupAndCallback)); } - [Fact] + // [Fact] public Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() { return Verify(VerifyCSharpFix(GoodSetupAndParameterlessCallback)); } - [Fact] + // [Fact] public Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() { return Verify(VerifyCSharpFix(GoodSetupAndReturnsAndCallback)); diff --git a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs index d825fbb3..6d34dcf2 100644 --- a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs @@ -8,7 +8,7 @@ namespace Moq.Analyzers.Test; public class ConstructorArgumentsShouldMatchAnalyzerTests : DiagnosticVerifier { - [Fact] + // [Fact] public Task ShouldPassWhenConstructorArgumentsMatch() { return Verify(VerifyCSharpDiagnostic( @@ -61,7 +61,7 @@ private void TestGood() ])); } - [Fact] + // [Fact] public Task ShouldFailWhenConstructorArumentsDoNotMatch() { return Verify(VerifyCSharpDiagnostic( @@ -98,7 +98,7 @@ private void TestBad() ])); } - [Fact] + // [Fact] public Task ShouldFailWhenConstructorArumentsWithExplicitMockBehaviorDoNotMatch() { return Verify(VerifyCSharpDiagnostic( diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs index 947198bd..c26c61bf 100644 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs @@ -8,7 +8,7 @@ namespace Moq.Analyzers.Test; public class NoConstructorArgumentsForInterfaceMockAnalyzerTests : DiagnosticVerifier { - [Fact] + // [Fact] public Task ShouldFailIfMockedInterfaceHasConstructorParameters() { return Verify(VerifyCSharpDiagnostic( @@ -37,7 +37,7 @@ private void TestBad() ])); } - [Fact] + // [Fact] public Task ShouldFailIfMockedInterfaceHasConstructorParametersAndExplicitMockBehavior() { return Verify(VerifyCSharpDiagnostic( @@ -66,7 +66,7 @@ private void TestBadWithMockBehavior() ])); } - [Fact] + // [Fact] public Task ShouldPassIfMockedInterfaceDoesNotHaveConstructorParameters() { return Verify(VerifyCSharpDiagnostic( @@ -96,7 +96,7 @@ private void TestGood() } // TODO: This feels like it should be in every analyzer's tests - [Fact] + // [Fact] public Task ShouldPassIfCustomMockClassIsUsed() { return Verify(VerifyCSharpDiagnostic( @@ -145,7 +145,7 @@ private void TestFakeMoq() } // TODO: This feels duplicated with other tests - [Fact] + // [Fact] public Task ShouldFailIsRealMoqIsUsedWithInvalidParameters() { return Verify(VerifyCSharpDiagnostic( @@ -193,7 +193,7 @@ private void TestRealMoqWithBadParameters() ])); } - [Fact] + // [Fact] public Task ShouldPassIfRealMoqIsUsedWithValidParameters() { return Verify(VerifyCSharpDiagnostic( diff --git a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs index ec2e96a9..b7c8e173 100644 --- a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs @@ -8,7 +8,7 @@ namespace Moq.Analyzers.Test; public class NoMethodsInPropertySetupAnalyzerTests : DiagnosticVerifier { - [Fact] + // [Fact] public Task ShouldPassWhenPropertiesUsePropertySetup() { return Verify(VerifyCSharpDiagnostic( @@ -45,7 +45,7 @@ private void TestGood() ])); } - [Fact] + // [Fact] public Task ShouldFailWhenMethodsUsePropertySetup() { return Verify(VerifyCSharpDiagnostic( diff --git a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs index 748fcf8e..2cadac0a 100644 --- a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs @@ -8,7 +8,7 @@ namespace Moq.Analyzers.Test; public class NoSealedClassMocksAnalyzerTests : DiagnosticVerifier { - [Fact] + // [Fact] public Task ShouldFailWhenClassIsSealed() { return Verify(VerifyCSharpDiagnostic( @@ -34,7 +34,7 @@ private void Sealed() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenClassIsNotSealed() { return Verify(VerifyCSharpDiagnostic( diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs index 2503d7e1..3c8391d8 100644 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs @@ -8,7 +8,7 @@ namespace Moq.Analyzers.Test; public class SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests : DiagnosticVerifier { - [Fact] + // [Fact] public Task ShouldFailWhenSetupIsCalledWithANonVirtualMethod() { return Verify(VerifyCSharpDiagnostic( @@ -39,7 +39,7 @@ private void TestBadSetupForNonVirtualMethod() ])); } - [Fact] + // [Fact] public Task ShouldFailWhenSetupIsCalledWithANonVirtualProperty() { return Verify(VerifyCSharpDiagnostic( @@ -67,7 +67,7 @@ private void TestBadSetupForNonVirtualProperty() ])); } - [Fact] + // [Fact] public Task ShouldFailWhenSetupIsCalledWithASealedMethod() { return Verify(VerifyCSharpDiagnostic( @@ -106,7 +106,7 @@ private void TestBadSetupForSealedMethod() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenSetupIsCalledWithAnAbstractMethod() { return Verify(VerifyCSharpDiagnostic( @@ -137,7 +137,7 @@ private void TestOkForAbstractMethod() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenSetupIsCalledWithAnInterfaceMethod() { return Verify(VerifyCSharpDiagnostic( @@ -164,7 +164,7 @@ private void TestOkForInterfaceMethod() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenSetupIsCalledWithAnInterfaceProperty() { return Verify(VerifyCSharpDiagnostic( @@ -191,7 +191,7 @@ private void TestOkForInterfaceProperty() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenSetupIsCalledWithAnOverrideOfAnAbstractMethod() { return Verify(VerifyCSharpDiagnostic( @@ -230,7 +230,7 @@ private void TestOkForOverrideAbstractMethod() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenSetupIsCalledWithAVirtualMethod() { return Verify(VerifyCSharpDiagnostic( diff --git a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs index 47819bc8..ea21d478 100644 --- a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs @@ -8,7 +8,7 @@ namespace Moq.Analyzers.Test; public class SetupShouldNotIncludeAsyncResultAnalyzerTests : DiagnosticVerifier { - [Fact] + // [Fact] public Task ShouldPassWhenSetupWithoutReturn() { return Verify(VerifyCSharpDiagnostic( @@ -38,7 +38,7 @@ private void TestOkForTask() ])); } - [Fact] + // [Fact] public Task ShouldPassWhenSetupWithReturnsAsync() { return Verify(VerifyCSharpDiagnostic( @@ -69,7 +69,7 @@ private void TestOkForGenericTaskProperSetup() ])); } - [Fact] + // [Fact] public Task ShouldFailWhenSetupWithTaskResult() { return Verify(VerifyCSharpDiagnostic( From 81900de3edbb6e0f8e35f0663fb758a390ceac09 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Wed, 5 Jun 2024 16:47:13 -0700 Subject: [PATCH 02/16] Add nuget.config and test harness --- .../Moq.Analyzers.Test.csproj | 4 ++++ build/targets/tests/Packages.props | 2 ++ build/targets/tests/Tests.props | 2 -- nuget.config | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 nuget.config diff --git a/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj b/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj index f23cac3e..9c75a87b 100644 --- a/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj +++ b/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj @@ -19,8 +19,12 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + + + + diff --git a/build/targets/tests/Packages.props b/build/targets/tests/Packages.props index a05dbacb..d6d77013 100644 --- a/build/targets/tests/Packages.props +++ b/build/targets/tests/Packages.props @@ -1,5 +1,7 @@ + + diff --git a/build/targets/tests/Tests.props b/build/targets/tests/Tests.props index 7583b1f5..80693a59 100644 --- a/build/targets/tests/Tests.props +++ b/build/targets/tests/Tests.props @@ -2,8 +2,6 @@ - - all diff --git a/nuget.config b/nuget.config new file mode 100644 index 00000000..acd01c43 --- /dev/null +++ b/nuget.config @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + From 9b24ca8f60d32684af05122bf2383892fa9d8049 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 16:05:33 -0700 Subject: [PATCH 03/16] Move CodeAnalysis package forward to oldest supported version --- Directory.Packages.props | 13 ++++++++++++- README.md | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 369bcdd2..dad54e4b 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -6,9 +6,20 @@ + + + + + - diff --git a/README.md b/README.md index dca6700c..55efbe12 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ Install ["Moq.Analyzers" NuGet package](https://www.nuget.org/packages/Moq.Analyzers) into test projects using Moq. +You must use an in-support version of the .NET SDK (i.e. 6+). + ## Contributions are welcome! Moq.Analyzers continues to evolve and add new features. Any help will be appreciated. You can report issues, develop new features, improve the documention, or do other cool stuff. From aadd15eb3874daeea48ec1dcdc936ca947343e9e Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 16:04:42 -0700 Subject: [PATCH 04/16] WIP: Tests work --- .../Moq.Analyzers.Test/AbstractClassTests.cs | 40 +-- .../AsAcceptOnlyInterfaceAnalyzerTests.cs | 21 +- ...ureShouldMatchMockedMethodAnalyzerTests.cs | 56 ++-- ...ackSignatureShouldMatchMockedMethodBase.cs | 317 +++++++++--------- ...tureShouldMatchMockedMethodCodeFixTests.cs | 57 ++-- ...ructorArgumentsShouldMatchAnalyzerTests.cs | 18 +- .../Helpers/CodeFixVerifier.Helper.cs | 83 ----- .../Helpers/CodeFixVerifier.cs | 112 ------- .../Helpers/DiagnosticResult.cs | 54 --- .../Helpers/DiagnosticResultLocation.cs | 32 -- .../Helpers/DiagnosticVerifier.Helper.cs | 180 ---------- .../Helpers/DiagnosticVerifier.cs | 96 +----- ...rArgumentsForInterfaceMockAnalyzerTests.cs | 27 +- .../NoMethodsInPropertySetupAnalyzerTests.cs | 16 +- .../NoSealedClassMocksAnalyzerTests.cs | 15 +- ...dOnlyForOverridableMembersAnalyzerTests.cs | 33 +- ...houldNotIncludeAsyncResultAnalyzerTests.cs | 18 +- 17 files changed, 288 insertions(+), 887 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.Helper.cs delete mode 100644 Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs delete mode 100644 Source/Moq.Analyzers.Test/Helpers/DiagnosticResult.cs delete mode 100644 Source/Moq.Analyzers.Test/Helpers/DiagnosticResultLocation.cs delete mode 100644 Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.Helper.cs diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.cs b/Source/Moq.Analyzers.Test/AbstractClassTests.cs index fd3fd448..09fb416e 100644 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.cs +++ b/Source/Moq.Analyzers.Test/AbstractClassTests.cs @@ -1,18 +1,17 @@ using System.IO; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -public class AbstractClassTests : DiagnosticVerifier +public class AbstractClassTests : DiagnosticVerifier { // TODO: Review use of `.As<>()` in the test cases. It is not clear what purpose it serves. - // [Fact] - public Task ShouldFailOnGenericTypesWithMismatchArgs() + [Fact] + public async Task ShouldFailOnGenericTypesWithMismatchArgs() { - return Verify(VerifyCSharpDiagnostic( - [ + await VerifyCSharpDiagnostic( """ namespace Moq.Analyzers.Test.Data.AbstractClass.GenericMistmatchArgs; @@ -39,24 +38,22 @@ internal class MyUnitTests private void TestBadWithGeneric() { // The class has a constructor that takes an Int32 but passes a String - var mock = new Mock>("42"); + var mock = new Mock>{|Moq1002:("42")|}; // The class has a ctor with two arguments [Int32, String], but they are passed in reverse order - var mock1 = new Mock>("42", 42); + var mock1 = new Mock>{|Moq1002:("42", 42)|}; // The class has a ctor but does not take any arguments - var mock2 = new Mock>(42); + var mock2 = new Mock>{|Moq1002:(42)|}; } } - """ - ])); + """); } // [Fact] public Task ShouldPassOnGenericTypesWithNoArgs() { return Verify(VerifyCSharpDiagnostic( - [ """ namespace Moq.Analyzers.Test.Data.AbstractClass.GenericNoArgs; @@ -80,14 +77,13 @@ private void TestForBaseGenericNoArgs() } } """ - ])); + )); } // [Fact] public Task ShouldFailOnMismatchArgs() { return Verify(VerifyCSharpDiagnostic( - [ """ namespace Moq.Analyzers.Test.Data.AbstractClass.MismatchArgs; @@ -124,14 +120,13 @@ private void TestBad() } } """ - ])); + )); } // [Fact] public Task ShouldPassWithNoArgs() { return Verify(VerifyCSharpDiagnostic( - [ """ namespace Moq.Analyzers.Test.Data.AbstractClass.NoArgs; @@ -152,14 +147,13 @@ private void TestForBaseNoArgs() } } """ - ])); + )); } // [Fact(Skip = "I think this _should_ fail, but currently passes. Tracked by #55.")] public Task ShouldFailWithArgsNonePassed() { return Verify(VerifyCSharpDiagnostic( - [ """ namespace Moq.Analyzers.Test.Data.AbstractClass.WithArgsNonePassed; @@ -186,14 +180,13 @@ private void TestForBaseWithArgsNonePassed() } } """ - ])); + )); } // [Fact] public Task ShouldPassWithArgsPassed() { return Verify(VerifyCSharpDiagnostic( - [ """ namespace Moq.Analyzers.Test.DataAbstractClass.WithArgsPassed; @@ -234,11 +227,6 @@ private void TestForBaseWithArgsPassed() } } """ - ])); - } - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new ConstructorArgumentsShouldMatchAnalyzer(); + )); } } diff --git a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs index e3c20687..09b6d9da 100644 --- a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs @@ -1,18 +1,17 @@ using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier +public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier { // [Fact] public Task ShouldFailWhenUsingAsWithAbstractClass() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -32,14 +31,13 @@ private void TestBadAsForAbstractClass() } } """ - ])); + )); } // [Fact] public Task ShouldFailWhenUsingAsWithConcreteClass() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -70,14 +68,13 @@ private void TestBadAsForNonAbstractClass() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenUsingAsWithInterface() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -102,14 +99,13 @@ private void TestOkAsForInterface() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenUsingAsWithInterfaceWithSetup() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -136,11 +132,6 @@ private void TestOkAsForInterfaceWithConfiguration() } } """ - ])); - } - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new AsShouldBeUsedOnlyForInterfaceAnalyzer(); + )); } } diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs index fc619c22..3fdc861b 100644 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs @@ -4,35 +4,35 @@ namespace Moq.Analyzers.Test; -public class CallbackSignatureShouldMatchMockedMethodAnalyzerTests : CallbackSignatureShouldMatchMockedMethodBase -{ - // [Fact] - public Task ShouldPassWhenCorrectSetupAndReturns() - { - return Verify(VerifyCSharpDiagnostic(GoodSetupAndCallback)); - } +//public class CallbackSignatureShouldMatchMockedMethodAnalyzerTests : CallbackSignatureShouldMatchMockedMethodBase +//{ +// // [Fact] +// public Task ShouldPassWhenCorrectSetupAndReturns() +// { +// return Verify(VerifyCSharpDiagnostic(GoodSetupAndCallback)); +// } - // [Fact] - public Task ShouldFailWhenIncorrectCallbacks() - { - return Verify(VerifyCSharpDiagnostic(BadCallbacks)); - } +// // [Fact] +// public Task ShouldFailWhenIncorrectCallbacks() +// { +// return Verify(VerifyCSharpDiagnostic(BadCallbacks)); +// } - // [Fact] - public Task ShouldPassWhenCorrectSetupAndCallbacks() - { - return Verify(VerifyCSharpDiagnostic(GoodSetupAndCallback)); - } +// // [Fact] +// public Task ShouldPassWhenCorrectSetupAndCallbacks() +// { +// return Verify(VerifyCSharpDiagnostic(GoodSetupAndCallback)); +// } - // [Fact] - public Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() - { - return Verify(VerifyCSharpDiagnostic(GoodSetupAndParameterlessCallback)); - } +// // [Fact] +// public Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() +// { +// return Verify(VerifyCSharpDiagnostic(GoodSetupAndParameterlessCallback)); +// } - // [Fact] - public Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() - { - return Verify(VerifyCSharpDiagnostic(GoodSetupAndReturnsAndCallback)); - } -} +// // [Fact] +// public Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() +// { +// return Verify(VerifyCSharpDiagnostic(GoodSetupAndReturnsAndCallback)); +// } +//} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodBase.cs b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodBase.cs index 87a09ebe..d1e27437 100644 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodBase.cs +++ b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodBase.cs @@ -1,166 +1,165 @@ using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; namespace Moq.Analyzers.Test; // TODO: These tests should be broken down further // TODO: Merge the diagnostic and codefix tests into a single harness -public abstract class CallbackSignatureShouldMatchMockedMethodBase : CodeFixVerifier -{ - protected string GoodSetupAndReturns { get; } = - """ - using System; - using System.Collections.Generic; - using Moq; - - namespace CallbackSignatureShouldMatchMockedMethod.MyGoodSetupAndReturns; - - internal interface IFoo - { - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); - } - - internal class MyUnitTests - { - private void MyGoodSetupAndReturns() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Returns((string s) => { return 0; }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns((int i, string s, DateTime dt) => { return 0; }); - mock.Setup(x => x.Do(It.IsAny>())).Returns((List l) => { return 0; }); - } - } - """; - - protected string BadCallbacks { get; } = - """ - using System; - using System.Collections.Generic; - using Moq; - - namespace CallbackSignatureShouldMatchMockedMethod.TestBadCallbacks; - - internal interface IFoo - { - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); - } - - internal class MyUnitTests - { - private void TestBadCallbacks() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Callback((int i) => { }); - mock.Setup(x => x.Do(It.IsAny())).Callback((string s1, string s2) => { }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((string s1, int i1) => { }); - mock.Setup(x => x.Do(It.IsAny>())).Callback((int i) => { }); - } - } - """; - - protected string GoodSetupAndCallback { get; } = - """ - using System; - using System.Collections.Generic; - using Moq; - - namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndCallback; - - internal interface IFoo - { - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); - } - - internal class MyUnitTests - { - private void TestGoodSetupAndCallback() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((int i, string s, DateTime dt) => { }); - mock.Setup(x => x.Do(It.IsAny>())).Callback((List l) => { }); - } - } - """; - - protected string GoodSetupAndParameterlessCallback { get; } = - """ - using System; - using System.Collections.Generic; - using Moq; - - namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndParameterlessCallback; - - internal interface IFoo - { - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); - } - - internal class MyUnitTests - { - private void TestGoodSetupAndParameterlessCallback() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Callback(() => { }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback(() => { }); - mock.Setup(x => x.Do(It.IsAny>())).Callback(() => { }); - } - } - """; - - protected string GoodSetupAndReturnsAndCallback { get; } = - """ - using System; - using System.Collections.Generic; - using Moq; - - namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndReturnsAndCallback; - - internal interface IFoo - { - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); - } - - internal class MyUnitTests - { - private void TestGoodSetupAndReturnsAndCallback() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Returns(0).Callback((string s) => { }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns(0).Callback((int i, string s, DateTime dt) => { }); - mock.Setup(x => x.Do(It.IsAny>())).Returns(0).Callback((List l) => { }); - } - } - """; - - protected override CodeFixProvider GetCSharpCodeFixProvider() - { - return new CallbackSignatureShouldMatchMockedMethodCodeFix(); - } - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new CallbackSignatureShouldMatchMockedMethodAnalyzer(); - } -} +//public abstract class CallbackSignatureShouldMatchMockedMethodBase : CodeFixVerifier +//{ +// protected string GoodSetupAndReturns { get; } = +// """ +// using System; +// using System.Collections.Generic; +// using Moq; + +// namespace CallbackSignatureShouldMatchMockedMethod.MyGoodSetupAndReturns; + +// internal interface IFoo +// { +// int Do(string s); + +// int Do(int i, string s, DateTime dt); + +// int Do(List l); +// } + +// internal class MyUnitTests +// { +// private void MyGoodSetupAndReturns() +// { +// var mock = new Mock(); +// mock.Setup(x => x.Do(It.IsAny())).Returns((string s) => { return 0; }); +// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns((int i, string s, DateTime dt) => { return 0; }); +// mock.Setup(x => x.Do(It.IsAny>())).Returns((List l) => { return 0; }); +// } +// } +// """; + +// protected string BadCallbacks { get; } = +// """ +// using System; +// using System.Collections.Generic; +// using Moq; + +// namespace CallbackSignatureShouldMatchMockedMethod.TestBadCallbacks; + +// internal interface IFoo +// { +// int Do(string s); + +// int Do(int i, string s, DateTime dt); + +// int Do(List l); +// } + +// internal class MyUnitTests +// { +// private void TestBadCallbacks() +// { +// var mock = new Mock(); +// mock.Setup(x => x.Do(It.IsAny())).Callback((int i) => { }); +// mock.Setup(x => x.Do(It.IsAny())).Callback((string s1, string s2) => { }); +// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((string s1, int i1) => { }); +// mock.Setup(x => x.Do(It.IsAny>())).Callback((int i) => { }); +// } +// } +// """; + +// protected string GoodSetupAndCallback { get; } = +// """ +// using System; +// using System.Collections.Generic; +// using Moq; + +// namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndCallback; + +// internal interface IFoo +// { +// int Do(string s); + +// int Do(int i, string s, DateTime dt); + +// int Do(List l); +// } + +// internal class MyUnitTests +// { +// private void TestGoodSetupAndCallback() +// { +// var mock = new Mock(); +// mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); +// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((int i, string s, DateTime dt) => { }); +// mock.Setup(x => x.Do(It.IsAny>())).Callback((List l) => { }); +// } +// } +// """; + +// protected string GoodSetupAndParameterlessCallback { get; } = +// """ +// using System; +// using System.Collections.Generic; +// using Moq; + +// namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndParameterlessCallback; + +// internal interface IFoo +// { +// int Do(string s); + +// int Do(int i, string s, DateTime dt); + +// int Do(List l); +// } + +// internal class MyUnitTests +// { +// private void TestGoodSetupAndParameterlessCallback() +// { +// var mock = new Mock(); +// mock.Setup(x => x.Do(It.IsAny())).Callback(() => { }); +// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback(() => { }); +// mock.Setup(x => x.Do(It.IsAny>())).Callback(() => { }); +// } +// } +// """; + +// protected string GoodSetupAndReturnsAndCallback { get; } = +// """ +// using System; +// using System.Collections.Generic; +// using Moq; + +// namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndReturnsAndCallback; + +// internal interface IFoo +// { +// int Do(string s); + +// int Do(int i, string s, DateTime dt); + +// int Do(List l); +// } + +// internal class MyUnitTests +// { +// private void TestGoodSetupAndReturnsAndCallback() +// { +// var mock = new Mock(); +// mock.Setup(x => x.Do(It.IsAny())).Returns(0).Callback((string s) => { }); +// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns(0).Callback((int i, string s, DateTime dt) => { }); +// mock.Setup(x => x.Do(It.IsAny>())).Returns(0).Callback((List l) => { }); +// } +// } +// """; + +// protected override CodeFixProvider GetCSharpCodeFixProvider() +// { +// return new CallbackSignatureShouldMatchMockedMethodCodeFix(); +// } + +// protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() +// { +// return new CallbackSignatureShouldMatchMockedMethodAnalyzer(); +// } +//} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs index 6e6253ab..3ca533c5 100644 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs +++ b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs @@ -2,40 +2,39 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; using Xunit; namespace Moq.Analyzers.Test; -public class CallbackSignatureShouldMatchMockedMethodCodeFixTests : CallbackSignatureShouldMatchMockedMethodBase -{ - // [Fact] - public Task ShouldPassWhenCorrectSetupAndReturns() - { - return Verify(VerifyCSharpFix(GoodSetupAndReturns)); - } +//public class CallbackSignatureShouldMatchMockedMethodCodeFixTests : CallbackSignatureShouldMatchMockedMethodBase +//{ +// // [Fact] +// public Task ShouldPassWhenCorrectSetupAndReturns() +// { +// return Verify(VerifyCSharpFix(GoodSetupAndReturns)); +// } - // [Fact] - public Task ShouldSuggestQuickFixWhenIncorrectCallbacks() - { - return Verify(VerifyCSharpFix(BadCallbacks)); - } +// // [Fact] +// public Task ShouldSuggestQuickFixWhenIncorrectCallbacks() +// { +// return Verify(VerifyCSharpFix(BadCallbacks)); +// } - // [Fact] - public Task ShouldPassWhenCorrectSetupAndCallbacks() - { - return Verify(VerifyCSharpFix(GoodSetupAndCallback)); - } +// // [Fact] +// public Task ShouldPassWhenCorrectSetupAndCallbacks() +// { +// return Verify(VerifyCSharpFix(GoodSetupAndCallback)); +// } - // [Fact] - public Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() - { - return Verify(VerifyCSharpFix(GoodSetupAndParameterlessCallback)); - } +// // [Fact] +// public Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() +// { +// return Verify(VerifyCSharpFix(GoodSetupAndParameterlessCallback)); +// } - // [Fact] - public Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() - { - return Verify(VerifyCSharpFix(GoodSetupAndReturnsAndCallback)); - } -} +// // [Fact] +// public Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() +// { +// return Verify(VerifyCSharpFix(GoodSetupAndReturnsAndCallback)); +// } +//} diff --git a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs index 6d34dcf2..cd5ea39b 100644 --- a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs @@ -1,18 +1,17 @@ using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -public class ConstructorArgumentsShouldMatchAnalyzerTests : DiagnosticVerifier +public class ConstructorArgumentsShouldMatchAnalyzerTests : DiagnosticVerifier { // [Fact] public Task ShouldPassWhenConstructorArgumentsMatch() { return Verify(VerifyCSharpDiagnostic( - [ """ using System; using System.Collections.Generic; @@ -58,14 +57,13 @@ private void TestGood() } } """ - ])); + )); } // [Fact] public Task ShouldFailWhenConstructorArumentsDoNotMatch() { return Verify(VerifyCSharpDiagnostic( - [ """ using System; using System.Collections.Generic; @@ -95,14 +93,13 @@ private void TestBad() } } """ - ])); + )); } // [Fact] public Task ShouldFailWhenConstructorArumentsWithExplicitMockBehaviorDoNotMatch() { return Verify(VerifyCSharpDiagnostic( - [ """ using System; using System.Collections.Generic; @@ -131,11 +128,6 @@ private void TestBadWithMockBehavior() } } """ - ])); - } - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new ConstructorArgumentsShouldMatchAnalyzer(); + )); } } diff --git a/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.Helper.cs b/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.Helper.cs deleted file mode 100644 index a6b06168..00000000 --- a/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.Helper.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeActions; -using Microsoft.CodeAnalysis.Formatting; -using Microsoft.CodeAnalysis.Simplification; - -namespace TestHelper; - -/// -/// Diagnostic Producer class with extra methods dealing with applying codefixes -/// All methods are static -/// -public abstract partial class CodeFixVerifier : DiagnosticVerifier -{ - /// - /// Apply the inputted CodeAction to the inputted document. - /// Meant to be used to apply codefixes. - /// - /// The Document to apply the fix on - /// A CodeAction that will be applied to the Document. - /// A Document with the changes from the CodeAction - private static Document ApplyFix(Document document, CodeAction codeAction) - { - var operations = codeAction.GetOperationsAsync(CancellationToken.None).Result; - var solution = operations.OfType().Single().ChangedSolution; - return solution.GetDocument(document.Id); - } - - /// - /// Compare two collections of Diagnostics,and return a list of any new diagnostics that appear only in the second collection. - /// Note: Considers Diagnostics to be the same if they have the same Ids. In the case of multiple diagnostics with the same Id in a row, - /// this method may not necessarily return the new one. - /// - /// The Diagnostics that existed in the code before the CodeFix was applied - /// The Diagnostics that exist in the code after the CodeFix was applied - /// A list of Diagnostics that only surfaced in the code after the CodeFix was applied - private static IEnumerable GetNewDiagnostics(IEnumerable diagnostics, IEnumerable newDiagnostics) - { - var oldArray = diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); - var newArray = newDiagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); - - int oldIndex = 0; - int newIndex = 0; - - while (newIndex < newArray.Length) - { - if (oldIndex < oldArray.Length && oldArray[oldIndex].Id == newArray[newIndex].Id) - { - ++oldIndex; - ++newIndex; - } - else - { - yield return newArray[newIndex++]; - } - } - } - - /// - /// Get the existing compiler diagnostics on the inputted document. - /// - /// The Document to run the compiler diagnostic analyzers on - /// The compiler diagnostics that were found in the code - private static IEnumerable GetCompilerDiagnostics(Document document) - { - return document.GetSemanticModelAsync().Result.GetDiagnostics(); - } - - /// - /// Given a document, turn it into a string based on the syntax root - /// - /// The Document to be converted to a string - /// A string containing the syntax of the Document after formatting - private static string GetStringFromDocument(Document document) - { - var simplifiedDoc = Simplifier.ReduceAsync(document, Simplifier.Annotation).Result; - var root = simplifiedDoc.GetSyntaxRootAsync().Result; - root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace); - return root.GetText().ToString(); - } -} diff --git a/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs b/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs deleted file mode 100644 index 8f87121f..00000000 --- a/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeActions; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Formatting; -using Xunit; - -namespace TestHelper; - -/// -/// Superclass of all Unit tests made for diagnostics with codefixes. -/// Contains methods used to verify correctness of codefixes -/// -public abstract partial class CodeFixVerifier : DiagnosticVerifier -{ - /// - /// Returns the codefix being tested (C#) - to be implemented in non-abstract class - /// - /// The CodeFixProvider to be used for CSharp code - protected virtual CodeFixProvider GetCSharpCodeFixProvider() - { - return null; - } - - /// - /// Called to test a C# codefix when applied on the inputted string as a source - /// - /// A class in the form of a string before the CodeFix was applied to it - /// Index determining which codefix to apply if there are multiple - /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied - /// Code with quick fix applied - protected string VerifyCSharpFix(string oldSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false) - { - return VerifyFix(LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), GetCSharpCodeFixProvider(), oldSource, codeFixIndex, allowNewCompilerDiagnostics); - } - - /// - /// General verifier for codefixes. - /// Creates a Document from the source string, then gets diagnostics on it and applies the relevant codefixes. - /// Then gets the string after the codefix is applied and compares it with the expected result. - /// Note: If any codefix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true. - /// - /// The language the source code is in - /// The analyzer to be applied to the source code - /// The codefix to be applied to the code wherever the relevant Diagnostic is found - /// A class in the form of a string before the CodeFix was applied to it - /// Index determining which codefix to apply if there are multiple - /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied - /// Code with quick fix applied - private string VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, int? codeFixIndex, bool allowNewCompilerDiagnostics) - { - var document = CreateDocument(oldSource, language); - var analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }); - var compilerDiagnostics = GetCompilerDiagnostics(document); - var attempts = analyzerDiagnostics.Length; - - for (int i = 0; i < attempts; ++i) - { - var actions = new List(); - var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None); - codeFixProvider.RegisterCodeFixesAsync(context).Wait(context.CancellationToken); - - if (!actions.Any()) - { - break; - } - - if (codeFixIndex != null) - { - document = ApplyFix(document, actions.ElementAt((int)codeFixIndex)); - break; - } - - document = ApplyFix(document, actions.ElementAt(0)); - analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }); - - var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document)); - - // check if applying the code fix introduced any new compiler diagnostics - if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any()) - { - // Format and get the compiler diagnostics again so that the locations make sense in the output - SyntaxNode root = Formatter.Format( - document.GetSyntaxRootAsync(context.CancellationToken).Result, - Formatter.Annotation, - document.Project.Solution.Workspace, - cancellationToken: context.CancellationToken); - document = document.WithSyntaxRoot(root); - newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document)); - - Assert.True( - false, - string.Format( - "Fix introduced new compiler diagnostics:\r\n{0}\r\n\r\nNew document:\r\n{1}\r\n", - string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())), - document.GetSyntaxRootAsync(context.CancellationToken).Result.ToFullString())); - } - - // check if there are analyzer diagnostics left after the code fix - if (!analyzerDiagnostics.Any()) - { - break; - } - } - - // after applying all of the code fixes, compare the resulting string to the inputted one - return GetStringFromDocument(document); - } -} diff --git a/Source/Moq.Analyzers.Test/Helpers/DiagnosticResult.cs b/Source/Moq.Analyzers.Test/Helpers/DiagnosticResult.cs deleted file mode 100644 index 1d47fbd3..00000000 --- a/Source/Moq.Analyzers.Test/Helpers/DiagnosticResult.cs +++ /dev/null @@ -1,54 +0,0 @@ -using Microsoft.CodeAnalysis; - -namespace TestHelper; - -/// -/// Struct that stores information about a Diagnostic appearing in a source -/// -public struct DiagnosticResult -{ - private DiagnosticResultLocation[] locations; - - public DiagnosticResultLocation[] Locations - { - get - { - return locations ?? (this.locations = new DiagnosticResultLocation[] { }); - } - - set - { - locations = value; - } - } - - public DiagnosticSeverity Severity { get; set; } - - public string Id { get; set; } - - public string Message { get; set; } - - public string Path - { - get - { - return this.Locations.Length > 0 ? this.Locations[0].Path : string.Empty; - } - } - - public int Line - { - get - { - return this.Locations.Length > 0 ? this.Locations[0].Line : -1; - } - } - - public int Column - { - get - { - return this.Locations.Length > 0 ? this.Locations[0].Column : -1; - } - } -} diff --git a/Source/Moq.Analyzers.Test/Helpers/DiagnosticResultLocation.cs b/Source/Moq.Analyzers.Test/Helpers/DiagnosticResultLocation.cs deleted file mode 100644 index 114061d7..00000000 --- a/Source/Moq.Analyzers.Test/Helpers/DiagnosticResultLocation.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; - -namespace TestHelper; - -/// -/// Location where the diagnostic appears, as determined by path, line number, and column number. -/// -public struct DiagnosticResultLocation -{ - public DiagnosticResultLocation(string path, int line, int column) - { - if (line < -1) - { - throw new ArgumentOutOfRangeException(nameof(line), "line must be >= -1"); - } - - if (column < -1) - { - throw new ArgumentOutOfRangeException(nameof(column), "column must be >= -1"); - } - - this.Path = path; - this.Line = line; - this.Column = column; - } - - public string Path { get; } - - public int Line { get; } - - public int Column { get; } -} diff --git a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.Helper.cs b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.Helper.cs deleted file mode 100644 index eedb9e92..00000000 --- a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.Helper.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Diagnostics; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Text; -using Moq; - -namespace TestHelper; - -/// -/// Class for turning strings into documents and getting the diagnostics on them -/// All methods are static. -/// -public abstract partial class DiagnosticVerifier -{ - private const string DefaultFilePathPrefix = "Test"; - private const string CSharpDefaultFileExt = "cs"; - private const string VisualBasicDefaultExt = "vb"; - private const string TestProjectName = "TestProject"; - - private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); - private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location); - private static readonly MetadataReference SystemLinqExpressions = MetadataReference.CreateFromFile(typeof(Expression).Assembly.Location); - private static readonly MetadataReference GenericCollectionsReference = MetadataReference.CreateFromFile(typeof(IEnumerable<>).Assembly.Location); - private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location); - private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location); - private static readonly MetadataReference MoqReference = MetadataReference.CreateFromFile(typeof(Mock).Assembly.Location); - private static readonly MetadataReference NetStandardReference = MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("netstandard")).Location); - private static readonly MetadataReference SystemRuntimeReference = MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location); - - /// - /// Given an analyzer and a document to apply it to, run the analyzer and gather an array of diagnostics found in it. - /// The returned diagnostics are then ordered by location in the source document. - /// - /// The analyzer to run on the documents. - /// The Documents that the analyzer will be run on. - /// An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location. - protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents) - { - Debug.Assert(documents != null, nameof(documents) + " != null"); - - var projects = new HashSet(); - foreach (var document in documents) - { - projects.Add(document.Project); - } - - var diagnostics = new List(); - foreach (var project in projects) - { - Debug.Assert(analyzer != null, nameof(analyzer) + " != null"); - var compilationWithAnalyzers = project.GetCompilationAsync().Result.WithAnalyzers(ImmutableArray.Create(analyzer)); - var diags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(compilationWithAnalyzers.CancellationToken).Result; - foreach (var diag in diags) - { - if (diag.Location == Location.None || diag.Location.IsInMetadata) - { - diagnostics.Add(diag); - } - else - { - for (int i = 0; i < documents.Length; i++) - { - var document = documents[i]; - var tree = document.GetSyntaxTreeAsync(compilationWithAnalyzers.CancellationToken).Result; - if (tree == diag.Location.SourceTree) - { - diagnostics.Add(diag); - } - } - } - } - } - - var results = SortDiagnostics(diagnostics); - diagnostics.Clear(); - return results; - } - - /// - /// Create a Document from a string through creating a project that contains it. - /// - /// Classes in the form of a string. - /// The language the source code is in. - /// A Document created from the source string. - protected static Document CreateDocument(string source, string language = LanguageNames.CSharp) - { - return CreateProject(new[] { source }, language).Documents.First(); - } - - /// - /// Given classes in the form of strings, their language, and an IDiagnosticAnlayzer to apply to it, return the diagnostics found in the string after converting it to a document. - /// - /// Classes in the form of strings. - /// The language the source classes are in. - /// The analyzer to be run on the sources. - /// An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location. - private static Diagnostic[] GetSortedDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer) - { - return GetSortedDiagnosticsFromDocuments(analyzer, GetDocuments(sources, language)); - } - - /// - /// Sort diagnostics by location in source document. - /// - /// The list of Diagnostics to be sorted. - /// An IEnumerable containing the Diagnostics in order of Location. - private static Diagnostic[] SortDiagnostics(IEnumerable diagnostics) - { - return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); - } - - /// - /// Given an array of strings as sources and a language, turn them into a project and return the documents and spans of it. - /// - /// Classes in the form of strings. - /// The language the source code is in. - /// A Tuple containing the Documents produced from the sources and their TextSpans if relevant. - private static Document[] GetDocuments(string[] sources, string language) - { - if (language != LanguageNames.CSharp && language != LanguageNames.VisualBasic) - { - throw new ArgumentException("Unsupported Language"); - } - - var project = CreateProject(sources, language); - var documents = project.Documents.ToArray(); - - if (sources.Length != documents.Length) - { - throw new SystemException("Amount of sources did not match amount of Documents created"); - } - - return documents; - } - - /// - /// Create a project using the inputted strings as sources. - /// - /// Classes in the form of strings. - /// The language the source code is in. - /// A Project created out of the Documents created from the source strings. - private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp) - { - const string fileNamePrefix = DefaultFilePathPrefix; - string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; - - var projectId = ProjectId.CreateNewId(debugName: TestProjectName); - - var solution = new AdhocWorkspace() - .CurrentSolution - .AddProject(projectId, TestProjectName, TestProjectName, language) - .AddMetadataReference(projectId, CorlibReference) - .AddMetadataReference(projectId, SystemCoreReference) - .AddMetadataReference(projectId, CodeAnalysisReference) - .AddMetadataReference(projectId, SystemLinqExpressions) - .AddMetadataReference(projectId, CSharpSymbolsReference) - .AddMetadataReference(projectId, GenericCollectionsReference) - .AddMetadataReference(projectId, SystemRuntimeReference) - .AddMetadataReference(projectId, NetStandardReference) - .AddMetadataReference(projectId, MoqReference); - - int count = 0; - foreach (var source in sources) - { - var newFileName = fileNamePrefix + count + "." + fileExt; - var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); - solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); - count++; - } - - return solution.GetProject(projectId); - } -} diff --git a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs index 7e52849f..5ce26a88 100644 --- a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs +++ b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs @@ -1,88 +1,28 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; -namespace TestHelper; +namespace Moq.Analyzers.Test.Helpers; -/// -/// Superclass of all Unit Tests for DiagnosticAnalyzers -/// -public abstract partial class DiagnosticVerifier +public abstract class DiagnosticVerifier + where TAnalyzer : DiagnosticAnalyzer, new() { - /// - /// Get the CSharp analyzer being tested - to be implemented in non-abstract class - /// - /// Diagnostics to be used in test - protected virtual DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() + protected async Task VerifyCSharpDiagnostic(string source) { - return null; - } - - /// - /// Called to test a C# DiagnosticAnalyzer when applied on the single inputted string as a source - /// Note: input a DiagnosticResult for each Diagnostic expected - /// - /// A class in the form of a string to run the analyzer on - /// String representation of diagnostics results - protected string VerifyCSharpDiagnostic(string source) - { - return VerifyDiagnostics(new[] { source }, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer()); - } - - /// - /// Called to test a C# DiagnosticAnalyzer when applied on the inputted strings as a source - /// Note: input a DiagnosticResult for each Diagnostic expected - /// - /// An array of strings to create source documents from to run the analyzers on - /// String representation of diagnostics results - protected string VerifyCSharpDiagnostic(string[] sources) - { - return VerifyDiagnostics(sources, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer()); - } - - /// - /// General method that gets a collection of actual diagnostics found in the source after the analyzer is run, - /// then verifies each of them. - /// - /// An array of strings to create source documents from to run the analyzers on - /// The language of the classes represented by the source strings - /// The analyzer to be run on the sources - /// String representation of diagnostics results - private string VerifyDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer) - { - var diagnostics = GetSortedDiagnostics(sources, language, analyzer); - return VerifyDiagnosticResults(diagnostics); - } - - /// - /// Checks each of the actual Diagnostics found and compares them with the corresponding DiagnosticResult in the array of expected results. - /// Diagnostics are considered equal only if the DiagnosticResultLocation, Id, Severity, and Message of the DiagnosticResult match the actual diagnostic. - /// - /// The Diagnostics found by the compiler after running the analyzer on the source code - /// String representation of diagnostics results - private string VerifyDiagnosticResults(IEnumerable actualResults) - { - StringBuilder result = new StringBuilder(); - int i = 1; - foreach (var diagnostic in actualResults) + CSharpAnalyzerTest context = new() { - result.AppendLine("Diagnostic " + i); - result.AppendLine("\tId: " + diagnostic.Id); - result.AppendLine("\tLocation: " + diagnostic.Location); - var sourceSpan = diagnostic.Location.SourceSpan; - var code = diagnostic.Location.SourceTree.GetText(); - result.AppendLine("\tHighlight: " + code.GetSubText(sourceSpan)); - var lineSpan = diagnostic.Location.GetLineSpan(); - result.AppendLine("\tLines: " + string.Join("\n", code.Lines.Where(x => x.LineNumber >= lineSpan.StartLinePosition.Line && x.LineNumber <= lineSpan.EndLinePosition.Line).Select(x => x.ToString().Trim()))); - result.AppendLine("\tSeverity: " + diagnostic.Severity); - result.AppendLine("\tMessage: " + diagnostic.GetMessage()); - result.AppendLine(); + ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Moq", "4.18.4")]), + TestCode = source, + TestBehaviors = TestBehaviors.SkipGeneratedCodeCheck, // TODO: We should enable the generated code check + }; - i += 1; - } + context.SolutionTransforms.Add((solution, projectId) => + { + return solution.WithProjectParseOptions(projectId, new CSharpParseOptions( + languageVersion: LanguageVersion.Latest)); + }); - return result.ToString(); + await context.RunAsync().ConfigureAwait(false); } } diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs index c26c61bf..3cc01900 100644 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs @@ -1,18 +1,17 @@ using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -public class NoConstructorArgumentsForInterfaceMockAnalyzerTests : DiagnosticVerifier +public class NoConstructorArgumentsForInterfaceMockAnalyzerTests : DiagnosticVerifier { // [Fact] public Task ShouldFailIfMockedInterfaceHasConstructorParameters() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -34,14 +33,13 @@ private void TestBad() } } """ - ])); + )); } // [Fact] public Task ShouldFailIfMockedInterfaceHasConstructorParametersAndExplicitMockBehavior() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -63,14 +61,13 @@ private void TestBadWithMockBehavior() } } """ - ])); + )); } // [Fact] public Task ShouldPassIfMockedInterfaceDoesNotHaveConstructorParameters() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -92,7 +89,7 @@ private void TestGood() } } """ - ])); + )); } // TODO: This feels like it should be in every analyzer's tests @@ -100,7 +97,6 @@ private void TestGood() public Task ShouldPassIfCustomMockClassIsUsed() { return Verify(VerifyCSharpDiagnostic( - [ """ namespace NoConstructorArgumentsForInterfaceMock.TestFakeMoq; @@ -141,7 +137,7 @@ private void TestFakeMoq() } } """ - ])); + )); } // TODO: This feels duplicated with other tests @@ -149,7 +145,6 @@ private void TestFakeMoq() public Task ShouldFailIsRealMoqIsUsedWithInvalidParameters() { return Verify(VerifyCSharpDiagnostic( - [ """ namespace NoConstructorArgumentsForInterfaceMock.TestRealMoqWithBadParameters; @@ -190,14 +185,13 @@ private void TestRealMoqWithBadParameters() } } """ - ])); + )); } // [Fact] public Task ShouldPassIfRealMoqIsUsedWithValidParameters() { return Verify(VerifyCSharpDiagnostic( - [ """ namespace NoConstructorArgumentsForInterfaceMock.TestRealMoqWithGoodParameters; @@ -234,11 +228,6 @@ private void TestRealMoqWithGoodParameters() } } """ - ])); - } - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new NoConstructorArgumentsForInterfaceMockAnalyzer(); + )); } } diff --git a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs index b7c8e173..474c79de 100644 --- a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs @@ -1,18 +1,17 @@ using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -public class NoMethodsInPropertySetupAnalyzerTests : DiagnosticVerifier +public class NoMethodsInPropertySetupAnalyzerTests : DiagnosticVerifier { // [Fact] public Task ShouldPassWhenPropertiesUsePropertySetup() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -42,14 +41,13 @@ private void TestGood() } } """ - ])); + )); } // [Fact] public Task ShouldFailWhenMethodsUsePropertySetup() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -76,12 +74,6 @@ private void TestBad() } } """ - ])); - } - - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new NoMethodsInPropertySetupAnalyzer(); + )); } } diff --git a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs index 2cadac0a..3e3f17c2 100644 --- a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs @@ -1,18 +1,17 @@ using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -public class NoSealedClassMocksAnalyzerTests : DiagnosticVerifier +public class NoSealedClassMocksAnalyzerTests : DiagnosticVerifier { // [Fact] public Task ShouldFailWhenClassIsSealed() { return Verify(VerifyCSharpDiagnostic( - [ """ using System; using Moq; @@ -31,14 +30,13 @@ private void Sealed() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenClassIsNotSealed() { return Verify(VerifyCSharpDiagnostic( - [ """ using System; using Moq; @@ -57,11 +55,6 @@ private void NotSealed() } } """ - ])); - } - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new NoSealedClassMocksAnalyzer(); + )); } } diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs index 3c8391d8..0e9ba1b7 100644 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs @@ -1,18 +1,17 @@ using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -public class SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests : DiagnosticVerifier +public class SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests : DiagnosticVerifier { // [Fact] public Task ShouldFailWhenSetupIsCalledWithANonVirtualMethod() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -36,14 +35,13 @@ private void TestBadSetupForNonVirtualMethod() } } """ - ])); + )); } // [Fact] public Task ShouldFailWhenSetupIsCalledWithANonVirtualProperty() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -64,14 +62,13 @@ private void TestBadSetupForNonVirtualProperty() } } """ - ])); + )); } // [Fact] public Task ShouldFailWhenSetupIsCalledWithASealedMethod() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -103,14 +100,13 @@ private void TestBadSetupForSealedMethod() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenSetupIsCalledWithAnAbstractMethod() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -134,14 +130,13 @@ private void TestOkForAbstractMethod() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenSetupIsCalledWithAnInterfaceMethod() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -161,14 +156,13 @@ private void TestOkForInterfaceMethod() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenSetupIsCalledWithAnInterfaceProperty() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -188,14 +182,13 @@ private void TestOkForInterfaceProperty() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenSetupIsCalledWithAnOverrideOfAnAbstractMethod() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -227,14 +220,13 @@ private void TestOkForOverrideAbstractMethod() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenSetupIsCalledWithAVirtualMethod() { return Verify(VerifyCSharpDiagnostic( - [ """ using Moq; @@ -254,11 +246,6 @@ private void TestOkForVirtualMethod() } } """ - ])); - } - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new SetupShouldBeUsedOnlyForOverridableMembersAnalyzer(); + )); } } diff --git a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs index ea21d478..8a00f2ac 100644 --- a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs @@ -1,18 +1,17 @@ using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; -using TestHelper; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -public class SetupShouldNotIncludeAsyncResultAnalyzerTests : DiagnosticVerifier +public class SetupShouldNotIncludeAsyncResultAnalyzerTests : DiagnosticVerifier { // [Fact] public Task ShouldPassWhenSetupWithoutReturn() { return Verify(VerifyCSharpDiagnostic( - [ """ using System.Threading.Tasks; using Moq; @@ -35,14 +34,13 @@ private void TestOkForTask() } } """ - ])); + )); } // [Fact] public Task ShouldPassWhenSetupWithReturnsAsync() { return Verify(VerifyCSharpDiagnostic( - [ """ using System.Threading.Tasks; using Moq; @@ -66,14 +64,13 @@ private void TestOkForGenericTaskProperSetup() } } """ - ])); + )); } // [Fact] public Task ShouldFailWhenSetupWithTaskResult() { return Verify(VerifyCSharpDiagnostic( - [ """ using System.Threading.Tasks; using Moq; @@ -96,11 +93,6 @@ private void TestBadForGenericTask() } } """ - ])); - } - - protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() - { - return new SetupShouldNotIncludeAsyncResultAnalyzer(); + )); } } From 85d89f557ad1f64b8e2ff8c973c46a5afc5a10d9 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 10:17:46 -0700 Subject: [PATCH 05/16] Convert AbstractClassTests --- ...nGenericTypesWithMismatchArgs.verified.txt | 24 --------- ...ests.ShouldFailOnMismatchArgs.verified.txt | 24 --------- ...dPassOnGenericTypesWithNoArgs.verified.txt | 1 - ...ests.ShouldPassWithArgsPassed.verified.txt | 1 - ...assTests.ShouldPassWithNoArgs.verified.txt | 1 - .../Moq.Analyzers.Test/AbstractClassTests.cs | 53 ++++++++----------- 6 files changed, 23 insertions(+), 81 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/AbstractClassTests.ShouldFailOnGenericTypesWithMismatchArgs.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/AbstractClassTests.ShouldFailOnMismatchArgs.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassOnGenericTypesWithNoArgs.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassWithArgsPassed.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassWithNoArgs.verified.txt diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldFailOnGenericTypesWithMismatchArgs.verified.txt b/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldFailOnGenericTypesWithMismatchArgs.verified.txt deleted file mode 100644 index 9e7027ce..00000000 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldFailOnGenericTypesWithMismatchArgs.verified.txt +++ /dev/null @@ -1,24 +0,0 @@ -Diagnostic 1 - Id: Moq1002 - Location: SourceFile(Test0.cs[608..614)) - Highlight: ("42") - Lines: var mock = new Mock>("42"); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 2 - Id: Moq1002 - Location: SourceFile(Test0.cs[788..798)) - Highlight: ("42", 42) - Lines: var mock1 = new Mock>("42", 42); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 3 - Id: Moq1002 - Location: SourceFile(Test0.cs[934..938)) - Highlight: (42) - Lines: var mock2 = new Mock>(42); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldFailOnMismatchArgs.verified.txt b/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldFailOnMismatchArgs.verified.txt deleted file mode 100644 index e5cd7e3f..00000000 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldFailOnMismatchArgs.verified.txt +++ /dev/null @@ -1,24 +0,0 @@ -Diagnostic 1 - Id: Moq1002 - Location: SourceFile(Test0.cs[526..532)) - Highlight: ("42") - Lines: var mock = new Mock("42"); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 2 - Id: Moq1002 - Location: SourceFile(Test0.cs[691..701)) - Highlight: ("42", 42) - Lines: var mock1 = new Mock("42", 42); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 3 - Id: Moq1002 - Location: SourceFile(Test0.cs[822..826)) - Highlight: (42) - Lines: var mock2 = new Mock(42); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassOnGenericTypesWithNoArgs.verified.txt b/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassOnGenericTypesWithNoArgs.verified.txt deleted file mode 100644 index c1b8d743..00000000 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassOnGenericTypesWithNoArgs.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString \ No newline at end of file diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassWithArgsPassed.verified.txt b/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassWithArgsPassed.verified.txt deleted file mode 100644 index c1b8d743..00000000 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassWithArgsPassed.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString \ No newline at end of file diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassWithNoArgs.verified.txt b/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassWithNoArgs.verified.txt deleted file mode 100644 index c1b8d743..00000000 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassWithNoArgs.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString \ No newline at end of file diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.cs b/Source/Moq.Analyzers.Test/AbstractClassTests.cs index 09fb416e..226f11cc 100644 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.cs +++ b/Source/Moq.Analyzers.Test/AbstractClassTests.cs @@ -50,10 +50,10 @@ private void TestBadWithGeneric() """); } - // [Fact] - public Task ShouldPassOnGenericTypesWithNoArgs() + [Fact] + public async Task ShouldPassOnGenericTypesWithNoArgs() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ namespace Moq.Analyzers.Test.Data.AbstractClass.GenericNoArgs; @@ -76,14 +76,13 @@ private void TestForBaseGenericNoArgs() var mock2 = new Mock>(MockBehavior.Default); } } - """ - )); + """); } - // [Fact] - public Task ShouldFailOnMismatchArgs() + [Fact] + public async Task ShouldFailOnMismatchArgs() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ namespace Moq.Analyzers.Test.Data.AbstractClass.MismatchArgs; @@ -110,23 +109,22 @@ internal class MyUnitTests private void TestBad() { // The class has a ctor that takes an Int32 but passes a String - var mock = new Mock("42"); + var mock = new Mock{|Moq1002:("42")|}; // The class has a ctor with two arguments [Int32, String], but they are passed in reverse order - var mock1 = new Mock("42", 42); + var mock1 = new Mock{|Moq1002:("42", 42)|}; // The class has a ctor but does not take any arguments - var mock2 = new Mock(42); + var mock2 = new Mock{|Moq1002:(42)|}; } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWithNoArgs() + [Fact] + public async Task ShouldPassWithNoArgs() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ namespace Moq.Analyzers.Test.Data.AbstractClass.NoArgs; @@ -146,14 +144,13 @@ private void TestForBaseNoArgs() mock.As(); } } - """ - )); + """); } - // [Fact(Skip = "I think this _should_ fail, but currently passes. Tracked by #55.")] - public Task ShouldFailWithArgsNonePassed() + [Fact(Skip = "I think this _should_ fail, but currently passes. Tracked by #55.")] + public async Task ShouldFailWithArgsNonePassed() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ namespace Moq.Analyzers.Test.Data.AbstractClass.WithArgsNonePassed; @@ -175,18 +172,15 @@ private void TestForBaseWithArgsNonePassed() { var mock = new Mock(); mock.As(); - - var mock2 = new Mock(MockBehavior.Default); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWithArgsPassed() + [Fact] + public async Task ShouldPassWithArgsPassed() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ namespace Moq.Analyzers.Test.DataAbstractClass.WithArgsPassed; @@ -226,7 +220,6 @@ private void TestForBaseWithArgsPassed() var mock6 = new Mock>(MockBehavior.Default, 42); } } - """ - )); + """); } } From 1ecb1d1f9bc11cfb1f8bbf34113f215bbf1e0ff3 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 10:44:27 -0700 Subject: [PATCH 06/16] Convert AsAcceptOnlyInterfaceAnalyzerTests --- ...lWhenUsingAsWithAbstractClass.verified.txt | 8 ---- ...lWhenUsingAsWithConcreteClass.verified.txt | 8 ---- ...dPassWhenUsingAsWithInterface.verified.txt | 1 - ...UsingAsWithInterfaceWithSetup.verified.txt | 1 - .../AsAcceptOnlyInterfaceAnalyzerTests.cs | 40 +++++++++---------- .../Helpers/DiagnosticVerifier.cs | 2 +- 6 files changed, 19 insertions(+), 41 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldFailWhenUsingAsWithAbstractClass.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldFailWhenUsingAsWithConcreteClass.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldPassWhenUsingAsWithInterface.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldPassWhenUsingAsWithInterfaceWithSetup.verified.txt diff --git a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldFailWhenUsingAsWithAbstractClass.verified.txt b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldFailWhenUsingAsWithAbstractClass.verified.txt deleted file mode 100644 index 3207abdd..00000000 --- a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldFailWhenUsingAsWithAbstractClass.verified.txt +++ /dev/null @@ -1,8 +0,0 @@ -Diagnostic 1 - Id: Moq1300 - Location: SourceFile(Test0.cs[292..307)) - Highlight: BaseSampleClass - Lines: mock.As(); - Severity: Error - Message: Mock.As() should take interfaces only - diff --git a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldFailWhenUsingAsWithConcreteClass.verified.txt b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldFailWhenUsingAsWithConcreteClass.verified.txt deleted file mode 100644 index e54f2188..00000000 --- a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldFailWhenUsingAsWithConcreteClass.verified.txt +++ /dev/null @@ -1,8 +0,0 @@ -Diagnostic 1 - Id: Moq1300 - Location: SourceFile(Test0.cs[433..443)) - Highlight: OtherClass - Lines: mock.As(); - Severity: Error - Message: Mock.As() should take interfaces only - diff --git a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldPassWhenUsingAsWithInterface.verified.txt b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldPassWhenUsingAsWithInterface.verified.txt deleted file mode 100644 index c1b8d743..00000000 --- a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldPassWhenUsingAsWithInterface.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString \ No newline at end of file diff --git a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldPassWhenUsingAsWithInterfaceWithSetup.verified.txt b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldPassWhenUsingAsWithInterfaceWithSetup.verified.txt deleted file mode 100644 index c1b8d743..00000000 --- a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.ShouldPassWhenUsingAsWithInterfaceWithSetup.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString \ No newline at end of file diff --git a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs index 09b6d9da..069c125d 100644 --- a/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs @@ -8,10 +8,10 @@ namespace Moq.Analyzers.Test; public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier { - // [Fact] - public Task ShouldFailWhenUsingAsWithAbstractClass() + [Fact] + public async Task ShouldFailWhenUsingAsWithAbstractClass() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -27,17 +27,16 @@ internal class MyUnitTests private void TestBadAsForAbstractClass() { var mock = new Mock(); - mock.As(); + mock.As<{|Moq1300:BaseSampleClass|}>(); } } - """ - )); + """); } - // [Fact] - public Task ShouldFailWhenUsingAsWithConcreteClass() + [Fact] + public async Task ShouldFailWhenUsingAsWithConcreteClass() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -64,17 +63,16 @@ internal class MyUnitTests private void TestBadAsForNonAbstractClass() { var mock = new Mock(); - mock.As(); + mock.As<{|Moq1300:OtherClass|}>(); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenUsingAsWithInterface() + [Fact] + public async Task ShouldPassWhenUsingAsWithInterface() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -98,14 +96,13 @@ private void TestOkAsForInterface() mock.As(); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenUsingAsWithInterfaceWithSetup() + [Fact] + public async Task ShouldPassWhenUsingAsWithInterfaceWithSetup() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -131,7 +128,6 @@ private void TestOkAsForInterfaceWithConfiguration() .Returns(10); } } - """ - )); + """); } } diff --git a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs index 5ce26a88..fb3811b3 100644 --- a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs +++ b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs @@ -12,7 +12,7 @@ protected async Task VerifyCSharpDiagnostic(string source) { CSharpAnalyzerTest context = new() { - ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Moq", "4.18.4")]), + ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Moq", "4.8.2")]), // TODO: See https://github.com/Litee/moq.analyzers/issues/58 TestCode = source, TestBehaviors = TestBehaviors.SkipGeneratedCodeCheck, // TODO: We should enable the generated code check }; From 26aae7a7efccaa8278f71d5b98d725c1b8ba7f11 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 11:02:40 -0700 Subject: [PATCH 07/16] Convert ConstructorArgumentsShouldMatch tests --- ...ConstructorArumentsDoNotMatch.verified.txt | 32 --------------- ...xplicitMockBehaviorDoNotMatch.verified.txt | 24 ----------- ...WhenConstructorArgumentsMatch.verified.txt | 1 - ...ructorArgumentsShouldMatchAnalyzerTests.cs | 41 +++++++++---------- 4 files changed, 19 insertions(+), 79 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailWhenConstructorArumentsDoNotMatch.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailWhenConstructorArumentsWithExplicitMockBehaviorDoNotMatch.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldPassWhenConstructorArgumentsMatch.verified.txt diff --git a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailWhenConstructorArumentsDoNotMatch.verified.txt b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailWhenConstructorArumentsDoNotMatch.verified.txt deleted file mode 100644 index 17eea554..00000000 --- a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailWhenConstructorArumentsDoNotMatch.verified.txt +++ /dev/null @@ -1,32 +0,0 @@ -Diagnostic 1 - Id: Moq1002 - Location: SourceFile(Test0.cs[392..401)) - Highlight: (1, true) - Lines: var mock1 = new Mock(1, true); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 2 - Id: Moq1002 - Location: SourceFile(Test0.cs[436..445)) - Highlight: (2, true) - Lines: var mock2 = new Mock(2, true); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 3 - Id: Moq1002 - Location: SourceFile(Test0.cs[480..488)) - Highlight: ("1", 3) - Lines: var mock3 = new Mock("1", 3); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 4 - Id: Moq1002 - Location: SourceFile(Test0.cs[523..546)) - Highlight: (new int[] { 1, 2, 3 }) - Lines: var mock4 = new Mock(new int[] { 1, 2, 3 }); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - diff --git a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailWhenConstructorArumentsWithExplicitMockBehaviorDoNotMatch.verified.txt b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailWhenConstructorArumentsWithExplicitMockBehaviorDoNotMatch.verified.txt deleted file mode 100644 index 1976d03e..00000000 --- a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailWhenConstructorArumentsWithExplicitMockBehaviorDoNotMatch.verified.txt +++ /dev/null @@ -1,24 +0,0 @@ -Diagnostic 1 - Id: Moq1002 - Location: SourceFile(Test0.cs[423..453)) - Highlight: (MockBehavior.Strict, 4, true) - Lines: var mock1 = new Mock(MockBehavior.Strict, 4, true); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 2 - Id: Moq1002 - Location: SourceFile(Test0.cs[488..517)) - Highlight: (MockBehavior.Loose, 5, true) - Lines: var mock2 = new Mock(MockBehavior.Loose, 5, true); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - -Diagnostic 3 - Id: Moq1002 - Location: SourceFile(Test0.cs[552..580)) - Highlight: (MockBehavior.Loose, "2", 6) - Lines: var mock3 = new Mock(MockBehavior.Loose, "2", 6); - Severity: Warning - Message: Parameters provided into mock do not match any existing constructors. - diff --git a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldPassWhenConstructorArgumentsMatch.verified.txt b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldPassWhenConstructorArgumentsMatch.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldPassWhenConstructorArgumentsMatch.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs index cd5ea39b..63bf3171 100644 --- a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs @@ -8,10 +8,10 @@ namespace Moq.Analyzers.Test; public class ConstructorArgumentsShouldMatchAnalyzerTests : DiagnosticVerifier { - // [Fact] - public Task ShouldPassWhenConstructorArgumentsMatch() + [Fact] + public async Task ShouldPassWhenConstructorArgumentsMatch() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using System; using System.Collections.Generic; @@ -56,14 +56,13 @@ private void TestGood() var mock16 = new Mock(MockBehavior.Default, new List()); } } - """ - )); + """); } - // [Fact] - public Task ShouldFailWhenConstructorArumentsDoNotMatch() + [Fact] + public async Task ShouldFailWhenConstructorArumentsDoNotMatch() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using System; using System.Collections.Generic; @@ -86,20 +85,19 @@ internal class MyUnitTests { private void TestBad() { - var mock1 = new Mock(1, true); - var mock2 = new Mock(2, true); - var mock3 = new Mock("1", 3); - var mock4 = new Mock(new int[] { 1, 2, 3 }); + var mock1 = new Mock{|Moq1002:(1, true)|}; + var mock2 = new Mock{|Moq1002:(2, true)|}; + var mock3 = new Mock{|Moq1002:("1", 3)|}; + var mock4 = new Mock{|Moq1002:(new int[] { 1, 2, 3 })|}; } } - """ - )); + """); } - // [Fact] - public Task ShouldFailWhenConstructorArumentsWithExplicitMockBehaviorDoNotMatch() + [Fact] + public async Task ShouldFailWhenConstructorArumentsWithExplicitMockBehaviorDoNotMatch() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using System; using System.Collections.Generic; @@ -122,12 +120,11 @@ internal class MyUnitTests { private void TestBadWithMockBehavior() { - var mock1 = new Mock(MockBehavior.Strict, 4, true); - var mock2 = new Mock(MockBehavior.Loose, 5, true); - var mock3 = new Mock(MockBehavior.Loose, "2", 6); + var mock1 = new Mock{|Moq1002:(MockBehavior.Strict, 4, true)|}; + var mock2 = new Mock{|Moq1002:(MockBehavior.Loose, 5, true)|}; + var mock3 = new Mock{|Moq1002:(MockBehavior.Loose, "2", 6)|}; } } - """ - )); + """); } } From 9278c76f4dada8db41524d83d9f6f4b1078e1edf Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 11:33:01 -0700 Subject: [PATCH 08/16] Convert NoConstructorArgumentsForInterface tests --- ...rfaceHasConstructorParameters.verified.txt | 32 -------- ...metersAndExplicitMockBehavior.verified.txt | 32 -------- ...oqIsUsedWithInvalidParameters.verified.txt | 48 ----------- ...ldPassIfCustomMockClassIsUsed.verified.txt | 1 - ...sNotHaveConstructorParameters.verified.txt | 1 - ...lMoqIsUsedWithValidParameters.verified.txt | 1 - ...rArgumentsForInterfaceMockAnalyzerTests.cs | 82 +++++++++---------- 7 files changed, 38 insertions(+), 159 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIfMockedInterfaceHasConstructorParameters.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIfMockedInterfaceHasConstructorParametersAndExplicitMockBehavior.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIsRealMoqIsUsedWithInvalidParameters.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfCustomMockClassIsUsed.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfMockedInterfaceDoesNotHaveConstructorParameters.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfRealMoqIsUsedWithValidParameters.verified.txt diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIfMockedInterfaceHasConstructorParameters.verified.txt b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIfMockedInterfaceHasConstructorParameters.verified.txt deleted file mode 100644 index 8822aefe..00000000 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIfMockedInterfaceHasConstructorParameters.verified.txt +++ /dev/null @@ -1,32 +0,0 @@ -Diagnostic 1 - Id: Moq1001 - Location: SourceFile(Test0.cs[231..241)) - Highlight: (25, true) - Lines: var mock1 = new Mock(25, true); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 2 - Id: Moq1001 - Location: SourceFile(Test0.cs[283..290)) - Highlight: ("123") - Lines: var mock2 = new Mock("123"); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 3 - Id: Moq1001 - Location: SourceFile(Test0.cs[332..342)) - Highlight: (25, true) - Lines: var mock3 = new Mock(25, true); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 4 - Id: Moq1001 - Location: SourceFile(Test0.cs[384..391)) - Highlight: ("123") - Lines: var mock4 = new Mock("123"); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIfMockedInterfaceHasConstructorParametersAndExplicitMockBehavior.verified.txt b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIfMockedInterfaceHasConstructorParametersAndExplicitMockBehavior.verified.txt deleted file mode 100644 index 3501f8d2..00000000 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIfMockedInterfaceHasConstructorParametersAndExplicitMockBehavior.verified.txt +++ /dev/null @@ -1,32 +0,0 @@ -Diagnostic 1 - Id: Moq1001 - Location: SourceFile(Test0.cs[263..292)) - Highlight: (MockBehavior.Default, "123") - Lines: var mock1 = new Mock(MockBehavior.Default, "123"); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 2 - Id: Moq1001 - Location: SourceFile(Test0.cs[334..365)) - Highlight: (MockBehavior.Strict, 25, true) - Lines: var mock2 = new Mock(MockBehavior.Strict, 25, true); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 3 - Id: Moq1001 - Location: SourceFile(Test0.cs[407..436)) - Highlight: (MockBehavior.Default, "123") - Lines: var mock3 = new Mock(MockBehavior.Default, "123"); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 4 - Id: Moq1001 - Location: SourceFile(Test0.cs[478..508)) - Highlight: (MockBehavior.Loose, 25, true) - Lines: var mock4 = new Mock(MockBehavior.Loose, 25, true); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIsRealMoqIsUsedWithInvalidParameters.verified.txt b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIsRealMoqIsUsedWithInvalidParameters.verified.txt deleted file mode 100644 index efa76335..00000000 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldFailIsRealMoqIsUsedWithInvalidParameters.verified.txt +++ /dev/null @@ -1,48 +0,0 @@ -Diagnostic 1 - Id: Moq1001 - Location: SourceFile(Test0.cs[550..559)) - Highlight: (1, true) - Lines: var mock1 = new Moq.Mock(1, true); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 2 - Id: Moq1001 - Location: SourceFile(Test0.cs[605..610)) - Highlight: ("2") - Lines: var mock2 = new Moq.Mock("2"); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 3 - Id: Moq1001 - Location: SourceFile(Test0.cs[656..687)) - Highlight: (Moq.MockBehavior.Default, "3") - Lines: var mock3 = new Moq.Mock(Moq.MockBehavior.Default, "3"); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 4 - Id: Moq1001 - Location: SourceFile(Test0.cs[733..762)) - Highlight: (MockBehavior.Loose, 4, true) - Lines: var mock4 = new Moq.Mock(MockBehavior.Loose, 4, true); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 5 - Id: Moq1001 - Location: SourceFile(Test0.cs[808..830)) - Highlight: (MockBehavior.Default) - Lines: var mock5 = new Moq.Mock(MockBehavior.Default); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - -Diagnostic 6 - Id: Moq1001 - Location: SourceFile(Test0.cs[876..898)) - Highlight: (MockBehavior.Default) - Lines: var mock6 = new Moq.Mock(MockBehavior.Default); - Severity: Warning - Message: Mocked interfaces cannot have constructor parameters. - diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfCustomMockClassIsUsed.verified.txt b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfCustomMockClassIsUsed.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfCustomMockClassIsUsed.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfMockedInterfaceDoesNotHaveConstructorParameters.verified.txt b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfMockedInterfaceDoesNotHaveConstructorParameters.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfMockedInterfaceDoesNotHaveConstructorParameters.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfRealMoqIsUsedWithValidParameters.verified.txt b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfRealMoqIsUsedWithValidParameters.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.ShouldPassIfRealMoqIsUsedWithValidParameters.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs index 3cc01900..fa84af60 100644 --- a/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoConstructorArgumentsForInterfaceMockAnalyzerTests.cs @@ -8,10 +8,10 @@ namespace Moq.Analyzers.Test; public class NoConstructorArgumentsForInterfaceMockAnalyzerTests : DiagnosticVerifier { - // [Fact] - public Task ShouldFailIfMockedInterfaceHasConstructorParameters() + [Fact] + public async Task ShouldFailIfMockedInterfaceHasConstructorParameters() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -26,20 +26,19 @@ internal class MyUnitTests { private void TestBad() { - var mock1 = new Mock(25, true); - var mock2 = new Mock("123"); - var mock3 = new Mock(25, true); - var mock4 = new Mock("123"); + var mock1 = new Mock{|Moq1001:(25, true)|}; + var mock2 = new Mock{|Moq1001:("123")|}; + var mock3 = new Mock{|Moq1001:(25, true)|}; + var mock4 = new Mock{|Moq1001:("123")|}; } } - """ - )); + """); } - // [Fact] - public Task ShouldFailIfMockedInterfaceHasConstructorParametersAndExplicitMockBehavior() + [Fact] + public async Task ShouldFailIfMockedInterfaceHasConstructorParametersAndExplicitMockBehavior() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -54,20 +53,19 @@ internal class MyUnitTests { private void TestBadWithMockBehavior() { - var mock1 = new Mock(MockBehavior.Default, "123"); - var mock2 = new Mock(MockBehavior.Strict, 25, true); - var mock3 = new Mock(MockBehavior.Default, "123"); - var mock4 = new Mock(MockBehavior.Loose, 25, true); + var mock1 = new Mock{|Moq1001:(MockBehavior.Default, "123")|}; + var mock2 = new Mock{|Moq1001:(MockBehavior.Strict, 25, true)|}; + var mock3 = new Mock{|Moq1001:(MockBehavior.Default, "123")|}; + var mock4 = new Mock{|Moq1001:(MockBehavior.Loose, 25, true)|}; } } - """ - )); + """); } - // [Fact] - public Task ShouldPassIfMockedInterfaceDoesNotHaveConstructorParameters() + [Fact] + public async Task ShouldPassIfMockedInterfaceDoesNotHaveConstructorParameters() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -88,15 +86,14 @@ private void TestGood() var mock4 = new Mock(MockBehavior.Loose); } } - """ - )); + """); } // TODO: This feels like it should be in every analyzer's tests - // [Fact] - public Task ShouldPassIfCustomMockClassIsUsed() + [Fact] + public async Task ShouldPassIfCustomMockClassIsUsed() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ namespace NoConstructorArgumentsForInterfaceMock.TestFakeMoq; @@ -136,15 +133,14 @@ private void TestFakeMoq() var mock6 = new Mock(MockBehavior.Loose); } } - """ - )); + """); } // TODO: This feels duplicated with other tests - // [Fact] - public Task ShouldFailIsRealMoqIsUsedWithInvalidParameters() + [Fact] + public async Task ShouldFailIsRealMoqIsUsedWithInvalidParameters() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ namespace NoConstructorArgumentsForInterfaceMock.TestRealMoqWithBadParameters; @@ -176,22 +172,21 @@ internal class MyUnitTests { private void TestRealMoqWithBadParameters() { - var mock1 = new Moq.Mock(1, true); - var mock2 = new Moq.Mock("2"); - var mock3 = new Moq.Mock(Moq.MockBehavior.Default, "3"); - var mock4 = new Moq.Mock(MockBehavior.Loose, 4, true); - var mock5 = new Moq.Mock(MockBehavior.Default); - var mock6 = new Moq.Mock(MockBehavior.Default); + var mock1 = new Moq.Mock{|Moq1001:(1, true)|}; + var mock2 = new Moq.Mock{|Moq1001:("2")|}; + var mock3 = new Moq.Mock{|Moq1001:(Moq.MockBehavior.Default, "3")|}; + var mock4 = new Moq.Mock{|Moq1001:(MockBehavior.Loose, 4, true)|}; + var mock5 = new Moq.Mock{|Moq1001:(MockBehavior.Default)|}; + var mock6 = new Moq.Mock{|Moq1001:(MockBehavior.Default)|}; } } - """ - )); + """); } - // [Fact] - public Task ShouldPassIfRealMoqIsUsedWithValidParameters() + [Fact] + public async Task ShouldPassIfRealMoqIsUsedWithValidParameters() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ namespace NoConstructorArgumentsForInterfaceMock.TestRealMoqWithGoodParameters; @@ -227,7 +222,6 @@ private void TestRealMoqWithGoodParameters() var mock2 = new Moq.Mock(Moq.MockBehavior.Default); } } - """ - )); + """); } } From 90144daf5a01c5b9c2052e01dfb20d5a3099a1c3 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 11:37:51 -0700 Subject: [PATCH 09/16] Convert NoMethodsInPropertySetupAnalyzer tests --- ...ilWhenMethodsUsePropertySetup.verified.txt | 16 -------------- ...henPropertiesUsePropertySetup.verified.txt | 1 - .../NoMethodsInPropertySetupAnalyzerTests.cs | 22 +++++++++---------- 3 files changed, 10 insertions(+), 29 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.ShouldFailWhenMethodsUsePropertySetup.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.ShouldPassWhenPropertiesUsePropertySetup.verified.txt diff --git a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.ShouldFailWhenMethodsUsePropertySetup.verified.txt b/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.ShouldFailWhenMethodsUsePropertySetup.verified.txt deleted file mode 100644 index 03718f92..00000000 --- a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.ShouldFailWhenMethodsUsePropertySetup.verified.txt +++ /dev/null @@ -1,16 +0,0 @@ -Diagnostic 1 - Id: Moq1101 - Location: SourceFile(Test0.cs[311..321)) - Highlight: x.Method() - Lines: mock.SetupGet(x => x.Method()); - Severity: Warning - Message: SetupGet/SetupSet should be used for properties, not for methods. - -Diagnostic 2 - Id: Moq1101 - Location: SourceFile(Test0.cs[351..361)) - Highlight: x.Method() - Lines: mock.SetupSet(x => x.Method()); - Severity: Warning - Message: SetupGet/SetupSet should be used for properties, not for methods. - diff --git a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.ShouldPassWhenPropertiesUsePropertySetup.verified.txt b/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.ShouldPassWhenPropertiesUsePropertySetup.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.ShouldPassWhenPropertiesUsePropertySetup.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs index 474c79de..c335aae7 100644 --- a/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoMethodsInPropertySetupAnalyzerTests.cs @@ -8,10 +8,10 @@ namespace Moq.Analyzers.Test; public class NoMethodsInPropertySetupAnalyzerTests : DiagnosticVerifier { - // [Fact] - public Task ShouldPassWhenPropertiesUsePropertySetup() + [Fact] + public async Task ShouldPassWhenPropertiesUsePropertySetup() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -40,14 +40,13 @@ private void TestGood() mock.Setup(x => x.Method()); } } - """ - )); + """); } - // [Fact] - public Task ShouldFailWhenMethodsUsePropertySetup() + [Fact] + public async Task ShouldFailWhenMethodsUsePropertySetup() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -69,11 +68,10 @@ public class MyUnitTests private void TestBad() { var mock = new Mock(); - mock.SetupGet(x => x.Method()); - mock.SetupSet(x => x.Method()); + mock.SetupGet(x => {|Moq1101:x.Method()|}); + mock.SetupSet(x => {|Moq1101:x.Method()|}); } } - """ - )); + """); } } From 3a90b918021c7d0204b67931d81248c8b2d95c19 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 11:41:44 -0700 Subject: [PATCH 10/16] Convert NoSealedClassMocksAnalyzer tests --- ...s.ShouldFailWhenClassIsSealed.verified.txt | 8 -------- ...houldPassWhenClassIsNotSealed.verified.txt | 1 - .../NoSealedClassMocksAnalyzerTests.cs | 20 +++++++++---------- 3 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.ShouldFailWhenClassIsSealed.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.ShouldPassWhenClassIsNotSealed.verified.txt diff --git a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.ShouldFailWhenClassIsSealed.verified.txt b/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.ShouldFailWhenClassIsSealed.verified.txt deleted file mode 100644 index 8712e81a..00000000 --- a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.ShouldFailWhenClassIsSealed.verified.txt +++ /dev/null @@ -1,8 +0,0 @@ -Diagnostic 1 - Id: Moq1000 - Location: SourceFile(Test0.cs[214..223)) - Highlight: FooSealed - Lines: var mock = new Mock(); - Severity: Warning - Message: Sealed classes cannot be mocked. - diff --git a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.ShouldPassWhenClassIsNotSealed.verified.txt b/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.ShouldPassWhenClassIsNotSealed.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.ShouldPassWhenClassIsNotSealed.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs b/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs index 3e3f17c2..632986d7 100644 --- a/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/NoSealedClassMocksAnalyzerTests.cs @@ -8,10 +8,10 @@ namespace Moq.Analyzers.Test; public class NoSealedClassMocksAnalyzerTests : DiagnosticVerifier { - // [Fact] - public Task ShouldFailWhenClassIsSealed() + [Fact] + public async Task ShouldFailWhenClassIsSealed() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using System; using Moq; @@ -26,17 +26,16 @@ internal class MyUnitTests { private void Sealed() { - var mock = new Mock(); + var mock = new Mock<{|Moq1000:FooSealed|}>(); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenClassIsNotSealed() + [Fact] + public async Task ShouldPassWhenClassIsNotSealed() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using System; using Moq; @@ -54,7 +53,6 @@ private void NotSealed() var mock = new Mock(); } } - """ - )); + """); } } From aebaab54f14f8fd97bd64a3297214ded55f228b9 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 11:49:08 -0700 Subject: [PATCH 11/16] Convert SetupShouldBeUsedOnlyForOverridableMembersAnalyzer tests --- ...IsCalledWithANonVirtualMethod.verified.txt | 8 -- ...CalledWithANonVirtualProperty.verified.txt | 8 -- ...etupIsCalledWithASealedMethod.verified.txt | 8 -- ...tupIsCalledWithAVirtualMethod.verified.txt | 1 - ...pIsCalledWithAnAbstractMethod.verified.txt | 1 - ...IsCalledWithAnInterfaceMethod.verified.txt | 1 - ...CalledWithAnInterfaceProperty.verified.txt | 1 - ...hAnOverrideOfAnAbstractMethod.verified.txt | 1 - ...dOnlyForOverridableMembersAnalyzerTests.cs | 78 +++++++++---------- 9 files changed, 35 insertions(+), 72 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithANonVirtualMethod.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithANonVirtualProperty.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithASealedMethod.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAVirtualMethod.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnAbstractMethod.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnInterfaceMethod.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnInterfaceProperty.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnOverrideOfAnAbstractMethod.verified.txt diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithANonVirtualMethod.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithANonVirtualMethod.verified.txt deleted file mode 100644 index 11477c42..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithANonVirtualMethod.verified.txt +++ /dev/null @@ -1,8 +0,0 @@ -Diagnostic 1 - Id: Moq1200 - Location: SourceFile(Test0.cs[428..441)) - Highlight: x.Calculate() - Lines: mock.Setup(x => x.Calculate()); - Severity: Error - Message: Setup should be used only for overridable members. - diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithANonVirtualProperty.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithANonVirtualProperty.verified.txt deleted file mode 100644 index 82663df6..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithANonVirtualProperty.verified.txt +++ /dev/null @@ -1,8 +0,0 @@ -Diagnostic 1 - Id: Moq1200 - Location: SourceFile(Test0.cs[314..324)) - Highlight: x.Property - Lines: mock.Setup(x => x.Property); - Severity: Error - Message: Setup should be used only for overridable members. - diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithASealedMethod.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithASealedMethod.verified.txt deleted file mode 100644 index 7beb8584..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldFailWhenSetupIsCalledWithASealedMethod.verified.txt +++ /dev/null @@ -1,8 +0,0 @@ -Diagnostic 1 - Id: Moq1200 - Location: SourceFile(Test0.cs[588..650)) - Highlight: x.Calculate(It.IsAny(), It.IsAny(), It.IsAny()) - Lines: mock.Setup(x => x.Calculate(It.IsAny(), It.IsAny(), It.IsAny())); - Severity: Error - Message: Setup should be used only for overridable members. - diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAVirtualMethod.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAVirtualMethod.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAVirtualMethod.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnAbstractMethod.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnAbstractMethod.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnAbstractMethod.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnInterfaceMethod.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnInterfaceMethod.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnInterfaceMethod.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnInterfaceProperty.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnInterfaceProperty.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnInterfaceProperty.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnOverrideOfAnAbstractMethod.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnOverrideOfAnAbstractMethod.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.ShouldPassWhenSetupIsCalledWithAnOverrideOfAnAbstractMethod.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs index 0e9ba1b7..f3f3e38b 100644 --- a/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests.cs @@ -8,10 +8,10 @@ namespace Moq.Analyzers.Test; public class SetupShouldBeUsedOnlyForOverridableMembersAnalyzerTests : DiagnosticVerifier { - // [Fact] - public Task ShouldFailWhenSetupIsCalledWithANonVirtualMethod() + [Fact] + public async Task ShouldFailWhenSetupIsCalledWithANonVirtualMethod() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -31,17 +31,16 @@ internal class MyUnitTests private void TestBadSetupForNonVirtualMethod() { var mock = new Mock(); - mock.Setup(x => x.Calculate()); + mock.Setup(x => {|Moq1200:x.Calculate()|}); } } - """ - )); + """); } - // [Fact] - public Task ShouldFailWhenSetupIsCalledWithANonVirtualProperty() + [Fact] + public async Task ShouldFailWhenSetupIsCalledWithANonVirtualProperty() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -58,17 +57,16 @@ internal class MyUnitTests private void TestBadSetupForNonVirtualProperty() { var mock = new Mock(); - mock.Setup(x => x.Property); + mock.Setup(x => {|Moq1200:x.Property|}); } } - """ - )); + """); } - // [Fact] - public Task ShouldFailWhenSetupIsCalledWithASealedMethod() + [Fact] + public async Task ShouldFailWhenSetupIsCalledWithASealedMethod() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -96,17 +94,16 @@ internal class MyUnitTests private void TestBadSetupForSealedMethod() { var mock = new Mock(); - mock.Setup(x => x.Calculate(It.IsAny(), It.IsAny(), It.IsAny())); + mock.Setup(x => {|Moq1200:x.Calculate(It.IsAny(), It.IsAny(), It.IsAny())|}); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenSetupIsCalledWithAnAbstractMethod() + [Fact] + public async Task ShouldPassWhenSetupIsCalledWithAnAbstractMethod() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -129,14 +126,13 @@ private void TestOkForAbstractMethod() mock.Setup(x => x.Calculate(It.IsAny(), It.IsAny())); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenSetupIsCalledWithAnInterfaceMethod() + [Fact] + public async Task ShouldPassWhenSetupIsCalledWithAnInterfaceMethod() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -155,14 +151,13 @@ private void TestOkForInterfaceMethod() mock.Setup(x => x.Calculate(It.IsAny(), It.IsAny())); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenSetupIsCalledWithAnInterfaceProperty() + [Fact] + public async Task ShouldPassWhenSetupIsCalledWithAnInterfaceProperty() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -181,14 +176,13 @@ private void TestOkForInterfaceProperty() mock.Setup(x => x.TestProperty); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenSetupIsCalledWithAnOverrideOfAnAbstractMethod() + [Fact] + public async Task ShouldPassWhenSetupIsCalledWithAnOverrideOfAnAbstractMethod() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -219,14 +213,13 @@ private void TestOkForOverrideAbstractMethod() mock.Setup(x => x.Calculate(It.IsAny(), It.IsAny())); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenSetupIsCalledWithAVirtualMethod() + [Fact] + public async Task ShouldPassWhenSetupIsCalledWithAVirtualMethod() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using Moq; @@ -245,7 +238,6 @@ private void TestOkForVirtualMethod() mock.Setup(x => x.DoSth()); } } - """ - )); + """); } } From 48cef9b8d94ad367d5c7041e7b0854e108f03f0c Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 11:57:27 -0700 Subject: [PATCH 12/16] Convert SetupShouldNotIncludeAsyncResultAnalyzer tests --- ...ldFailWhenSetupWithTaskResult.verified.txt | 8 ----- ...PassWhenSetupWithReturnsAsync.verified.txt | 1 - ...uldPassWhenSetupWithoutReturn.verified.txt | 1 - ...houldNotIncludeAsyncResultAnalyzerTests.cs | 29 +++++++++---------- 4 files changed, 13 insertions(+), 26 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldFailWhenSetupWithTaskResult.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldPassWhenSetupWithReturnsAsync.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldPassWhenSetupWithoutReturn.verified.txt diff --git a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldFailWhenSetupWithTaskResult.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldFailWhenSetupWithTaskResult.verified.txt deleted file mode 100644 index 57c69be0..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldFailWhenSetupWithTaskResult.verified.txt +++ /dev/null @@ -1,8 +0,0 @@ -Diagnostic 1 - Id: Moq1201 - Location: SourceFile(Test0.cs[428..455)) - Highlight: c.GenericTaskAsync().Result - Lines: mock.Setup(c => c.GenericTaskAsync().Result); - Severity: Error - Message: Setup of async methods should use ReturnsAsync instead of .Result - diff --git a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldPassWhenSetupWithReturnsAsync.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldPassWhenSetupWithReturnsAsync.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldPassWhenSetupWithReturnsAsync.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldPassWhenSetupWithoutReturn.verified.txt b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldPassWhenSetupWithoutReturn.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.ShouldPassWhenSetupWithoutReturn.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs index 8a00f2ac..05565867 100644 --- a/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs @@ -8,10 +8,10 @@ namespace Moq.Analyzers.Test; public class SetupShouldNotIncludeAsyncResultAnalyzerTests : DiagnosticVerifier { - // [Fact] - public Task ShouldPassWhenSetupWithoutReturn() + [Fact] + public async Task ShouldPassWhenSetupWithoutReturn() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using System.Threading.Tasks; using Moq; @@ -33,14 +33,13 @@ private void TestOkForTask() mock.Setup(c => c.TaskAsync()); } } - """ - )); + """); } - // [Fact] - public Task ShouldPassWhenSetupWithReturnsAsync() + [Fact] + public async Task ShouldPassWhenSetupWithReturnsAsync() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using System.Threading.Tasks; using Moq; @@ -63,14 +62,13 @@ private void TestOkForGenericTaskProperSetup() .ReturnsAsync(string.Empty); } } - """ - )); + """); } - // [Fact] - public Task ShouldFailWhenSetupWithTaskResult() + [Fact] + public async Task ShouldFailWhenSetupWithTaskResult() { - return Verify(VerifyCSharpDiagnostic( + await VerifyCSharpDiagnostic( """ using System.Threading.Tasks; using Moq; @@ -89,10 +87,9 @@ internal class MyUnitTests private void TestBadForGenericTask() { var mock = new Mock(); - mock.Setup(c => c.GenericTaskAsync().Result); + mock.Setup(c => {|Moq1201:c.GenericTaskAsync().Result|}); } } - """ - )); + """); } } From d40c37b794ef0fee52e9135fb021f38cbcd56f15 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 13:17:54 -0700 Subject: [PATCH 13/16] Convert CallbackSignatureShouldMatchMockedMethodCodeFix tests (and merge with analyzer tests --- ...uldFailWhenIncorrectCallbacks.verified.txt | 32 -- ...sWhenCorrectSetupAndCallbacks.verified.txt | 1 - ...etupAndParameterlessCallbacks.verified.txt | 1 - ...assWhenCorrectSetupAndReturns.verified.txt | 1 - ...ctSetupAndReturnsAndCallbacks.verified.txt | 1 - ...ureShouldMatchMockedMethodAnalyzerTests.cs | 38 -- ...ackSignatureShouldMatchMockedMethodBase.cs | 165 --------- ...sWhenCorrectSetupAndCallbacks.verified.txt | 25 -- ...etupAndParameterlessCallbacks.verified.txt | 25 -- ...assWhenCorrectSetupAndReturns.verified.txt | 25 -- ...ctSetupAndReturnsAndCallbacks.verified.txt | 25 -- ...uickFixWhenIncorrectCallbacks.verified.txt | 26 -- ...tureShouldMatchMockedMethodCodeFixTests.cs | 338 ++++++++++++++++-- .../Helpers/CodeFixVerifier.cs | 25 ++ .../Helpers/DiagnosticVerifier.cs | 3 +- 15 files changed, 333 insertions(+), 398 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldFailWhenIncorrectCallbacks.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndCallbacks.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndParameterlessCallbacks.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndReturns.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndReturnsAndCallbacks.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodBase.cs delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndCallbacks.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndParameterlessCallbacks.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndReturns.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndReturnsAndCallbacks.verified.txt delete mode 100644 Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldSuggestQuickFixWhenIncorrectCallbacks.verified.txt create mode 100644 Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldFailWhenIncorrectCallbacks.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldFailWhenIncorrectCallbacks.verified.txt deleted file mode 100644 index bfaeee0c..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldFailWhenIncorrectCallbacks.verified.txt +++ /dev/null @@ -1,32 +0,0 @@ -Diagnostic 1 - Id: Moq1100 - Location: SourceFile(Test0.cs[420..427)) - Highlight: (int i) - Lines: mock.Setup(x => x.Do(It.IsAny())).Callback((int i) => { }); - Severity: Warning - Message: Callback signature must match the signature of the mocked method. - -Diagnostic 2 - Id: Moq1100 - Location: SourceFile(Test0.cs[496..518)) - Highlight: (string s1, string s2) - Lines: mock.Setup(x => x.Do(It.IsAny())).Callback((string s1, string s2) => { }); - Severity: Warning - Message: Callback signature must match the signature of the mocked method. - -Diagnostic 3 - Id: Moq1100 - Location: SourceFile(Test0.cs[626..645)) - Highlight: (string s1, int i1) - Lines: mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((string s1, int i1) => { }); - Severity: Warning - Message: Callback signature must match the signature of the mocked method. - -Diagnostic 4 - Id: Moq1100 - Location: SourceFile(Test0.cs[720..727)) - Highlight: (int i) - Lines: mock.Setup(x => x.Do(It.IsAny>())).Callback((int i) => { }); - Severity: Warning - Message: Callback signature must match the signature of the mocked method. - diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndCallbacks.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndCallbacks.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndCallbacks.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndParameterlessCallbacks.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndParameterlessCallbacks.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndParameterlessCallbacks.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndReturns.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndReturns.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndReturns.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndReturnsAndCallbacks.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndReturnsAndCallbacks.verified.txt deleted file mode 100644 index d39fa8d4..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.ShouldPassWhenCorrectSetupAndReturnsAndCallbacks.verified.txt +++ /dev/null @@ -1 +0,0 @@ -emptyString diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs deleted file mode 100644 index 3fdc861b..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodAnalyzerTests.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.IO; -using System.Threading.Tasks; -using Xunit; - -namespace Moq.Analyzers.Test; - -//public class CallbackSignatureShouldMatchMockedMethodAnalyzerTests : CallbackSignatureShouldMatchMockedMethodBase -//{ -// // [Fact] -// public Task ShouldPassWhenCorrectSetupAndReturns() -// { -// return Verify(VerifyCSharpDiagnostic(GoodSetupAndCallback)); -// } - -// // [Fact] -// public Task ShouldFailWhenIncorrectCallbacks() -// { -// return Verify(VerifyCSharpDiagnostic(BadCallbacks)); -// } - -// // [Fact] -// public Task ShouldPassWhenCorrectSetupAndCallbacks() -// { -// return Verify(VerifyCSharpDiagnostic(GoodSetupAndCallback)); -// } - -// // [Fact] -// public Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() -// { -// return Verify(VerifyCSharpDiagnostic(GoodSetupAndParameterlessCallback)); -// } - -// // [Fact] -// public Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() -// { -// return Verify(VerifyCSharpDiagnostic(GoodSetupAndReturnsAndCallback)); -// } -//} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodBase.cs b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodBase.cs deleted file mode 100644 index d1e27437..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodBase.cs +++ /dev/null @@ -1,165 +0,0 @@ -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.Diagnostics; - -namespace Moq.Analyzers.Test; - -// TODO: These tests should be broken down further -// TODO: Merge the diagnostic and codefix tests into a single harness -//public abstract class CallbackSignatureShouldMatchMockedMethodBase : CodeFixVerifier -//{ -// protected string GoodSetupAndReturns { get; } = -// """ -// using System; -// using System.Collections.Generic; -// using Moq; - -// namespace CallbackSignatureShouldMatchMockedMethod.MyGoodSetupAndReturns; - -// internal interface IFoo -// { -// int Do(string s); - -// int Do(int i, string s, DateTime dt); - -// int Do(List l); -// } - -// internal class MyUnitTests -// { -// private void MyGoodSetupAndReturns() -// { -// var mock = new Mock(); -// mock.Setup(x => x.Do(It.IsAny())).Returns((string s) => { return 0; }); -// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns((int i, string s, DateTime dt) => { return 0; }); -// mock.Setup(x => x.Do(It.IsAny>())).Returns((List l) => { return 0; }); -// } -// } -// """; - -// protected string BadCallbacks { get; } = -// """ -// using System; -// using System.Collections.Generic; -// using Moq; - -// namespace CallbackSignatureShouldMatchMockedMethod.TestBadCallbacks; - -// internal interface IFoo -// { -// int Do(string s); - -// int Do(int i, string s, DateTime dt); - -// int Do(List l); -// } - -// internal class MyUnitTests -// { -// private void TestBadCallbacks() -// { -// var mock = new Mock(); -// mock.Setup(x => x.Do(It.IsAny())).Callback((int i) => { }); -// mock.Setup(x => x.Do(It.IsAny())).Callback((string s1, string s2) => { }); -// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((string s1, int i1) => { }); -// mock.Setup(x => x.Do(It.IsAny>())).Callback((int i) => { }); -// } -// } -// """; - -// protected string GoodSetupAndCallback { get; } = -// """ -// using System; -// using System.Collections.Generic; -// using Moq; - -// namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndCallback; - -// internal interface IFoo -// { -// int Do(string s); - -// int Do(int i, string s, DateTime dt); - -// int Do(List l); -// } - -// internal class MyUnitTests -// { -// private void TestGoodSetupAndCallback() -// { -// var mock = new Mock(); -// mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); -// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((int i, string s, DateTime dt) => { }); -// mock.Setup(x => x.Do(It.IsAny>())).Callback((List l) => { }); -// } -// } -// """; - -// protected string GoodSetupAndParameterlessCallback { get; } = -// """ -// using System; -// using System.Collections.Generic; -// using Moq; - -// namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndParameterlessCallback; - -// internal interface IFoo -// { -// int Do(string s); - -// int Do(int i, string s, DateTime dt); - -// int Do(List l); -// } - -// internal class MyUnitTests -// { -// private void TestGoodSetupAndParameterlessCallback() -// { -// var mock = new Mock(); -// mock.Setup(x => x.Do(It.IsAny())).Callback(() => { }); -// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback(() => { }); -// mock.Setup(x => x.Do(It.IsAny>())).Callback(() => { }); -// } -// } -// """; - -// protected string GoodSetupAndReturnsAndCallback { get; } = -// """ -// using System; -// using System.Collections.Generic; -// using Moq; - -// namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndReturnsAndCallback; - -// internal interface IFoo -// { -// int Do(string s); - -// int Do(int i, string s, DateTime dt); - -// int Do(List l); -// } - -// internal class MyUnitTests -// { -// private void TestGoodSetupAndReturnsAndCallback() -// { -// var mock = new Mock(); -// mock.Setup(x => x.Do(It.IsAny())).Returns(0).Callback((string s) => { }); -// mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns(0).Callback((int i, string s, DateTime dt) => { }); -// mock.Setup(x => x.Do(It.IsAny>())).Returns(0).Callback((List l) => { }); -// } -// } -// """; - -// protected override CodeFixProvider GetCSharpCodeFixProvider() -// { -// return new CallbackSignatureShouldMatchMockedMethodCodeFix(); -// } - -// protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() -// { -// return new CallbackSignatureShouldMatchMockedMethodAnalyzer(); -// } -//} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndCallbacks.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndCallbacks.verified.txt deleted file mode 100644 index 80913877..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndCallbacks.verified.txt +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using Moq; - -namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndCallback; - -internal interface IFoo -{ - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); -} - -internal class MyUnitTests -{ - private void TestGoodSetupAndCallback() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((int i, string s, DateTime dt) => { }); - mock.Setup(x => x.Do(It.IsAny>())).Callback((List l) => { }); - } -} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndParameterlessCallbacks.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndParameterlessCallbacks.verified.txt deleted file mode 100644 index ccd5ce3e..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndParameterlessCallbacks.verified.txt +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using Moq; - -namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndParameterlessCallback; - -internal interface IFoo -{ - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); -} - -internal class MyUnitTests -{ - private void TestGoodSetupAndParameterlessCallback() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Callback(() => { }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback(() => { }); - mock.Setup(x => x.Do(It.IsAny>())).Callback(() => { }); - } -} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndReturns.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndReturns.verified.txt deleted file mode 100644 index 756d8892..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndReturns.verified.txt +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using Moq; - -namespace CallbackSignatureShouldMatchMockedMethod.MyGoodSetupAndReturns; - -internal interface IFoo -{ - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); -} - -internal class MyUnitTests -{ - private void MyGoodSetupAndReturns() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Returns((string s) => { return 0; }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns((int i, string s, DateTime dt) => { return 0; }); - mock.Setup(x => x.Do(It.IsAny>())).Returns((List l) => { return 0; }); - } -} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndReturnsAndCallbacks.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndReturnsAndCallbacks.verified.txt deleted file mode 100644 index 17f6b904..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldPassWhenCorrectSetupAndReturnsAndCallbacks.verified.txt +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using Moq; - -namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndReturnsAndCallback; - -internal interface IFoo -{ - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); -} - -internal class MyUnitTests -{ - private void TestGoodSetupAndReturnsAndCallback() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Returns(0).Callback((string s) => { }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns(0).Callback((int i, string s, DateTime dt) => { }); - mock.Setup(x => x.Do(It.IsAny>())).Returns(0).Callback((List l) => { }); - } -} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldSuggestQuickFixWhenIncorrectCallbacks.verified.txt b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldSuggestQuickFixWhenIncorrectCallbacks.verified.txt deleted file mode 100644 index 2ae7ce25..00000000 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.ShouldSuggestQuickFixWhenIncorrectCallbacks.verified.txt +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using Moq; - -namespace CallbackSignatureShouldMatchMockedMethod.TestBadCallbacks; - -internal interface IFoo -{ - int Do(string s); - - int Do(int i, string s, DateTime dt); - - int Do(List l); -} - -internal class MyUnitTests -{ - private void TestBadCallbacks() - { - var mock = new Mock(); - mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); - mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); - mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((int i, string s, DateTime dt) => { }); - mock.Setup(x => x.Do(It.IsAny>())).Callback((List l) => { }); - } -} diff --git a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs index 3ca533c5..e923af8c 100644 --- a/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs +++ b/Source/Moq.Analyzers.Test/CallbackSignatureShouldMatchMockedMethodCodeFixTests.cs @@ -1,40 +1,314 @@ using System.IO; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; +using Moq.Analyzers.Test.Helpers; using Xunit; namespace Moq.Analyzers.Test; -//public class CallbackSignatureShouldMatchMockedMethodCodeFixTests : CallbackSignatureShouldMatchMockedMethodBase -//{ -// // [Fact] -// public Task ShouldPassWhenCorrectSetupAndReturns() -// { -// return Verify(VerifyCSharpFix(GoodSetupAndReturns)); -// } - -// // [Fact] -// public Task ShouldSuggestQuickFixWhenIncorrectCallbacks() -// { -// return Verify(VerifyCSharpFix(BadCallbacks)); -// } - -// // [Fact] -// public Task ShouldPassWhenCorrectSetupAndCallbacks() -// { -// return Verify(VerifyCSharpFix(GoodSetupAndCallback)); -// } - -// // [Fact] -// public Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() -// { -// return Verify(VerifyCSharpFix(GoodSetupAndParameterlessCallback)); -// } - -// // [Fact] -// public Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() -// { -// return Verify(VerifyCSharpFix(GoodSetupAndReturnsAndCallback)); -// } -//} +public class CallbackSignatureShouldMatchMockedMethodCodeFixTests : CodeFixVerifier +{ + [Fact] + public async Task ShouldPassWhenCorrectSetupAndReturns() + { + await VerifyCSharpFix( + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.MyGoodSetupAndReturns; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void MyGoodSetupAndReturns() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Returns((string s) => { return 0; }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns((int i, string s, DateTime dt) => { return 0; }); + mock.Setup(x => x.Do(It.IsAny>())).Returns((List l) => { return 0; }); + } + } + """, + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.MyGoodSetupAndReturns; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void MyGoodSetupAndReturns() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Returns((string s) => { return 0; }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns((int i, string s, DateTime dt) => { return 0; }); + mock.Setup(x => x.Do(It.IsAny>())).Returns((List l) => { return 0; }); + } + } + """); + } + + [Fact] + public async Task ShouldSuggestQuickFixWhenIncorrectCallbacks() + { + await VerifyCSharpFix( + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.TestBadCallbacks; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void TestBadCallbacks() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Callback({|Moq1100:(int i)|} => { }); + mock.Setup(x => x.Do(It.IsAny())).Callback({|Moq1100:(string s1, string s2)|} => { }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback({|Moq1100:(string s1, int i1)|} => { }); + mock.Setup(x => x.Do(It.IsAny>())).Callback({|Moq1100:(int i)|} => { }); + } + } + """, + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.TestBadCallbacks; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void TestBadCallbacks() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); + mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((int i, string s, DateTime dt) => { }); + mock.Setup(x => x.Do(It.IsAny>())).Callback((List l) => { }); + } + } + """); + } + + [Fact] + public async Task ShouldPassWhenCorrectSetupAndCallbacks() + { + await VerifyCSharpFix( + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndCallback; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void TestGoodSetupAndCallback() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((int i, string s, DateTime dt) => { }); + mock.Setup(x => x.Do(It.IsAny>())).Callback((List l) => { }); + } + } + """, + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndCallback; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void TestGoodSetupAndCallback() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Callback((string s) => { }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback((int i, string s, DateTime dt) => { }); + mock.Setup(x => x.Do(It.IsAny>())).Callback((List l) => { }); + } + } + """); + } + + [Fact] + public async Task ShouldPassWhenCorrectSetupAndParameterlessCallbacks() + { + await VerifyCSharpFix( + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndParameterlessCallback; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void TestGoodSetupAndParameterlessCallback() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Callback(() => { }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback(() => { }); + mock.Setup(x => x.Do(It.IsAny>())).Callback(() => { }); + } + } + """, + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndParameterlessCallback; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void TestGoodSetupAndParameterlessCallback() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Callback(() => { }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Callback(() => { }); + mock.Setup(x => x.Do(It.IsAny>())).Callback(() => { }); + } + } + """); + } + + [Fact] + public async Task ShouldPassWhenCorrectSetupAndReturnsAndCallbacks() + { + await VerifyCSharpFix( + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndReturnsAndCallback; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void TestGoodSetupAndReturnsAndCallback() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Returns(0).Callback((string s) => { }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns(0).Callback((int i, string s, DateTime dt) => { }); + mock.Setup(x => x.Do(It.IsAny>())).Returns(0).Callback((List l) => { }); + } + } + """, + """ + using System; + using System.Collections.Generic; + using Moq; + + namespace CallbackSignatureShouldMatchMockedMethod.TestGoodSetupAndReturnsAndCallback; + + internal interface IFoo + { + int Do(string s); + + int Do(int i, string s, DateTime dt); + + int Do(List l); + } + + internal class MyUnitTests + { + private void TestGoodSetupAndReturnsAndCallback() + { + var mock = new Mock(); + mock.Setup(x => x.Do(It.IsAny())).Returns(0).Callback((string s) => { }); + mock.Setup(x => x.Do(It.IsAny(), It.IsAny(), It.IsAny())).Returns(0).Callback((int i, string s, DateTime dt) => { }); + mock.Setup(x => x.Do(It.IsAny>())).Returns(0).Callback((List l) => { }); + } + } + """); + } +} diff --git a/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs b/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs new file mode 100644 index 00000000..0f7f25de --- /dev/null +++ b/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs @@ -0,0 +1,25 @@ +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Testing; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; + +namespace Moq.Analyzers.Test.Helpers; + +public abstract class CodeFixVerifier + where TAnalyzer : DiagnosticAnalyzer, new() + where TCodeFixProvider : CodeFixProvider, new() +{ + protected async Task VerifyCSharpFix(string originalSource, string fixedSource) + { + CSharpCodeFixTest context = new() + { + // TODO: Refactor this out + ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Moq", "4.8.2")]), // TODO: See https://github.com/Litee/moq.analyzers/issues/58 + TestBehaviors = TestBehaviors.SkipGeneratedCodeCheck, // TODO: We should enable the generated code check + TestCode = originalSource, + FixedCode = fixedSource, + }; + + await context.RunAsync().ConfigureAwait(false); + } +} diff --git a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs index fb3811b3..abe0085a 100644 --- a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs +++ b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs @@ -12,9 +12,10 @@ protected async Task VerifyCSharpDiagnostic(string source) { CSharpAnalyzerTest context = new() { + // TODO: Refactor this out ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Moq", "4.8.2")]), // TODO: See https://github.com/Litee/moq.analyzers/issues/58 - TestCode = source, TestBehaviors = TestBehaviors.SkipGeneratedCodeCheck, // TODO: We should enable the generated code check + TestCode = source, }; context.SolutionTransforms.Add((solution, projectId) => From 4dbd110a528cfe48ba314308fd33c1be58097d41 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 13:43:05 -0700 Subject: [PATCH 14/16] Refactor common test setup code --- .../Helpers/AnalyzerTestExtensions.cs | 16 ++++++++++++++++ .../Helpers/CodeFixVerifier.cs | 5 ++--- .../Helpers/DiagnosticVerifier.cs | 9 +-------- 3 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 Source/Moq.Analyzers.Test/Helpers/AnalyzerTestExtensions.cs diff --git a/Source/Moq.Analyzers.Test/Helpers/AnalyzerTestExtensions.cs b/Source/Moq.Analyzers.Test/Helpers/AnalyzerTestExtensions.cs new file mode 100644 index 00000000..14a77a2b --- /dev/null +++ b/Source/Moq.Analyzers.Test/Helpers/AnalyzerTestExtensions.cs @@ -0,0 +1,16 @@ +using Microsoft.CodeAnalysis.Testing; + +namespace Moq.Analyzers.Test.Helpers; + +internal static class AnalyzerTestExtensions +{ + public static TAnalyzerTest SetDefaults(this TAnalyzerTest test) + where TAnalyzerTest : AnalyzerTest + where TVerifier : IVerifier, new() + { + test.ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Moq", "4.8.2")]); // TODO: See https://github.com/Litee/moq.analyzers/issues/58 + test.TestBehaviors = TestBehaviors.SkipGeneratedCodeCheck; // TODO: We should enable the generated code check + + return test; + } +} diff --git a/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs b/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs index 0f7f25de..a2b39f4b 100644 --- a/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs +++ b/Source/Moq.Analyzers.Test/Helpers/CodeFixVerifier.cs @@ -13,13 +13,12 @@ protected async Task VerifyCSharpFix(string originalSource, string fixedSource) { CSharpCodeFixTest context = new() { - // TODO: Refactor this out - ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Moq", "4.8.2")]), // TODO: See https://github.com/Litee/moq.analyzers/issues/58 - TestBehaviors = TestBehaviors.SkipGeneratedCodeCheck, // TODO: We should enable the generated code check TestCode = originalSource, FixedCode = fixedSource, }; + context.SetDefaults, DefaultVerifier>(); + await context.RunAsync().ConfigureAwait(false); } } diff --git a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs index abe0085a..6f87cdd7 100644 --- a/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs +++ b/Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.cs @@ -12,17 +12,10 @@ protected async Task VerifyCSharpDiagnostic(string source) { CSharpAnalyzerTest context = new() { - // TODO: Refactor this out - ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Moq", "4.8.2")]), // TODO: See https://github.com/Litee/moq.analyzers/issues/58 - TestBehaviors = TestBehaviors.SkipGeneratedCodeCheck, // TODO: We should enable the generated code check TestCode = source, }; - context.SolutionTransforms.Add((solution, projectId) => - { - return solution.WithProjectParseOptions(projectId, new CSharpParseOptions( - languageVersion: LanguageVersion.Latest)); - }); + context.SetDefaults, DefaultVerifier>(); await context.RunAsync().ConfigureAwait(false); } From 75ba7a61bf573a015eb2f8b213ac786111df31b7 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 16:21:02 -0700 Subject: [PATCH 15/16] Remove Moq from tests --- Directory.Packages.props | 1 - Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj | 1 - 2 files changed, 2 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index dad54e4b..80334e13 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -20,7 +20,6 @@ - \ No newline at end of file diff --git a/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj b/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj index 9c75a87b..a5f5f708 100644 --- a/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj +++ b/Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj @@ -22,7 +22,6 @@ - From a6d2ecdc4b6de6e189e5ea24db0a8effa31b1558 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Thu, 6 Jun 2024 17:51:07 -0700 Subject: [PATCH 16/16] Fix insufficent nuget.config patterns --- nuget.config | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/nuget.config b/nuget.config index acd01c43..d1086cdd 100644 --- a/nuget.config +++ b/nuget.config @@ -11,8 +11,21 @@ - - + + + + + + + + + + + + + + +