2
// MultipleEnumerationIssueTests.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.Refactoring;
28
using NUnit.Framework;
30
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
33
public class MultipleEnumerationIssueTests : InspectionActionTestBase
36
public void TestVariableInvocation ()
39
using System.Collections.Generic;
45
IEnumerable<object> e = null;
46
var type = e.GetType();
51
Test<MultipleEnumerationIssue> (input, 2);
55
public void TestVariableForeach ()
58
using System.Collections.Generic;
64
IEnumerable<object> e = null;
65
foreach (var x in e) ;
66
foreach (var y in e) ;
69
Test<MultipleEnumerationIssue> (input, 2);
73
public void TestVariableMixed ()
76
using System.Collections.Generic;
82
IEnumerable<object> e = null;
83
foreach (var x in e) ;
87
Test<MultipleEnumerationIssue> (input, 2);
91
public void TestParameter ()
94
using System.Collections.Generic;
98
void TestMethod (IEnumerable<object> e)
100
foreach (var x in e) ;
104
Test<MultipleEnumerationIssue> (input, 2);
108
public void TestObjectMethodInvocation ()
111
using System.Collections.Generic;
117
IEnumerable<object> e;
118
var a = e.GetType ();
119
var b = e.ToString ();
122
Test<MultipleEnumerationIssue> (input, 0);
126
public void TestIf ()
129
using System.Collections.Generic;
133
void TestMethod (int i)
135
IEnumerable<object> e;
144
Test<MultipleEnumerationIssue> (input, 2);
148
public void TestIf2 ()
151
using System.Collections.Generic;
155
void TestMethod (int i)
157
IEnumerable<object> e;
166
Test<MultipleEnumerationIssue> (input, 3);
170
public void TestIf3 ()
173
using System.Collections.Generic;
177
void TestMethod (int i)
179
IEnumerable<object> e;
187
Test<MultipleEnumerationIssue> (input, 0);
191
public void TestFor ()
194
using System.Collections.Generic;
200
IEnumerable<object> e;
201
for (int i = 0; i < 10; i++) {
206
Test<MultipleEnumerationIssue> (input, 1);
210
public void TestWhile ()
213
using System.Collections.Generic;
219
IEnumerable<object> e;
226
Test<MultipleEnumerationIssue> (input, 1);
230
public void TestWhile2 ()
233
using System.Collections.Generic;
239
IEnumerable<object> e;
241
while (i > e.Count ()) {
245
Test<MultipleEnumerationIssue> (input, 1);
249
public void TestWhile3 ()
252
using System.Collections.Generic;
258
IEnumerable<object> e;
269
Test<MultipleEnumerationIssue> (input, 0);
273
public void TestWhile4 ()
277
using System.Collections.Generic;
281
IEnumerable<object> GetEnum () { }
282
void TestMethod (int i)
284
IEnumerable<object> e = GetEnum ();
286
while ((e = GetEnum ()) != null) {
291
Test<MultipleEnumerationIssue> (input, 0);
295
public void TestDo ()
299
using System.Collections.Generic;
303
IEnumerable<object> GetEnum () { }
304
void TestMethod (int i)
306
IEnumerable<object> e = GetEnum ();
310
} while ((e = GetEnum ()) != null);
313
Test<MultipleEnumerationIssue> (input, 2);
317
public void TestDo2 ()
321
using System.Collections.Generic;
325
IEnumerable<object> GetEnum () { }
326
void TestMethod (int i)
328
IEnumerable<object> e = GetEnum ();
331
} while ((e = GetEnum ()) != null);
334
Test<MultipleEnumerationIssue> (input, 0);
338
public void TestLambda ()
342
using System.Collections.Generic;
348
IEnumerable<object> e;
356
Test<MultipleEnumerationIssue> (input, 2);
360
public void TestLambda2 ()
364
using System.Collections.Generic;
368
void Test (object a, object b) { }
371
IEnumerable<object> e;
372
Action a = () => Test(e.First (), e.Count ());
375
Test<MultipleEnumerationIssue> (input, 2);
379
public void TestLambda3 ()
383
using System.Collections.Generic;
387
void Test (object a, Action b) { }
390
IEnumerable<object> e;
391
Test(e.First (), () => e.Count ());
394
Action a = () => e.Count();
397
Test<MultipleEnumerationIssue> (input, 0);
401
public void TestLambda4 ()
405
using System.Collections.Generic;
409
void Test (object a, object b) { }
412
IEnumerable<object> e;
413
Action a = () => Test(e.ToString (), e.ToString ());
416
Test<MultipleEnumerationIssue> (input, 0);
420
public void TestConditionalExpression ()
424
using System.Collections.Generic;
428
void TestMethod (int i)
430
IEnumerable<object> e;
431
var a = i > 0 ? e.First () : e.FirstOrDefault ();
432
Action b = () => i > 0 ? e.First () : e.FirstOrDefault ();
435
Test<MultipleEnumerationIssue> (input, 0);
438
public void TestConditionalExpression2 ()
442
using System.Collections.Generic;
446
void TestMethod (int i)
448
IEnumerable<object> e;
449
var a = i > 0 ? e.First () : new object ();
453
Test<MultipleEnumerationIssue> (input, 2);
457
public void TestConstantConditionalExpression ()
461
using System.Collections.Generic;
465
void TestMethod (int i)
467
IEnumerable<object> e;
468
var a = 1 > 2 ? e.First () : new object ();
472
Test<MultipleEnumerationIssue> (input, 0);
476
public void TestAssignmentInConditionalExpression ()
480
using System.Collections.Generic;
484
IEnumerable<object> GetEnum () { }
485
void TestMethod (int i)
487
IEnumerable<object> e;
489
var a = i > 0 ? e = GetEnum () : GetEnum ();
493
Test<MultipleEnumerationIssue> (input, 2);
497
public void TestAssignmentInConditionalExpression2 ()
501
using System.Collections.Generic;
505
IEnumerable<object> GetEnum () { }
506
void TestMethod (int i)
508
IEnumerable<object> e;
510
var a = i > 0 ? e = GetEnum () : e = GetEnum ();
514
Test<MultipleEnumerationIssue> (input, 0);
518
public void TestAssignment ()
521
using System.Collections.Generic;
525
void TestMethod (IEnumerable<object> e)
527
foreach (var x in e) ;
532
Test<MultipleEnumerationIssue> (input, 0);
536
public void TestAssignment2 ()
539
using System.Collections.Generic;
543
void TestMethod (IEnumerable<object> e)
545
foreach (var x in e) ;
553
Test<MultipleEnumerationIssue> (input, 2);
557
public void TestNoIssue ()
560
using System.Collections.Generic;
564
void TestMethod (IEnumerable<object> e)
566
foreach (var x in e) ;
567
IEnumerable<object> e2;
570
Test<MultipleEnumerationIssue> (input, 0);
574
public void TestExpression ()
577
using System.Collections.Generic;
581
int Test (params object[] args) { }
584
IEnumerable<object> e = null;
585
var type = e.GetType();
586
var x = Test (e.First (), e.Count ());
589
Test<MultipleEnumerationIssue> (input, 2);
593
public void TestExpression2 ()
596
using System.Collections.Generic;
600
int Test (params object[] args) { }
603
IEnumerable<object> e = null;
604
var type = e.GetType();
605
var x = Test (e.First (), e = new objct[0], e.Count ());
608
Test<MultipleEnumerationIssue> (input, 0);
612
public void TestOutArgument ()
615
using System.Collections.Generic;
619
void Test (out IEnumerable<object> e)
624
void TestMethod (IEnumerable<object> e)
626
foreach (var x in e) ;
631
Test<MultipleEnumerationIssue> (input, 0);
635
public void TestOutArgument2 ()
638
using System.Collections.Generic;
642
void Test (out IEnumerable<object> e)
647
void TestMethod (IEnumerable<object> e)
649
foreach (var x in e) ;
655
Test<MultipleEnumerationIssue> (input, 2);
659
public void TestOutArgument3 ()
662
using System.Collections.Generic;
666
void Test (object arg1, out IEnumerable<object> e, object arg2)
671
void TestMethod (IEnumerable<object> e)
673
Test (e.First (), out e, e.First ());
676
Test<MultipleEnumerationIssue> (input, 2);