PageRenderTime 24ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/icsharpcode/NRefactory
C# | 309 lines | 272 code | 12 blank | 25 comment | 0 complexity | ab0fa959b0838b991fe977f299253544 MD5 | raw file
  1. //
  2. // DontUseLinqWhenItsVerboseAndInefficientTests.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.CodeActions;
  27. using ICSharpCode.NRefactory.CSharp.Refactoring;
  28. using NUnit.Framework;
  29. namespace ICSharpCode.NRefactory.CSharp.CodeIssues
  30. {
  31. [TestFixture]
  32. public class DontUseLinqWhenItsVerboseAndInefficientTests : InspectionActionTestBase
  33. {
  34. [Test]
  35. public void TestStringLength()
  36. {
  37. Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  38. using System.Linq;
  39. class TestClass
  40. {
  41. int TestMethod()
  42. {
  43. string x = ""Hello"";
  44. return x.Count ();
  45. }
  46. }", @"
  47. using System.Linq;
  48. class TestClass
  49. {
  50. int TestMethod()
  51. {
  52. string x = ""Hello"";
  53. return x.Length;
  54. }
  55. }");
  56. }
  57. [Test]
  58. public void TestArrayCount()
  59. {
  60. Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  61. using System.Linq;
  62. class TestClass
  63. {
  64. int TestMethod()
  65. {
  66. int[] x = { 1, 2, 3 };
  67. return x.Count ();
  68. }
  69. }", @"
  70. using System.Linq;
  71. class TestClass
  72. {
  73. int TestMethod()
  74. {
  75. int[] x = { 1, 2, 3 };
  76. return x.Length;
  77. }
  78. }");
  79. }
  80. [Test]
  81. public void TestStaticMethod()
  82. {
  83. TestWrongContext<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  84. using System.Linq;
  85. class TestClass
  86. {
  87. int TestMethod()
  88. {
  89. int[] x = { 1, 2, 3 };
  90. return Enumerable.Count<int> (x);
  91. }
  92. }");
  93. }
  94. [Test]
  95. public void TestListCount()
  96. {
  97. Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  98. using System.Linq;
  99. using System.Collections.Generic;
  100. class TestClass
  101. {
  102. int TestMethod()
  103. {
  104. IList<int> x = new List<int> ();
  105. return x.Count ();
  106. }
  107. }", @"
  108. using System.Linq;
  109. using System.Collections.Generic;
  110. class TestClass
  111. {
  112. int TestMethod()
  113. {
  114. IList<int> x = new List<int> ();
  115. return x.Count;
  116. }
  117. }");
  118. }
  119. [Test]
  120. public void TestListFirst()
  121. {
  122. Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  123. using System.Linq;
  124. using System.Collections.Generic;
  125. class TestClass
  126. {
  127. int TestMethod()
  128. {
  129. IList<int> x = new List<int> ();
  130. return x.First ();
  131. }
  132. }", @"
  133. using System.Linq;
  134. using System.Collections.Generic;
  135. class TestClass
  136. {
  137. int TestMethod()
  138. {
  139. IList<int> x = new List<int> ();
  140. return x [0];
  141. }
  142. }");
  143. }
  144. [Test]
  145. public void TestListElementAt()
  146. {
  147. Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  148. using System.Linq;
  149. using System.Collections.Generic;
  150. class TestClass
  151. {
  152. int TestMethod()
  153. {
  154. IList<int> x = new List<int> ();
  155. return x.ElementAt (1);
  156. }
  157. }", @"
  158. using System.Linq;
  159. using System.Collections.Generic;
  160. class TestClass
  161. {
  162. int TestMethod()
  163. {
  164. IList<int> x = new List<int> ();
  165. return x [1];
  166. }
  167. }");
  168. }
  169. [Test]
  170. public void TestListLast()
  171. {
  172. TestWrongContext<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  173. using System.Linq;
  174. using System.Collections.Generic;
  175. class TestClass
  176. {
  177. int TestMethod()
  178. {
  179. IList<int> x = new List<int> ();
  180. return x.Last ();
  181. }
  182. }");
  183. }
  184. [Test]
  185. public void TestListSkippedReversedLast()
  186. {
  187. Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  188. using System.Linq;
  189. using System.Collections.Generic;
  190. class TestClass
  191. {
  192. int TestMethod()
  193. {
  194. int[] x = new int [10];
  195. return x.Skip (5).Reverse ().Last ();
  196. }
  197. }", @"
  198. using System.Linq;
  199. using System.Collections.Generic;
  200. class TestClass
  201. {
  202. int TestMethod()
  203. {
  204. int[] x = new int [10];
  205. return x [5];
  206. }
  207. }");
  208. }
  209. [Test]
  210. public void TestListSkippedLast()
  211. {
  212. Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  213. using System.Linq;
  214. using System.Collections.Generic;
  215. class TestClass
  216. {
  217. int TestMethod()
  218. {
  219. int[] x = new int [10];
  220. return x.Skip (5).Reverse ().Skip (5).Reverse ().Last ();
  221. }
  222. }", @"
  223. using System.Linq;
  224. using System.Collections.Generic;
  225. class TestClass
  226. {
  227. int TestMethod()
  228. {
  229. int[] x = new int [10];
  230. return x [x.Length - 1 - (5)];
  231. }
  232. }");
  233. }
  234. [Test]
  235. public void TestListSkip()
  236. {
  237. Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  238. using System.Linq;
  239. using System.Collections.Generic;
  240. class TestClass
  241. {
  242. int TestMethod()
  243. {
  244. IList<int> x = new List<int> ();
  245. int y = 5;
  246. return x.Skip (y).First ();
  247. }
  248. }", @"
  249. using System.Linq;
  250. using System.Collections.Generic;
  251. class TestClass
  252. {
  253. int TestMethod()
  254. {
  255. IList<int> x = new List<int> ();
  256. int y = 5;
  257. return x [y];
  258. }
  259. }");
  260. }
  261. [Test]
  262. public void TestDisabledForNoCount()
  263. {
  264. TestWrongContext<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
  265. using System.Linq;
  266. using System.Collections;
  267. using System.Collections.Generic;
  268. class MyCollection : ICollection<int>
  269. {
  270. int ICollection<int>.Count { get { return 0; } }
  271. bool ICollection<int>.IsReadOnly { get { return true; } }
  272. public IEnumerator<int> GetEnumerator()
  273. {
  274. return null;
  275. }
  276. IEnumerator IEnumerable.GetEnumerator()
  277. {
  278. return GetEnumerator();
  279. }
  280. public void Add(int item) { }
  281. public bool Contains(int item) { return false; }
  282. public void CopyTo(int[] array, int arrayIndex) {}
  283. public bool Remove(int item) { return false; }
  284. }
  285. class TestClass
  286. {
  287. int TestMethod()
  288. {
  289. MyCollection x = new MyCollection ();
  290. return x.Count ();
  291. }
  292. }");
  293. }
  294. }
  295. }