Skip to content

Commit a485bd1

Browse files
authored
bug with expression statements fixed (#22)
* bug with expression statements fixed * variable declaration invocation fix
1 parent d5ff4cc commit a485bd1

File tree

6 files changed

+435
-134
lines changed

6 files changed

+435
-134
lines changed

src/Amusoft.CodeAnalysis.Analyzers.Test/Tests/ACA0001/DetectionTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,77 @@ public async Task EmptySourceNoAction()
2828
}
2929

3030

31+
32+
[TestMethod]
33+
public async Task RespondsToNotImplemented()
34+
{
35+
var test = @"
36+
using System;
37+
using System.Collections.Generic;
38+
using System.Linq;
39+
using System.Linq.Expressions;
40+
using System.Text;
41+
using System.Threading.Tasks;
42+
using System.Diagnostics;
43+
44+
namespace ConsoleApplication1
45+
{
46+
public interface ICustomInterface
47+
{
48+
Task Method1(object p1);
49+
}
50+
51+
class TypeName : ICustomInterface
52+
{
53+
private ICollection<ICustomInterface> _disposables;
54+
55+
public async Task Method1(object p1) => throw new NotImplementedException();
56+
}
57+
}";
58+
59+
await Verifier.VerifyAnalyzerAsync(test,
60+
new[]
61+
{
62+
// Test0.cs(21,27): info ACA0001: Forward execution of "Method1" to member "_disposables"
63+
Verifier.Diagnostic().WithSpan(21, 27, 21, 34).WithArguments("Method1", "_disposables")
64+
}
65+
);
66+
}
67+
68+
[TestMethod]
69+
public async Task DoesNotRespondToNonNotImplemented()
70+
{
71+
var testWithOtherException = @"
72+
using System;
73+
using System.Collections.Generic;
74+
using System.Linq;
75+
using System.Linq.Expressions;
76+
using System.Text;
77+
using System.Threading.Tasks;
78+
using System.Diagnostics;
79+
80+
namespace ConsoleApplication1
81+
{
82+
public interface ICustomInterface
83+
{
84+
Task Method1(object p1);
85+
}
86+
87+
class TypeName : ICustomInterface
88+
{
89+
private ICollection<ICustomInterface> _disposables;
90+
91+
public async Task Method1(object p1) => throw new Exception();
92+
}
93+
}";
94+
95+
await Verifier.VerifyAnalyzerAsync(testWithOtherException,
96+
new DiagnosticResult[]
97+
{
98+
}
99+
);
100+
}
101+
31102
[TestMethod]
32103
public async Task FieldButNoDelegation()
33104
{

src/Amusoft.CodeAnalysis.Analyzers.Test/Tests/ACA0001/FixByForwardingToCollectionChildrenTests.cs

Lines changed: 143 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void EmptyNoAction()
2222
{
2323
Verifier.VerifyCodeFixAsync(string.Empty, string.Empty);
2424
}
25+
2526
[TestMethod]
2627
public async Task CanFixTask()
2728
{
@@ -81,11 +82,137 @@ public Task Method1(object p1)
8182
{
8283
// Test0.cs(20,21): info ACA0001: Forward execution of "Method1" to member "_disposables"
8384
Verifier.Diagnostic().WithSpan(20, 21, 20, 28).WithArguments("Method1", "_disposables")
84-
};
85+
};
86+
87+
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
88+
}
89+
90+
[TestMethod]
91+
public async Task CanFixEmptyExpression()
92+
{
93+
var test = @"
94+
using System;
95+
using System.Collections.Generic;
96+
using System.Linq;
97+
using System.Linq.Expressions;
98+
using System.Text;
99+
using System.Threading.Tasks;
100+
using System.Diagnostics;
101+
102+
namespace ConsoleApplication1
103+
{
104+
public interface ICustomInterface
105+
{
106+
Task Method1(object p1);
107+
}
108+
109+
class TypeName : ICustomInterface
110+
{
111+
private ICollection<ICustomInterface> _disposables;
112+
113+
public async Task Method1(object p1) => Expression.Empty();
114+
}
115+
}";
116+
var fixtest = @"
117+
using System;
118+
using System.Collections.Generic;
119+
using System.Linq;
120+
using System.Linq.Expressions;
121+
using System.Text;
122+
using System.Threading.Tasks;
123+
using System.Diagnostics;
124+
125+
namespace ConsoleApplication1
126+
{
127+
public interface ICustomInterface
128+
{
129+
Task Method1(object p1);
130+
}
131+
132+
class TypeName : ICustomInterface
133+
{
134+
private ICollection<ICustomInterface> _disposables;
135+
136+
public async Task Method1(object p1)
137+
{
138+
await Task.WhenAll(_disposables.Select(item => item.Method1(p1)));
139+
}
140+
}
141+
}";
142+
143+
var expected = new DiagnosticResult[]
144+
{
145+
// Test0.cs(21,27): info ACA0001: Forward execution of "Method1" to member "_disposables"
146+
Verifier.Diagnostic().WithSpan(21, 27, 21, 34).WithArguments("Method1", "_disposables")
147+
};
148+
149+
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
150+
}
151+
152+
153+
[TestMethod]
154+
public async Task CanFixArrowThrow()
155+
{
156+
var test = @"
157+
using System;
158+
using System.Collections.Generic;
159+
using System.Linq;
160+
using System.Linq.Expressions;
161+
using System.Text;
162+
using System.Threading.Tasks;
163+
using System.Diagnostics;
164+
165+
namespace ConsoleApplication1
166+
{
167+
public interface ICustomInterface
168+
{
169+
Task Method1(object p1);
170+
}
171+
172+
class TypeName : ICustomInterface
173+
{
174+
private ICollection<ICustomInterface> _disposables;
175+
176+
public async Task Method1(object p1) => throw new NotImplementedException();
177+
}
178+
}";
179+
var fixtest = @"
180+
using System;
181+
using System.Collections.Generic;
182+
using System.Linq;
183+
using System.Linq.Expressions;
184+
using System.Text;
185+
using System.Threading.Tasks;
186+
using System.Diagnostics;
187+
188+
namespace ConsoleApplication1
189+
{
190+
public interface ICustomInterface
191+
{
192+
Task Method1(object p1);
193+
}
194+
195+
class TypeName : ICustomInterface
196+
{
197+
private ICollection<ICustomInterface> _disposables;
198+
199+
public async Task Method1(object p1)
200+
{
201+
await Task.WhenAll(_disposables.Select(item => item.Method1(p1)));
202+
}
203+
}
204+
}";
205+
206+
var expected = new DiagnosticResult[]
207+
{
208+
// Test0.cs(21,27): info ACA0001: Forward execution of "Method1" to member "_disposables"
209+
Verifier.Diagnostic().WithSpan(21, 27, 21, 34).WithArguments("Method1", "_disposables")
210+
};
85211

86212
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
87213
}
88214

