~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/MultipleEnumerationIssueTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// 
 
2
// MultipleEnumerationIssueTests.cs
 
3
// 
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
28
using NUnit.Framework;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
 
31
{
 
32
        [TestFixture]
 
33
        public class MultipleEnumerationIssueTests : InspectionActionTestBase
 
34
        {
 
35
                [Test]
 
36
                public void TestVariableInvocation ()
 
37
                {
 
38
                        var input = @"
 
39
using System.Collections.Generic;
 
40
using System.Linq;
 
41
class TestClass
 
42
{
 
43
        void TestMethod ()
 
44
        {
 
45
                IEnumerable<object> e = null;
 
46
                var type = e.GetType();
 
47
                var x = e.First ();
 
48
                var y = e.Count ();
 
49
        }
 
50
}";
 
51
                        Test<MultipleEnumerationIssue> (input, 2);
 
52
                }
 
53
 
 
54
                [Test] 
 
55
                public void TestVariableForeach ()
 
56
                {
 
57
                        var input = @"
 
58
using System.Collections.Generic;
 
59
using System.Linq;
 
60
class TestClass
 
61
{
 
62
        void TestMethod ()
 
63
        {
 
64
                IEnumerable<object> e = null;
 
65
                foreach (var x in e) ;
 
66
                foreach (var y in e) ;
 
67
        }
 
68
}";
 
69
                        Test<MultipleEnumerationIssue> (input, 2);
 
70
                }
 
71
 
 
72
                [Test]
 
73
                public void TestVariableMixed ()
 
74
                {
 
75
                        var input = @"
 
76
using System.Collections.Generic;
 
77
using System.Linq;
 
78
class TestClass
 
79
{
 
80
        void TestMethod ()
 
81
        {
 
82
                IEnumerable<object> e = null;
 
83
                foreach (var x in e) ;
 
84
                var y = e.Count ();
 
85
        }
 
86
}";
 
87
                        Test<MultipleEnumerationIssue> (input, 2);
 
88
                }
 
89
 
 
90
                [Test]
 
91
                public void TestParameter ()
 
92
                {
 
93
                        var input = @"
 
94
using System.Collections.Generic;
 
95
using System.Linq;
 
96
class TestClass
 
97
{
 
98
        void TestMethod (IEnumerable<object> e)
 
99
        {
 
100
                foreach (var x in e) ;
 
101
                var y = e.Count ();
 
102
        }
 
103
}";
 
104
                        Test<MultipleEnumerationIssue> (input, 2);
 
105
                }
 
106
 
 
107
                [Test]
 
108
                public void TestObjectMethodInvocation ()
 
109
                {
 
110
                        var input = @"
 
111
using System.Collections.Generic;
 
112
using System.Linq;
 
113
class TestClass
 
114
{
 
115
        void TestMethod ()
 
116
        {
 
117
                IEnumerable<object> e;
 
118
                var a = e.GetType ();
 
119
                var b = e.ToString ();
 
120
        }
 
121
}";
 
122
                        Test<MultipleEnumerationIssue> (input, 0);
 
123
                }
 
124
 
 
125
                [Test]
 
126
                public void TestIf ()
 
127
                {
 
128
                        var input = @"
 
129
using System.Collections.Generic;
 
130
using System.Linq;
 
131
class TestClass
 
132
{
 
133
        void TestMethod (int i)
 
134
        {
 
135
                IEnumerable<object> e;
 
136
                if (i > 0) {
 
137
                        var a = e.Count ();
 
138
                } else {
 
139
                        var b = e.First ();
 
140
                        var c = e.Count ();
 
141
                }
 
142
        }
 
143
}";
 
144
                        Test<MultipleEnumerationIssue> (input, 2);
 
145
                }
 
146
 
 
147
                [Test]
 
148
                public void TestIf2 ()
 
149
                {
 
150
                        var input = @"
 
151
using System.Collections.Generic;
 
152
using System.Linq;
 
153
class TestClass
 
154
{
 
155
        void TestMethod (int i)
 
156
        {
 
157
                IEnumerable<object> e;
 
158
                if (i > 0) {
 
159
                        var a = e.Count ();
 
160
                } else {
 
161
                        var b = e.First ();
 
162
                }
 
163
                var c = e.Count ();
 
164
        }
 
165
}";
 
166
                        Test<MultipleEnumerationIssue> (input, 3);
 
167
                }
 
168
 
 
169
                [Test]
 
170
                public void TestIf3 ()
 
171
                {
 
172
                        var input = @"
 
173
using System.Collections.Generic;
 
174
using System.Linq;
 
175
class TestClass
 
176
{
 
177
        void TestMethod (int i)
 
178
        {
 
179
                IEnumerable<object> e;
 
180
                if (i > 0) {
 
181
                        var a = e.Count ();
 
182
                } else {
 
183
                        var b = e.First ();
 
184
                }
 
185
        }
 
186
}";
 
187
                        Test<MultipleEnumerationIssue> (input, 0);
 
188
                }
 
189
 
 
190
                [Test]
 
191
                public void TestFor ()
 
192
                {
 
193
                        var input = @"
 
194
using System.Collections.Generic;
 
195
using System.Linq;
 
196
class TestClass
 
197
{
 
198
        void TestMethod ()
 
199
        {
 
200
                IEnumerable<object> e;
 
201
                for (int i = 0; i < 10; i++) {
 
202
                        var a = e.Count ();
 
203
                }
 
204
        }
 
205
}";
 
206
                        Test<MultipleEnumerationIssue> (input, 1);
 
207
                }
 
208
 
 
209
                [Test]
 
210
                public void TestWhile ()
 
211
                {
 
212
                        var input = @"
 
213
using System.Collections.Generic;
 
214
using System.Linq;
 
215
class TestClass
 
216
{
 
217
        void TestMethod ()
 
218
        {
 
219
                IEnumerable<object> e;
 
220
                int i;
 
221
                while (i > 1) {
 
222
                        var a = e.Count ();
 
223
                }
 
224
        }
 
225
}";
 
226
                        Test<MultipleEnumerationIssue> (input, 1);
 
227
                }
 
228
 
 
229
                [Test]
 
230
                public void TestWhile2 ()
 
231
                {
 
232
                        var input = @"
 
233
using System.Collections.Generic;
 
234
using System.Linq;
 
235
class TestClass
 
236
{
 
237
        void TestMethod ()
 
238
        {
 
239
                IEnumerable<object> e;
 
240
                int i;
 
241
                while (i > e.Count ()) {
 
242
                }
 
243
        }
 
244
}";
 
245
                        Test<MultipleEnumerationIssue> (input, 1);
 
246
                }
 
247
 
 
248
                [Test]
 
249
                public void TestWhile3 ()
 
250
                {
 
251
                        var input = @"
 
252
using System.Collections.Generic;
 
253
using System.Linq;
 
254
class TestClass
 
255
{
 
256
        void TestMethod ()
 
257
        {
 
258
                IEnumerable<object> e;
 
259
                int i;
 
260
                object x;
 
261
                while (true) {
 
262
                        if (i > 1) {
 
263
                                x = e.First ();
 
264
                                break;
 
265
                        } 
 
266
                }
 
267
        }
 
268
}";
 
269
                        Test<MultipleEnumerationIssue> (input, 0);
 
270
                }
 
271
 
 
272
                [Test]
 
273
                public void TestWhile4 ()
 
274
                {
 
275
                        var input = @"
 
276
using System;
 
277
using System.Collections.Generic;
 
278
using System.Linq;
 
279
class TestClass
 
280
{
 
281
        IEnumerable<object> GetEnum () { }
 
282
        void TestMethod (int i)
 
283
        {
 
284
                IEnumerable<object> e = GetEnum ();
 
285
                var a1 = e.First ();
 
286
                while ((e = GetEnum ()) != null) {
 
287
                        var a2 = e.First ();
 
288
                }
 
289
        }
 
290
}";
 
291
                        Test<MultipleEnumerationIssue> (input, 0);
 
292
                }
 
293
 
 
294
                [Test]
 
295
                public void TestDo ()
 
296
                {
 
297
                        var input = @"
 
298
using System;
 
299
using System.Collections.Generic;
 
300
using System.Linq;
 
301
class TestClass
 
302
{
 
303
        IEnumerable<object> GetEnum () { }
 
304
        void TestMethod (int i)
 
305
        {
 
306
                IEnumerable<object> e = GetEnum ();
 
307
                var a1 = e.First ();
 
308
                do {
 
309
                        var a2 = e.First ();
 
310
                } while ((e = GetEnum ()) != null);
 
311
        }
 
312
}";
 
313
                        Test<MultipleEnumerationIssue> (input, 2);
 
314
                }
 
315
 
 
316
                [Test]
 
317
                public void TestDo2 ()
 
318
                {
 
319
                        var input = @"
 
320
using System;
 
321
using System.Collections.Generic;
 
322
using System.Linq;
 
323
class TestClass
 
324
{
 
325
        IEnumerable<object> GetEnum () { }
 
326
        void TestMethod (int i)
 
327
        {
 
328
                IEnumerable<object> e = GetEnum ();
 
329
                do {
 
330
                        var a2 = e.First ();
 
331
                } while ((e = GetEnum ()) != null);
 
332
        }
 
333
}";
 
334
                        Test<MultipleEnumerationIssue> (input, 0);
 
335
                }
 
336
 
 
337
                [Test]
 
338
                public void TestLambda ()
 
339
                {
 
340
                        var input = @"
 
341
using System;
 
342
using System.Collections.Generic;
 
343
using System.Linq;
 
344
class TestClass
 
345
{
 
346
        void TestMethod ()
 
347
        {
 
348
                IEnumerable<object> e;
 
349
                Action a = () => {
 
350
                        var x = e.Count ();
 
351
                        var y = e.Count ();
 
352
                };
 
353
                var z = e.Count ();
 
354
        }
 
355
}";
 
356
                        Test<MultipleEnumerationIssue> (input, 2);
 
357
                }
 
358
 
 
359
                [Test]
 
360
                public void TestLambda2 ()
 
361
                {
 
362
                        var input = @"
 
363
using System;
 
364
using System.Collections.Generic;
 
365
using System.Linq;
 
366
class TestClass
 
367
{
 
368
        void Test (object a, object b) { }
 
369
        void TestMethod ()
 
370
        {
 
371
                IEnumerable<object> e;
 
372
                Action a = () => Test(e.First (), e.Count ());
 
373
        }
 
374
}";
 
375
                        Test<MultipleEnumerationIssue> (input, 2);
 
376
                }
 
377
 
 
378
                [Test]
 
379
                public void TestLambda3 ()
 
380
                {
 
381
                        var input = @"
 
382
using System;
 
383
using System.Collections.Generic;
 
384
using System.Linq;
 
385
class TestClass
 
386
{
 
387
        void Test (object a, Action b) { }
 
388
        void TestMethod ()
 
389
        {
 
390
                IEnumerable<object> e;
 
391
                Test(e.First (), () => e.Count ());
 
392
                e = null;
 
393
                var x = e.First ();
 
394
                Action a = () => e.Count();
 
395
        }
 
396
}";
 
397
                        Test<MultipleEnumerationIssue> (input, 0);
 
398
                }
 
399
 
 
400
                [Test]
 
401
                public void TestLambda4 ()
 
402
                {
 
403
                        var input = @"
 
404
using System;
 
405
using System.Collections.Generic;
 
406
using System.Linq;
 
407
class TestClass
 
408
{
 
409
        void Test (object a, object b) { }
 
410
        void TestMethod ()
 
411
        {
 
412
                IEnumerable<object> e;
 
413
                Action a = () => Test(e.ToString (), e.ToString ());
 
414
        }
 
415
}";
 
416
                        Test<MultipleEnumerationIssue> (input, 0);
 
417
                }
 
418
 
 
419
                [Test]
 
420
                public void TestConditionalExpression ()
 
421
                {
 
422
                        var input = @"
 
423
using System;
 
424
using System.Collections.Generic;
 
425
using System.Linq;
 
426
class TestClass
 
427
{
 
428
        void TestMethod (int i)
 
429
        {
 
430
                IEnumerable<object> e;
 
431
                var a = i > 0 ? e.First () : e.FirstOrDefault ();
 
432
                Action b = () => i > 0 ? e.First () : e.FirstOrDefault ();
 
433
        }
 
434
}";
 
435
                        Test<MultipleEnumerationIssue> (input, 0);
 
436
                }
 
437
                [Test]
 
438
                public void TestConditionalExpression2 ()
 
439
                {
 
440
                        var input = @"
 
441
using System;
 
442
using System.Collections.Generic;
 
443
using System.Linq;
 
444
class TestClass
 
445
{
 
446
        void TestMethod (int i)
 
447
        {
 
448
                IEnumerable<object> e;
 
449
                var a = i > 0 ? e.First () : new object ();
 
450
                var b = e.First ();
 
451
        }
 
452
}";
 
453
                        Test<MultipleEnumerationIssue> (input, 2);
 
454
                }
 
455
 
 
456
                [Test]
 
457
                public void TestConstantConditionalExpression ()
 
458
                {
 
459
                        var input = @"
 
460
using System;
 
461
using System.Collections.Generic;
 
462
using System.Linq;
 
463
class TestClass
 
464
{
 
465
        void TestMethod (int i)
 
466
        {
 
467
                IEnumerable<object> e;
 
468
                var a = 1 > 2 ? e.First () : new object ();
 
469
                var b = e.First ();
 
470
        }
 
471
}";
 
472
                        Test<MultipleEnumerationIssue> (input, 0);
 
473
                }
 
474
 
 
475
                [Test]
 
476
                public void TestAssignmentInConditionalExpression ()
 
477
                {
 
478
                        var input = @"
 
479
using System;
 
480
using System.Collections.Generic;
 
481
using System.Linq;
 
482
class TestClass
 
483
{
 
484
        IEnumerable<object> GetEnum () { }
 
485
        void TestMethod (int i)
 
486
        {
 
487
                IEnumerable<object> e;
 
488
                var x1 = e.First ();
 
489
                var a = i > 0 ? e = GetEnum () : GetEnum ();
 
490
                var x2 = e.First ();
 
491
        }
 
492
}";
 
493
                        Test<MultipleEnumerationIssue> (input, 2);
 
494
                }
 
495
 
 
496
                [Test]
 
497
                public void TestAssignmentInConditionalExpression2 ()
 
498
                {
 
499
                        var input = @"
 
500
using System;
 
501
using System.Collections.Generic;
 
502
using System.Linq;
 
503
class TestClass
 
504
{
 
505
        IEnumerable<object> GetEnum () { }
 
506
        void TestMethod (int i)
 
507
        {
 
508
                IEnumerable<object> e;
 
509
                var x1 = e.First ();
 
510
                var a = i > 0 ? e = GetEnum () : e = GetEnum ();
 
511
                var x2 = e.First ();
 
512
        }
 
513
}";
 
514
                        Test<MultipleEnumerationIssue> (input, 0);
 
515
                }
 
516
 
 
517
                [Test]
 
518
                public void TestAssignment ()
 
519
                {
 
520
                        var input = @"
 
521
using System.Collections.Generic;
 
522
using System.Linq;
 
523
class TestClass
 
524
{
 
525
        void TestMethod (IEnumerable<object> e)
 
526
        {
 
527
                foreach (var x in e) ;
 
528
                e = null;
 
529
                var y = e.Count ();
 
530
        }
 
531
}";
 
532
                        Test<MultipleEnumerationIssue> (input, 0);
 
533
                }
 
534
 
 
535
                [Test]
 
536
                public void TestAssignment2 ()
 
537
                {
 
538
                        var input = @"
 
539
using System.Collections.Generic;
 
540
using System.Linq;
 
541
class TestClass
 
542
{
 
543
        void TestMethod (IEnumerable<object> e)
 
544
        {
 
545
                foreach (var x in e) ;
 
546
                e = null;
 
547
                var y = e.Count ();
 
548
                e = null;
 
549
                var a = e.First ();
 
550
                var b = e.First ();
 
551
        }
 
552
}";
 
553
                        Test<MultipleEnumerationIssue> (input, 2);
 
554
                }
 
555
 
 
556
                [Test]
 
557
                public void TestNoIssue ()
 
558
                {
 
559
                        var input = @"
 
560
using System.Collections.Generic;
 
561
using System.Linq;
 
562
class TestClass
 
563
{
 
564
        void TestMethod (IEnumerable<object> e)
 
565
        {
 
566
                foreach (var x in e) ;
 
567
                IEnumerable<object> e2;
 
568
        }
 
569
}";
 
570
                        Test<MultipleEnumerationIssue> (input, 0);
 
571
                }
 
572
 
 
573
                [Test]
 
574
                public void TestExpression ()
 
575
                {
 
576
                        var input = @"
 
577
using System.Collections.Generic;
 
578
using System.Linq;
 
579
class TestClass
 
580
{
 
581
        int Test (params object[] args) { }
 
582
        void TestMethod ()
 
583
        {
 
584
                IEnumerable<object> e = null;
 
585
                var type = e.GetType();
 
586
                var x = Test (e.First (), e.Count ());
 
587
        }
 
588
}";
 
589
                        Test<MultipleEnumerationIssue> (input, 2);
 
590
                }
 
591
 
 
592
                [Test]
 
593
                public void TestExpression2 ()
 
594
                {
 
595
                        var input = @"
 
596
using System.Collections.Generic;
 
597
using System.Linq;
 
598
class TestClass
 
599
{
 
600
        int Test (params object[] args) { }
 
601
        void TestMethod ()
 
602
        {
 
603
                IEnumerable<object> e = null;
 
604
                var type = e.GetType();
 
605
                var x = Test (e.First (), e = new objct[0], e.Count ());
 
606
        }
 
607
}";
 
608
                        Test<MultipleEnumerationIssue> (input, 0);
 
609
                }
 
610
 
 
611
                [Test]
 
612
                public void TestOutArgument ()
 
613
                {
 
614
                        var input = @"
 
615
using System.Collections.Generic;
 
616
using System.Linq;
 
617
class TestClass
 
618
{
 
619
        void Test (out IEnumerable<object> e)
 
620
        {
 
621
                e = null;
 
622
        }
 
623
 
 
624
        void TestMethod (IEnumerable<object> e)
 
625
        {
 
626
                foreach (var x in e) ;
 
627
                Test (out e);
 
628
                var y = e.Count ();
 
629
        }
 
630
}";
 
631
                        Test<MultipleEnumerationIssue> (input, 0);
 
632
                }
 
633
 
 
634
                [Test]
 
635
                public void TestOutArgument2 ()
 
636
                {
 
637
                        var input = @"
 
638
using System.Collections.Generic;
 
639
using System.Linq;
 
640
class TestClass
 
641
{
 
642
        void Test (out IEnumerable<object> e)
 
643
        {
 
644
                e = null;
 
645
        }
 
646
 
 
647
        void TestMethod (IEnumerable<object> e)
 
648
        {
 
649
                foreach (var x in e) ;
 
650
                Test (out e);
 
651
                var y = e.Count ();
 
652
                var z = e.Count ();
 
653
        }
 
654
}";
 
655
                        Test<MultipleEnumerationIssue> (input, 2);
 
656
                }
 
657
 
 
658
                [Test]
 
659
                public void TestOutArgument3 ()
 
660
                {
 
661
                        var input = @"
 
662
using System.Collections.Generic;
 
663
using System.Linq;
 
664
class TestClass
 
665
{
 
666
        void Test (object arg1, out IEnumerable<object> e, object arg2)
 
667
        {
 
668
                e = null;
 
669
        }
 
670
 
 
671
        void TestMethod (IEnumerable<object> e)
 
672
        {
 
673
                Test (e.First (), out e, e.First ());
 
674
        }
 
675
}";
 
676
                        Test<MultipleEnumerationIssue> (input, 2);
 
677
                }
 
678
        }
 
679
}