PageRenderTime 465ms CodeModel.GetById 234ms app.highlight 16ms RepoModel.GetById 139ms app.codeStats 1ms

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

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