215+
89216
[TestMethod]
90217
public async Task CanFixAsyncTask()
91218
{
@@ -144,12 +271,12 @@ public async Task Method1(object p1)
144271
{
145272
// Test0.cs(20,27): info ACA0001: Forward execution of "Method1" to member "_disposables"
146273
Verifier.Diagnostic().WithSpan(20, 27, 20, 34).WithArguments("Method1", "_disposables")
147-
};
274+
};
148275

149276
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
150277
}
151278

152-
[TestMethod]
279+
[TestMethod]
153280
public async Task CanFixBool()
154281
{
155282
var test = @"
@@ -213,8 +340,8 @@ public bool Method1(object p1)
213340

214341
await Verifier.VerifyCodeFixAsync(test, expectedDiagnostics, fixtest);
215342
}
216-
217-
[TestMethod]
343+
344+
[TestMethod]
218345
public async Task VerifyFullRewrite()
219346
{
220347
var test = @"
@@ -530,7 +657,7 @@ public void Method3()
530657
// Test0.cs(43,21): info ACA0001: Forward execution of "Method3" to member "_disposables"
531658
Verifier.Diagnostic().WithSpan(43, 21, 43, 28).WithArguments("Method3", "_disposables")
532659
};
533-
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
660+
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
534661

535662
await new CodeFixTest<Analyzer, FixByForwardingToCollectionChildren>()
536663
{
@@ -540,22 +667,22 @@ public void Method3()
540667
{
541668
Sources = {test},
542669
ExpectedDiagnostics =
543-
{
544-
// Test0.cs(24,21): info ACA0001: Forward execution of "Method1" to member "_disposables"
545-
Verifier.Diagnostic().WithSpan(24, 21, 24, 28).WithArguments("Method1", "_disposables"),
670+
{
671+
// Test0.cs(24,21): info ACA0001: Forward execution of "Method1" to member "_disposables"
672+
Verifier.Diagnostic().WithSpan(24, 21, 24, 28).WithArguments("Method1", "_disposables"),
546673
// Test0.cs(33,21): info ACA0001: Forward execution of "Method2" to member "_disposables"
547-
Verifier.Diagnostic().WithSpan(33, 21, 33, 28).WithArguments("Method2", "_disposables"),
674+
Verifier.Diagnostic().WithSpan(33, 21, 33, 28).WithArguments("Method2", "_disposables"),
548675
// Test0.cs(38,21): info ACA0001: Forward execution of "Method2" to member "_disposables"
549-
Verifier.Diagnostic().WithSpan(38, 21, 38, 28).WithArguments("Method2", "_disposables"),
676+
Verifier.Diagnostic().WithSpan(38, 21, 38, 28).WithArguments("Method2", "_disposables"),
550677
// Test0.cs(43,21): info ACA0001: Forward execution of "Method3" to member "_disposables"
551-
Verifier.Diagnostic().WithSpan(43, 21, 43, 28).WithArguments("Method3", "_disposables")
552-
},
678+
Verifier.Diagnostic().WithSpan(43, 21, 43, 28).WithArguments("Method3", "_disposables")
679+
},
553680
},
554681
FixedState =
555682
{
556683
ExpectedDiagnostics =
557684
{
558-
},
685+
},
559686
Sources = {fixtest}
560687
},
561688
}.RunAsync();
@@ -889,7 +1016,7 @@ public void Method3()
8891016
Verifier.Diagnostic().WithSpan(38, 21, 38, 28).WithArguments("Method2", "Disposables"),
8901017
// Test0.cs(43,21): info ACA0001: Forward execution of "Method3" to member "Disposables"
8911018
Verifier.Diagnostic().WithSpan(43, 21, 43, 28).WithArguments("Method3", "Disposables")
892-
};
1019+
};
8931020

