Skip to content

Commit 7f88c4a

Browse files
rjmurilloclaude
andauthored
test: add unit tests for 16 untested Common extension methods (#898)
## Summary - Add 53 new unit tests covering 16 previously untested methods across 8 Common extension classes - New test files for IMethodSymbolExtensions, ITypeSymbolExtensions, MoqKnownSymbolExtensions, SyntaxNodeExtensions, InvocationExpressionSyntaxExtensions - Expanded existing tests for ISymbolExtensions, MoqVerificationHelpers, SemanticModelExtensions - All tests use Roslyn compilation for accurate semantic analysis ## Test plan - [x] CI pedantic build passes (0 warnings, 0 errors) - [x] All 2328 tests pass locally (60 new tests) - [ ] CI pipeline passes all checks Closes #897 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Expanded unit test coverage across symbol, type, invocation, syntax and verification helpers, adding numerous new scenarios for overloads, parameter/type matching, raises/verify behaviors and Moq-specific detection. * **Chores** * Centralized and simplified test compilation/setup helpers to streamline in-memory compilations, LINQ and Moq-enabled test flows. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cdc195c commit 7f88c4a

13 files changed

Lines changed: 1624 additions & 263 deletions

tests/Moq.Analyzers.Test/Common/DiagnosticExtensionsTests.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ namespace Moq.Analyzers.Test.Common;
44

55
public class DiagnosticExtensionsTests
66
{
7-
private static readonly MetadataReference CorlibReference;
8-
private static readonly MetadataReference SystemRuntimeReference;
9-
107
#pragma warning disable RS2008 // Enable analyzer release tracking (test-only descriptor)
118
#pragma warning disable ECS1300 // Test-only descriptor; inline init is simpler than static constructor
129
private static readonly DiagnosticDescriptor TestRule = new(
@@ -19,17 +16,6 @@ public class DiagnosticExtensionsTests
1916
#pragma warning restore ECS1300
2017
#pragma warning restore RS2008
2118

22-
#pragma warning disable S3963 // Static fields should be initialized inline - conflicts with ECS1300
23-
static DiagnosticExtensionsTests()
24-
{
25-
CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
26-
string runtimeDir = Path.GetDirectoryName(typeof(object).Assembly.Location)!;
27-
SystemRuntimeReference = MetadataReference.CreateFromFile(Path.Combine(runtimeDir, "System.Runtime.dll"));
28-
}
29-
#pragma warning restore S3963
30-
31-
private static MetadataReference[] CoreReferences => [CorlibReference, SystemRuntimeReference];
32-
3319
// Overload #1: SyntaxNode + rule + messageArgs
3420
[Fact]
3521
public void CreateDiagnostic_FromSyntaxNode_Basic()
@@ -169,7 +155,7 @@ public void CreateDiagnostic_FromOperation_DelegatesToSyntax()
169155
CSharpCompilation compilation = CSharpCompilation.Create(
170156
"Test",
171157
new[] { tree },
172-
CoreReferences,
158+
CompilationHelper.CoreReferences,
173159
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
174160
SemanticModel model = compilation.GetSemanticModel(tree);
175161
MethodDeclarationSyntax methodDecl = root.DescendantNodes().OfType<MethodDeclarationSyntax>().First();
@@ -189,7 +175,7 @@ public void CreateDiagnostic_FromOperation_WithProperties()
189175
CSharpCompilation compilation = CSharpCompilation.Create(
190176
"Test",
191177
new[] { tree },
192-
CoreReferences,
178+
CompilationHelper.CoreReferences,
193179
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
194180
SemanticModel model = compilation.GetSemanticModel(tree);
195181
MethodDeclarationSyntax methodDecl = root.DescendantNodes().OfType<MethodDeclarationSyntax>().First();
@@ -210,7 +196,7 @@ public void CreateDiagnostic_FromOperation_WithAdditionalLocationsAndProperties_
210196
CSharpCompilation compilation = CSharpCompilation.Create(
211197
"Test",
212198
new[] { tree },
213-
CoreReferences,
199+
CompilationHelper.CoreReferences,
214200
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
215201
SemanticModel model = compilation.GetSemanticModel(tree);
216202
MethodDeclarationSyntax methodDecl = root.DescendantNodes().OfType<MethodDeclarationSyntax>().First();

tests/Moq.Analyzers.Test/Common/EventSyntaxExtensionsTests.cs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@ namespace Moq.Analyzers.Test.Common;
44

55
public class EventSyntaxExtensionsTests
66
{
7-
private static readonly MetadataReference CorlibReference;
8-
private static readonly MetadataReference SystemRuntimeReference;
9-
10-
#pragma warning disable S3963 // "static fields" should be initialized inline - conflicts with ECS1300
11-
static EventSyntaxExtensionsTests()
12-
{
13-
CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
14-
string runtimeDir = Path.GetDirectoryName(typeof(object).Assembly.Location)!;
15-
SystemRuntimeReference = MetadataReference.CreateFromFile(Path.Combine(runtimeDir, "System.Runtime.dll"));
16-
}
17-
#pragma warning restore S3963
18-
19-
private static MetadataReference[] CoreReferences => [CorlibReference, SystemRuntimeReference];
20-
217
[Fact]
228
public void GetEventParameterTypes_ActionDelegate_ReturnsTypeArguments()
239
{
@@ -112,7 +98,7 @@ class C
11298
{
11399
int[] Field;
114100
}";
115-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
101+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
116102
VariableDeclaratorSyntax fieldSyntax = tree.GetRoot()
117103
.DescendantNodes().OfType<VariableDeclaratorSyntax>().First();
118104
IFieldSymbol field = (IFieldSymbol)model.GetDeclaredSymbol(fieldSyntax)!;
@@ -204,7 +190,7 @@ class C
204190
{
205191
int[] Field;
206192
}";
207-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
193+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
208194
KnownSymbols knownSymbols = new KnownSymbols(model.Compilation);
209195
VariableDeclaratorSyntax fieldSyntax = tree.GetRoot()
210196
.DescendantNodes().OfType<VariableDeclaratorSyntax>().First();
@@ -227,7 +213,7 @@ void M()
227213
}
228214
void SomeMethod() {}
229215
}";
230-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
216+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
231217
InvocationExpressionSyntax invocation = tree.GetRoot()
232218
.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
233219

@@ -255,7 +241,7 @@ void M()
255241
}
256242
void SomeMethod(int x) {}
257243
}";
258-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
244+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
259245
InvocationExpressionSyntax invocation = tree.GetRoot()
260246
.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
261247

@@ -283,7 +269,7 @@ void M()
283269
}
284270
void SomeMethod(int x) {}
285271
}";
286-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
272+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
287273
InvocationExpressionSyntax invocation = tree.GetRoot()
288274
.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
289275

@@ -319,7 +305,7 @@ void SomeMethod(int selector) {}
319305
class C { event MyDelegate MyEvent; }",
320306
"MyEvent");
321307

322-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
308+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
323309
InvocationExpressionSyntax invocation = tree.GetRoot()
324310
.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
325311

@@ -354,7 +340,7 @@ void SomeMethod(int selector, int a, string b) {}
354340
class C { event MyDelegate MyEvent; }",
355341
"MyEvent");
356342

