PageRenderTime 67ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 1ms

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

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