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

/src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/LinqFluentToQueryTests.cs

https://github.com/pombredanne/SharpDevelop
C# | 627 lines | 579 code | 23 blank | 25 comment | 0 complexity | c49239fd0f8ea72885ef1fa315e15b4d MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-3.0
  1. //
  2. // LinqFluentToQueryTests.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 ICSharpCode.NRefactory.CSharp.Refactoring;
  27. using NUnit.Framework;
  28. namespace ICSharpCode.NRefactory.CSharp.CodeActions
  29. {
  30. [TestFixture]
  31. public class LinqFluentToQueryTests : ContextActionTestBase
  32. {
  33. [Test]
  34. public void TestBasicCase()
  35. {
  36. Test<LinqFluentToQueryAction>(@"
  37. using System.Linq;
  38. class TestClass
  39. {
  40. void TestMethod ()
  41. {
  42. var x = new int[0].$Select (t => t);
  43. }
  44. }", @"
  45. using System.Linq;
  46. class TestClass
  47. {
  48. void TestMethod ()
  49. {
  50. var x = from t in new int[0]
  51. select t;
  52. }
  53. }");
  54. }
  55. [Test]
  56. public void TestAddedParenthesis()
  57. {
  58. Test<LinqFluentToQueryAction>(@"
  59. using System.Linq;
  60. class TestClass
  61. {
  62. void TestMethod ()
  63. {
  64. var x = new int[0].$Select (t => t) + 1;
  65. }
  66. }", @"
  67. using System.Linq;
  68. class TestClass
  69. {
  70. void TestMethod ()
  71. {
  72. var x = (from t in new int[0]
  73. select t) + 1;
  74. }
  75. }");
  76. }
  77. [Test]
  78. public void TestCast()
  79. {
  80. Test<LinqFluentToQueryAction>(@"
  81. using System.Linq;
  82. class TestClass
  83. {
  84. void TestMethod ()
  85. {
  86. var x = new int[0].$Cast<int> ();
  87. }
  88. }", @"
  89. using System.Linq;
  90. class TestClass
  91. {
  92. void TestMethod ()
  93. {
  94. var x = from int _1 in new int[0]
  95. select _1;
  96. }
  97. }");
  98. }
  99. [Test]
  100. public void TestLet()
  101. {
  102. Test<LinqFluentToQueryAction>(@"
  103. using System.Linq;
  104. class TestClass
  105. {
  106. void TestMethod ()
  107. {
  108. var x = new int[0].Select (w => new { w, two = w * 2 }).$Select (_ => _.two);
  109. }
  110. }", @"
  111. using System.Linq;
  112. class TestClass
  113. {
  114. void TestMethod ()
  115. {
  116. var x = from w in new int[0]
  117. let two = w * 2
  118. select two;
  119. }
  120. }");
  121. }
  122. [Test]
  123. public void TestLet2()
  124. {
  125. Test<LinqFluentToQueryAction>(@"
  126. using System.Linq;
  127. class TestClass
  128. {
  129. void TestMethod ()
  130. {
  131. var x = new int[0].Select (w => new { two = w * 2, w }).$Select (_ => _.two);
  132. }
  133. }", @"
  134. using System.Linq;
  135. class TestClass
  136. {
  137. void TestMethod ()
  138. {
  139. var x = from w in new int[0]
  140. let two = w * 2
  141. select two;
  142. }
  143. }");
  144. }
  145. [Test]
  146. public void TestLongLetChain()
  147. {
  148. Test<LinqFluentToQueryAction>(@"
  149. using System.Linq;
  150. class TestClass
  151. {
  152. void TestMethod ()
  153. {
  154. var x = new int[0].Select (w => new { w, two = w * 2 })
  155. .Select (h => new { h, three = h.w * 3 })
  156. .Select (k => new { k, four = k.h.w * 4 })
  157. .$Select (_ => _.k.h.w + _.k.h.two + _.k.three + _.four)
  158. .Select (sum => sum * 2);
  159. }
  160. }", @"
  161. using System.Linq;
  162. class TestClass
  163. {
  164. void TestMethod ()
  165. {
  166. var x = from w in new int[0]
  167. let two = w * 2
  168. let three = w * 3
  169. let four = w * 4
  170. select w + two + three + four into sum
  171. select sum * 2;
  172. }
  173. }");
  174. }
  175. [Test]
  176. public void TestLongLetChain2()
  177. {
  178. Test<LinqFluentToQueryAction>(@"
  179. using System.Linq;
  180. class TestClass
  181. {
  182. void TestMethod ()
  183. {
  184. var x = new int[0].Select (w => new { two = w * 2, w })
  185. .Select (h => new { three = h.w * 3, h })
  186. .Select (k => new { four = k.h.w * 4, k })
  187. .$Select (_ => _.k.h.w + _.k.h.two + _.k.three + _.four)
  188. .Select (sum => sum * 2);
  189. }
  190. }", @"
  191. using System.Linq;
  192. class TestClass
  193. {
  194. void TestMethod ()
  195. {
  196. var x = from w in new int[0]
  197. let two = w * 2
  198. let three = w * 3
  199. let four = w * 4
  200. select w + two + three + four into sum
  201. select sum * 2;
  202. }
  203. }");
  204. }
  205. [Test]
  206. public void TestSelectMany()
  207. {
  208. Test<LinqFluentToQueryAction>(@"
  209. using System.Linq;
  210. class TestClass
  211. {
  212. void TestMethod ()
  213. {
  214. var x = new int[0].$SelectMany (elem => new int[0], (elem1, elem2) => elem1 + elem2);
  215. }
  216. }", @"
  217. using System.Linq;
  218. class TestClass
  219. {
  220. void TestMethod ()
  221. {
  222. var x = from elem1 in new int[0]
  223. from elem2 in new int[0]
  224. select elem1 + elem2;
  225. }
  226. }");
  227. }
  228. [Test]
  229. public void TestSelectManyLet()
  230. {
  231. Test<LinqFluentToQueryAction>(@"
  232. using System.Linq;
  233. class TestClass
  234. {
  235. void TestMethod ()
  236. {
  237. var x = new int[0].$SelectMany (elem => new int[0], (elem1, elem2) => new { elem1, elem2 }).Select(i => new { i, sum = i.elem1 + i.elem2 })
  238. .Select(j => j.i.elem1 + j.i.elem2 + j.sum);
  239. }
  240. }", @"
  241. using System.Linq;
  242. class TestClass
  243. {
  244. void TestMethod ()
  245. {
  246. var x = from elem1 in new int[0]
  247. from elem2 in new int[0]
  248. let sum = elem1 + elem2
  249. select elem1 + elem2 + sum;
  250. }
  251. }");
  252. }
  253. [Test]
  254. public void TestSelectManyLet2()
  255. {
  256. Test<LinqFluentToQueryAction>(@"
  257. using System.Linq;
  258. class TestClass
  259. {
  260. void TestMethod ()
  261. {
  262. var x = new int[0].$SelectMany (elem => new int[0], (elem1, elem2) => new { elem1, elem2 = elem2 + 1 }).Select(i => new { i, sum = i.elem1 + i.elem2 })
  263. .Select(j => j.i.elem1 + j.i.elem2 + j.sum);
  264. }
  265. }", @"
  266. using System.Linq;
  267. class TestClass
  268. {
  269. void TestMethod ()
  270. {
  271. var x = from elem1 in new int[0]
  272. from elem2 in new int[0]
  273. select new {
  274. elem1,
  275. elem2 = elem2 + 1
  276. } into i
  277. let sum = i.elem1 + i.elem2
  278. select i.elem1 + i.elem2 + sum;
  279. }
  280. }");
  281. }
  282. [Test]
  283. public void TestCastSelect()
  284. {
  285. Test<LinqFluentToQueryAction>(@"
  286. using System.Linq;
  287. class TestClass
  288. {
  289. void TestMethod ()
  290. {
  291. var x = new int[0].$Cast<int> ().Select (t => t * 2);
  292. }
  293. }", @"
  294. using System.Linq;
  295. class TestClass
  296. {
  297. void TestMethod ()
  298. {
  299. var x = from int t in new int[0]
  300. select t * 2;
  301. }
  302. }");
  303. }
  304. [Test]
  305. public void TestSelectWhere ()
  306. {
  307. Test<LinqFluentToQueryAction>(@"
  308. using System.Linq;
  309. class TestClass
  310. {
  311. void TestMethod ()
  312. {
  313. var x = new int[0].$Where (t => t > 0).Select (t => t * 2);
  314. }
  315. }", @"
  316. using System.Linq;
  317. class TestClass
  318. {
  319. void TestMethod ()
  320. {
  321. var x = from t in new int[0]
  322. where t > 0
  323. select t * 2;
  324. }
  325. }");
  326. }
  327. [Test]
  328. public void TestSorting ()
  329. {
  330. Test<LinqFluentToQueryAction>(@"
  331. using System.Linq;
  332. class TestClass
  333. {
  334. void TestMethod ()
  335. {
  336. var x = new int[0].$OrderBy (t => t).ThenByDescending (t => t);
  337. }
  338. }", @"
  339. using System.Linq;
  340. class TestClass
  341. {
  342. void TestMethod ()
  343. {
  344. var x = from t in new int[0]
  345. orderby t, t descending
  346. select t;
  347. }
  348. }");
  349. }
  350. [Test]
  351. public void TestDegenerateWhere ()
  352. {
  353. Test<LinqFluentToQueryAction>(@"
  354. using System.Linq;
  355. class TestClass
  356. {
  357. void TestMethod ()
  358. {
  359. var x = new int[0].$Where (t => t > 0);
  360. }
  361. }", @"
  362. using System.Linq;
  363. class TestClass
  364. {
  365. void TestMethod ()
  366. {
  367. var x = from t in new int[0]
  368. where t > 0
  369. select t;
  370. }
  371. }");
  372. }
  373. [Test]
  374. public void TestChain ()
  375. {
  376. Test<LinqFluentToQueryAction>(@"
  377. using System.Linq;
  378. class TestClass
  379. {
  380. void TestMethod ()
  381. {
  382. var x = new int[0].Where (t => t > 0).$Where (u => u > 0);
  383. }
  384. }", @"
  385. using System.Linq;
  386. class TestClass
  387. {
  388. void TestMethod ()
  389. {
  390. var x = from t in new int[0]
  391. where t > 0
  392. select t into u
  393. where u > 0
  394. select u;
  395. }
  396. }");
  397. }
  398. [Test]
  399. public void TestJoin()
  400. {
  401. Test<LinqFluentToQueryAction>(@"
  402. using System.Linq;
  403. class TestClass
  404. {
  405. void TestMethod ()
  406. {
  407. var x = new int[0].Cast<char> ().$Join(new int[0].Cast<float> (), a => a * 2, b => b, (l, r) => l * r);
  408. }
  409. }", @"
  410. using System.Linq;
  411. class TestClass
  412. {
  413. void TestMethod ()
  414. {
  415. var x = from char a in new int[0]
  416. join float b in new int[0] on a * 2 equals b
  417. select a * b;
  418. }
  419. }");
  420. }
  421. [Test]
  422. public void TestGroupJoin()
  423. {
  424. Test<LinqFluentToQueryAction>(@"
  425. using System.Linq;
  426. class TestClass
  427. {
  428. void TestMethod ()
  429. {
  430. var x = new int[0].Cast<char> ().$GroupJoin(new int[0].Cast<float> (), a => a * 2, b => b, (l, r) => l * r [0]);
  431. }
  432. }", @"
  433. using System.Linq;
  434. class TestClass
  435. {
  436. void TestMethod ()
  437. {
  438. var x = from char a in new int[0]
  439. join float b in new int[0] on a * 2 equals b into r
  440. select a * r [0];
  441. }
  442. }");
  443. }
  444. [Test]
  445. public void TestNonRecursive()
  446. {
  447. Test<LinqFluentToQueryAction>(@"
  448. using System.Linq;
  449. class TestClass
  450. {
  451. void TestMethod ()
  452. {
  453. var x = Enumerable.Empty<int[]> ().$Select (t => t.Select (v => v));
  454. }
  455. }", @"
  456. using System.Linq;
  457. class TestClass
  458. {
  459. void TestMethod ()
  460. {
  461. var x = from t in Enumerable.Empty<int[]> ()
  462. select t.Select (v => v);
  463. }
  464. }");
  465. }
  466. [Test]
  467. public void TestNonRecursiveCombineQueries()
  468. {
  469. Test<LinqFluentToQueryAction>(@"
  470. using System.Linq;
  471. class TestClass
  472. {
  473. void TestMethod ()
  474. {
  475. var x = Enumerable.Empty<int[]> ().$Select (t => (from g in t select g));
  476. }
  477. }", @"
  478. using System.Linq;
  479. class TestClass
  480. {
  481. void TestMethod ()
  482. {
  483. var x = from t in Enumerable.Empty<int[]> ()
  484. select (from g in t
  485. select g);
  486. }
  487. }");
  488. }
  489. [Test]
  490. public void TestGroupBy()
  491. {
  492. Test<LinqFluentToQueryAction>(@"
  493. using System.Linq;
  494. class TestClass
  495. {
  496. void TestMethod ()
  497. {
  498. var x = new int[0].$GroupBy (t => t, t => new int[0]);
  499. }
  500. }", @"
  501. using System.Linq;
  502. class TestClass
  503. {
  504. void TestMethod ()
  505. {
  506. var x = from t in new int[0]
  507. group new int[0] by t;
  508. }
  509. }");
  510. }
  511. [Test]
  512. public void Test_1AlreadyUsed()
  513. {
  514. Test<LinqFluentToQueryAction>(@"
  515. using System.Linq;
  516. class TestClass
  517. {
  518. void TestMethod ()
  519. {
  520. int _1;
  521. var x = new int[0].$Cast<float> ();
  522. }
  523. }", @"
  524. using System.Linq;
  525. class TestClass
  526. {
  527. void TestMethod ()
  528. {
  529. int _1;
  530. var x = from float _2 in new int[0]
  531. select _2;
  532. }
  533. }");
  534. }
  535. [Test]
  536. public void TestDoubleCasts()
  537. {
  538. Test<LinqFluentToQueryAction>(@"
  539. using System.Linq;
  540. class TestClass
  541. {
  542. void TestMethod ()
  543. {
  544. var x = new int[0].$Cast<float> ().Cast<int> ();
  545. }
  546. }", @"
  547. using System.Linq;
  548. class TestClass
  549. {
  550. void TestMethod ()
  551. {
  552. var x = from int _1 in
  553. from float _2 in new int[0]
  554. select _2
  555. select _1;
  556. }
  557. }");
  558. }
  559. }
  560. }