PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/icsharpcode/NRefactory
C# | 690 lines | 603 code | 62 blank | 25 comment | 0 complexity | 91b93366188f65da6e9ab46b488b1c99 MD5 | raw file
  1. //
  2. // LinqQueryToFluentTests.cs
  3. //
  4. // Author:
  5. // Luís Reis <luiscubal@gmail.com>
  6. //
  7. // Copyright (c) 2013 Luís Reis
  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 System;
  27. using NUnit.Framework;
  28. using ICSharpCode.NRefactory.CSharp.Refactoring;
  29. namespace ICSharpCode.NRefactory.CSharp.CodeActions
  30. {
  31. [TestFixture]
  32. public class LinqQueryToFluentTests : ContextActionTestBase
  33. {
  34. [Test]
  35. public void TestSimpleQuery()
  36. {
  37. string input = @"
  38. using System.Linq;
  39. public class TestClass
  40. {
  41. public void TestMethod()
  42. {
  43. var data = $from x in System.Enumerable.Empty<int> ()
  44. select x;
  45. }
  46. }
  47. ";
  48. string output = @"
  49. using System.Linq;
  50. public class TestClass
  51. {
  52. public void TestMethod()
  53. {
  54. var data = System.Enumerable.Empty<int> ().Select (x => x);
  55. }
  56. }
  57. ";
  58. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  59. }
  60. [Test]
  61. public void TestName()
  62. {
  63. string input = @"
  64. using System.Linq;
  65. public class TestClass
  66. {
  67. public void TestMethod()
  68. {
  69. int _;
  70. var _2 = $from x in System.Enumerable.Empty<int> ()
  71. let _1 = x
  72. select x;
  73. }
  74. }
  75. ";
  76. string output = @"
  77. using System.Linq;
  78. public class TestClass
  79. {
  80. public void TestMethod()
  81. {
  82. int _;
  83. var _2 = System.Enumerable.Empty<int> ().Select (x => new {
  84. x,
  85. _1 = x
  86. }).Select (_3 => _3.x);
  87. }
  88. }
  89. ";
  90. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  91. }
  92. [Test]
  93. public void TestPrecedence()
  94. {
  95. string input = @"
  96. using System.Linq;
  97. public class TestClass
  98. {
  99. public void TestMethod()
  100. {
  101. var data = $from x in true ? System.Enumerable.Empty<int> () : null
  102. select x;
  103. }
  104. }
  105. ";
  106. string output = @"
  107. using System.Linq;
  108. public class TestClass
  109. {
  110. public void TestMethod()
  111. {
  112. var data = (true ? System.Enumerable.Empty<int> () : null).Select (x => x);
  113. }
  114. }
  115. ";
  116. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  117. }
  118. [Test]
  119. public void TestWhereQuery()
  120. {
  121. string input = @"
  122. using System.Linq;
  123. public class TestClass
  124. {
  125. public void TestMethod()
  126. {
  127. var data = $from x in System.Enumerable.Empty<int> ()
  128. where x > 1
  129. select x;
  130. }
  131. }
  132. ";
  133. string output = @"
  134. using System.Linq;
  135. public class TestClass
  136. {
  137. public void TestMethod()
  138. {
  139. var data = System.Enumerable.Empty<int> ().Where (x => x > 1);
  140. }
  141. }
  142. ";
  143. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  144. }
  145. [Test]
  146. public void TestOrderByQuery()
  147. {
  148. string input = @"
  149. using System.Linq;
  150. public class TestClass
  151. {
  152. public void TestMethod()
  153. {
  154. var data = $from x in System.Enumerable.Empty<int> ()
  155. orderby x, x * 2 descending
  156. select x;
  157. }
  158. }
  159. ";
  160. string output = @"
  161. using System.Linq;
  162. public class TestClass
  163. {
  164. public void TestMethod()
  165. {
  166. var data = System.Enumerable.Empty<int> ().OrderBy (x => x).ThenByDescending (x => x * 2);
  167. }
  168. }
  169. ";
  170. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  171. }
  172. [Test]
  173. public void TestDoubleFromWithSelectQuery()
  174. {
  175. string input = @"
  176. using System.Linq;
  177. public class TestClass
  178. {
  179. public void TestMethod()
  180. {
  181. var newEnumerable = System.Enumerable.Empty<int> ();
  182. var data = $from x in System.Enumerable.Empty<int> ()
  183. from y in newEnumerable
  184. select x * y;
  185. }
  186. }
  187. ";
  188. string output = @"
  189. using System.Linq;
  190. public class TestClass
  191. {
  192. public void TestMethod()
  193. {
  194. var newEnumerable = System.Enumerable.Empty<int> ();
  195. var data = System.Enumerable.Empty<int> ().SelectMany (x => newEnumerable, (x, y) => x * y);
  196. }
  197. }
  198. ";
  199. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  200. }
  201. [Test]
  202. public void TestDoubleFromWithCastQuery()
  203. {
  204. string input = @"
  205. using System.Linq;
  206. public class TestClass
  207. {
  208. public void TestMethod()
  209. {
  210. var newEnumerable = System.Enumerable.Empty<int> ();
  211. var data = $from x in System.Enumerable.Empty<int> ()
  212. from float y in newEnumerable
  213. select x * y;
  214. }
  215. }
  216. ";
  217. string output = @"
  218. using System.Linq;
  219. public class TestClass
  220. {
  221. public void TestMethod()
  222. {
  223. var newEnumerable = System.Enumerable.Empty<int> ();
  224. var data = System.Enumerable.Empty<int> ().SelectMany (x => newEnumerable.Cast<float> (), (x, y) => x * y);
  225. }
  226. }
  227. ";
  228. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  229. }
  230. [Test]
  231. public void TestDoubleFromWithIntermediateQuery()
  232. {
  233. string input = @"
  234. using System.Linq;
  235. public class TestClass
  236. {
  237. public void TestMethod()
  238. {
  239. var newEnumerable = System.Enumerable.Empty<int> ();
  240. var data = $from x in System.Enumerable.Empty<int> ()
  241. from y in newEnumerable
  242. where x > y
  243. select x * y;
  244. }
  245. }
  246. ";
  247. string output = @"
  248. using System.Linq;
  249. public class TestClass
  250. {
  251. public void TestMethod()
  252. {
  253. var newEnumerable = System.Enumerable.Empty<int> ();
  254. var data = System.Enumerable.Empty<int> ().SelectMany (x => newEnumerable, (x, y) => new {
  255. x,
  256. y
  257. }).Where (_ => _.x > _.y).Select (_1 => _1.x * _1.y);
  258. }
  259. }
  260. ";
  261. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  262. }
  263. [Test]
  264. public void TestLetQuery()
  265. {
  266. string input = @"
  267. using System.Linq;
  268. public class TestClass
  269. {
  270. public void TestMethod()
  271. {
  272. var data = $from x in System.Enumerable.Empty<int> ()
  273. let y = x * 2
  274. select x * y;
  275. }
  276. }
  277. ";
  278. string output = @"
  279. using System.Linq;
  280. public class TestClass
  281. {
  282. public void TestMethod()
  283. {
  284. var data = System.Enumerable.Empty<int> ().Select (x => new {
  285. x,
  286. y = x * 2
  287. }).Select (_ => _.x * _.y);
  288. }
  289. }
  290. ";
  291. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  292. }
  293. [Test]
  294. public void TestLongChainQuery()
  295. {
  296. string input = @"
  297. using System.Linq;
  298. public class TestClass
  299. {
  300. public void TestMethod()
  301. {
  302. var data = $from x in System.Enumerable.Empty<int> ()
  303. let y = x * 2
  304. let z = x * y * 2
  305. select x * y * z;
  306. }
  307. }
  308. ";
  309. string output = @"
  310. using System.Linq;
  311. public class TestClass
  312. {
  313. public void TestMethod()
  314. {
  315. var data = System.Enumerable.Empty<int> ().Select (x => new {
  316. x,
  317. y = x * 2
  318. }).Select (_ => new {
  319. _,
  320. z = _.x * _.y * 2
  321. }).Select (_1 => _1._.x * _1._.y * _1.z);
  322. }
  323. }
  324. ";
  325. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  326. }
  327. [Test]
  328. public void TestCastQuery()
  329. {
  330. string input = @"
  331. using System.Linq;
  332. public class TestClass
  333. {
  334. public void TestMethod()
  335. {
  336. var data = $from float x in System.Enumerable.Empty<int> ()
  337. select x;
  338. }
  339. }
  340. ";
  341. string output = @"
  342. using System.Linq;
  343. public class TestClass
  344. {
  345. public void TestMethod()
  346. {
  347. var data = System.Enumerable.Empty<int> ().Cast<float> ().Select (x => x);
  348. }
  349. }
  350. ";
  351. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  352. }
  353. [Test]
  354. public void TestJoinQuery()
  355. {
  356. string input = @"
  357. using System.Linq;
  358. public class TestClass
  359. {
  360. public void TestMethod()
  361. {
  362. var newEnumerable = new int[] { 4, 5, 6 };
  363. var data = $from x in System.Enumerable.Empty<int> ()
  364. join float yy in newEnumerable on x * 2 equals yy
  365. select x * yy;
  366. }
  367. }
  368. ";
  369. string output = @"
  370. using System.Linq;
  371. public class TestClass
  372. {
  373. public void TestMethod()
  374. {
  375. var newEnumerable = new int[] { 4, 5, 6 };
  376. var data = System.Enumerable.Empty<int> ().Join (newEnumerable.Cast<float> (), x => x * 2, yy => yy, (x, yy) => x * yy);
  377. }
  378. }
  379. ";
  380. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  381. }
  382. [Test]
  383. public void TestJoinWithIntermediateQuery()
  384. {
  385. string input = @"
  386. using System.Linq;
  387. public class TestClass
  388. {
  389. public void TestMethod()
  390. {
  391. var newEnumerable = new int[] { 4, 5, 6 };
  392. var data = $from x in System.Enumerable.Empty<int> ()
  393. join float y in newEnumerable on x * 2 equals y
  394. where x == 2
  395. select x * y;
  396. }
  397. }
  398. ";
  399. string output = @"
  400. using System.Linq;
  401. public class TestClass
  402. {
  403. public void TestMethod()
  404. {
  405. var newEnumerable = new int[] { 4, 5, 6 };
  406. var data = System.Enumerable.Empty<int> ().Join (newEnumerable.Cast<float> (), x => x * 2, y => y, (x, y) => new {
  407. x,
  408. y
  409. }).Where (_ => _.x == 2).Select (_1 => _1.x * _1.y);
  410. }
  411. }
  412. ";
  413. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  414. }
  415. [Test]
  416. public void TestJoinWithIntoSelectQuery()
  417. {
  418. string input = @"
  419. using System.Linq;
  420. public class TestClass
  421. {
  422. public void TestMethod()
  423. {
  424. var newEnumerable = new int[] { 1, 2, 3 };
  425. var data = $from x in System.Enumerable.Empty<int> ()
  426. join y in newEnumerable on x * 2 equals y
  427. into g
  428. select g;
  429. }
  430. }
  431. ";
  432. string output = @"
  433. using System.Linq;
  434. public class TestClass
  435. {
  436. public void TestMethod()
  437. {
  438. var newEnumerable = new int[] { 1, 2, 3 };
  439. var data = System.Enumerable.Empty<int> ().GroupJoin (newEnumerable, x => x * 2, y => y, (x, g) => g);
  440. }
  441. }
  442. ";
  443. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  444. }
  445. [Test]
  446. public void TestJoinWithIntoIntermediateQuery()
  447. {
  448. string input = @"
  449. using System.Linq;
  450. public class TestClass
  451. {
  452. public void TestMethod()
  453. {
  454. var newEnumerable = new int[] { 1, 2, 3 };
  455. var data = $from x in System.Enumerable.Empty<int> ()
  456. join y in newEnumerable on x * 2 equals y
  457. into g
  458. where true
  459. select g;
  460. }
  461. }
  462. ";
  463. string output = @"
  464. using System.Linq;
  465. public class TestClass
  466. {
  467. public void TestMethod()
  468. {
  469. var newEnumerable = new int[] { 1, 2, 3 };
  470. var data = System.Enumerable.Empty<int> ().GroupJoin (newEnumerable, x => x * 2, y => y, (x, g) => new {
  471. x,
  472. g
  473. }).Where (_ => true).Select (_1 => _1.g);
  474. }
  475. }
  476. ";
  477. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  478. }
  479. [Test]
  480. public void TestSimpleGroup()
  481. {
  482. string input = @"
  483. using System.Linq;
  484. public class TestClass
  485. {
  486. public void TestMethod()
  487. {
  488. var data = $from x in System.Enumerable.Empty<int> ()
  489. group x by x % 10;
  490. }
  491. }
  492. ";
  493. string output = @"
  494. using System.Linq;
  495. public class TestClass
  496. {
  497. public void TestMethod()
  498. {
  499. var data = System.Enumerable.Empty<int> ().GroupBy (x => x % 10);
  500. }
  501. }
  502. ";
  503. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  504. }
  505. [Test]
  506. public void TestDifferentGroup()
  507. {
  508. string input = @"
  509. using System.Linq;
  510. public class TestClass
  511. {
  512. public void TestMethod()
  513. {
  514. var data = $from x in System.Enumerable.Empty<int> ()
  515. group x / 10 by x % 10;
  516. }
  517. }
  518. ";
  519. string output = @"
  520. using System.Linq;
  521. public class TestClass
  522. {
  523. public void TestMethod()
  524. {
  525. var data = System.Enumerable.Empty<int> ().GroupBy (x => x % 10, x => x / 10);
  526. }
  527. }
  528. ";
  529. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  530. }
  531. [Test]
  532. public void TestInto()
  533. {
  534. string input = @"
  535. using System.Linq;
  536. public class TestClass
  537. {
  538. public void TestMethod()
  539. {
  540. var data = $from x in System.Enumerable.Empty<int> ()
  541. select x * 2 into y
  542. select y * 3;
  543. }
  544. }
  545. ";
  546. string output = @"
  547. using System.Linq;
  548. public class TestClass
  549. {
  550. public void TestMethod()
  551. {
  552. var data = System.Enumerable.Empty<int> ().Select (x => x * 2).Select (y => y * 3);
  553. }
  554. }
  555. ";
  556. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  557. }
  558. [Test]
  559. public void TestDoubleFromWithLet()
  560. {
  561. string input = @"
  562. using System.Linq;
  563. public class TestClass
  564. {
  565. public void TestMethod()
  566. {
  567. var src = System.Enumerable.Empty<int> ();
  568. var data = $from x in src
  569. from y in src
  570. let k = x * y
  571. select k;
  572. }
  573. }
  574. ";
  575. string output = @"
  576. using System.Linq;
  577. public class TestClass
  578. {
  579. public void TestMethod()
  580. {
  581. var src = System.Enumerable.Empty<int> ();
  582. var data = src.SelectMany (x => src, (x, y) => new {
  583. x,
  584. y
  585. }).Select (_ => new {
  586. _,
  587. k = _.x * _.y
  588. }).Select (_1 => _1.k);
  589. }
  590. }
  591. ";
  592. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  593. }
  594. [Test]
  595. public void TestDoubleFromWithMidLet()
  596. {
  597. string input = @"
  598. using System.Linq;
  599. public class TestClass
  600. {
  601. public void TestMethod()
  602. {
  603. var src = System.Enumerable.Empty<int> ();
  604. var data = $from x in src
  605. let k = x * x
  606. from y in src
  607. select k * y;
  608. }
  609. }
  610. ";
  611. string output = @"
  612. using System.Linq;
  613. public class TestClass
  614. {
  615. public void TestMethod()
  616. {
  617. var src = System.Enumerable.Empty<int> ();
  618. var data = src.Select (x => new {
  619. x,
  620. k = x * x
  621. }).SelectMany (_ => src, (_1, y) => _1.k * y);
  622. }
  623. }
  624. ";
  625. Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
  626. }
  627. }
  628. }