2
// AccessToModifiedClosureTests.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using ICSharpCode.NRefactory.CSharp.CodeActions;
28
using ICSharpCode.NRefactory.CSharp.Refactoring;
29
using NUnit.Framework;
31
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
34
public class AccessToModifiedClosureTests : InspectionActionTestBase
36
static void Test(string input, int issueCount, string output = null, int fixIndex = -1)
38
TestRefactoringContext context;
39
var issues = GetIssues (new AccessToModifiedClosureIssue (), input, out context);
40
Assert.AreEqual (issueCount, issues.Count);
44
CheckFix (context, issues, output);
46
CheckFix (context, issues [fixIndex], output);
48
foreach (var issue in issues)
49
Assert.AreEqual (0, issue.Actions.Count);
55
public void TestForeachVariable ()
60
void TestMethod (int[] a)
62
System.Func<int, int> f;
64
f = new System.Func<int, int> (x => x + i);
70
void TestMethod (int[] a)
72
System.Func<int, int> f;
73
foreach (var i in a) {
75
f = new System.Func<int, int> (x => x + i1);
79
Test (input, 1, output);
83
public void TestFor ()
90
for (int i = 0; i < 10; i++) {
91
var f = new System.Func<int, int> (x => x + i);
100
for (int i = 0; i < 10; i++) {
102
var f = new System.Func<int, int> (x => x + i1);
106
Test (input, 1, output);
110
public void TestForInitializer ()
115
void TestMethod (int j)
118
for (i = 0; j < 10; j++) {
119
var f = new System.Func<int, int> (x => x + i);
127
public void TestForBody ()
134
for (int i = 0; i < 10;) {
135
var f = new System.Func<int, int> (delegate (int x) { return x + i; });
145
for (int i = 0; i < 10;) {
147
var f = new System.Func<int, int> (delegate (int x) { return x + i1; });
152
Test (input, 1, output);
156
public void TestWhileBody ()
166
var f = new System.Func<int, int> (x => x + i);
179
var f = new System.Func<int, int> (x => x + i1);
183
Test (input, 1, output);
187
public void TestWhileCondition ()
196
var f = new System.Func<int, int> (delegate (int x) { return x + i; });
208
var f = new System.Func<int, int> (delegate (int x) { return x + i1; });
212
Test (input, 1, output);
216
public void TestDoWhileBody ()
226
var f = new System.Func<int, int> (x => x + i);
239
var f = new System.Func<int, int> (x => x + i1);
243
Test (input, 1, output);
247
public void TestDoWhileCondition ()
256
var f = new System.Func<int, int> (x => x + i);
268
var f = new System.Func<int, int> (x => x + i1);
272
Test (input, 1, output);
276
public void TestMultipleLambdas ()
281
void TestMethod (int[] a)
283
foreach (var i in a) {
284
var f = new System.Func<int, int> (x => x + i);
285
var f2 = new System.Func<int, bool> (x => x + i > 10);
292
void TestMethod (int[] a)
294
foreach (var i in a) {
296
var f = new System.Func<int, int> (x => x + i1);
298
var f2 = new System.Func<int, bool> (x => x + i1 > 10);
302
Test (input, 2, output);
306
public void TestFixAllInLambda ()
311
void TestMethod (int[] array)
313
foreach (var i in array) {
314
var f = new System.Func<int, int> (x => {
325
void TestMethod (int[] array)
327
foreach (var i in array) {
329
var f = new System.Func<int, int> (x => {
337
Test (input, 3, output, 0);
341
public void TestModifiedInLambda ()
346
void TestMethod (int[] array)
348
foreach (var i in array) {
349
var f = new System.Func<int, int> (x => {
362
void TestMethod (int[] array)
364
foreach (var i in array) {
365
var f = new System.Func<int, int> (x => {
378
void TestMethod (int[] array)
380
foreach (var i in array) {
382
var f = new System.Func<int, int> (x => {
392
Test (input, 1, output);
396
public void TestNestedLambda ()
401
void TestMethod (int[] a)
403
foreach (var i in a) {
404
var f = new System.Func<int, int> (x => {
405
var f2 = new System.Func<int, int> (y => y - i);
414
void TestMethod (int[] a)
416
foreach (var i in a) {
418
var f = new System.Func<int, int> (x => {
419
var f2 = new System.Func<int, int> (y => y - i1);
425
Test (input, 2, output1, 1);
429
void TestMethod (int[] a)
431
foreach (var i in a) {
432
var f = new System.Func<int, int> (x => {
434
var f2 = new System.Func<int, int> (y => y - i1);
440
Test (input, 2, output2, 0);
444
public void TestMultipleVariables ()
449
void TestMethod (int[] a)
451
foreach (var i in a) {
452
foreach (var j in a) {
453
var f = new System.Func<int, int> (x => x + i + j);
461
void TestMethod (int[] a)
463
foreach (var i in a) {
464
foreach (var j in a) {
467
var f = new System.Func<int, int> (x => x + i1 + j1);
472
Test (input, 2, output);
476
public void TestModificationAfterLambdaDecl ()
484
System.Func<int, int> f = x => i + x;
495
System.Func<int, int> f = x => i1 + x;
499
Test (input, 1, output);
503
public void TestModificationBeforeLambdaDecl ()
512
System.Func<int, int> f = x => i + x;
519
public void TestUnreachable ()
527
System.Func<int, int> f = x => i + x;
538
System.Func<int, int> f;
545
Test (returnCase, 0);
550
public void TestParameter ()
555
void TestMethod (int i)
557
System.Func<int, int> f = x => i + x;
564
void TestMethod (int i)
567
System.Func<int, int> f = x => i1 + x;
571
Test (input, 1, output);
575
public void TestModificationInSameStatement ()
580
void TestMethod2 (int b, System.Func<int, int> a)
582
TestMethod2 (b++, c => c + b);
588
void TestMethod3 (System.Func<int, int> a, int b)
590
TestMethod3 (c => c + b, b++);
596
void TestMethod3 (System.Func<int, int> a, int b)
599
TestMethod3 (c => c + b1, b++);
603
Test (input2, 1, output2);
607
public void TestInsertion ()
615
System.Func<int, int> f = null;
616
if ((f = x => x + i) != null)
626
System.Func<int, int> f = null;
628
if ((f = x => x + i1) != null)
632
Test (input1, 1, output1);
637
void TestMethod (int k)
640
System.Func<int, int> f = null;
652
void TestMethod (int k)
655
System.Func<int, int> f = null;
665
Test (input2, 1, output2);
669
public void TestLambdaInLoopCondition ()
676
System.Func<int, int> f = null;
677
for (int i = 0; i < 10 && ((f = x => x + i) != null); i++) {
686
System.Func<int, int> f = null;
688
while (i < 10 && (f = x => x + i) != null) i++;
696
System.Func<int, int> f = null;
698
do { i++; } while (i < 10 && (f = x => x + i) != null);
703
Test (doWhileCase, 1);
707
public void TestLambdaInForIterator ()
715
System.Func<int, int> f = null;
716
for (int i = 0; i < 10; i++, f = x => x + i) {
724
public void TestExistingVariableName ()
729
void TestMethod (int[] a)
731
foreach (var i in a) {
733
var f = new System.Func<int, int> (x => x + i);
740
void TestMethod (int[] a)
742
foreach (var i in a) {
745
var f = new System.Func<int, int> (x => x + i2);
749
Test (input, 1, output);
753
public void TestConstructor ()
758
public TestClass (int[] a)
760
foreach (var i in a) {
762
var f = new System.Func<int, int> (x => x + i);
769
public TestClass (int[] a)
771
foreach (var i in a) {
774
var f = new System.Func<int, int> (x => x + i2);
778
Test (input, 1, output);
782
public void TestField ()
787
System.Action<int> a = i =>
789
System.Func<int> f = () => i + 1;
796
System.Action<int> a = i =>
799
System.Func<int> f = () => i1 + 1;
803
Test (input, 1, output);