1+ using Microsoft . CodeAnalysis ;
2+ using Microsoft . CodeAnalysis . CodeFixes ;
3+ using Microsoft . CodeAnalysis . Diagnostics ;
4+ using System ;
5+ using TestHelper ;
6+ using CodeCracker ;
7+ using Xunit ;
8+
9+ namespace CodeCracker . Test
10+ {
11+ public class TernaryOperatorTests : CodeFixVerifier
12+ {
13+ private const string source = @"
14+ namespace ConsoleApplication1
15+ {
16+ class TypeName
17+ {
18+ public int Foo()
19+ {
20+ var something = true;
21+ if (something)
22+ return 1;
23+ else
24+ return 2;
25+ }
26+ }
27+ }" ;
28+
29+ [ Fact ]
30+ public void WhenUsingIfWithoutElseAnalyzerDoesNotCreateDiagnostic ( )
31+ {
32+ const string sourceWithoutElse = @"
33+ namespace ConsoleApplication1
34+ {
35+ class TypeName
36+ {
37+ public int Foo()
38+ {
39+ var something = true;
40+ if (something)
41+ return 1;
42+ }
43+ }
44+ }" ;
45+ VerifyCSharpHasNoDiagnostics ( sourceWithoutElse ) ;
46+ }
47+
48+ [ Fact ]
49+ public void WhenUsingIfWithElseButWithBlockWith2StatementsOnIfAnalyzerDoesNotCreateDiagnostic ( )
50+ {
51+ const string sourceWithoutElse = @"
52+ namespace ConsoleApplication1
53+ {
54+ class TypeName
55+ {
56+ public int Foo()
57+ {
58+ var something = true;
59+ if (something)
60+ {
61+ string a = null;
62+ return 1;
63+ }
64+ else
65+ {
66+ return 2;
67+ }
68+ }
69+ }
70+ }" ;
71+ VerifyCSharpHasNoDiagnostics ( sourceWithoutElse ) ;
72+ }
73+
74+ [ Fact ]
75+ public void WhenUsingIfWithElseButWithBlockWith2StatementsOnElseAnalyzerDoesNotCreateDiagnostic ( )
76+ {
77+ const string sourceWithoutElse = @"
78+ namespace ConsoleApplication1
79+ {
80+ class TypeName
81+ {
82+ public int Foo()
83+ {
84+ var something = true;
85+ if (something)
86+ {
87+ return 1;
88+ }
89+ else
90+ {
91+ string a = null;
92+ return 2;
93+ }
94+ }
95+ }
96+ }" ;
97+ VerifyCSharpHasNoDiagnostics ( sourceWithoutElse ) ;
98+ }
99+
100+ [ Fact ]
101+ public void WhenUsingIfWithElseButWithoutReturnOnElseAnalyzerDoesNotCreateDiagnostic ( )
102+ {
103+ const string sourceWithoutElse = @"
104+ namespace ConsoleApplication1
105+ {
106+ class TypeName
107+ {
108+ public int Foo()
109+ {
110+ var something = true;
111+ if (something)
112+ {
113+ return 2;
114+ }
115+ else
116+ {
117+ string a = null;
118+ }
119+ return 1;
120+ }
121+ }
122+ }" ;
123+ VerifyCSharpHasNoDiagnostics ( sourceWithoutElse ) ;
124+ }
125+
126+ [ Fact ]
127+ public void WhenUsingIfWithElseButWithoutReturnOnIfAnalyzerDoesNotCreateDiagnostic ( )
128+ {
129+ const string sourceWithoutElse = @"
130+ namespace ConsoleApplication1
131+ {
132+ class TypeName
133+ {
134+ public int Foo()
135+ {
136+ var something = true;
137+ if (something)
138+ {
139+ string a = null;
140+ }
141+ else
142+ {
143+ return 2;
144+ }
145+ return 1;
146+ }
147+ }
148+ }" ;
149+ VerifyCSharpHasNoDiagnostics ( sourceWithoutElse ) ;
150+ }
151+
152+ [ Fact ]
153+ public void WhenUsingIfAndElseWithDirectReturnAnalyzerCreatesDiagnostic ( )
154+ {
155+ var expected = new DiagnosticResult
156+ {
157+ Id = TernaryOperatorAnalyzer . DiagnosticId ,
158+ Message = "You can use a ternary operator." ,
159+ Severity = DiagnosticSeverity . Error ,
160+ Locations =
161+ new [ ] {
162+ new DiagnosticResultLocation ( "Test0.cs" , 9 , 17 )
163+ }
164+ } ;
165+
166+ VerifyCSharpDiagnostic ( source , expected ) ;
167+ }
168+
169+ [ Fact ]
170+ public void WhenUsingIfAndElseWithDirectReturnChangeToTernaryFix ( )
171+ {
172+
173+ var fixtest = @"
174+ namespace ConsoleApplication1
175+ {
176+ class TypeName
177+ {
178+ public int Foo()
179+ {
180+ var something = true;
181+ return something ? 1 : 2;
182+ }
183+ }
184+ }" ;
185+ VerifyCSharpFix ( source , fixtest , 0 ) ;
186+ }
187+
188+ protected override CodeFixProvider GetCSharpCodeFixProvider ( )
189+ {
190+ return new TernaryOperatorCodeFixProvider ( ) ;
191+ }
192+
193+ protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer ( )
194+ {
195+ return new TernaryOperatorAnalyzer ( ) ;
196+ }
197+ }
198+ }
0 commit comments