@@ -4,20 +4,6 @@ namespace Moq.Analyzers.Test.Common;
44
55public 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) {}
319305class 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) {}
354340class 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) {}
450436class 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