PageRenderTime 52ms CodeModel.GetById 28ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/MultipleEnumerationIssueTests.cs

https://github.com/davidroth/ILSpy
C# | 679 lines | 622 code | 32 blank | 25 comment | 0 complexity | 4848deb72d9401429d685d76b0000c2d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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
 27using ICSharpCode.NRefactory.CSharp.Refactoring;
 28using NUnit.Framework;
 29
 30namespace ICSharpCode.NRefactory.CSharp.CodeIssues
 31{
 32	[TestFixture]
 33	public class MultipleEnumerationIssueTests : InspectionActionTestBase
 34	{
 35		[Test]
 36		public void TestVariableInvocation ()
 37		{
 38			var input = @"
 39using System.Collections.Generic;
 40using System.Linq;
 41class 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 = @"
 58using System.Collections.Generic;
 59using System.Linq;
 60class 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 = @"
 76using System.Collections.Generic;
 77using System.Linq;
 78class 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 = @"
 94using System.Collections.Generic;
 95using System.Linq;
 96class 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 = @"
111using System.Collections.Generic;
112using System.Linq;
113class 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 = @"
129using System.Collections.Generic;
130using System.Linq;
131class 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 = @"
151using System.Collections.Generic;
152using System.Linq;
153class 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 = @"
173using System.Collections.Generic;
174using System.Linq;
175class 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 = @"
194using System.Collections.Generic;
195using System.Linq;
196class 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 = @"
213using System.Collections.Generic;
214using System.Linq;
215class 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 = @"
233using System.Collections.Generic;
234using System.Linq;
235class 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 = @"
252using System.Collections.Generic;
253using System.Linq;
254class 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 = @"
276using System;
277using System.Collections.Generic;
278using System.Linq;
279class 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 = @"
298using System;
299using System.Collections.Generic;
300using System.Linq;
301class 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 = @"
320using System;
321using System.Collections.Generic;
322using System.Linq;
323class 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 = @"
341using System;
342using System.Collections.Generic;
343using System.Linq;
344class 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 = @"
363using System;
364using System.Collections.Generic;
365using System.Linq;
366class 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 = @"
382using System;
383using System.Collections.Generic;
384using System.Linq;
385class 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 = @"
404using System;
405using System.Collections.Generic;
406using System.Linq;
407class 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 = @"
423using System;
424using System.Collections.Generic;
425using System.Linq;
426class 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 = @"
441using System;
442using System.Collections.Generic;
443using System.Linq;
444class 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 = @"
460using System;
461using System.Collections.Generic;
462using System.Linq;
463class 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 = @"
479using System;
480using System.Collections.Generic;
481using System.Linq;
482class 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 = @"
500using System;
501using System.Collections.Generic;
502using System.Linq;
503class 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 = @"
521using System.Collections.Generic;
522using System.Linq;
523class 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 = @"
539using System.Collections.Generic;
540using System.Linq;
541class 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 = @"
560using System.Collections.Generic;
561using System.Linq;
562class 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 = @"
577using System.Collections.Generic;
578using System.Linq;
579class 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 = @"
596using System.Collections.Generic;
597using System.Linq;
598class 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 = @"
615using System.Collections.Generic;
616using System.Linq;
617class 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 = @"
638using System.Collections.Generic;
639using System.Linq;
640class 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 = @"
662using System.Collections.Generic;
663using System.Linq;
664class 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}