Skip to content

Commit 19f245d

Browse files
Fix a bug with It.Is<T>() and It.IsAny<T>() parameters check with the PosInfo1006 rule when using inherited types (fixes #47).
1 parent ba3d285 commit 19f245d

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
### New Rules
2-
Rule ID | Category | Severity | Notes
3-
--------|----------|----------|-------
4-
PosInfoMoq1008 | Design | Warning | VerifyStaticMethodsRequiresMockParametersAnalyzer, [Documentation](https://posinformatique.github.io/PosInformatique.Moq.Analyzers/docs/Compilation/PosInfoMoq1008.html)
1+


src/Moq.Analyzers/Analyzers/ItArgumentsMustMatchMockedMethodArgumentsAnalyzer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,24 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
6464
var itIsAnyType = moqSymbols.GetItIsAnyType(invocationArgument.Symbol);
6565
if (itIsAnyType is not null)
6666
{
67-
if (!SymbolEqualityComparer.Default.Equals(itIsAnyType, invocationArgument.ParameterSymbol.Type))
67+
if (!itIsAnyType.IsOrInheritFrom(invocationArgument.ParameterSymbol.Type))
6868
{
6969
context.ReportDiagnostic(Rule, invocationArgument.Syntax.GetLocation());
70-
continue;
7170
}
71+
72+
continue;
7273
}
7374

7475
// Check if the parameter is "It.Is<xxx>()"
7576
var itIsType = moqSymbols.GetItIsType(invocationArgument.Symbol);
7677
if (itIsType is not null)
7778
{
78-
if (!SymbolEqualityComparer.Default.Equals(itIsType, invocationArgument.ParameterSymbol.Type))
79+
if (!itIsType.IsOrInheritFrom(invocationArgument.ParameterSymbol.Type))
7980
{
8081
context.ReportDiagnostic(Rule, invocationArgument.Syntax.GetLocation());
81-
continue;
8282
}
83+
84+
continue;
8385
}
8486
}
8587
}

src/Moq.Analyzers/MoqSymbols.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public bool IsAnyType(ITypeSymbol symbol)
131131
return true;
132132
}
133133

134-
public ISymbol? GetItIsType(ISymbol? symbol)
134+
public ITypeSymbol? GetItIsType(ISymbol? symbol)
135135
{
136136
if (symbol is not IMethodSymbol methodSymbol)
137137
{
@@ -164,7 +164,7 @@ public bool IsItIsAny(ISymbol? symbol)
164164
return true;
165165
}
166166

167-
public ISymbol? GetItIsAnyType(ISymbol? symbol)
167+
public ITypeSymbol? GetItIsAnyType(ISymbol? symbol)
168168
{
169169
if (symbol is not IMethodSymbol methodSymbol)
170170
{

tests/Moq.Analyzers.Tests/Analyzers/ItArgumentsMustMatchMockedMethodArgumentsAnalyzerTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ public class TestClass
2626
public void TestMethod()
2727
{
2828
var mock1 = new Mock<I>();
29+
2930
mock1.Setup(m => m.NoParametersMethod());
3031
mock1.Setup(m => m.TestMethod(It.IsAny<int?>(), 1, ""Ignored"", C.OtherMethod()));
3132
mock1.Setup(m => m.TestMethod(It.Is<int?>(p => p == 10), 1, ""Ignored"", C.OtherMethod()));
3233
mock1.Setup(m => m.TestMethod(It.Is<int?>(10, EqualityComparer<int?>.Default), 1, ""Ignored"", C.OtherMethod()));
34+
35+
mock1.Setup(m => m.TestMethodWithBaseClass(null));
36+
mock1.Setup(m => m.TestMethodWithBaseClass(It.IsAny<BaseClass>()));
37+
mock1.Setup(m => m.TestMethodWithBaseClass(It.IsAny<InheritedClass>()));
38+
mock1.Setup(m => m.TestMethodWithBaseClass(It.Is<BaseClass>(c => c.Property == 10)));
39+
mock1.Setup(m => m.TestMethodWithBaseClass(It.Is<InheritedClass>(c => c.Property == 10)));
3340
}
3441
}
3542
@@ -38,12 +45,23 @@ public interface I
3845
void NoParametersMethod();
3946
4047
void TestMethod(int? a, int b, string c, int d);
48+
49+
void TestMethodWithBaseClass(BaseClass a);
4150
}
4251
4352
public class C
4453
{
4554
public static int OtherMethod() => default;
4655
}
56+
57+
public abstract class BaseClass
58+
{
59+
public int Property { get; set; }
60+
}
61+
62+
public class InheritedClass : BaseClass
63+
{
64+
}
4765
}";
4866

4967
await Verifier.VerifyAnalyzerAsync(source);

0 commit comments

Comments
 (0)