PageRenderTime 79ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 17ms app.codeStats 1ms

/src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/LinqQueryToFluentTests.cs

https://github.com/pombredanne/SharpDevelop
C# | 690 lines | 603 code | 62 blank | 25 comment | 0 complexity | 91b93366188f65da6e9ab46b488b1c99 MD5 | raw file
  1// 
  2// LinqQueryToFluentTests.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
 27using System;
 28using NUnit.Framework;
 29using ICSharpCode.NRefactory.CSharp.Refactoring;
 30
 31namespace ICSharpCode.NRefactory.CSharp.CodeActions
 32{
 33	[TestFixture]
 34	public class LinqQueryToFluentTests : ContextActionTestBase
 35	{
 36		[Test]
 37		public void TestSimpleQuery()
 38		{
 39			string input = @"
 40using System.Linq;
 41public class TestClass
 42{
 43	public void TestMethod()
 44	{
 45		var data = $from x in System.Enumerable.Empty<int> ()
 46                   select x;
 47	}
 48}
 49";
 50
 51			string output = @"
 52using System.Linq;
 53public class TestClass
 54{
 55	public void TestMethod()
 56	{
 57		var data = System.Enumerable.Empty<int> ().Select (x => x);
 58	}
 59}
 60";
 61
 62			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
 63		}
 64
 65		[Test]
 66		public void TestName()
 67		{
 68			string input = @"
 69using System.Linq;
 70public class TestClass
 71{
 72	public void TestMethod()
 73	{
 74		int _;
 75		var _2 = $from x in System.Enumerable.Empty<int> ()
 76		         let _1 = x
 77		         select x;
 78	}
 79}
 80";
 81
 82			string output = @"
 83using System.Linq;
 84public class TestClass
 85{
 86	public void TestMethod()
 87	{
 88		int _;
 89		var _2 = System.Enumerable.Empty<int> ().Select (x => new {
 90	x,
 91	_1 = x
 92}).Select (_3 => _3.x);
 93	}
 94}
 95";
 96
 97			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
 98		}
 99
100		[Test]
101		public void TestPrecedence()
102		{
103			string input = @"
104using System.Linq;
105public class TestClass
106{
107	public void TestMethod()
108	{
109		var data = $from x in true ? System.Enumerable.Empty<int> () : null
110			       select x;
111	}
112}
113";
114
115			string output = @"
116using System.Linq;
117public class TestClass
118{
119	public void TestMethod()
120	{
121		var data = (true ? System.Enumerable.Empty<int> () : null).Select (x => x);
122	}
123}
124";
125
126			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
127		}
128
129		[Test]
130		public void TestWhereQuery()
131		{
132			string input = @"
133using System.Linq;
134public class TestClass
135{
136	public void TestMethod()
137	{
138		var data = $from x in System.Enumerable.Empty<int> ()
139		           where x > 1
140		           select x;
141	}
142}
143";
144
145			string output = @"
146using System.Linq;
147public class TestClass
148{
149	public void TestMethod()
150	{
151		var data = System.Enumerable.Empty<int> ().Where (x => x > 1);
152	}
153}
154";
155
156			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
157		}
158
159		[Test]
160		public void TestOrderByQuery()
161		{
162			string input = @"
163using System.Linq;
164public class TestClass
165{
166	public void TestMethod()
167	{
168		var data = $from x in System.Enumerable.Empty<int> ()
169		           orderby x, x * 2 descending
170		           select x;
171	}
172}
173";
174
175			string output = @"
176using System.Linq;
177public class TestClass
178{
179	public void TestMethod()
180	{
181		var data = System.Enumerable.Empty<int> ().OrderBy (x => x).ThenByDescending (x => x * 2);
182	}
183}
184";
185
186			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
187		}
188
189		[Test]
190		public void TestDoubleFromWithSelectQuery()
191		{
192			string input = @"
193using System.Linq;
194public class TestClass
195{
196	public void TestMethod()
197	{
198		var newEnumerable = System.Enumerable.Empty<int> ();
199		var data = $from x in System.Enumerable.Empty<int> ()
200		           from y in newEnumerable
201		           select x * y;
202	}
203}
204";
205
206			string output = @"
207using System.Linq;
208public class TestClass
209{
210	public void TestMethod()
211	{
212		var newEnumerable = System.Enumerable.Empty<int> ();
213		var data = System.Enumerable.Empty<int> ().SelectMany (x => newEnumerable, (x, y) => x * y);
214	}
215}
216";
217
218			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
219		}
220
221		[Test]
222		public void TestDoubleFromWithCastQuery()
223		{
224			string input = @"
225using System.Linq;
226public class TestClass
227{
228	public void TestMethod()
229	{
230		var newEnumerable = System.Enumerable.Empty<int> ();
231		var data = $from x in System.Enumerable.Empty<int> ()
232		           from float y in newEnumerable
233		           select x * y;
234	}
235}
236";
237
238			string output = @"
239using System.Linq;
240public class TestClass
241{
242	public void TestMethod()
243	{
244		var newEnumerable = System.Enumerable.Empty<int> ();
245		var data = System.Enumerable.Empty<int> ().SelectMany (x => newEnumerable.Cast<float> (), (x, y) => x * y);
246	}
247}
248";
249
250			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
251		}
252
253		[Test]
254		public void TestDoubleFromWithIntermediateQuery()
255		{
256			string input = @"
257using System.Linq;
258public class TestClass
259{
260	public void TestMethod()
261	{
262		var newEnumerable = System.Enumerable.Empty<int> ();
263		var data = $from x in System.Enumerable.Empty<int> ()
264		           from y in newEnumerable
265		           where x > y
266		           select x * y;
267	}
268}
269";
270
271			string output = @"
272using System.Linq;
273public class TestClass
274{
275	public void TestMethod()
276	{
277		var newEnumerable = System.Enumerable.Empty<int> ();
278		var data = System.Enumerable.Empty<int> ().SelectMany (x => newEnumerable, (x, y) => new {
279	x,
280	y
281}).Where (_ => _.x > _.y).Select (_1 => _1.x * _1.y);
282	}
283}
284";
285
286			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
287		}
288
289		[Test]
290		public void TestLetQuery()
291		{
292			string input = @"
293using System.Linq;
294public class TestClass
295{
296	public void TestMethod()
297	{
298		var data = $from x in System.Enumerable.Empty<int> ()
299		           let y = x * 2
300		           select x * y;
301	}
302}
303";
304
305			string output = @"
306using System.Linq;
307public class TestClass
308{
309	public void TestMethod()
310	{
311		var data = System.Enumerable.Empty<int> ().Select (x => new {
312	x,
313	y = x * 2
314}).Select (_ => _.x * _.y);
315	}
316}
317";
318
319			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
320		}
321
322		[Test]
323		public void TestLongChainQuery()
324		{
325			string input = @"
326using System.Linq;
327public class TestClass
328{
329	public void TestMethod()
330	{
331		var data = $from x in System.Enumerable.Empty<int> ()
332		           let y = x * 2
333		           let z = x * y * 2
334		           select x * y * z;
335	}
336}
337";
338
339			string output = @"
340using System.Linq;
341public class TestClass
342{
343	public void TestMethod()
344	{
345		var data = System.Enumerable.Empty<int> ().Select (x => new {
346	x,
347	y = x * 2
348}).Select (_ => new {
349	_,
350	z = _.x * _.y * 2
351}).Select (_1 => _1._.x * _1._.y * _1.z);
352	}
353}
354";
355
356			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
357		}
358
359		[Test]
360		public void TestCastQuery()
361		{
362			string input = @"
363using System.Linq;
364public class TestClass
365{
366	public void TestMethod()
367	{
368		var data = $from float x in System.Enumerable.Empty<int> ()
369		           select x;
370	}
371}
372";
373
374			string output = @"
375using System.Linq;
376public class TestClass
377{
378	public void TestMethod()
379	{
380		var data = System.Enumerable.Empty<int> ().Cast<float> ().Select (x => x);
381	}
382}
383";
384
385			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
386		}
387
388		[Test]
389		public void TestJoinQuery()
390		{
391			string input = @"
392using System.Linq;
393public class TestClass
394{
395	public void TestMethod()
396	{
397		var newEnumerable = new int[] { 4, 5, 6 };
398		var data = $from x in System.Enumerable.Empty<int> ()
399		           join float yy in newEnumerable on x * 2 equals yy
400		           select x * yy;
401	}
402}
403";
404
405			string output = @"
406using System.Linq;
407public class TestClass
408{
409	public void TestMethod()
410	{
411		var newEnumerable = new int[] { 4, 5, 6 };
412		var data = System.Enumerable.Empty<int> ().Join (newEnumerable.Cast<float> (), x => x * 2, yy => yy, (x, yy) => x * yy);
413	}
414}
415";
416
417			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
418		}
419
420		[Test]
421		public void TestJoinWithIntermediateQuery()
422		{
423			string input = @"
424using System.Linq;
425public class TestClass
426{
427	public void TestMethod()
428	{
429		var newEnumerable = new int[] { 4, 5, 6 };
430		var data = $from x in System.Enumerable.Empty<int> ()
431		           join float y in newEnumerable on x * 2 equals y
432		           where x == 2
433		           select x * y;
434	}
435}
436";
437
438			string output = @"
439using System.Linq;
440public class TestClass
441{
442	public void TestMethod()
443	{
444		var newEnumerable = new int[] { 4, 5, 6 };
445		var data = System.Enumerable.Empty<int> ().Join (newEnumerable.Cast<float> (), x => x * 2, y => y, (x, y) => new {
446	x,
447	y
448}).Where (_ => _.x == 2).Select (_1 => _1.x * _1.y);
449	}
450}
451";
452
453			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
454		}
455
456		[Test]
457		public void TestJoinWithIntoSelectQuery()
458		{
459			string input = @"
460using System.Linq;
461public class TestClass
462{
463	public void TestMethod()
464	{
465		var newEnumerable = new int[] { 1, 2, 3 };
466		var data = $from x in System.Enumerable.Empty<int> ()
467		           join y in newEnumerable on x * 2 equals y
468		           into g
469		           select g;
470	}
471}
472";
473
474			string output = @"
475using System.Linq;
476public class TestClass
477{
478	public void TestMethod()
479	{
480		var newEnumerable = new int[] { 1, 2, 3 };
481		var data = System.Enumerable.Empty<int> ().GroupJoin (newEnumerable, x => x * 2, y => y, (x, g) => g);
482	}
483}
484";
485
486			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
487		}
488
489		[Test]
490		public void TestJoinWithIntoIntermediateQuery()
491		{
492			string input = @"
493using System.Linq;
494public class TestClass
495{
496	public void TestMethod()
497	{
498		var newEnumerable = new int[] { 1, 2, 3 };
499		var data = $from x in System.Enumerable.Empty<int> ()
500		           join y in newEnumerable on x * 2 equals y
501		           into g
502		           where true
503		           select g;
504	}
505}
506";
507
508			string output = @"
509using System.Linq;
510public class TestClass
511{
512	public void TestMethod()
513	{
514		var newEnumerable = new int[] { 1, 2, 3 };
515		var data = System.Enumerable.Empty<int> ().GroupJoin (newEnumerable, x => x * 2, y => y, (x, g) => new {
516	x,
517	g
518}).Where (_ => true).Select (_1 => _1.g);
519	}
520}
521";
522
523			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
524		}
525
526		[Test]
527		public void TestSimpleGroup()
528		{
529			string input = @"
530using System.Linq;
531public class TestClass
532{
533	public void TestMethod()
534	{
535		var data = $from x in System.Enumerable.Empty<int> ()
536		           group x by x % 10;
537	}
538}
539";
540
541			string output = @"
542using System.Linq;
543public class TestClass
544{
545	public void TestMethod()
546	{
547		var data = System.Enumerable.Empty<int> ().GroupBy (x => x % 10);
548	}
549}
550";
551
552			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
553		}
554
555		[Test]
556		public void TestDifferentGroup()
557		{
558			string input = @"
559using System.Linq;
560public class TestClass
561{
562	public void TestMethod()
563	{
564		var data = $from x in System.Enumerable.Empty<int> ()
565		           group x / 10 by x % 10;
566	}
567}
568";
569
570			string output = @"
571using System.Linq;
572public class TestClass
573{
574	public void TestMethod()
575	{
576		var data = System.Enumerable.Empty<int> ().GroupBy (x => x % 10, x => x / 10);
577	}
578}
579";
580
581			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
582		}
583
584		[Test]
585		public void TestInto()
586		{
587			string input = @"
588using System.Linq;
589public class TestClass
590{
591	public void TestMethod()
592	{
593		var data = $from x in System.Enumerable.Empty<int> ()
594		           select x * 2 into y
595		           select y * 3;
596	}
597}
598";
599
600			string output = @"
601using System.Linq;
602public class TestClass
603{
604	public void TestMethod()
605	{
606		var data = System.Enumerable.Empty<int> ().Select (x => x * 2).Select (y => y * 3);
607	}
608}
609";
610
611			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
612		}
613
614		[Test]
615		public void TestDoubleFromWithLet()
616		{
617			string input = @"
618using System.Linq;
619public class TestClass
620{
621	public void TestMethod()
622	{
623		var src = System.Enumerable.Empty<int> ();
624		var data = $from x in src
625		           from y in src
626		           let k = x * y
627		           select k;
628	}
629}
630";
631
632			string output = @"
633using System.Linq;
634public class TestClass
635{
636	public void TestMethod()
637	{
638		var src = System.Enumerable.Empty<int> ();
639		var data = src.SelectMany (x => src, (x, y) => new {
640	x,
641	y
642}).Select (_ => new {
643	_,
644	k = _.x * _.y
645}).Select (_1 => _1.k);
646	}
647}
648";
649
650			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
651		}
652
653		[Test]
654		public void TestDoubleFromWithMidLet()
655		{
656			string input = @"
657using System.Linq;
658public class TestClass
659{
660	public void TestMethod()
661	{
662		var src = System.Enumerable.Empty<int> ();
663		var data = $from x in src
664		           let k = x * x
665		           from y in src
666		           select k * y;
667	}
668}
669";
670
671			string output = @"
672using System.Linq;
673public class TestClass
674{
675	public void TestMethod()
676	{
677		var src = System.Enumerable.Empty<int> ();
678		var data = src.Select (x => new {
679	x,
680	k = x * x
681}).SelectMany (_ => src, (_1, y) => _1.k * y);
682	}
683}
684";
685
686			Assert.AreEqual(output, RunContextAction(new LinqQueryToFluentAction(), input));
687		}
688	}
689}
690