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