PageRenderTime 345ms CodeModel.GetById 181ms app.highlight 6ms RepoModel.GetById 155ms app.codeStats 0ms

/src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/LoopCanBeConvertedToQueryIssueTests.cs

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