PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/EdwardWu99/ILSpy
C# | 335 lines | 297 code | 13 blank | 25 comment | 0 complexity | d7ccf600c8a85fd9b102d92e324619bb MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // IterateViaForeachTests.cs
  3. //
  4. // Author:
  5. // Simon Lindgren <simon.n.lindgren@gmail.com>
  6. //
  7. // Copyright (c) 2012 Simon Lindgren
  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 NUnit.Framework;
  27. using ICSharpCode.NRefactory.CSharp.Refactoring;
  28. using System.Linq;
  29. namespace ICSharpCode.NRefactory.CSharp.CodeActions
  30. {
  31. [TestFixture]
  32. public class IterateViaForeachTests : ContextActionTestBase
  33. {
  34. [Test]
  35. public void HandlesNonGenericCase()
  36. {
  37. Test<IterateViaForeachAction>(@"
  38. using System.Collections;
  39. class TestClass
  40. {
  41. public void F()
  42. {
  43. var $list = new ArrayList ();
  44. }
  45. }", @"
  46. using System.Collections;
  47. class TestClass
  48. {
  49. public void F()
  50. {
  51. var list = new ArrayList ();
  52. foreach (var o in list) {
  53. }
  54. }
  55. }");
  56. }
  57. [Test]
  58. public void HandlesExpressionStatement()
  59. {
  60. Test<IterateViaForeachAction>(@"
  61. using System.Collections.Generic;
  62. class TestClass
  63. {
  64. public IEnumerable<int> GetInts()
  65. {
  66. return new int[] { };
  67. }
  68. public void F()
  69. {
  70. GetInts$();
  71. }
  72. }", @"
  73. using System.Collections.Generic;
  74. class TestClass
  75. {
  76. public IEnumerable<int> GetInts()
  77. {
  78. return new int[] { };
  79. }
  80. public void F()
  81. {
  82. foreach (var i in GetInts ()) {
  83. }
  84. }
  85. }");
  86. }
  87. [Test]
  88. public void HandlesAssignmentExpressionStatement()
  89. {
  90. Test<IterateViaForeachAction>(@"
  91. using System.Collections.Generic;
  92. class TestClass
  93. {
  94. public IEnumerable<int> GetInts ()
  95. {
  96. return new int[] { };
  97. }
  98. public void F()
  99. {
  100. IEnumerable<int> ints;
  101. ints = GetIn$ts ();
  102. }
  103. }", @"
  104. using System.Collections.Generic;
  105. class TestClass
  106. {
  107. public IEnumerable<int> GetInts ()
  108. {
  109. return new int[] { };
  110. }
  111. public void F()
  112. {
  113. IEnumerable<int> ints;
  114. ints = GetInts ();
  115. foreach (var i in ints) {
  116. }
  117. }
  118. }");
  119. }
  120. [Test]
  121. public void HandlesAsExpressionStatement()
  122. {
  123. Test<IterateViaForeachAction>(@"
  124. using System.Collections.Generic;
  125. class TestClass
  126. {
  127. public void F()
  128. {
  129. object s = """";
  130. s as IEnumerable$<char>;
  131. }
  132. }", @"
  133. using System.Collections.Generic;
  134. class TestClass
  135. {
  136. public void F()
  137. {
  138. object s = """";
  139. foreach (var c in s as IEnumerable<char>) {
  140. }
  141. }
  142. }", 0, true);
  143. }
  144. [Test]
  145. public void NonKnownTypeNamingTest()
  146. {
  147. Test<IterateViaForeachAction>(@"
  148. using System.Collections.Generic;
  149. class TestClass
  150. {
  151. public void F()
  152. {
  153. var items$ = new List<TestClass> ();
  154. }
  155. }", @"
  156. using System.Collections.Generic;
  157. class TestClass
  158. {
  159. public void F()
  160. {
  161. var items = new List<TestClass> ();
  162. foreach (var testClass in items) {
  163. }
  164. }
  165. }");
  166. }
  167. [Test]
  168. public void HandlesAsExpression()
  169. {
  170. Test<IterateViaForeachAction>(@"
  171. using System.Collections.Generic;
  172. class TestClass
  173. {
  174. public void F()
  175. {
  176. object s = """";
  177. s as IEnumerable$<char>
  178. }
  179. }", @"
  180. using System.Collections.Generic;
  181. class TestClass
  182. {
  183. public void F()
  184. {
  185. object s = """";
  186. foreach (var c in s as IEnumerable<char>) {
  187. }
  188. }
  189. }", 0, true);
  190. }
  191. [Test]
  192. public void HandlesLinqExpressionAssignment()
  193. {
  194. Test<IterateViaForeachAction>(@"
  195. using System.Collections.Generic;
  196. using System.Linq;
  197. class TestClass
  198. {
  199. public IEnumerable<int> GetInts()
  200. {
  201. return new int[] { };
  202. }
  203. public void F()
  204. {
  205. var $filteredInts = from item in GetInts ()
  206. where item > 0
  207. select item;
  208. }
  209. }", @"
  210. using System.Collections.Generic;
  211. using System.Linq;
  212. class TestClass
  213. {
  214. public IEnumerable<int> GetInts()
  215. {
  216. return new int[] { };
  217. }
  218. public void F()
  219. {
  220. var filteredInts = from item in GetInts ()
  221. where item > 0
  222. select item;
  223. foreach (var i in filteredInts) {
  224. }
  225. }
  226. }");
  227. }
  228. [Test]
  229. public void IgnoresExpressionsInForeachStatement()
  230. {
  231. TestWrongContext<IterateViaForeachAction>(@"
  232. using System.Collections.Generic;
  233. class TestClass
  234. {
  235. public IEnumerable<int> GetInts()
  236. {
  237. return new int[] { };
  238. }
  239. public void F()
  240. {
  241. foreach (var i in $GetInts ()) {
  242. }
  243. }
  244. }");
  245. }
  246. [Test]
  247. public void IgnoresInitializersInForStatement()
  248. {
  249. TestWrongContext<IterateViaForeachAction>(@"
  250. class TestClass
  251. {
  252. public void F()
  253. {
  254. for (int[] i = new $int[] {} ;;) {
  255. }
  256. }
  257. }");
  258. }
  259. [Test]
  260. public void AddsForToBodyOfUsingStatement()
  261. {
  262. Test<IterateViaForeachAction>(@"
  263. class TestClass
  264. {
  265. public void F()
  266. {
  267. using (int[] i = new $int[] {}) {
  268. }
  269. }
  270. }",@"
  271. class TestClass
  272. {
  273. public void F()
  274. {
  275. using (int[] i = new int[] {}) {
  276. foreach (var j in i) {
  277. }
  278. }
  279. }
  280. }");
  281. }
  282. [Test]
  283. public void AddsBlockStatementToUsingStatement()
  284. {
  285. Test<IterateViaForeachAction>(@"
  286. class TestClass
  287. {
  288. public void F()
  289. {
  290. using (int[] i = new $int[] {});
  291. }
  292. }",@"
  293. class TestClass
  294. {
  295. public void F()
  296. {
  297. using (int[] i = new int[] {}) {
  298. foreach (var j in i) {
  299. }
  300. }
  301. }
  302. }");
  303. }
  304. [Test]
  305. public void IgnoresFieldDeclarations()
  306. {
  307. TestWrongContext<IterateViaForeachAction>(@"
  308. using System.Collections.Generic;
  309. class TestClass
  310. {
  311. List<int> list = $new List<int>();
  312. }");
  313. }
  314. }
  315. }