8941021
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
8951022
}
@@ -1073,8 +1200,7 @@ public void Method3()
10731200
}
10741201
}
10751202
}";
1076-
10771203
}
10781204
#pragma warning restore 219
1079-
}
1205+
}
10801206
}

src/Amusoft.CodeAnalysis.Analyzers.Test/Tests/ACA0006/FixByImportingTypeAsStaticTests.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ private string GetTypeName()
125125
{
126126
// Test0.cs(16,20): info ACA0006: Import type "Helper" as static.
127127
Verifier.Diagnostic().WithSpan(16, 20, 16, 26).WithArguments("Helper")
128-
129128
},
130129
},
131130
FixedState =
@@ -338,6 +337,49 @@ class TypeName
338337
}.RunAsync();
339338
}
340339

340+
[TestMethod]
341+
public async Task DoNotShowDiagnosticsForInstanceMethodInvocations()
342+
{
343+
var test = @"
344+
using System;
345+
using System.Collections.Generic;
346+
using System.Linq;
347+
using System.Text;
348+
using System.Threading.Tasks;
349+
using System.Diagnostics;
350+
using ConsoleApplication1;
351+
352+
namespace ConsoleApplication1
353+
{
354+
class TypeName
355+
{
356+
TypeName()
357+
{
358+
var someText = ""something"";
359+
var bla = someText.ToString();
360+
}
361+
}
362+
}";
363+
364+
await new CodeFixTest<StaticImportAnalyzer, FixByImportingTypeAsStatic>()
365+
{
366+
CompilerDiagnostics = CompilerDiagnostics.Errors,
367+
TestState =
368+
{
369+
Sources = {test, LibraryFileCandidate},
370+
ExpectedDiagnostics =
371+
{
372+
373+
},
374+
},
375+
FixedState =
376+
{
377+
Sources = {test, LibraryFileCandidate},
378+
},
379+
}.RunAsync();
380+
}
381+
382+
341383

342384
[TestMethod]
343385
public async Task DoNotRewriteIfLessThan5Methods()

0 commit comments

Comments
 (0)