1+ using System . Collections . Immutable ;
2+ using Microsoft . CodeAnalysis ;
3+ using Microsoft . CodeAnalysis . CSharp ;
4+ using Microsoft . CodeAnalysis . Diagnostics ;
5+
6+ namespace DataverseAnalyzer . Tests ;
7+
8+ public sealed class PragmaWarningDisableMA0051AnalyzerTests
9+ {
10+ [ Fact ]
11+ public async Task PragmaDisableMA0051ShouldTrigger ( )
12+ {
13+ var source = """
14+ class TestClass
15+ {
16+ #pragma warning disable MA0051
17+ public void TestMethod() { }
18+ #pragma warning restore MA0051
19+ }
20+ """ ;
21+
22+ var diagnostics = await GetDiagnosticsAsync ( source ) ;
23+ Assert . Single ( diagnostics ) ;
24+ Assert . Equal ( "CT0011" , diagnostics [ 0 ] . Id ) ;
25+ }
26+
27+ [ Fact ]
28+ public async Task PragmaDisableMA0051WithOtherWarningsShouldTrigger ( )
29+ {
30+ var source = """
31+ class TestClass
32+ {
33+ #pragma warning disable MA0051, CS0168
34+ public void TestMethod() { }
35+ #pragma warning restore MA0051, CS0168
36+ }
37+ """ ;
38+
39+ var diagnostics = await GetDiagnosticsAsync ( source ) ;
40+ Assert . Single ( diagnostics ) ;
41+ Assert . Equal ( "CT0011" , diagnostics [ 0 ] . Id ) ;
42+ }
43+
44+ [ Fact ]
45+ public async Task PragmaRestoreMA0051ShouldNotTrigger ( )
46+ {
47+ var source = """
48+ class TestClass
49+ {
50+ #pragma warning restore MA0051
51+ public void TestMethod() { }
52+ }
53+ """ ;
54+
55+ var diagnostics = await GetDiagnosticsAsync ( source ) ;
56+ Assert . Empty ( diagnostics ) ;
57+ }
58+
59+ [ Fact ]
60+ public async Task PragmaDisableOtherWarningShouldNotTrigger ( )
61+ {
62+ var source = """
63+ class TestClass
64+ {
65+ #pragma warning disable CS0168
66+ public void TestMethod() { }
67+ #pragma warning restore CS0168
68+ }
69+ """ ;
70+
71+ var diagnostics = await GetDiagnosticsAsync ( source ) ;
72+ Assert . Empty ( diagnostics ) ;
73+ }
74+
75+ [ Fact ]
76+ public async Task MultiplePragmaDisableMA0051ShouldTriggerMultipleTimes ( )
77+ {
78+ var source = """
79+ class TestClass
80+ {
81+ #pragma warning disable MA0051
82+ public void TestMethodA() { }
83+ #pragma warning restore MA0051
84+ #pragma warning disable MA0051
85+ public void TestMethodB() { }
86+ #pragma warning restore MA0051
87+ }
88+ """ ;
89+
90+ var diagnostics = await GetDiagnosticsAsync ( source ) ;
91+ Assert . Equal ( 2 , diagnostics . Length ) ;
92+ Assert . All ( diagnostics , d => Assert . Equal ( "CT0011" , d . Id ) ) ;
93+ }
94+
95+ private static async Task < Diagnostic [ ] > GetDiagnosticsAsync ( string source )
96+ {
97+ var syntaxTree = CSharpSyntaxTree . ParseText ( source , new CSharpParseOptions ( LanguageVersion . Latest ) ) ;
98+ var references = new List < MetadataReference >
99+ {
100+ MetadataReference . CreateFromFile ( typeof ( object ) . Assembly . Location ) ,
101+ } ;
102+
103+ var compilation = CSharpCompilation . Create (
104+ "TestAssembly" ,
105+ new [ ] { syntaxTree } ,
106+ references ,
107+ new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) ) ;
108+
109+ var analyzer = new PragmaWarningDisableMA0051Analyzer ( ) ;
110+ var compilationWithAnalyzers = compilation . WithAnalyzers ( ImmutableArray . Create < DiagnosticAnalyzer > ( analyzer ) ) ;
111+
112+ var diagnostics = await compilationWithAnalyzers . GetAnalyzerDiagnosticsAsync ( ) ;
113+ return diagnostics . Where ( d => d . Id == "CT0011" ) . ToArray ( ) ;
114+ }
115+ }
0 commit comments