Skip to content

Commit ba32035

Browse files
v1.9.3 (#32)
* Fix the PosInfoMoq2006 rule when Setup() a member in inherited class (fixes #31).
1 parent b0e56a2 commit ba32035

6 files changed

Lines changed: 41 additions & 3 deletions

File tree

.github/workflows/github-actions-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
type: string
88
description: The version of the library
99
required: true
10-
default: 1.9.2
10+
default: 1.9.3
1111
VersionSuffix:
1212
type: string
1313
description: The version suffix of the library (for example rc.1)

PosInformatique.Moq.Analyzers.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Compilation", "Compilation"
5353
EndProject
5454
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.Analyzers.Sandbox", "tests\Moq.Analyzers.Sandbox\Moq.Analyzers.Sandbox.csproj", "{07F970A1-1477-4D4C-B233-C9B4DA6E3AD6}"
5555
EndProject
56+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{FA7258E1-65F0-4C40-A122-D76A1F0C79A0}"
57+
EndProject
58+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{0EACA365-AFB2-4200-91FD-A20DC9617799}"
59+
ProjectSection(SolutionItems) = preProject
60+
.github\workflows\github-actions-ci.yaml = .github\workflows\github-actions-ci.yaml
61+
.github\workflows\github-actions-release.yml = .github\workflows\github-actions-release.yml
62+
EndProjectSection
63+
EndProject
5664
Global
5765
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5866
Debug|Any CPU = Debug|Any CPU
@@ -80,6 +88,8 @@ Global
8088
{3C20D95F-AB5F-44EC-8FB6-CB9827B7FD63} = {1D59B801-B4D3-44FC-A2BE-F2F53AC54061}
8189
{815BE8D0-C7D5-4B4E-82E0-DE29C11F258E} = {3C20D95F-AB5F-44EC-8FB6-CB9827B7FD63}
8290
{D9C84D36-7F9C-4EFB-BE6F-9F7A05FE957D} = {3C20D95F-AB5F-44EC-8FB6-CB9827B7FD63}
91+
{FA7258E1-65F0-4C40-A122-D76A1F0C79A0} = {1D59B801-B4D3-44FC-A2BE-F2F53AC54061}
92+
{0EACA365-AFB2-4200-91FD-A20DC9617799} = {FA7258E1-65F0-4C40-A122-D76A1F0C79A0}
8393
EndGlobalSection
8494
GlobalSection(ExtensibilityGlobals) = postSolution
8595
SolutionGuid = {3307E7F7-9CD7-4C12-B34A-943F5A8B62A4}

src/Moq.Analyzers/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
8181
}
8282

8383
// Check if a method exists with the specified name
84-
foreach (var method in mockedType.GetMembers(methodName).OfType<IMethodSymbol>())
84+
foreach (var method in mockedType.GetAllMembers(methodName).OfType<IMethodSymbol>())
8585
{
8686
if (!method.IsAbstract && !method.IsVirtual && !method.IsOverride)
8787
{
@@ -90,7 +90,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
9090

9191
if (method.IsSealed)
9292
{
93-
continue;
93+
break;
9494
}
9595

9696
if (method.DeclaredAccessibility == Accessibility.Protected)

src/Moq.Analyzers/Moq.Analyzers.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.Moq.Analyzers</PackageProjectUrl>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
1919
<PackageReleaseNotes>
20+
1.9.3
21+
- Fix the PosInfoMoq2006 when Setup() a method/property in inherited class.
22+
2023
1.9.2
2124
- Fix the PosInfoMoq1003 to raise warnings when using InSequence() method.
2225
- Fix the PosInfoMoq2003 to raise errors when using InSequence() method.

src/Moq.Analyzers/SymbolExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,21 @@ public static bool IsOrInheritFrom(this ITypeSymbol? symbol, ITypeSymbol type)
3232

3333
return IsOrInheritFrom(symbol.BaseType, type);
3434
}
35+
36+
public static IEnumerable<ISymbol> GetAllMembers(this ITypeSymbol symbol, string name)
37+
{
38+
IEnumerable<ISymbol> baseTypeMembers;
39+
40+
if (symbol.BaseType is not null)
41+
{
42+
baseTypeMembers = symbol.BaseType.GetAllMembers(name);
43+
}
44+
else
45+
{
46+
baseTypeMembers = Enumerable.Empty<ISymbol>();
47+
}
48+
49+
return symbol.GetMembers(name).Concat(baseTypeMembers);
50+
}
3551
}
3652
}

tests/Moq.Analyzers.Tests/Analyzers/SetupProtectedMustBeUsedWithProtectedOrInternalMembersAnalyzerTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ public void TestMethod()
3131
mock1.Protected().Setup(""ProtectedAbstractMethod"");
3232
mock1.Protected().Setup(""ProtectedInternalAbstractMethod"");
3333
mock1.Protected().Setup(""InternalAbstractMethod"");
34+
mock1.Protected().Setup(""ProtectedMethod"");
3435
mock1.Protected().Setup(""ProtectedOverrideMethod"");
3536
mock1.Protected().Setup(""ProtectedInternalOverrideMethod"");
37+
mock1.Protected().Setup(""ProtectedInternalMethod"");
38+
mock1.Protected().Setup(""InternalMethod"");
3639
mock1.Protected().Setup(""InternalOverrideMethod"");
3740
3841
mock1.Protected().Setup<int>(""ProtectedVirtualMethod"");
@@ -75,9 +78,15 @@ public abstract class BaseClass
7578
{
7679
protected virtual void ProtectedOverrideMethod() { }
7780
81+
protected virtual void ProtectedMethod() { }
82+
7883
protected internal virtual void ProtectedInternalOverrideMethod() { }
7984
85+
protected internal virtual void ProtectedInternalMethod() { }
86+
8087
internal virtual void InternalOverrideMethod() { }
88+
89+
internal virtual void InternalMethod() { }
8190
}
8291
}";
8392

0 commit comments

Comments
 (0)