Skip to content

Commit 55a2992

Browse files
committed
also cover unreachable code blocks as testing cases
1 parent d909e5a commit 55a2992

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

test/ProgressOnderwijsUtils.Analyzers.Tests/IfNotFalseAnalyzerTest.cs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Threading.Tasks;
33
using ExpressionToCodeLib;
4+
using Microsoft.CodeAnalysis;
45
using Xunit;
56

67
namespace ProgressOnderwijsUtils.Analyzers.Tests;
@@ -151,11 +152,78 @@ void M()
151152
}
152153
""",
153154
},
155+
new {
156+
Source = """
157+
class C
158+
{
159+
void M()
160+
{
161+
if (!false) { System.Console.WriteLine("A"); } else { System.Console.WriteLine("B"); }
162+
}
163+
}
164+
""",
165+
Expected = """
166+
class C
167+
{
168+
void M()
169+
{
170+
System.Console.WriteLine("A");
171+
}
172+
}
173+
""",
174+
},
175+
new {
176+
Source = """
177+
class C
178+
{
179+
void M()
180+
{
181+
if (!false) { System.Console.WriteLine("A"); } else { System.Console.WriteLine("B"); }
182+
}
183+
}
184+
""",
185+
Expected = """
186+
class C
187+
{
188+
void M()
189+
{
190+
System.Console.WriteLine("A");
191+
}
192+
}
193+
""",
194+
},
195+
new {
196+
Source = """
197+
class C
198+
{
199+
void M()
200+
{
201+
if (!false)
202+
{
203+
System.Console.WriteLine("A");
204+
}
205+
else
206+
{
207+
System.Console.WriteLine("B");
208+
}
209+
}
210+
}
211+
""",
212+
Expected = """
213+
class C
214+
{
215+
void M()
216+
{
217+
System.Console.WriteLine("A");
218+
}
219+
}
220+
""",
221+
},
154222
};
155223

156224
foreach (var test in testCases) {
157225
var workspace = DiagnosticHelper.CreateProjectWithTestFile(test.Source);
158-
var diagnostic = DiagnosticHelper.GetDiagnostics(new IfNotFalseAnalyzer(), workspace).Single();
226+
var diagnostic = DiagnosticHelper.GetDiagnostics(new IfNotFalseAnalyzer(), workspace).Single(d => d.Id == IfNotFalseAnalyzer.Rule.Id); // otherwise we could get diagnostic like CS0162 (unreachable code)
159227
var fixesMade = await DiagnosticHelper.ApplyAllCodeFixes(workspace, diagnostic, new IfNotFalseCodeFix());
160228
PAssert.That(() => fixesMade == 1);
161229

0 commit comments

Comments
 (0)