PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/tetonedge/linux
C# | 562 lines | 520 code | 17 blank | 25 comment | 0 complexity | 532263e4e8d595f6c3dbd95f64f6a76c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, CC-BY-SA-3.0, MIT, WTFPL
  1. //
  2. // LoopCanBeConvertedToQueryIssueTests.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mkrueger@xamarin.com>
  6. //
  7. // Copyright (c) 2013 Xamarin Inc. (http://xamarin.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.CodeActions;
  27. using ICSharpCode.NRefactory.CSharp.Refactoring;
  28. using NUnit.Framework;
  29. namespace ICSharpCode.NRefactory.CSharp.CodeIssues
  30. {
  31. [Ignore]
  32. [TestFixture]
  33. public class LoopCanBeConvertedToQueryIssueTests : InspectionActionTestBase
  34. {
  35. [Test]
  36. public void TestCount()
  37. {
  38. Test<LoopCanBeConvertedToQueryIssue>(@"
  39. using System;
  40. public class Bar
  41. {
  42. public void Foo (int[] i)
  43. {
  44. int count = 0;
  45. foreach (var x in i)
  46. count++;
  47. }
  48. }
  49. ", @"
  50. using System;
  51. using System.Linq;
  52. public class Bar
  53. {
  54. public void Foo (int[] i)
  55. {
  56. int sum = i.Count ();
  57. }
  58. }
  59. ");
  60. }
  61. [Test]
  62. public void TestCountCase2()
  63. {
  64. Test<LoopCanBeConvertedToQueryIssue>(@"
  65. using System;
  66. public class Bar
  67. {
  68. public void Foo (int[] i)
  69. {
  70. int count = 1;
  71. foreach (var x in i)
  72. count = count + 1;
  73. }
  74. }
  75. ", @"
  76. using System;
  77. using System.Linq;
  78. public class Bar
  79. {
  80. public void Foo (int[] i)
  81. {
  82. int sum = 1 + i.Count ();
  83. }
  84. }
  85. ");
  86. }
  87. [Test]
  88. public void TestCountPredicate()
  89. {
  90. Test<LoopCanBeConvertedToQueryIssue>(@"
  91. using System;
  92. public class Bar
  93. {
  94. public void Foo (int[] i)
  95. {
  96. int count = 0;
  97. foreach (var x in i)
  98. if (x < 10)
  99. count++;
  100. }
  101. }
  102. ", @"
  103. using System;
  104. using System.Linq;
  105. public class Bar
  106. {
  107. public void Foo (int[] i)
  108. {
  109. int sum = 1 + i.Count (x => x < 10);
  110. }
  111. }
  112. ");
  113. }
  114. [Test]
  115. public void TestForeachSum()
  116. {
  117. Test<LoopCanBeConvertedToQueryIssue>(@"
  118. using System;
  119. public class Bar
  120. {
  121. public void Foo (int[] i)
  122. {
  123. int sum = 0;
  124. foreach (var j in i) {
  125. sum += j;
  126. }
  127. }
  128. }
  129. ", @"
  130. using System;
  131. using System.Linq;
  132. public class Bar
  133. {
  134. public void Foo (int[] i)
  135. {
  136. int sum = i.Sum ();
  137. }
  138. }
  139. ");
  140. }
  141. [Test]
  142. public void TestForSum()
  143. {
  144. Test<LoopCanBeConvertedToQueryIssue>(@"
  145. using System;
  146. public class Bar
  147. {
  148. public void Foo (int[] i)
  149. {
  150. int sum = 0;
  151. for (int j = 0; j < i.Length; j++)
  152. sum += i [j];
  153. }
  154. }
  155. ", @"
  156. using System;
  157. using System.Linq;
  158. public class Bar
  159. {
  160. public void Foo (int[] i)
  161. {
  162. int sum = i.Sum ();
  163. }
  164. }
  165. ");
  166. }
  167. [Test]
  168. public void TestForBinaryExpression()
  169. {
  170. Test<LoopCanBeConvertedToQueryIssue>(@"
  171. using System;
  172. public class Bar
  173. {
  174. public void Foo (int[] i)
  175. {
  176. int mult = 1;
  177. for (int j = 0; j < i.Length; j++) {
  178. mult = mult * i[j];
  179. }
  180. }
  181. }
  182. ", @"
  183. using System;
  184. using System.Linq;
  185. public class Bar
  186. {
  187. public void Foo (int[] i)
  188. {
  189. int mult = i.Aggregate (1, (current, t) => current*t);
  190. }
  191. }
  192. ");
  193. }
  194. [Test]
  195. public void TestMaxCase1()
  196. {
  197. Test<LoopCanBeConvertedToQueryIssue>(@"
  198. using System;
  199. public class Bar
  200. {
  201. public void Foo (int[] i)
  202. {
  203. int max = 0;
  204. foreach (var x in i) {
  205. max = Math.Max (max, x);
  206. }
  207. }
  208. }
  209. ", @"
  210. using System;
  211. using System.Linq;
  212. public class Bar
  213. {
  214. public void Foo (int[] i)
  215. {
  216. int max = i.Concat (new[] {0}).Max ();
  217. }
  218. }
  219. ");
  220. }
  221. [Test]
  222. public void TestMaxCase2()
  223. {
  224. Test<LoopCanBeConvertedToQueryIssue>(@"
  225. using System;
  226. public class Bar
  227. {
  228. public void Foo (int[] i)
  229. {
  230. int max = 0;
  231. foreach (var x in i) {
  232. max = Math.Max (x, max);
  233. }
  234. }
  235. }
  236. ", @"
  237. using System;
  238. using System.Linq;
  239. public class Bar
  240. {
  241. public void Foo (int[] i)
  242. {
  243. int max = i.Concat (new[] {0}).Max ();
  244. }
  245. }
  246. ");
  247. }
  248. [Test]
  249. public void TestMaxCase3()
  250. {
  251. Test<LoopCanBeConvertedToQueryIssue>(@"
  252. using System;
  253. public class Bar
  254. {
  255. public void Foo (int[] i)
  256. {
  257. int max = 0;
  258. foreach (var x in i) {
  259. max = x > max ? x : max;
  260. }
  261. }
  262. }
  263. ", @"
  264. using System;
  265. using System.Linq;
  266. public class Bar
  267. {
  268. public void Foo (int[] i)
  269. {
  270. int max = i.Concat (new[] {0}).Max ();
  271. }
  272. }
  273. ");
  274. Test<LoopCanBeConvertedToQueryIssue>(@"
  275. using System;
  276. public class Bar
  277. {
  278. public void Foo (int[] i)
  279. {
  280. int max = 0;
  281. foreach (var x in i) {
  282. max = x >= max ? x : max;
  283. }
  284. }
  285. }
  286. ", @"
  287. using System;
  288. using System.Linq;
  289. public class Bar
  290. {
  291. public void Foo (int[] i)
  292. {
  293. int max = i.Concat (new[] {0}).Max ();
  294. }
  295. }
  296. ");
  297. }
  298. [Test]
  299. public void TestMaxCase4()
  300. {
  301. Test<LoopCanBeConvertedToQueryIssue>(@"
  302. using System;
  303. public class Bar
  304. {
  305. public void Foo (int[] i)
  306. {
  307. int max = 0;
  308. foreach (var x in i) {
  309. max = x < max ? max : x;
  310. }
  311. }
  312. }
  313. ", @"
  314. using System;
  315. using System.Linq;
  316. public class Bar
  317. {
  318. public void Foo (int[] i)
  319. {
  320. int max = i.Concat (new[] {0}).Max ();
  321. }
  322. }
  323. ");
  324. Test<LoopCanBeConvertedToQueryIssue>(@"
  325. using System;
  326. public class Bar
  327. {
  328. public void Foo (int[] i)
  329. {
  330. int max = 0;
  331. foreach (var x in i) {
  332. max = x <= max ? max : x;
  333. }
  334. }
  335. }
  336. ", @"
  337. using System;
  338. using System.Linq;
  339. public class Bar
  340. {
  341. public void Foo (int[] i)
  342. {
  343. int max = i.Concat (new[] {0}).Max ();
  344. }
  345. }
  346. ");
  347. }
  348. [Test]
  349. public void TestMinCase1()
  350. {
  351. Test<LoopCanBeConvertedToQueryIssue>(@"
  352. using System;
  353. public class Bar
  354. {
  355. public void Foo (int[] i)
  356. {
  357. int max = 0;
  358. foreach (var x in i) {
  359. max = Math.Min (max, x);
  360. }
  361. }
  362. }
  363. ", @"
  364. using System;
  365. using System.Linq;
  366. public class Bar
  367. {
  368. public void Foo (int[] i)
  369. {
  370. int max = i.Concat (new[] {0}).Min ();
  371. }
  372. }
  373. ");
  374. }
  375. [Test]
  376. public void TestCastMatch()
  377. {
  378. Test<LoopCanBeConvertedToQueryIssue>(@"
  379. using System;
  380. using System.Collections.Generic;
  381. using System.Linq;
  382. public class Bar
  383. {
  384. public void Foo (object[] i)
  385. {
  386. List<Bar> list = new List<Bar>();
  387. foreach (var x in i) {
  388. list.Add((Bar)x);
  389. }
  390. }
  391. }
  392. ", @"
  393. using System;
  394. using System.Collections.Generic;
  395. using System.Linq;
  396. public class Bar
  397. {
  398. public void Foo (object[] i)
  399. {
  400. List<Bar> list = i.Cast<Bar> ().ToList ();
  401. }
  402. }
  403. ");
  404. }
  405. [Test]
  406. public void TestOfTypeMatch()
  407. {
  408. Test<LoopCanBeConvertedToQueryIssue>(@"
  409. using System;
  410. using System.Collections.Generic;
  411. using System.Linq;
  412. public class Bar
  413. {
  414. public void Foo (object[] i)
  415. {
  416. List<Bar> list = new List<Bar>();
  417. foreach (var x in i) {
  418. if (x is Bar)
  419. list.Add((Bar)x);
  420. }
  421. }
  422. }
  423. ", @"
  424. using System;
  425. using System.Collections.Generic;
  426. using System.Linq;
  427. public class Bar
  428. {
  429. public void Foo (object[] i)
  430. {
  431. List<Bar> list = i.OfType<Bar> ().ToList ();
  432. }
  433. }
  434. ");
  435. }
  436. [Test]
  437. public void TestFirstOrDefault()
  438. {
  439. Test<LoopCanBeConvertedToQueryIssue>(@"
  440. using System;
  441. public class Bar
  442. {
  443. public void Foo (object[] i)
  444. {
  445. object first = null;
  446. foreach (var x in i){
  447. first = x;
  448. break;
  449. }
  450. }
  451. }
  452. ", @"
  453. using System;
  454. using System.Linq;
  455. public class Bar
  456. {
  457. public void Foo (object[] i)
  458. {
  459. object first = i.FirstOrDefault ();
  460. }
  461. }
  462. ");
  463. }
  464. [Test]
  465. public void TestLastOrDefault()
  466. {
  467. Test<LoopCanBeConvertedToQueryIssue>(@"
  468. using System;
  469. public class Bar
  470. {
  471. public void Foo (object[] i)
  472. {
  473. object last = null;
  474. foreach (var x in i){
  475. last = x;
  476. }
  477. }
  478. }
  479. ", @"
  480. using System;
  481. using System.Linq;
  482. public class Bar
  483. {
  484. public void Foo (object[] i)
  485. {
  486. object last = i.LastOrDefault ();
  487. }
  488. }
  489. ");
  490. }
  491. [Test]
  492. public void TestDisable()
  493. {
  494. TestWrongContext<LoopCanBeConvertedToQueryIssue>(@"
  495. using System;
  496. public class Bar
  497. {
  498. public void Foo(int[] i)
  499. {
  500. int sum = 0;
  501. // ReSharper disable once LoopCanBeConvertedToQuery
  502. foreach (var j in i) {
  503. sum += j;
  504. }
  505. }
  506. }
  507. ");
  508. }
  509. }
  510. }