PageRenderTime 426ms CodeModel.GetById 211ms app.highlight 13ms RepoModel.GetById 199ms app.codeStats 0ms

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

http://github.com/icsharpcode/NRefactory
C# | 775 lines | 681 code | 70 blank | 24 comment | 0 complexity | 845986b743ff5e477ec2386f2260ef32 MD5 | raw file
  1// AutoLinqTests.cs
  2//  
  3// Author:
  4//      Luís Reis <luiscubal@gmail.com>
  5// 
  6// Copyright (c) 2013 Luís Reis
  7// 
  8// Permission is hereby granted, free of charge, to any person obtaining a copy
  9// of this software and associated documentation files (the "Software"), to deal
 10// in the Software without restriction, including without limitation the rights
 11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12// copies of the Software, and to permit persons to whom the Software is
 13// furnished to do so, subject to the following conditions:
 14// 
 15// The above copyright notice and this permission notice shall be included in
 16// all copies or substantial portions of the Software.
 17// 
 18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24// THE SOFTWARE.
 25
 26using System;
 27using NUnit.Framework;
 28using ICSharpCode.NRefactory.CSharp.Refactoring;
 29
 30namespace ICSharpCode.NRefactory.CSharp.CodeActions
 31{
 32	[TestFixture]
 33	public class AutoLinqSumActionTests : ContextActionTestBase
 34	{
 35		[Test]
 36		public void TestSimpleIntegerLoop() {
 37			string source = @"
 38using System.Linq;
 39
 40class TestClass
 41{
 42	void TestMethod() {
 43		int result = 0;
 44		var list = new int[] { 1, 2, 3 };
 45		$foreach (var x in list)
 46			result += x;
 47	}
 48}";
 49
 50			string result = @"
 51using System.Linq;
 52
 53class TestClass
 54{
 55	void TestMethod() {
 56		int result = 0;
 57		var list = new int[] { 1, 2, 3 };
 58		result += list.Sum ();
 59	}
 60}";
 61
 62			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
 63		}
 64
 65		[Test]
 66		public void TestMergedIntegerLoop() {
 67			string source = @"
 68using System.Linq;
 69
 70class TestClass
 71{
 72	void TestMethod() {
 73		var list = new int[] { 1, 2, 3 };
 74		int result = 0;
 75		$foreach (var x in list)
 76			result += x;
 77	}
 78}";
 79
 80			string result = @"
 81using System.Linq;
 82
 83class TestClass
 84{
 85	void TestMethod() {
 86		var list = new int[] { 1, 2, 3 };
 87		int result = list.Sum ();
 88	}
 89}";
 90
 91			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
 92		}
 93
 94		[Test]
 95		public void TestNonZeroMergedIntegerLoop() {
 96			string source = @"
 97using System.Linq;
 98
 99class TestClass
100{
101	void TestMethod() {
102		var list = new int[] { 1, 2, 3 };
103		int result = 1;
104		$foreach (var x in list)
105			result += x;
106	}
107}";
108
109			string result = @"
110using System.Linq;
111
112class TestClass
113{
114	void TestMethod() {
115		var list = new int[] { 1, 2, 3 };
116		int result = 1 + list.Sum ();
117	}
118}";
119
120			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
121		}
122
123		[Test]
124		public void TestMergedAssignmentIntegerLoop() {
125			string source = @"
126using System.Linq;
127
128class TestClass
129{
130	void TestMethod() {
131		var list = new int[] { 1, 2, 3 };
132		int result;
133		result = 1;
134		$foreach (var x in list)
135			result += x;
136	}
137}";
138
139			string result = @"
140using System.Linq;
141
142class TestClass
143{
144	void TestMethod() {
145		var list = new int[] { 1, 2, 3 };
146		int result;
147		result = 1 + list.Sum ();
148	}
149}";
150
151			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
152		}
153
154		[Test]
155		public void TestMergedDecimal() {
156			string source = @"
157using System.Linq;
158
159class TestClass
160{
161	void TestMethod() {
162		var list = new int[] { 1, 2, 3 };
163		decimal result = 0.0m;
164		$foreach (var x in list)
165			result += x;
166	}
167}";
168
169			string result = @"
170using System.Linq;
171
172class TestClass
173{
174	void TestMethod() {
175		var list = new int[] { 1, 2, 3 };
176		decimal result = list.Sum ();
177	}
178}";
179
180			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
181		}
182
183		[Test]
184		public void TestIntegerLoopInBlock() {
185			string source = @"
186using System.Linq;
187
188class TestClass
189{
190	void TestMethod() {
191		int result = 0;
192		var list = new int[] { 1, 2, 3 };
193		$foreach (var x in list) {
194			result += x;
195		}
196	}
197}";
198
199			string result = @"
200using System.Linq;
201
202class TestClass
203{
204	void TestMethod() {
205		int result = 0;
206		var list = new int[] { 1, 2, 3 };
207		result += list.Sum ();
208	}
209}";
210
211			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
212		}
213
214		[Test]
215		public void TestExpression() {
216			string source = @"
217using System.Linq;
218
219class TestClass
220{
221	void TestMethod() {
222		int result = 0;
223		var list = new int[] { 1, 2, 3 };
224		$foreach (var x in list) {
225			result += x * 2;
226		}
227	}
228}";
229
230			string result = @"
231using System.Linq;
232
233class TestClass
234{
235	void TestMethod() {
236		int result = 0;
237		var list = new int[] { 1, 2, 3 };
238		result += list.Sum (x => x * 2);
239	}
240}";
241
242			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
243		}
244
245		[Test]
246		public void TestDisabledForStrings() {
247			string source = @"
248using System.Linq;
249
250class TestClass
251{
252	void TestMethod() {
253		string result = string.Empty;
254		var list = new string[] { ""a"", ""b"" };
255		$foreach (var x in list) {
256			result += x;
257		}
258	}
259}";
260			TestWrongContext<AutoLinqSumAction>(source);
261		}
262
263		[Test]
264		public void TestShort() {
265			string source = @"
266using System.Linq;
267
268class TestClass
269{
270	void TestMethod() {
271		short result = 0;
272		var list = new short[] { 1, 2, 3 };
273		$foreach (var x in list)
274			result += x;
275	}
276}";
277
278			string result = @"
279using System.Linq;
280
281class TestClass
282{
283	void TestMethod() {
284		short result = 0;
285		var list = new short[] { 1, 2, 3 };
286		result += list.Sum ();
287	}
288}";
289
290			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
291		}
292
293		[Test]
294		public void TestLong() {
295			string source = @"
296using System.Linq;
297
298class TestClass
299{
300	void TestMethod() {
301		long result = 0;
302		var list = new long[] { 1, 2, 3 };
303		$foreach (var x in list)
304			result += x;
305	}
306}";
307
308			string result = @"
309using System.Linq;
310
311class TestClass
312{
313	void TestMethod() {
314		long result = 0;
315		var list = new long[] { 1, 2, 3 };
316		result += list.Sum ();
317	}
318}";
319
320			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
321		}
322
323		[Test]
324		public void TestUnsignedLong() {
325			string source = @"
326using System.Linq;
327
328class TestClass
329{
330	void TestMethod() {
331		ulong result = 0;
332		var list = new ulong[] { 1, 2, 3 };
333		$foreach (var x in list)
334			result += x;
335	}
336}";
337
338			string result = @"
339using System.Linq;
340
341class TestClass
342{
343	void TestMethod() {
344		ulong result = 0;
345		var list = new ulong[] { 1, 2, 3 };
346		result += list.Sum ();
347	}
348}";
349
350			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
351		}
352
353		[Test]
354		public void TestFloat() {
355			string source = @"
356using System.Linq;
357
358class TestClass
359{
360	void TestMethod() {
361		float result = 0;
362		var list = new float[] { 1, 2, 3 };
363		$foreach (var x in list)
364			result += x;
365	}
366}";
367
368			string result = @"
369using System.Linq;
370
371class TestClass
372{
373	void TestMethod() {
374		float result = 0;
375		var list = new float[] { 1, 2, 3 };
376		result += list.Sum ();
377	}
378}";
379
380			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
381		}
382
383		[Test]
384		public void TestDouble() {
385			string source = @"
386using System.Linq;
387
388class TestClass
389{
390	void TestMethod() {
391		double result = 0;
392		var list = new double[] { 1, 2, 3 };
393		$foreach (var x in list)
394			result += x;
395	}
396}";
397
398			string result = @"
399using System.Linq;
400
401class TestClass
402{
403	void TestMethod() {
404		double result = 0;
405		var list = new double[] { 1, 2, 3 };
406		result += list.Sum ();
407	}
408}";
409
410			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
411		}
412
413		[Test]
414		public void TestDecimal() {
415			string source = @"
416using System.Linq;
417
418class TestClass
419{
420	void TestMethod() {
421		decimal result = 0;
422		var list = new decimal[] { 1, 2, 3 };
423		$foreach (var x in list)
424			result += x;
425	}
426}";
427
428			string result = @"
429using System.Linq;
430
431class TestClass
432{
433	void TestMethod() {
434		decimal result = 0;
435		var list = new decimal[] { 1, 2, 3 };
436		result += list.Sum ();
437	}
438}";
439
440			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
441		}
442
443		[Test]
444		public void TestMinus() {
445			string source = @"
446using System.Linq;
447
448class TestClass
449{
450	void TestMethod() {
451		int result = 0;
452		var list = new int[] { 1, 2, 3 };
453		$foreach (var x in list) {
454			result -= x;
455		}
456	}
457}";
458
459			string result = @"
460using System.Linq;
461
462class TestClass
463{
464	void TestMethod() {
465		int result = 0;
466		var list = new int[] { 1, 2, 3 };
467		result += list.Sum (x => -x);
468	}
469}";
470
471			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
472		}
473
474		[Test]
475		public void TestCombined() {
476			string source = @"
477using System.Linq;
478
479class TestClass
480{
481	void TestMethod() {
482		int result = 0;
483		var list = new int[] { 1, 2, 3 };
484		$foreach (var x in list) {
485			result += x;
486			result += 2 * x;
487		}
488	}
489}";
490
491			string result = @"
492using System.Linq;
493
494class TestClass
495{
496	void TestMethod() {
497		int result = 0;
498		var list = new int[] { 1, 2, 3 };
499		result += list.Sum (x => x + 2 * x);
500	}
501}";
502
503			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
504		}
505
506		[Test]
507		public void TestCombinedPrecedence() {
508			string source = @"
509using System.Linq;
510
511class TestClass
512{
513	void TestMethod() {
514		int result = 0;
515		var list = new int[] { 1, 2, 3 };
516		$foreach (var x in list) {
517			result += x;
518			result += x << 1;
519		}
520	}
521}";
522
523			string result = @"
524using System.Linq;
525
526class TestClass
527{
528	void TestMethod() {
529		int result = 0;
530		var list = new int[] { 1, 2, 3 };
531		result += list.Sum (x => x + (x << 1));
532	}
533}";
534
535			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
536		}
537
538		[Test]
539		public void TestEmptyStatements() {
540			string source = @"
541using System.Linq;
542
543class TestClass
544{
545	void TestMethod() {
546		int result = 0;
547		var list = new int[] { 1, 2, 3 };
548		$foreach (var x in list) {
549			result += x;
550			;
551		}
552	}
553}";
554
555			string result = @"
556using System.Linq;
557
558class TestClass
559{
560	void TestMethod() {
561		int result = 0;
562		var list = new int[] { 1, 2, 3 };
563		result += list.Sum ();
564	}
565}";
566
567			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
568		}
569
570		[Test]
571		public void TestSimpleConditional() {
572			string source = @"
573using System.Linq;
574
575class TestClass
576{
577	void TestMethod() {
578		int result = 0;
579		var list = new int[] { 1, 2, 3 };
580		$foreach (var x in list) {
581			if (x > 0)
582				result += x;
583		}
584	}
585}";
586
587			string result = @"
588using System.Linq;
589
590class TestClass
591{
592	void TestMethod() {
593		int result = 0;
594		var list = new int[] { 1, 2, 3 };
595		result += list.Where (x => x > 0).Sum ();
596	}
597}";
598
599			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
600		}
601
602		[Test]
603		public void TestInvertedConditional() {
604			string source = @"
605using System.Linq;
606
607class TestClass
608{
609	void TestMethod() {
610		int result = 0;
611		var list = new int[] { 1, 2, 3 };
612		$foreach (var x in list) {
613			if (x > 0)
614				;
615			else
616				result += x;
617		}
618	}
619}";
620
621			string result = @"
622using System.Linq;
623
624class TestClass
625{
626	void TestMethod() {
627		int result = 0;
628		var list = new int[] { 1, 2, 3 };
629		result += list.Where (x => x <= 0).Sum ();
630	}
631}";
632
633			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
634		}
635
636		[Test]
637		public void TestIncrement() {
638			string source = @"
639using System.Linq;
640
641class TestClass
642{
643	void TestMethod() {
644		int result = 0;
645		var list = new int[] { 1, 2, 3 };
646		$foreach (var x in list) {
647			result++;
648		}
649	}
650}";
651
652			string result = @"
653using System.Linq;
654
655class TestClass
656{
657	void TestMethod() {
658		int result = 0;
659		var list = new int[] { 1, 2, 3 };
660		result += list.Count ();
661	}
662}";
663
664			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
665		}
666
667		[Test]
668		public void TestCompleteConditional() {
669			string source = @"
670using System.Linq;
671
672class TestClass
673{
674	void TestMethod() {
675		int result = 0;
676		var list = new int[] { 1, 2, 3 };
677		$foreach (var x in list) {
678			if (x > 0)
679				result += x * 2;
680			else
681				result += x;
682		}
683	}
684}";
685
686			string result = @"
687using System.Linq;
688
689class TestClass
690{
691	void TestMethod() {
692		int result = 0;
693		var list = new int[] { 1, 2, 3 };
694		result += list.Sum (x => x > 0 ? x * 2 : x);
695	}
696}";
697
698			Assert.AreEqual(result, RunContextAction(new AutoLinqSumAction(), source));
699		}
700
701		[Test]
702		public void TestDisabledForSideEffects() {
703			string source = @"
704using System.Linq;
705
706class TestClass
707{
708	void TestMethod() {
709		string result = string.Empty;
710		var list = new string[] { ""a"", ""b"" };
711		$foreach (var x in list) {
712			TestMethod();
713			result += x;
714		}
715	}
716}";
717			TestWrongContext<AutoLinqSumAction>(source);
718		}
719
720		[Test]
721		public void TestDisabledForInnerAssignments() {
722			string source = @"
723using System.Linq;
724
725class TestClass
726{
727	void TestMethod() {
728		int result = 0;
729		var list = new int[] { 1, 2 };
730		int p = 0;
731		$foreach (var x in list) {
732			result += (p = x);
733		}
734	}
735}";
736			TestWrongContext<AutoLinqSumAction>(source);
737		}
738
739		[Test]
740		public void TestDisabledForInnerIncrements() {
741			string source = @"
742using System.Linq;
743
744class TestClass
745{
746	void TestMethod() {
747		int result = 0;
748		var list = new int[] { 1, 2 };
749		int p = 0;
750		$foreach (var x in list) {
751			result += (p++);
752		}
753	}
754}";
755			TestWrongContext<AutoLinqSumAction>(source);
756		}
757
758		[Test]
759		public void TestDisabledForNoLinq() {
760			string source = @"
761class TestClass
762{
763	void TestMethod() {
764		int result = 0;
765		var list = new int[] { 1, 2 };
766		$foreach (var x in list) {
767			result += x;
768		}
769	}
770}";
771			TestWrongContext<AutoLinqSumAction>(source);
772		}
773	}
774}
775