From 37433b569731a08761f4397448171d9bccd3188e Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Sun, 9 Jun 2024 22:54:46 -0700 Subject: [PATCH] Merge AbstractClassTests into ConstructorArgumentsShouldMatchAnalyzerTests --- .../Moq.Analyzers.Test/AbstractClassTests.cs | 156 ------------------ ...ructorArgumentsShouldMatchAnalyzerTests.cs | 46 +++++- 2 files changed, 43 insertions(+), 159 deletions(-) delete mode 100644 Source/Moq.Analyzers.Test/AbstractClassTests.cs diff --git a/Source/Moq.Analyzers.Test/AbstractClassTests.cs b/Source/Moq.Analyzers.Test/AbstractClassTests.cs deleted file mode 100644 index 79d5f0e1..00000000 --- a/Source/Moq.Analyzers.Test/AbstractClassTests.cs +++ /dev/null @@ -1,156 +0,0 @@ -using Verifier = Moq.Analyzers.Test.Helpers.AnalyzerVerifier; - -namespace Moq.Analyzers.Test; - -public class AbstractClassTests -{ - public static IEnumerable TestData() - { - foreach (string @namespace in new[] { string.Empty, "namespace MyNamespace;" }) - { - yield return - [ - @namespace, """new Mock>{|Moq1002:("42")|}; // The class has a constructor that takes an Int32 but passes a string""", - ]; - - yield return - [ - @namespace, """new Mock>{|Moq1002:("42", 42)|}; // The class has a ctor with two arguments [Int32, String], but they are passed in reverse order""", - ]; - - yield return - [ - @namespace, """new Mock>{|Moq1002:(42)|}; // The class has a ctor but does not take any arguments""", - ]; - - yield return - [ - // TODO: Review use of `.As<>()` in this test case. It is not clear what purpose it serves. - @namespace, """new Mock>().As>();""", - ]; - - yield return - [ - @namespace, """new Mock>();""", - ]; - - yield return - [ - @namespace, """new Mock>(MockBehavior.Default);""", - ]; - - // TODO: "I think this _should_ fail, but currently passes. Tracked by #55." - // yield return - // [ - // // TODO: Review use of `.As<>()` in this test case. It is not clear what purpose it serves. - // @namespace, """new Mock().As();""", - // ]; - - yield return - [ - @namespace, """new Mock{|Moq1002:("42")|}; // The class has a ctor that takes an Int32 but passes a String""", - ]; - - yield return - [ - @namespace, """new Mock{|Moq1002:("42", 42)|}; // The class has a ctor with two arguments [Int32, String], but they are passed in reverse order""", - ]; - - yield return - [ - @namespace, """new Mock{|Moq1002:(42)|}; // The class has a ctor but does not take any arguments""", - ]; - - yield return - [ - // TODO: Review use of `.As<>()` in this test case. It is not clear what purpose it serves. - @namespace, """new Mock().As();""", - ]; - - yield return - [ - @namespace, """new Mock(42);""", - ]; - - yield return - [ - @namespace, - """new Mock(MockBehavior.Default, 42);""", - ]; - - yield return - [ - @namespace, """new Mock(42, "42");""", - ]; - - yield return - [ - @namespace, """new Mock(MockBehavior.Default, 42, "42");""", - ]; - - yield return - [ - @namespace, """new Mock>(42);""", - ]; - - yield return - [ - @namespace, """new Mock>(MockBehavior.Default, 42);""", - ]; - } - } - - [Theory] - [MemberData(nameof(TestData))] - public async Task ShouldAnalyzeAbstractClasses(string @namespace, string mock) - { - await Verifier.VerifyAnalyzerAsync( - $$""" - {{@namespace}} - - internal abstract class AbstractClassDefaultCtor - { - protected AbstractClassDefaultCtor() - { - } - } - - internal abstract class AbstractGenericClassDefaultCtor - { - protected AbstractGenericClassDefaultCtor() - { - } - } - - internal abstract class AbstractClassWithCtor - { - protected AbstractClassWithCtor(int a) - { - } - - protected AbstractClassWithCtor(int a, string b) - { - } - } - - internal abstract class AbstractGenericClassWithCtor - { - protected AbstractGenericClassWithCtor(int a) - { - } - - protected AbstractGenericClassWithCtor(int a, string b) - { - } - } - - internal class UnitTest - { - private void Test() - { - {{mock}} - } - } - """); - } -} diff --git a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs index 6dec7045..0d1db285 100644 --- a/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs +++ b/Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.cs @@ -31,6 +31,27 @@ public static IEnumerable TestData() yield return [@namespace, """new Mock{|Moq1002:(MockBehavior.Strict, 4, true)|};"""]; yield return [@namespace, """new Mock{|Moq1002:(MockBehavior.Loose, 5, true)|};"""]; yield return [@namespace, """new Mock{|Moq1002:(MockBehavior.Loose, "2", 6)|};"""]; + yield return [@namespace, """new Mock>{|Moq1002:("42")|};"""]; + yield return [@namespace, """new Mock>{|Moq1002:("42", 42)|};"""]; + yield return [@namespace, """new Mock>{|Moq1002:(42)|};"""]; + // TODO: Review use of `.As<>()` in this test case. It is not clear what purpose it serves. + yield return [@namespace, """new Mock>().As>();"""]; + yield return [@namespace, """new Mock>();"""]; + yield return [@namespace, """new Mock>(MockBehavior.Default);"""]; + // TODO: "I think this _should_ fail, but currently passes. Tracked by #55." + // TODO: Review use of `.As<>()` in this test case. It is not clear what purpose it serves. + // yield return [@namespace, """new Mock().As();"""]; + yield return [@namespace, """new Mock{|Moq1002:("42")|};"""]; + yield return [@namespace, """new Mock{|Moq1002:("42", 42)|};"""]; + yield return [@namespace, """new Mock{|Moq1002:(42)|};"""]; + // TODO: Review use of `.As<>()` in this test case. It is not clear what purpose it serves. + yield return [@namespace, """new Mock().As();"""]; + yield return [@namespace, """new Mock(42);"""]; + yield return [@namespace, """new Mock(MockBehavior.Default, 42);"""]; + yield return [@namespace, """new Mock(42, "42");"""]; + yield return [@namespace, """new Mock(MockBehavior.Default, 42, "42");"""]; + yield return [@namespace, """new Mock>(42);"""]; + yield return [@namespace, """new Mock>(MockBehavior.Default, 42);"""]; } } @@ -45,14 +66,33 @@ await Verifier.VerifyAnalyzerAsync( internal class Foo { public Foo(string s) { } - public Foo(bool b, int i) { } - public Foo(params DateTime[] dates) { } - public Foo(List l, string s = "A") { } } + internal abstract class AbstractClassDefaultCtor + { + protected AbstractClassDefaultCtor() { } + } + + internal abstract class AbstractGenericClassDefaultCtor + { + protected AbstractGenericClassDefaultCtor() { } + } + + internal abstract class AbstractClassWithCtor + { + protected AbstractClassWithCtor(int a) { } + protected AbstractClassWithCtor(int a, string b) { } + } + + internal abstract class AbstractGenericClassWithCtor + { + protected AbstractGenericClassWithCtor(int a) { } + protected AbstractGenericClassWithCtor(int a, string b) { } + } + internal class UnitTest { private void Test()