PageRenderTime 2ms CodeModel.GetById 389ms app.highlight 53ms RepoModel.GetById 65ms app.codeStats 1ms

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

http://github.com/icsharpcode/NRefactory
C# | 541 lines | 472 code | 33 blank | 36 comment | 0 complexity | 9ae011d084bb91f1d1dfc46c9bd55691 MD5 | raw file
  1// 
  2// MethodNeverReturnsIssueTests.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.Linq;
 30
 31namespace ICSharpCode.NRefactory.CSharp.CodeIssues
 32{
 33	[TestFixture]
 34	public class FunctionNeverReturnsIssueTests : InspectionActionTestBase
 35	{
 36		[Test]
 37		public void TestEnd ()
 38		{
 39			var input = @"
 40class TestClass
 41{
 42	void TestMethod () 
 43	{
 44		int i = 1;
 45	}
 46}";
 47			Test<FunctionNeverReturnsIssue> (input, 0);
 48		}
 49
 50		[Test]
 51		public void TestReturn ()
 52		{
 53			var input = @"
 54class TestClass
 55{
 56	void TestMethod ()
 57	{
 58		return;
 59	}
 60}";
 61			Test<FunctionNeverReturnsIssue> (input, 0);
 62		}
 63
 64		[Test]
 65		public void TestThrow ()
 66		{
 67			var input = @"
 68class TestClass
 69{
 70	void TestMethod ()
 71	{
 72		throw new System.NotImplementedException();	
 73	}
 74}";
 75			Test<FunctionNeverReturnsIssue> (input, 0);
 76		}
 77
 78		[Test]
 79		public void TestNeverReturns ()
 80		{
 81			var input = @"
 82class TestClass
 83{
 84	void TestMethod ()
 85	{
 86		while (true) ;
 87	}
 88}";
 89			Test<FunctionNeverReturnsIssue> (input, 1);
 90		}
 91
 92		[Test]
 93		public void TestIfWithoutElse ()
 94		{
 95			var input = @"
 96class TestClass
 97{
 98	string TestMethod (int x)
 99	{
100		if (x <= 0) return ""Hi"";
101		return ""_"" + TestMethod(x - 1);
102	}
103}";
104			TestWrongContext<FunctionNeverReturnsIssue> (input);
105		}
106
107		[Test]
108		public void TestRecursive ()
109		{
110			var input = @"
111class TestClass
112{
113	void TestMethod ()
114	{
115		TestMethod ();
116	}
117}";
118			Test<FunctionNeverReturnsIssue> (input, 1);
119		}
120
121		[Test]
122		public void TestNonRecursive ()
123		{
124			var input = @"
125class TestClass
126{
127	void TestMethod ()
128	{
129		TestMethod (0);
130	}
131	void TestMethod (int i)
132	{
133	}
134}";
135			Test<FunctionNeverReturnsIssue> (input, 0);
136		}
137
138		[Test]
139		public void TestVirtualNonRecursive ()
140		{
141			var input = @"
142class Base
143{
144	public Base parent;
145	public virtual string Result {
146		get { return parent.Result; }
147	}
148}";
149			Test<FunctionNeverReturnsIssue> (input, 0);
150		}
151
152		[Test]
153		public void TestNonRecursiveProperty ()
154		{
155			var input = @"
156class TestClass
157{
158	int foo;
159	int Foo
160	{
161		get { return foo; }
162		set
163		{
164			if (Foo != value)
165				foo = value;
166		}
167	}
168}";
169			Test<FunctionNeverReturnsIssue> (input, 0);
170		}
171
172
173		[Test]
174		public void TestGetterNeverReturns ()
175		{
176			var input = @"
177class TestClass
178{
179	int TestProperty
180	{
181		get {
182			while (true) ;
183		}
184	}
185}";
186			Test<FunctionNeverReturnsIssue> (input, 1);
187		}
188
189		[Test]
190		public void TestRecursiveGetter ()
191		{
192			var input = @"
193class TestClass
194{
195	int TestProperty
196	{
197		get {
198			return TestProperty;
199		}
200	}
201}";
202			Test<FunctionNeverReturnsIssue> (input, 1);
203		}
204
205		[Test]
206		public void TestRecursiveSetter ()
207		{
208			var input = @"
209class TestClass
210{
211	int TestProperty
212	{
213		set {
214			TestProperty = value;
215		}
216	}
217}";
218			Test<FunctionNeverReturnsIssue> (input, 1);
219		}
220
221		[Test]
222		public void TestAutoProperty ()
223		{
224			var input = @"
225class TestClass
226{
227	int TestProperty
228	{
229		get;
230		set;
231	}
232}";
233			TestWrongContext<FunctionNeverReturnsIssue> (input);
234		}
235
236		[Test]
237		public void TestMethodGroupNeverReturns ()
238		{
239			var input = @"
240class TestClass
241{
242	int TestMethod()
243	{
244		return TestMethod();
245	}
246	int TestMethod(object o)
247	{
248		return TestMethod();
249	}
250}";
251			Test<FunctionNeverReturnsIssue> (input, 1);
252		}
253
254		[Test]
255		public void TestIncrementProperty()
256		{
257			var input = @"
258class TestClass
259{
260	int TestProperty
261	{
262		get { return TestProperty++; }
263		set { TestProperty++; }
264	}
265}";
266			Test<FunctionNeverReturnsIssue> (input, 2);
267		}
268
269		[Test]
270		public void TestLambdaNeverReturns ()
271		{
272			var input = @"
273class TestClass
274{
275	void TestMethod()
276	{
277		System.Action action = () => { while (true) ; };
278	}
279}";
280			Test<FunctionNeverReturnsIssue> (input, 1);
281		}
282
283		[Test]
284		public void TestDelegateNeverReturns ()
285		{
286			var input = @"
287class TestClass
288{
289	void TestMethod()
290	{
291		System.Action action = delegate() { while (true) ; };
292	}
293}";
294
295			Test<FunctionNeverReturnsIssue> (input, 1);
296		}
297		[Test]
298		public void YieldBreak ()
299		{
300			var input = @"
301class TestClass
302{
303	System.Collections.Generic.IEnumerable<string> TestMethod ()
304	{
305		yield break;
306	}
307}";
308			Test<FunctionNeverReturnsIssue> (input, 0);
309		}
310
311		[Test]
312		public void TestDisable ()
313		{
314			var input = @"
315class TestClass
316{
317	// ReSharper disable once FunctionNeverReturns
318	void TestMethod ()
319	{
320		while (true) ;
321	}
322}";
323			TestWrongContext<FunctionNeverReturnsIssue> (input);
324		}
325
326		[Test]
327		public void TestBug254 ()
328		{
329			//https://github.com/icsharpcode/NRefactory/issues/254
330			var input = @"
331class TestClass
332{
333	int state = 0;
334
335	bool Foo()
336	{
337		return state < 10;
338	}
339
340	void TestMethod()
341	{
342		if (Foo()) {
343			++state;
344			TestMethod ();	
345		}
346	}
347}";
348			TestWrongContext<FunctionNeverReturnsIssue> (input);
349		}
350
351		[Test]
352		public void TestSwitch ()
353		{
354			//https://github.com/icsharpcode/NRefactory/issues/254
355			var input = @"
356class TestClass
357{
358	int foo;
359	void TestMethod()
360	{
361		switch (foo) {
362			case 0: TestMethod();
363		}
364	}
365}";
366			TestWrongContext<FunctionNeverReturnsIssue> (input);
367		}
368
369		[Test]
370		public void TestSwitchWithDefault ()
371		{
372			//https://github.com/icsharpcode/NRefactory/issues/254
373			var input = @"
374class TestClass
375{
376	int foo;
377	void TestMethod()
378	{
379		switch (foo) {
380			case 0: case 1: TestMethod();
381			default: TestMethod();
382		}
383	}
384}";
385			Test<FunctionNeverReturnsIssue> (input, 1);
386		}
387
388		[Test]
389		public void TestSwitchValue ()
390		{
391			//https://github.com/icsharpcode/NRefactory/issues/254
392			var input = @"
393class TestClass
394{
395	int foo;
396	int TestMethod()
397	{
398		switch (TestMethod()) {
399			case 0: return 0;
400		}
401		return 1;
402	}
403}";
404			Test<FunctionNeverReturnsIssue> (input, 1);
405		}
406
407		[Test]
408		public void TestLinqFrom ()
409		{
410			//https://github.com/icsharpcode/NRefactory/issues/254
411			var input = @"
412using System.Linq;
413using System.Collections.Generic;
414class TestClass
415{
416	IEnumerable<int> TestMethod()
417	{
418		return from y in TestMethod() select y;
419	}
420}";
421			Test<FunctionNeverReturnsIssue> (input, 1);
422		}
423
424		[Test]
425		public void TestWrongLinqContexts ()
426		{
427			//https://github.com/icsharpcode/NRefactory/issues/254
428			var input = @"
429using System.Linq;
430using System.Collections.Generic;
431class TestClass
432{
433	IEnumerable<int> TestMethod()
434	{
435		return from y in Enumerable.Empty<int>()
436		       from z in TestMethod()
437		       select y;
438	}
439}";
440			TestWrongContext<FunctionNeverReturnsIssue> (input);
441		}
442
443		[Test]
444		public void TestForeach ()
445		{
446			//https://bugzilla.xamarin.com/show_bug.cgi?id=14732
447			var input = @"
448using System.Linq;
449class TestClass
450{
451	void TestMethod()
452	{
453		foreach (var x in new int[0])
454			TestMethod();
455	}
456}";
457			TestWrongContext<FunctionNeverReturnsIssue> (input);
458		}
459
460		[Test]
461		public void TestNoExecutionFor ()
462		{
463			var input = @"
464using System.Linq;
465class TestClass
466{
467	void TestMethod()
468	{
469		for (int i = 0; i < 0; ++i)
470			TestMethod ();
471	}
472}";
473			TestWrongContext<FunctionNeverReturnsIssue> (input);
474		}
475
476		[Test]
477		public void TestNullCoalescing ()
478		{
479			//https://bugzilla.xamarin.com/show_bug.cgi?id=14732
480			var input = @"
481using System.Linq;
482class TestClass
483{
484	TestClass parent;
485	int? value;
486	int TestMethod()
487	{
488		return value ?? parent.TestMethod();
489	}
490}";
491			TestWrongContext<FunctionNeverReturnsIssue> (input);
492		}
493
494		[Test]
495		public void TestPropertyGetterInSetter ()
496		{
497			TestWrongContext<FunctionNeverReturnsIssue> (@"using System;
498class TestClass
499{
500	int a;
501	int Foo {
502		get { return 1; }
503		set { a = Foo; }
504	}
505}");
506		}
507
508		[Test]
509		public void TestRecursiveFunctionBug ()
510		{
511			TestWrongContext<FunctionNeverReturnsIssue> (@"using System;
512class TestClass
513{
514	bool Foo (int i)
515	{
516		return i < 0 || Foo (i - 1);
517	}
518}");
519		}
520
521		/// <summary>
522		/// Bug 17769 - Incorrect "method never returns" warning
523		/// </summary>
524		[Test]
525		public void TestBug17769 ()
526		{
527			TestWrongContext<FunctionNeverReturnsIssue> (@"
528using System.Linq;
529class A
530{
531    A[] list = new A[0];
532
533    public bool Test ()
534    {
535        return list.Any (t => t.Test ());
536    }
537}
538");
539		}
540	}
541}