PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/vim/bundle/YouCompleteMe/third_party/ycmd/third_party/OmniSharpServer/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/FunctionNeverReturnsIssueTests.cs

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