PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AutoLinqSumTests.cs

https://github.com/bbqchickenrobot/NRefactory
C# | 775 lines | 681 code | 70 blank | 24 comment | 0 complexity | 845986b743ff5e477ec2386f2260ef32 MD5 | raw file
  1. // AutoLinqTests.cs
  2. //
  3. // Author:
  4. // Luís Reis <luiscubal@gmail.com>
  5. //
  6. // Copyright (c) 2013 Luís Reis
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. using System;
  26. using NUnit.Framework;
  27. using ICSharpCode.NRefactory.CSharp.Refactoring;
  28. namespace ICSharpCode.NRefactory.CSharp.CodeActions
  29. {
  30. [TestFixture]
  31. public class AutoLinqSumActionTests : ContextActionTestBase
  32. {
  33. [Test]
  34. public void TestSimpleIntegerLoop() {
  35. string source = @"
  36. using System.Linq;
  37. class TestClass
  38. {
  39. void TestMethod() {
  40. int result = 0;
  41. var list = new int[] { 1, 2, 3 };
  42. $foreach (var x in list)
  43. result += x;
  44. }
  45. }";
  46. string result = @"
  47. using System.Linq;
  48. class TestClass
  49. {
  50. void TestMethod() {
  51. int result = 0;
  52. var list = new int[] { 1, 2, 3 };
  53. result += list.Sum ();
  54. }
  55. }";
  56. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  57. }
  58. [Test]
  59. public void TestMergedIntegerLoop() {
  60. string source = @"
  61. using System.Linq;
  62. class TestClass
  63. {
  64. void TestMethod() {
  65. var list = new int[] { 1, 2, 3 };
  66. int result = 0;
  67. $foreach (var x in list)
  68. result += x;
  69. }
  70. }";
  71. string result = @"
  72. using System.Linq;
  73. class TestClass
  74. {
  75. void TestMethod() {
  76. var list = new int[] { 1, 2, 3 };
  77. int result = list.Sum ();
  78. }
  79. }";
  80. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  81. }
  82. [Test]
  83. public void TestNonZeroMergedIntegerLoop() {
  84. string source = @"
  85. using System.Linq;
  86. class TestClass
  87. {
  88. void TestMethod() {
  89. var list = new int[] { 1, 2, 3 };
  90. int result = 1;
  91. $foreach (var x in list)
  92. result += x;
  93. }
  94. }";
  95. string result = @"
  96. using System.Linq;
  97. class TestClass
  98. {
  99. void TestMethod() {
  100. var list = new int[] { 1, 2, 3 };
  101. int result = 1 + list.Sum ();
  102. }
  103. }";
  104. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  105. }
  106. [Test]
  107. public void TestMergedAssignmentIntegerLoop() {
  108. string source = @"
  109. using System.Linq;
  110. class TestClass
  111. {
  112. void TestMethod() {
  113. var list = new int[] { 1, 2, 3 };
  114. int result;
  115. result = 1;
  116. $foreach (var x in list)
  117. result += x;
  118. }
  119. }";
  120. string result = @"
  121. using System.Linq;
  122. class TestClass
  123. {
  124. void TestMethod() {
  125. var list = new int[] { 1, 2, 3 };
  126. int result;
  127. result = 1 + list.Sum ();
  128. }
  129. }";
  130. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  131. }
  132. [Test]
  133. public void TestMergedDecimal() {
  134. string source = @"
  135. using System.Linq;
  136. class TestClass
  137. {
  138. void TestMethod() {
  139. var list = new int[] { 1, 2, 3 };
  140. decimal result = 0.0m;
  141. $foreach (var x in list)
  142. result += x;
  143. }
  144. }";
  145. string result = @"
  146. using System.Linq;
  147. class TestClass
  148. {
  149. void TestMethod() {
  150. var list = new int[] { 1, 2, 3 };
  151. decimal result = list.Sum ();
  152. }
  153. }";
  154. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  155. }
  156. [Test]
  157. public void TestIntegerLoopInBlock() {
  158. string source = @"
  159. using System.Linq;
  160. class TestClass
  161. {
  162. void TestMethod() {
  163. int result = 0;
  164. var list = new int[] { 1, 2, 3 };
  165. $foreach (var x in list) {
  166. result += x;
  167. }
  168. }
  169. }";
  170. string result = @"
  171. using System.Linq;
  172. class TestClass
  173. {
  174. void TestMethod() {
  175. int result = 0;
  176. var list = new int[] { 1, 2, 3 };
  177. result += list.Sum ();
  178. }
  179. }";
  180. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  181. }
  182. [Test]
  183. public void TestExpression() {
  184. string source = @"
  185. using System.Linq;
  186. class TestClass
  187. {
  188. void TestMethod() {
  189. int result = 0;
  190. var list = new int[] { 1, 2, 3 };
  191. $foreach (var x in list) {
  192. result += x * 2;
  193. }
  194. }
  195. }";
  196. string result = @"
  197. using System.Linq;
  198. class TestClass
  199. {
  200. void TestMethod() {
  201. int result = 0;
  202. var list = new int[] { 1, 2, 3 };
  203. result += list.Sum (x => x * 2);
  204. }
  205. }";
  206. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  207. }
  208. [Test]
  209. public void TestDisabledForStrings() {
  210. string source = @"
  211. using System.Linq;
  212. class TestClass
  213. {
  214. void TestMethod() {
  215. string result = string.Empty;
  216. var list = new string[] { ""a"", ""b"" };
  217. $foreach (var x in list) {
  218. result += x;
  219. }
  220. }
  221. }";
  222. TestWrongContext<AutoLinqSumAction>(source);
  223. }
  224. [Test]
  225. public void TestShort() {
  226. string source = @"
  227. using System.Linq;
  228. class TestClass
  229. {
  230. void TestMethod() {
  231. short result = 0;
  232. var list = new short[] { 1, 2, 3 };
  233. $foreach (var x in list)
  234. result += x;
  235. }
  236. }";
  237. string result = @"
  238. using System.Linq;
  239. class TestClass
  240. {
  241. void TestMethod() {
  242. short result = 0;
  243. var list = new short[] { 1, 2, 3 };
  244. result += list.Sum ();
  245. }
  246. }";
  247. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  248. }
  249. [Test]
  250. public void TestLong() {
  251. string source = @"
  252. using System.Linq;
  253. class TestClass
  254. {
  255. void TestMethod() {
  256. long result = 0;
  257. var list = new long[] { 1, 2, 3 };
  258. $foreach (var x in list)
  259. result += x;
  260. }
  261. }";
  262. string result = @"
  263. using System.Linq;
  264. class TestClass
  265. {
  266. void TestMethod() {
  267. long result = 0;
  268. var list = new long[] { 1, 2, 3 };
  269. result += list.Sum ();
  270. }
  271. }";
  272. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  273. }
  274. [Test]
  275. public void TestUnsignedLong() {
  276. string source = @"
  277. using System.Linq;
  278. class TestClass
  279. {
  280. void TestMethod() {
  281. ulong result = 0;
  282. var list = new ulong[] { 1, 2, 3 };
  283. $foreach (var x in list)
  284. result += x;
  285. }
  286. }";
  287. string result = @"
  288. using System.Linq;
  289. class TestClass
  290. {
  291. void TestMethod() {
  292. ulong result = 0;
  293. var list = new ulong[] { 1, 2, 3 };
  294. result += list.Sum ();
  295. }
  296. }";
  297. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  298. }
  299. [Test]
  300. public void TestFloat() {
  301. string source = @"
  302. using System.Linq;
  303. class TestClass
  304. {
  305. void TestMethod() {
  306. float result = 0;
  307. var list = new float[] { 1, 2, 3 };
  308. $foreach (var x in list)
  309. result += x;
  310. }
  311. }";
  312. string result = @"
  313. using System.Linq;
  314. class TestClass
  315. {
  316. void TestMethod() {
  317. float result = 0;
  318. var list = new float[] { 1, 2, 3 };
  319. result += list.Sum ();
  320. }
  321. }";
  322. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  323. }
  324. [Test]
  325. public void TestDouble() {
  326. string source = @"
  327. using System.Linq;
  328. class TestClass
  329. {
  330. void TestMethod() {
  331. double result = 0;
  332. var list = new double[] { 1, 2, 3 };
  333. $foreach (var x in list)
  334. result += x;
  335. }
  336. }";
  337. string result = @"
  338. using System.Linq;
  339. class TestClass
  340. {
  341. void TestMethod() {
  342. double result = 0;
  343. var list = new double[] { 1, 2, 3 };
  344. result += list.Sum ();
  345. }
  346. }";
  347. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  348. }
  349. [Test]
  350. public void TestDecimal() {
  351. string source = @"
  352. using System.Linq;
  353. class TestClass
  354. {
  355. void TestMethod() {
  356. decimal result = 0;
  357. var list = new decimal[] { 1, 2, 3 };
  358. $foreach (var x in list)
  359. result += x;
  360. }
  361. }";
  362. string result = @"
  363. using System.Linq;
  364. class TestClass
  365. {
  366. void TestMethod() {
  367. decimal result = 0;
  368. var list = new decimal[] { 1, 2, 3 };
  369. result += list.Sum ();
  370. }
  371. }";
  372. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  373. }
  374. [Test]
  375. public void TestMinus() {
  376. string source = @"
  377. using System.Linq;
  378. class TestClass
  379. {
  380. void TestMethod() {
  381. int result = 0;
  382. var list = new int[] { 1, 2, 3 };
  383. $foreach (var x in list) {
  384. result -= x;
  385. }
  386. }
  387. }";
  388. string result = @"
  389. using System.Linq;
  390. class TestClass
  391. {
  392. void TestMethod() {
  393. int result = 0;
  394. var list = new int[] { 1, 2, 3 };
  395. result += list.Sum (x => -x);
  396. }
  397. }";
  398. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  399. }
  400. [Test]
  401. public void TestCombined() {
  402. string source = @"
  403. using System.Linq;
  404. class TestClass
  405. {
  406. void TestMethod() {
  407. int result = 0;
  408. var list = new int[] { 1, 2, 3 };
  409. $foreach (var x in list) {
  410. result += x;
  411. result += 2 * x;
  412. }
  413. }
  414. }";
  415. string result = @"
  416. using System.Linq;
  417. class TestClass
  418. {
  419. void TestMethod() {
  420. int result = 0;
  421. var list = new int[] { 1, 2, 3 };
  422. result += list.Sum (x => x + 2 * x);
  423. }
  424. }";
  425. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  426. }
  427. [Test]
  428. public void TestCombinedPrecedence() {
  429. string source = @"
  430. using System.Linq;
  431. class TestClass
  432. {
  433. void TestMethod() {
  434. int result = 0;
  435. var list = new int[] { 1, 2, 3 };
  436. $foreach (var x in list) {
  437. result += x;
  438. result += x << 1;
  439. }
  440. }
  441. }";
  442. string result = @"
  443. using System.Linq;
  444. class TestClass
  445. {
  446. void TestMethod() {
  447. int result = 0;
  448. var list = new int[] { 1, 2, 3 };
  449. result += list.Sum (x => x + (x << 1));
  450. }
  451. }";
  452. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  453. }
  454. [Test]
  455. public void TestEmptyStatements() {
  456. string source = @"
  457. using System.Linq;
  458. class TestClass
  459. {
  460. void TestMethod() {
  461. int result = 0;
  462. var list = new int[] { 1, 2, 3 };
  463. $foreach (var x in list) {
  464. result += x;
  465. ;
  466. }
  467. }
  468. }";
  469. string result = @"
  470. using System.Linq;
  471. class TestClass
  472. {
  473. void TestMethod() {
  474. int result = 0;
  475. var list = new int[] { 1, 2, 3 };
  476. result += list.Sum ();
  477. }
  478. }";
  479. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  480. }
  481. [Test]
  482. public void TestSimpleConditional() {
  483. string source = @"
  484. using System.Linq;
  485. class TestClass
  486. {
  487. void TestMethod() {
  488. int result = 0;
  489. var list = new int[] { 1, 2, 3 };
  490. $foreach (var x in list) {
  491. if (x > 0)
  492. result += x;
  493. }
  494. }
  495. }";
  496. string result = @"
  497. using System.Linq;
  498. class TestClass
  499. {
  500. void TestMethod() {
  501. int result = 0;
  502. var list = new int[] { 1, 2, 3 };
  503. result += list.Where (x => x > 0).Sum ();
  504. }
  505. }";
  506. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  507. }
  508. [Test]
  509. public void TestInvertedConditional() {
  510. string source = @"
  511. using System.Linq;
  512. class TestClass
  513. {
  514. void TestMethod() {
  515. int result = 0;
  516. var list = new int[] { 1, 2, 3 };
  517. $foreach (var x in list) {
  518. if (x > 0)
  519. ;
  520. else
  521. result += x;
  522. }
  523. }
  524. }";
  525. string result = @"
  526. using System.Linq;
  527. class TestClass
  528. {
  529. void TestMethod() {
  530. int result = 0;
  531. var list = new int[] { 1, 2, 3 };
  532. result += list.Where (x => x <= 0).Sum ();
  533. }
  534. }";
  535. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  536. }
  537. [Test]
  538. public void TestIncrement() {
  539. string source = @"
  540. using System.Linq;
  541. class TestClass
  542. {
  543. void TestMethod() {
  544. int result = 0;
  545. var list = new int[] { 1, 2, 3 };
  546. $foreach (var x in list) {
  547. result++;
  548. }
  549. }
  550. }";
  551. string result = @"
  552. using System.Linq;
  553. class TestClass
  554. {
  555. void TestMethod() {
  556. int result = 0;
  557. var list = new int[] { 1, 2, 3 };
  558. result += list.Count ();
  559. }
  560. }";
  561. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  562. }
  563. [Test]
  564. public void TestCompleteConditional() {
  565. string source = @"
  566. using System.Linq;
  567. class TestClass
  568. {
  569. void TestMethod() {
  570. int result = 0;
  571. var list = new int[] { 1, 2, 3 };
  572. $foreach (var x in list) {
  573. if (x > 0)
  574. result += x * 2;
  575. else
  576. result += x;
  577. }
  578. }
  579. }";
  580. string result = @"
  581. using System.Linq;
  582. class TestClass
  583. {
  584. void TestMethod() {
  585. int result = 0;
  586. var list = new int[] { 1, 2, 3 };
  587. result += list.Sum (x => x > 0 ? x * 2 : x);
  588. }
  589. }";
  590. Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
  591. }
  592. [Test]
  593. public void TestDisabledForSideEffects() {
  594. string source = @"
  595. using System.Linq;
  596. class TestClass
  597. {
  598. void TestMethod() {
  599. string result = string.Empty;
  600. var list = new string[] { ""a"", ""b"" };
  601. $foreach (var x in list) {
  602. TestMethod();
  603. result += x;
  604. }
  605. }
  606. }";
  607. TestWrongContext<AutoLinqSumAction>(source);
  608. }
  609. [Test]
  610. public void TestDisabledForInnerAssignments() {
  611. string source = @"
  612. using System.Linq;
  613. class TestClass
  614. {
  615. void TestMethod() {
  616. int result = 0;
  617. var list = new int[] { 1, 2 };
  618. int p = 0;
  619. $foreach (var x in list) {
  620. result += (p = x);
  621. }
  622. }
  623. }";
  624. TestWrongContext<AutoLinqSumAction>(source);
  625. }
  626. [Test]
  627. public void TestDisabledForInnerIncrements() {
  628. string source = @"
  629. using System.Linq;
  630. class TestClass
  631. {
  632. void TestMethod() {
  633. int result = 0;
  634. var list = new int[] { 1, 2 };
  635. int p = 0;
  636. $foreach (var x in list) {
  637. result += (p++);
  638. }
  639. }
  640. }";
  641. TestWrongContext<AutoLinqSumAction>(source);
  642. }
  643. [Test]
  644. public void TestDisabledForNoLinq() {
  645. string source = @"
  646. class TestClass
  647. {
  648. void TestMethod() {
  649. int result = 0;
  650. var list = new int[] { 1, 2 };
  651. $foreach (var x in list) {
  652. result += x;
  653. }
  654. }
  655. }";
  656. TestWrongContext<AutoLinqSumAction>(source);
  657. }
  658. }
  659. }