PageRenderTime 3ms CodeModel.GetById 806ms app.highlight 13ms RepoModel.GetById 208ms app.codeStats 1ms

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

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