357-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
343+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
358344
InvocationExpressionSyntax invocation = tree.GetRoot()
359345
.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
360346

@@ -382,7 +368,7 @@ void M()
382368
}
383369
void SomeMethod() {}
384370
}";
385-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
371+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
386372
KnownSymbols knownSymbols = new KnownSymbols(model.Compilation);
387373
InvocationExpressionSyntax invocation = tree.GetRoot()
388374
.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
@@ -412,7 +398,7 @@ void M()
412398
}
413399
void SomeMethod(int x) {}
414400
}";
415-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
401+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
416402
KnownSymbols knownSymbols = new KnownSymbols(model.Compilation);
417403
InvocationExpressionSyntax invocation = tree.GetRoot()
418404
.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
@@ -450,7 +436,7 @@ void SomeMethod(int selector, int a) {}
450436
class C { event MyDelegate MyEvent; }",
451437
"MyEvent");
452438

453-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
439+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
454440
KnownSymbols knownSymbols = new KnownSymbols(model.Compilation);
455441
InvocationExpressionSyntax invocation = tree.GetRoot()
456442
.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
@@ -474,22 +460,10 @@ class C { event MyDelegate MyEvent; }",
474460
// and RaisesEventArgumentsShouldMatchEventSignatureAnalyzerTests, which exercise all
475461
// branching logic (too few args, too many args, wrong type, matching types, with/without
476462
// eventName).
477-
private static (SemanticModel Model, SyntaxTree Tree) CreateCompilation(string code)
478-
{
479-
SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
480-
CSharpCompilation compilation = CSharpCompilation.Create(
481-
"TestAssembly",
482-
new[] { tree },
483-
CoreReferences,
484-
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
485-
SemanticModel model = compilation.GetSemanticModel(tree);
486-
return (model, tree);
487-
}
488-
489463
#pragma warning disable ECS0900 // Boxing needed to cast to IEventSymbol from GetDeclaredSymbol
490464
private static ITypeSymbol GetEventFieldType(string code, string eventName)
491465
{
492-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
466+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
493467
VariableDeclaratorSyntax variable = tree.GetRoot()
494468
.DescendantNodes()
495469
.OfType<VariableDeclaratorSyntax>()
@@ -503,7 +477,7 @@ private static (ITypeSymbol EventType, KnownSymbols KnownSymbols) GetEventFieldT
503477
string code,
504478
string eventName)
505479
{
506-
(SemanticModel model, SyntaxTree tree) = CreateCompilation(code);
480+
(SemanticModel model, SyntaxTree tree) = CompilationHelper.CreateCompilation(code);
507481
KnownSymbols knownSymbols = new KnownSymbols(model.Compilation);
508482
VariableDeclaratorSyntax variable = tree.GetRoot()
509483
.DescendantNodes()

0 commit comments

Comments
 (0)