Skip to content

Commit d909e5a

Browse files
committed
add some more test coverage
1 parent eaa336e commit d909e5a

File tree

2 files changed

+124
-35
lines changed

2 files changed

+124
-35
lines changed

src/ProgressOnderwijsUtils.Analyzers/IfNotFalseCodeFix.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.CodeAnalysis.CodeActions;
77
using Microsoft.CodeAnalysis.CodeFixes;
88
using Microsoft.CodeAnalysis.CSharp.Syntax;
9+
using Microsoft.CodeAnalysis.Formatting;
910

1011
namespace ProgressOnderwijsUtils.Analyzers;
1112

@@ -51,15 +52,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5152
);
5253
}
5354

54-
private static SyntaxNode ReplaceWithBlockStatements(SyntaxNode root, IfStatementSyntax ifStatement, BlockSyntax block)
55+
static SyntaxNode ReplaceWithBlockStatements(SyntaxNode root, IfStatementSyntax ifStatement, BlockSyntax block)
5556
{
56-
var statements = block.Statements;
57-
var firstStatement = statements[0]
58-
.WithLeadingTrivia(ifStatement.GetLeadingTrivia())
59-
.WithTrailingTrivia(block.GetTrailingTrivia());
57+
var formattedStatements = block.Statements
58+
.Select(s => s.WithAdditionalAnnotations(Formatter.Annotation))
59+
.ToArray();
6060

61-
var newStatements = statements.Replace(statements[0], firstStatement);
62-
63-
return root.ReplaceNode(ifStatement, newStatements);
61+
return root.ReplaceNode(ifStatement, formattedStatements);
6462
}
6563
}

test/ProgressOnderwijsUtils.Analyzers.Tests/IfNotFalseAnalyzerTest.cs

Lines changed: 118 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,125 @@ void M()
4343
[Fact]
4444
public async Task CodeFix_RemovesIfNotFalse()
4545
{
46-
var source = """
47-
class C
48-
{
49-
void M()
50-
{
51-
if (!false) { System.Console.WriteLine("Hello"); }
52-
}
53-
}
54-
""";
55-
var expected = """
56-
class C
57-
{
58-
void M()
59-
{
60-
System.Console.WriteLine("Hello");
61-
}
62-
}
63-
""";
46+
var testCases = new[] {
47+
new {
48+
Source = """
49+
class C
50+
{
51+
void M()
52+
{
53+
if (!false) { System.Console.WriteLine("Hello"); }
54+
}
55+
}
56+
""",
57+
Expected = """
58+
class C
59+
{
60+
void M()
61+
{
62+
System.Console.WriteLine("Hello");
63+
}
64+
}
65+
""",
66+
},
67+
new {
68+
Source = """
69+
class C
70+
{
71+
void M()
72+
{
73+
if (!false) { System.Console.WriteLine("A"); System.Console.WriteLine("B"); }
74+
}
75+
}
76+
""",
77+
Expected = """
78+
class C
79+
{
80+
void M()
81+
{
82+
System.Console.WriteLine("A"); System.Console.WriteLine("B");
83+
}
84+
}
85+
""",
86+
},
87+
new {
88+
Source = """
89+
class C
90+
{
91+
void M()
92+
{
93+
if (!false)
94+
{
95+
System.Console.WriteLine("Hello");
96+
}
97+
}
98+
}
99+
""",
100+
Expected = """
101+
class C
102+
{
103+
void M()
104+
{
105+
System.Console.WriteLine("Hello");
106+
}
107+
}
108+
""",
109+
},
110+
new {
111+
Source = """
112+
class C
113+
{
114+
void M()
115+
{
116+
if (!false) { }
117+
}
118+
}
119+
""",
120+
Expected = """
121+
class C
122+
{
123+
void M()
124+
{
125+
}
126+
}
127+
""",
128+
},
129+
new {
130+
Source = """
131+
class C
132+
{
133+
void M()
134+
{
135+
if (!false)
136+
{
137+
System.Console.WriteLine("A");
138+
System.Console.WriteLine("B");
139+
}
140+
}
141+
}
142+
""",
143+
Expected = """
144+
class C
145+
{
146+
void M()
147+
{
148+
System.Console.WriteLine("A");
149+
System.Console.WriteLine("B");
150+
}
151+
}
152+
""",
153+
},
154+
};
64155

65-
var workspace = DiagnosticHelper.CreateProjectWithTestFile(source);
66-
var diagnostic = DiagnosticHelper.GetDiagnostics(new IfNotFalseAnalyzer(), workspace).Single();
156+
foreach (var test in testCases) {
157+
var workspace = DiagnosticHelper.CreateProjectWithTestFile(test.Source);
158+
var diagnostic = DiagnosticHelper.GetDiagnostics(new IfNotFalseAnalyzer(), workspace).Single();
159+
var fixesMade = await DiagnosticHelper.ApplyAllCodeFixes(workspace, diagnostic, new IfNotFalseCodeFix());
160+
PAssert.That(() => fixesMade == 1);
67161

68-
var fixesMade = await DiagnosticHelper.ApplyAllCodeFixes(workspace, diagnostic, new IfNotFalseCodeFix());
69-
PAssert.That(() => fixesMade == 1);
70-
var result = await workspace.CurrentSolution.Projects.Single().Documents.Single().GetTextAsync();
71-
Assert.Equal(
72-
expected,
73-
result.ToString()
74-
);
162+
var result = await workspace.CurrentSolution.Projects.Single().Documents.Single().GetTextAsync();
163+
var resultText = result.ToString().Replace("\r\n", "\n");
164+
Assert.Equal(test.Expected, resultText);
165+
}
75166
}
76167
}

0 commit comments

Comments
 (0)