From 6e9ed72eb2de16b37f499c9b0255a520ed0e2751 Mon Sep 17 00:00:00 2001 From: Gilles TOURREAU Date: Fri, 16 Aug 2024 16:37:09 +0200 Subject: [PATCH 1/3] Fix the PosInfoMoq2006 rule when Setup() a member in inherited class (fixes #31). --- .github/workflows/github-actions-release.yml | 2 +- PosInformatique.Moq.Analyzers.sln | 10 ++++++++++ ...UsedWithProtectedOrInternalMembersAnalyzer.cs | 2 +- src/Moq.Analyzers/SymbolExtensions.cs | 16 ++++++++++++++++ ...WithProtectedOrInternalMembersAnalyzerTest.cs | 9 +++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/.github/workflows/github-actions-release.yml b/.github/workflows/github-actions-release.yml index 2b9cec9..e36436c 100644 --- a/.github/workflows/github-actions-release.yml +++ b/.github/workflows/github-actions-release.yml @@ -7,7 +7,7 @@ on: type: string description: The version of the library required: true - default: 1.9.2 + default: 1.9.3 VersionSuffix: type: string description: The version suffix of the library (for example rc.1) diff --git a/PosInformatique.Moq.Analyzers.sln b/PosInformatique.Moq.Analyzers.sln index 26db6a1..5195eea 100644 --- a/PosInformatique.Moq.Analyzers.sln +++ b/PosInformatique.Moq.Analyzers.sln @@ -53,6 +53,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Compilation", "Compilation" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.Analyzers.Sandbox", "tests\Moq.Analyzers.Sandbox\Moq.Analyzers.Sandbox.csproj", "{07F970A1-1477-4D4C-B233-C9B4DA6E3AD6}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{FA7258E1-65F0-4C40-A122-D76A1F0C79A0}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{0EACA365-AFB2-4200-91FD-A20DC9617799}" + ProjectSection(SolutionItems) = preProject + .github\workflows\github-actions-ci.yaml = .github\workflows\github-actions-ci.yaml + .github\workflows\github-actions-release.yml = .github\workflows\github-actions-release.yml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -80,6 +88,8 @@ Global {3C20D95F-AB5F-44EC-8FB6-CB9827B7FD63} = {1D59B801-B4D3-44FC-A2BE-F2F53AC54061} {815BE8D0-C7D5-4B4E-82E0-DE29C11F258E} = {3C20D95F-AB5F-44EC-8FB6-CB9827B7FD63} {D9C84D36-7F9C-4EFB-BE6F-9F7A05FE957D} = {3C20D95F-AB5F-44EC-8FB6-CB9827B7FD63} + {FA7258E1-65F0-4C40-A122-D76A1F0C79A0} = {1D59B801-B4D3-44FC-A2BE-F2F53AC54061} + {0EACA365-AFB2-4200-91FD-A20DC9617799} = {FA7258E1-65F0-4C40-A122-D76A1F0C79A0} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3307E7F7-9CD7-4C12-B34A-943F5A8B62A4} diff --git a/src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs b/src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs index 526a7de..6a6194e 100644 --- a/src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs +++ b/src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs @@ -81,7 +81,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) } // Check if a method exists with the specified name - foreach (var method in mockedType.GetMembers(methodName).OfType()) + foreach (var method in mockedType.GetAllMembers(methodName).OfType()) { if (!method.IsAbstract && !method.IsVirtual && !method.IsOverride) { diff --git a/src/Moq.Analyzers/SymbolExtensions.cs b/src/Moq.Analyzers/SymbolExtensions.cs index 8a8c5d4..8c5a0c1 100644 --- a/src/Moq.Analyzers/SymbolExtensions.cs +++ b/src/Moq.Analyzers/SymbolExtensions.cs @@ -32,5 +32,21 @@ public static bool IsOrInheritFrom(this ITypeSymbol? symbol, ITypeSymbol type) return IsOrInheritFrom(symbol.BaseType, type); } + + public static IEnumerable GetAllMembers(this ITypeSymbol symbol, string name) + { + IEnumerable baseTypeMembers; + + if (symbol.BaseType is not null) + { + baseTypeMembers = symbol.BaseType.GetAllMembers(name); + } + else + { + baseTypeMembers = Enumerable.Empty(); + } + + return symbol.GetMembers(name).Concat(baseTypeMembers); + } } } diff --git a/tests/Moq.Analyzers.Tests/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzerTest.cs b/tests/Moq.Analyzers.Tests/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzerTest.cs index 327d033..1652c4e 100644 --- a/tests/Moq.Analyzers.Tests/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzerTest.cs +++ b/tests/Moq.Analyzers.Tests/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzerTest.cs @@ -31,8 +31,11 @@ public void TestMethod() mock1.Protected().Setup(""ProtectedAbstractMethod""); mock1.Protected().Setup(""ProtectedInternalAbstractMethod""); mock1.Protected().Setup(""InternalAbstractMethod""); + mock1.Protected().Setup(""ProtectedMethod""); mock1.Protected().Setup(""ProtectedOverrideMethod""); mock1.Protected().Setup(""ProtectedInternalOverrideMethod""); + mock1.Protected().Setup(""ProtectedInternalMethod""); + mock1.Protected().Setup(""InternalMethod""); mock1.Protected().Setup(""InternalOverrideMethod""); mock1.Protected().Setup(""ProtectedVirtualMethod""); @@ -75,9 +78,15 @@ public abstract class BaseClass { protected virtual void ProtectedOverrideMethod() { } + protected virtual void ProtectedMethod() { } + protected internal virtual void ProtectedInternalOverrideMethod() { } + protected internal virtual void ProtectedInternalMethod() { } + internal virtual void InternalOverrideMethod() { } + + internal virtual void InternalMethod() { } } }"; From e266c5d3222ea349d8da15778303ed2273209f50 Mon Sep 17 00:00:00 2001 From: Gilles TOURREAU Date: Fri, 16 Aug 2024 16:44:41 +0200 Subject: [PATCH 2/3] Fix the unit tests failed. --- ...ProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs b/src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs index 6a6194e..7c93bdd 100644 --- a/src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs +++ b/src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs @@ -90,7 +90,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context) if (method.IsSealed) { - continue; + break; } if (method.DeclaredAccessibility == Accessibility.Protected) From eb2eaacb5144bd2d5ed4195b086c9161b68133b4 Mon Sep 17 00:00:00 2001 From: Gilles TOURREAU Date: Mon, 19 Aug 2024 06:21:54 +0200 Subject: [PATCH 3/3] Fix the release notes. --- src/Moq.Analyzers/Moq.Analyzers.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Moq.Analyzers/Moq.Analyzers.csproj b/src/Moq.Analyzers/Moq.Analyzers.csproj index 241b850..7b91a1c 100644 --- a/src/Moq.Analyzers/Moq.Analyzers.csproj +++ b/src/Moq.Analyzers/Moq.Analyzers.csproj @@ -17,6 +17,9 @@ https://github.com/PosInformatique/PosInformatique.Moq.Analyzers README.md + 1.9.3 + - Fix the PosInfoMoq2006 when Setup() a method/property in inherited class. + 1.9.2 - Fix the PosInfoMoq1003 to raise warnings when using InSequence() method. - Fix the PosInfoMoq2003 to raise errors when using InSequence() method.