Skip to content

Commit b0e56a2

Browse files
v1.9.2 (#30)
* Fix the PosInfoMoq1003 and PosInfoMoq2003 when using InSequence() method. * Fix version number.
1 parent d639007 commit b0e56a2

6 files changed

Lines changed: 97 additions & 32 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.1
10+
default: 1.9.2
1111
VersionSuffix:
1212
type: string
1313
description: The version suffix of the library (for example rc.1)

src/Moq.Analyzers/Moq.Analyzers.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.Moq.Analyzers</PackageProjectUrl>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
1919
<PackageReleaseNotes>
20+
1.9.2
21+
- Fix the PosInfoMoq1003 to raise warnings when using InSequence() method.
22+
- Fix the PosInfoMoq2003 to raise errors when using InSequence() method.
23+
2024
1.9.1
2125
- Add new rules:
2226
- PosInfoMoq2009: Mock.Of&lt;T&gt; method must be used only to mock non-sealed class

src/Moq.Analyzers/MoqSymbols.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ internal sealed class MoqSymbols
4444
private MoqSymbols(INamedTypeSymbol mockGenericClass, Compilation compilation)
4545
{
4646
this.mockGenericClass = mockGenericClass;
47+
48+
var setupConditionResultInterface = new Lazy<INamedTypeSymbol>(() => compilation.GetTypeByMetadataName("Moq.Language.ISetupConditionResult`1")!);
49+
4750
this.mockBehaviorEnum = new Lazy<INamedTypeSymbol>(() => compilation.GetTypeByMetadataName("Moq.MockBehavior")!);
4851
this.isAnyTypeClass = new Lazy<INamedTypeSymbol>(() => compilation.GetTypeByMetadataName("Moq.It+IsAnyType")!);
4952
this.isAnyMethod = new Lazy<ISymbol>(() => compilation.GetTypeByMetadataName("Moq.It")!.GetMembers("IsAny").Single());
5053
this.verifiesInterface = new Lazy<INamedTypeSymbol>(() => compilation.GetTypeByMetadataName("Moq.Language.IVerifies")!);
5154

52-
this.setupMethods = new Lazy<IReadOnlyList<IMethodSymbol>>(() => mockGenericClass.GetMembers("Setup").OfType<IMethodSymbol>().ToArray());
55+
this.setupMethods = new Lazy<IReadOnlyList<IMethodSymbol>>(() => mockGenericClass.GetMembers("Setup").Concat(setupConditionResultInterface.Value.GetMembers("Setup")).OfType<IMethodSymbol>().ToArray());
5356
this.mockBehaviorStrictField = new Lazy<ISymbol>(() => this.mockBehaviorEnum.Value.GetMembers("Strict").First());
5457
this.setupProtectedMethods = new Lazy<IReadOnlyList<IMethodSymbol>>(() => compilation.GetTypeByMetadataName("Moq.Protected.IProtectedMock`1")!.GetMembers("Setup").OfType<IMethodSymbol>().ToArray());
5558
this.asMethod = new Lazy<ISymbol>(() => mockGenericClass.GetMembers("As").Single());

tests/Moq.Analyzers.Tests/Analyzers/CallBackDelegateMustMatchMockedMethodAnalyzerTest.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ namespace PosInformatique.Moq.Analyzers.Tests
1010

1111
public class CallBackDelegateMustMatchMockedMethodAnalyzerTest
1212
{
13-
[Fact]
14-
public async Task CallBackSignatureMatch_NoDiagnosticReported()
13+
[Theory]
14+
[InlineData("")]
15+
[InlineData(".InSequence(sequence)")]
16+
public async Task CallBackSignatureMatch_NoDiagnosticReported(string sequence)
1517
{
1618
var source = @"
1719
namespace ConsoleApplication1
@@ -23,31 +25,33 @@ public class TestClass
2325
{
2426
public void TestMethod()
2527
{
28+
var sequence = new MockSequence();
29+
2630
var mock1 = new Mock<I>();
2731
28-
mock1.Setup(m => m.TestMethod())
32+
mock1" + sequence + @".Setup(m => m.TestMethod())
2933
.Callback(() => { })
3034
.Throws(new Exception());
31-
mock1.Setup(m => m.TestMethod(default))
35+
mock1" + sequence + @".Setup(m => m.TestMethod(default))
3236
.Callback((string x) => { })
3337
.Throws(new Exception());
34-
mock1.Setup(m => m.TestMethod(default, default))
38+
mock1" + sequence + @".Setup(m => m.TestMethod(default, default))
3539
.Callback((string x, int y) => { })
3640
.Throws(new Exception());
37-
mock1.Setup(m => m.TestGenericMethod(1234))
41+
mock1" + sequence + @".Setup(m => m.TestGenericMethod(1234))
3842
.Callback((int x) => { })
3943
.Throws(new Exception());
40-
mock1.Setup(m => m.TestGenericMethod(It.IsAny<It.IsAnyType>()))
44+
mock1" + sequence + @".Setup(m => m.TestGenericMethod(It.IsAny<It.IsAnyType>()))
4145
.Callback((object x) => { })
4246
.Throws(new Exception());
4347
44-
mock1.Setup(m => m.TestMethodReturn())
48+
mock1" + sequence + @".Setup(m => m.TestMethodReturn())
4549
.Callback(() => { })
4650
.Returns(1234);
47-
mock1.Setup(m => m.TestMethodReturn(default))
51+
mock1" + sequence + @".Setup(m => m.TestMethodReturn(default))
4852
.Callback((string x) => { })
4953
.Returns(1234);
50-
mock1.Setup(m => m.TestMethodReturn(default, default))
54+
mock1" + sequence + @".Setup(m => m.TestMethodReturn(default, default))
5155
.Callback((string x, int y) => { })
5256
.Returns(1234);
5357
@@ -80,8 +84,10 @@ public interface I
8084
await Verifier.VerifyAnalyzerAsync(source);
8185
}
8286

83-
[Fact]
84-
public async Task CallBackSignatureNotMatch_DiagnosticReported()
87+
[Theory]
88+
[InlineData("")]
89+
[InlineData(".InSequence(sequence)")]
90+
public async Task CallBackSignatureNotMatch_DiagnosticReported(string sequence)
8591
{
8692
var source = @"
8793
namespace ConsoleApplication1
@@ -93,37 +99,39 @@ public class TestClass
9399
{
94100
public void TestMethod()
95101
{
102+
var sequence = new MockSequence();
103+
96104
var mock1 = new Mock<I>();
97105
98-
mock1.Setup(m => m.TestMethod())
106+
mock1" + sequence + @".Setup(m => m.TestMethod())
99107
.Callback([|(int too, int much, int parameters)|] => { })
100108
.Throws(new Exception());
101-
mock1.Setup(m => m.TestMethod(default))
109+
mock1" + sequence + @".Setup(m => m.TestMethod(default))
102110
.Callback([|()|] => { })
103111
.Throws(new Exception());
104-
mock1.Setup(m => m.TestMethod(default))
112+
mock1" + sequence + @".Setup(m => m.TestMethod(default))
105113
.Callback(([|int otherType|]) => { })
106114
.Throws(new Exception());
107-
mock1.Setup(m => m.TestMethod(default))
115+
mock1" + sequence + @".Setup(m => m.TestMethod(default))
108116
.Callback([|(int too, int much, int parameters)|] => { })
109117
.Throws(new Exception());
110-
mock1.Setup(m => m.TestGenericMethod(1234))
118+
mock1" + sequence + @".Setup(m => m.TestGenericMethod(1234))
111119
.Callback(([|string x|]) => { })
112120
.Throws(new Exception());
113-
mock1.Setup(m => m.TestGenericMethod(It.IsAny<It.IsAnyType>()))
121+
mock1" + sequence + @".Setup(m => m.TestGenericMethod(It.IsAny<It.IsAnyType>()))
114122
.Callback(([|string x|]) => { })
115123
.Throws(new Exception());
116124
117-
mock1.Setup(m => m.TestMethodReturn())
125+
mock1" + sequence + @".Setup(m => m.TestMethodReturn())
118126
.Callback([|(int too, int much, int parameters)|] => { })
119127
.Returns(1234);
120-
mock1.Setup(m => m.TestMethodReturn(default))
128+
mock1" + sequence + @".Setup(m => m.TestMethodReturn(default))
121129
.Callback([|()|] => { })
122130
.Returns(1234);
123-
mock1.Setup(m => m.TestMethodReturn(default))
131+
mock1" + sequence + @".Setup(m => m.TestMethodReturn(default))
124132
.Callback(([|int otherType|]) => { })
125133
.Returns(1234);
126-
mock1.Setup(m => m.TestMethodReturn(default))
134+
mock1" + sequence + @".Setup(m => m.TestMethodReturn(default))
127135
.Callback([|(int too, int much, int parameters)|] => { })
128136
.Returns(1234);
129137
}

tests/Moq.Analyzers.Tests/Analyzers/CallBackDelegateShouldBeUsedWithItIsAnyParametersAnalyzerTest.cs

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ await Verifier.VerifyAnalyzerAsync(
5757
}
5858

5959
[Fact]
60-
public async Task Callback_NoDiagnosticReported()
60+
public async Task NoCallback_DiagnosticReported_WithSequence()
6161
{
6262
var source = @"
6363
namespace ConsoleApplication1
@@ -69,21 +69,72 @@ public class TestClass
6969
{
7070
public void TestMethod()
7171
{
72+
var sequence = new MockSequence();
73+
74+
var mock1 = new Mock<I>();
75+
mock1.InSequence(sequence).Setup(m => m.TestMethod({|#0:It.IsAny<string>()|#0}, {|#1:It.IsAny<int>()|#1}));
76+
mock1.InSequence(sequence).Setup(m => m.TestMethod(""Ignored"", {|#2:It.IsAny<int>()|#2}));
77+
mock1.InSequence(sequence).Setup(m => m.TestMethod({|#3:It.IsAny<string>()|#3}, 1234));
78+
mock1.InSequence(sequence).Setup(m => m.TestMethod({|#4:It.IsAny<string>()|#4}));
79+
}
80+
}
81+
82+
public interface I
83+
{
84+
void TestMethod(string a);
85+
86+
void TestMethod(string a, int b);
87+
}
88+
}";
89+
90+
await Verifier.VerifyAnalyzerAsync(
91+
source,
92+
[
93+
new DiagnosticResult(CallBackDelegateShouldBeUsedWithItIsAnyParametersAnalyzer.Rule)
94+
.WithSpan(14, 80, 14, 98).WithArguments("a"),
95+
new DiagnosticResult(CallBackDelegateShouldBeUsedWithItIsAnyParametersAnalyzer.Rule)
96+
.WithSpan(14, 100, 14, 115).WithArguments("b"),
97+
new DiagnosticResult(CallBackDelegateShouldBeUsedWithItIsAnyParametersAnalyzer.Rule)
98+
.WithSpan(15, 91, 15, 106).WithArguments("b"),
99+
new DiagnosticResult(CallBackDelegateShouldBeUsedWithItIsAnyParametersAnalyzer.Rule)
100+
.WithSpan(16, 80, 16, 98).WithArguments("a"),
101+
new DiagnosticResult(CallBackDelegateShouldBeUsedWithItIsAnyParametersAnalyzer.Rule)
102+
.WithSpan(17, 80, 17, 98).WithArguments("a"),
103+
]);
104+
}
105+
106+
[Theory]
107+
[InlineData("")]
108+
[InlineData(".InSequence(sequence)")]
109+
public async Task Callback_NoDiagnosticReported(string sequence)
110+
{
111+
var source = @"
112+
namespace ConsoleApplication1
113+
{
114+
using Moq;
115+
using System;
116+
117+
public class TestClass
118+
{
119+
public void TestMethod()
120+
{
121+
var sequence = new MockSequence();
122+
72123
var mock1 = new Mock<I>();
73-
mock1.Setup(m => m.TestMethod(It.IsAny<string>()))
124+
mock1" + sequence + @".Setup(m => m.TestMethod(It.IsAny<string>()))
74125
.Callback(() => { });
75-
mock1.Setup(m => m.TestMethod(It.IsAny<string>(), It.IsAny<int>()))
126+
mock1" + sequence + @".Setup(m => m.TestMethod(It.IsAny<string>(), It.IsAny<int>()))
76127
.Callback(() => { });
77-
mock1.Setup(m => m.TestMethod(""OK"", It.IsAny<int>()))
128+
mock1" + sequence + @".Setup(m => m.TestMethod(""OK"", It.IsAny<int>()))
78129
.Callback(() => { });
79-
mock1.Setup(m => m.TestMethod(It.IsAny<string>(), 1234))
130+
mock1" + sequence + @".Setup(m => m.TestMethod(It.IsAny<string>(), 1234))
80131
.Callback(() => { });
81132
82133
var mock2 = new Mock<I>();
83-
mock2.Setup(m => m.TestMethod());
134+
mock2" + sequence + @".Setup(m => m.TestMethod());
84135
85136
var mock3 = new Mock<I>();
86-
mock3.Setup(m => m.TestMethod(""OK"", 1234));
137+
mock3" + sequence + @".Setup(m => m.TestMethod(""OK"", 1234));
87138
88139
var o = new object();
89140
o.ToString(); // Ignored

tests/Moq.Analyzers.Tests/Analyzers/ConstructorArgumentsMustMatchAnalyzerTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ private C(int a, object b, int c, System.IDisposable d)
208208
await Verifier.VerifyAnalyzerAsync(source);
209209
}
210210

211-
212211
[Theory]
213212
[InlineData("class")]
214213
[InlineData("abstract class")]

0 commit comments

Comments
 (0)