~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// 
 
2
// CreateMethodDeclarationTests.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin <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.
 
26
using System;
 
27
using NUnit.Framework;
 
28
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
29
using System.Text;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.CodeActions
 
32
{
 
33
        [TestFixture]
 
34
        public class CreateMethodDeclarationTests : ContextActionTestBase
 
35
        {
 
36
                [Test()]
 
37
                public void TestPrivateSimpleCreateMethod ()
 
38
                {
 
39
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
40
{
 
41
        int member = 5;
 
42
        string Test { get; set; }
 
43
 
 
44
        void TestMethod ()
 
45
        {
 
46
                $NonExistantMethod (member, Test, 5);
 
47
        }
 
48
}", @"class TestClass
 
49
{
 
50
        int member = 5;
 
51
        string Test { get; set; }
 
52
 
 
53
        void NonExistantMethod (int member, string test, int i)
 
54
        {
 
55
                throw new System.NotImplementedException ();
 
56
        }
 
57
        void TestMethod ()
 
58
        {
 
59
                NonExistantMethod (member, Test, 5);
 
60
        }
 
61
}");
 
62
                }
 
63
 
 
64
                [Test()]
 
65
                public void TestStaticSimpleCreateMethod ()
 
66
                {
 
67
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
68
{
 
69
        public static void TestMethod ()
 
70
        {
 
71
                int testLocalVar;
 
72
                $NonExistantMethod (testLocalVar);
 
73
        }
 
74
}", @"class TestClass
 
75
{
 
76
        static void NonExistantMethod (int testLocalVar)
 
77
        {
 
78
                throw new System.NotImplementedException ();
 
79
        }
 
80
        public static void TestMethod ()
 
81
        {
 
82
                int testLocalVar;
 
83
                NonExistantMethod (testLocalVar);
 
84
        }
 
85
}");
 
86
                }
 
87
                
 
88
                [Test()]
 
89
                public void TestGuessAssignmentReturnType ()
 
90
                {
 
91
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
92
{
 
93
        static void TestMethod ()
 
94
        {
 
95
                int testLocalVar = $NonExistantMethod ();
 
96
        }
 
97
}", @"class TestClass
 
98
{
 
99
        static int NonExistantMethod ()
 
100
        {
 
101
                throw new System.NotImplementedException ();
 
102
        }
 
103
        static void TestMethod ()
 
104
        {
 
105
                int testLocalVar = NonExistantMethod ();
 
106
        }
 
107
}");
 
108
                }
 
109
 
 
110
                [Test()]
 
111
                public void TestGuessAssignmentReturnTypeCase2 ()
 
112
                {
 
113
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
114
{
 
115
        static void TestMethod ()
 
116
        {
 
117
                int testLocalVar;
 
118
                testLocalVar = $NonExistantMethod ();
 
119
        }
 
120
}", @"class TestClass
 
121
{
 
122
        static int NonExistantMethod ()
 
123
        {
 
124
                throw new System.NotImplementedException ();
 
125
        }
 
126
        static void TestMethod ()
 
127
        {
 
128
                int testLocalVar;
 
129
                testLocalVar = NonExistantMethod ();
 
130
        }
 
131
}");
 
132
                }
 
133
 
 
134
                [Test()]
 
135
                public void TestGuessAssignmentReturnTypeCase3 ()
 
136
                {
 
137
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
138
{
 
139
        static void TestMethod ()
 
140
        {
 
141
                var testLocalVar = (string)$NonExistantMethod ();
 
142
        }
 
143
}", @"class TestClass
 
144
{
 
145
        static string NonExistantMethod ()
 
146
        {
 
147
                throw new System.NotImplementedException ();
 
148
        }
 
149
        static void TestMethod ()
 
150
        {
 
151
                var testLocalVar = (string)NonExistantMethod ();
 
152
        }
 
153
}");
 
154
                }
 
155
 
 
156
 
 
157
                [Test()]
 
158
                public void TestGuessParameterType ()
 
159
                {
 
160
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
161
{
 
162
        void TestMethod ()
 
163
        {
 
164
                Test ($NonExistantMethod ());
 
165
        }
 
166
        void Test (int a) {}
 
167
 
 
168
}", @"class TestClass
 
169
{
 
170
        int NonExistantMethod ()
 
171
        {
 
172
                throw new System.NotImplementedException ();
 
173
        }
 
174
        void TestMethod ()
 
175
        {
 
176
                Test (NonExistantMethod ());
 
177
        }
 
178
        void Test (int a) {}
 
179
 
 
180
}");
 
181
                }
 
182
 
 
183
 
 
184
                [Test()]
 
185
                public void TestCreateDelegateDeclarationIdentifierCase ()
 
186
                {
 
187
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
188
{
 
189
        public event MyDelegate MyEvent;
 
190
 
 
191
        void TestMethod ()
 
192
        {
 
193
                MyEvent += $NonExistantMethod;
 
194
        }
 
195
}
 
196
 
 
197
public delegate string MyDelegate (int a, object b);
 
198
", @"class TestClass
 
199
{
 
200
        public event MyDelegate MyEvent;
 
201
 
 
202
        string NonExistantMethod (int a, object b)
 
203
        {
 
204
                throw new System.NotImplementedException ();
 
205
        }
 
206
        void TestMethod ()
 
207
        {
 
208
                MyEvent += NonExistantMethod;
 
209
        }
 
210
}
 
211
 
 
212
public delegate string MyDelegate (int a, object b);
 
213
");
 
214
                }
 
215
 
 
216
                [Test()]
 
217
                public void TestCreateDelegateDeclarationMemberReferenceCase ()
 
218
                {
 
219
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
220
{
 
221
        public event MyDelegate MyEvent;
 
222
 
 
223
        void TestMethod ()
 
224
        {
 
225
                MyEvent += $this.NonExistantMethod;
 
226
        }
 
227
}
 
228
 
 
229
public delegate string MyDelegate (int a, object b);
 
230
", @"class TestClass
 
231
{
 
232
        public event MyDelegate MyEvent;
 
233
 
 
234
        string NonExistantMethod (int a, object b)
 
235
        {
 
236
                throw new System.NotImplementedException ();
 
237
        }
 
238
        void TestMethod ()
 
239
        {
 
240
                MyEvent += this.NonExistantMethod;
 
241
        }
 
242
}
 
243
 
 
244
public delegate string MyDelegate (int a, object b);
 
245
");
 
246
                }
 
247
                
 
248
                [Test()]
 
249
                public void TestCreateDelegateDeclarationInOtherClassMemberReferenceCase ()
 
250
                {
 
251
                        Test<CreateMethodDeclarationAction> (@"class Foo {
 
252
}
 
253
 
 
254
class TestClass
 
255
{
 
256
        public event MyDelegate MyEvent;
 
257
 
 
258
        void TestMethod ()
 
259
        {
 
260
                MyEvent += $new Foo ().NonExistantMethod;
 
261
        }
 
262
}
 
263
 
 
264
public delegate string MyDelegate (int a, object b);
 
265
", @"class Foo {
 
266
        public string NonExistantMethod (int a, object b)
 
267
        {
 
268
                throw new System.NotImplementedException ();
 
269
        }
 
270
}
 
271
 
 
272
class TestClass
 
273
{
 
274
        public event MyDelegate MyEvent;
 
275
 
 
276
        void TestMethod ()
 
277
        {
 
278
                MyEvent += new Foo ().NonExistantMethod;
 
279
        }
 
280
}
 
281
 
 
282
public delegate string MyDelegate (int a, object b);
 
283
");
 
284
                }
 
285
 
 
286
                [Test()]
 
287
                public void TestRefOutCreateMethod ()
 
288
                {
 
289
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
290
{
 
291
        void TestMethod ()
 
292
        {
 
293
                int a, b;
 
294
                $NonExistantMethod (ref a, out b);
 
295
        }
 
296
}", @"class TestClass
 
297
{
 
298
        void NonExistantMethod (ref int a, out int b)
 
299
        {
 
300
                throw new System.NotImplementedException ();
 
301
        }
 
302
        void TestMethod ()
 
303
        {
 
304
                int a, b;
 
305
                NonExistantMethod (ref a, out b);
 
306
        }
 
307
}");
 
308
                }
 
309
                
 
310
                [Test()]
 
311
                public void TestExternMethod ()
 
312
                {
 
313
                        Test<CreateMethodDeclarationAction> (
 
314
@"
 
315
class FooBar
 
316
{
 
317
}
 
318
 
 
319
class TestClass
 
320
{
 
321
        void TestMethod ()
 
322
        {
 
323
                var fb = new FooBar ();
 
324
                fb.$NonExistantMethod ();
 
325
        }
 
326
}
 
327
", @"
 
328
class FooBar
 
329
{
 
330
        public void NonExistantMethod ()
 
331
        {
 
332
                throw new System.NotImplementedException ();
 
333
        }
 
334
}
 
335
 
 
336
class TestClass
 
337
{
 
338
        void TestMethod ()
 
339
        {
 
340
                var fb = new FooBar ();
 
341
                fb.NonExistantMethod ();
 
342
        }
 
343
}
 
344
");
 
345
                }
 
346
                
 
347
                [Test()]
 
348
                public void TestCreateInterfaceMethod ()
 
349
                {
 
350
                        Test<CreateMethodDeclarationAction> (
 
351
@"
 
352
interface FooBar
 
353
{
 
354
}
 
355
 
 
356
class TestClass
 
357
{
 
358
        void TestMethod ()
 
359
        {
 
360
                FooBar fb;
 
361
                fb.$NonExistantMethod ();
 
362
        }
 
363
}
 
364
", @"
 
365
interface FooBar
 
366
{
 
367
        void NonExistantMethod ();
 
368
}
 
369
 
 
370
class TestClass
 
371
{
 
372
        void TestMethod ()
 
373
        {
 
374
                FooBar fb;
 
375
                fb.NonExistantMethod ();
 
376
        }
 
377
}
 
378
");
 
379
                }
 
380
                
 
381
                [Test()]
 
382
                public void TestCreateInStaticClassMethod ()
 
383
                {
 
384
                        Test<CreateMethodDeclarationAction> (
 
385
@"
 
386
static class FooBar
 
387
{
 
388
}
 
389
 
 
390
class TestClass
 
391
{
 
392
        void TestMethod ()
 
393
        {
 
394
                FooBar.$NonExistantMethod ();
 
395
        }
 
396
}
 
397
", @"
 
398
static class FooBar
 
399
{
 
400
        public static void NonExistantMethod ()
 
401
        {
 
402
                throw new System.NotImplementedException ();
 
403
        }
 
404
}
 
405
 
 
406
class TestClass
 
407
{
 
408
        void TestMethod ()
 
409
        {
 
410
                FooBar.NonExistantMethod ();
 
411
        }
 
412
}
 
413
");
 
414
                }
 
415
                
 
416
 
 
417
                
 
418
                /// <summary>
 
419
                /// Bug 677522 - "Create Method" creates at wrong indent level
 
420
                /// </summary>
 
421
                [Test()]
 
422
                public void TestBug677522 ()
 
423
                {
 
424
                        Test<CreateMethodDeclarationAction> (
 
425
@"namespace Test {
 
426
        class TestClass
 
427
        {
 
428
                void TestMethod ()
 
429
                {
 
430
                        $NonExistantMethod ();
 
431
                }
 
432
        }
 
433
}
 
434
", @"namespace Test {
 
435
        class TestClass
 
436
        {
 
437
                void NonExistantMethod ()
 
438
                {
 
439
                        throw new System.NotImplementedException ();
 
440
                }
 
441
                void TestMethod ()
 
442
                {
 
443
                        NonExistantMethod ();
 
444
                }
 
445
        }
 
446
}
 
447
");
 
448
                }
 
449
                
 
450
                /// <summary>
 
451
                /// Bug 677527 - "Create Method" uses fully qualified namespace when "using" statement exists
 
452
                /// </summary>
 
453
                [Test()]
 
454
                public void TestBug677527 ()
 
455
                {
 
456
                        Test<CreateMethodDeclarationAction> (
 
457
@"using System.Text;
 
458
 
 
459
namespace Test {
 
460
        class TestClass
 
461
        {
 
462
                void TestMethod ()
 
463
                {
 
464
                        StringBuilder sb = new StringBuilder ();
 
465
                        $NonExistantMethod (sb);
 
466
                }
 
467
        }
 
468
}
 
469
", @"using System.Text;
 
470
 
 
471
namespace Test {
 
472
        class TestClass
 
473
        {
 
474
                void NonExistantMethod (StringBuilder sb)
 
475
                {
 
476
                        throw new System.NotImplementedException ();
 
477
                }
 
478
                void TestMethod ()
 
479
                {
 
480
                        StringBuilder sb = new StringBuilder ();
 
481
                        NonExistantMethod (sb);
 
482
                }
 
483
        }
 
484
}
 
485
");
 
486
                }
 
487
                
 
488
                
 
489
                /// <summary>
 
490
                /// Bug 693949 - Create method uses the wrong type for param
 
491
                /// </summary>
 
492
                [Ignore("TODO")]
 
493
                [Test()]
 
494
                public void TestBug693949 ()
 
495
                {
 
496
                        // the c# code isn't 100% correct since test isn't accessible in Main (can't call non static method from static member)
 
497
                        Test<CreateMethodDeclarationAction> (
 
498
@"using System.Text;
 
499
 
 
500
namespace Test {
 
501
        class TestClass
 
502
        {
 
503
                string test(string a)
 
504
                {
 
505
                }
 
506
        
 
507
                public static void Main(string[] args)
 
508
                {
 
509
                        Type a = $M(test(""a""));
 
510
                }
 
511
        }
 
512
}
 
513
", @"using System.Text;
 
514
 
 
515
namespace Test {
 
516
        class TestClass
 
517
        {
 
518
                string test(string a)
 
519
                {
 
520
                }
 
521
        
 
522
                static Type M (string par1)
 
523
                {
 
524
                        throw new System.NotImplementedException ();
 
525
                }
 
526
 
 
527
                public static void Main(string[] args)
 
528
                {
 
529
                        Type a = $M(test(""a""));
 
530
                }
 
531
        }
 
532
}
 
533
");
 
534
                }
 
535
                
 
536
                /// <summary>
 
537
                /// Bug 469 - CreateMethod created a method incorrectly
 
538
                /// </summary>
 
539
                [Test()]
 
540
                public void TestBug469 ()
 
541
                {
 
542
                        Test<CreateMethodDeclarationAction> (
 
543
@"class Test
 
544
{
 
545
        public override string ToString ()
 
546
        {
 
547
                $BeginDownloadingImage (this);
 
548
        }
 
549
}
 
550
", @"class Test
 
551
{
 
552
        void BeginDownloadingImage (Test test)
 
553
        {
 
554
                throw new System.NotImplementedException ();
 
555
        }
 
556
        public override string ToString ()
 
557
        {
 
558
                BeginDownloadingImage (this);
 
559
        }
 
560
}
 
561
");
 
562
                }
 
563
                
 
564
                [Test()]
 
565
                public void TestTestGuessReturnReturnType ()
 
566
                {
 
567
                        Test<CreateMethodDeclarationAction> (
 
568
@"class Test
 
569
{
 
570
        public override string ToString ()
 
571
        {
 
572
                return $BeginDownloadingImage (this);
 
573
        }
 
574
}
 
575
", @"class Test
 
576
{
 
577
        string BeginDownloadingImage (Test test)
 
578
        {
 
579
                throw new System.NotImplementedException ();
 
580
        }
 
581
        public override string ToString ()
 
582
        {
 
583
                return BeginDownloadingImage (this);
 
584
        }
 
585
}
 
586
");
 
587
                }
 
588
 
 
589
                [Test()]
 
590
                public void TestStringParameterNameGuessing ()
 
591
                {
 
592
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
593
{
 
594
        static void TestMethod ()
 
595
        {
 
596
                $NonExistantMethod (""Hello World!"");
 
597
        }
 
598
}", @"class TestClass
 
599
{
 
600
        static void NonExistantMethod (string helloWorld)
 
601
        {
 
602
                throw new System.NotImplementedException ();
 
603
        }
 
604
        static void TestMethod ()
 
605
        {
 
606
                NonExistantMethod (""Hello World!"");
 
607
        }
 
608
}");
 
609
                }
 
610
 
 
611
                [Test()]
 
612
                public void TestMethodInFrameworkClass ()
 
613
                {
 
614
                        TestWrongContext<CreateMethodDeclarationAction> (
 
615
@"class TestClass
 
616
{
 
617
        void TestMethod ()
 
618
        {
 
619
                $System.Console.ImprovedWriteLine (""Think of it"");
 
620
        }
 
621
}
 
622
");
 
623
                }
 
624
 
 
625
                [Test()]
 
626
                public void TestCreateMethodOutOfDelegateCreation ()
 
627
                {
 
628
                        Test<CreateMethodDeclarationAction> (
 
629
@"using System;
 
630
class Test
 
631
{
 
632
        public void ATest ()
 
633
        {
 
634
                new System.EventHandler<System.EventArgs>($BeginDownloadingImage);
 
635
        }
 
636
}
 
637
", @"using System;
 
638
class Test
 
639
{
 
640
        void BeginDownloadingImage (object sender, EventArgs e)
 
641
        {
 
642
                throw new NotImplementedException ();
 
643
        }
 
644
        public void ATest ()
 
645
        {
 
646
                new System.EventHandler<System.EventArgs>(BeginDownloadingImage);
 
647
        }
 
648
}
 
649
");
 
650
                }
 
651
                
 
652
                [Test()]
 
653
                public void TestStaticClassMethod ()
 
654
                {
 
655
                        // Not 100% correct input code, but should work in that case as well.
 
656
                        Test<CreateMethodDeclarationAction> (@"static class TestClass
 
657
{
 
658
        public TestClass ()
 
659
        {
 
660
                $Foo (5);
 
661
        }
 
662
}", @"static class TestClass
 
663
{
 
664
        static void Foo (int i)
 
665
        {
 
666
                throw new System.NotImplementedException ();
 
667
        }
 
668
        public TestClass ()
 
669
        {
 
670
                Foo (5);
 
671
        }
 
672
}");
 
673
                }
 
674
 
 
675
                [Test()]
 
676
                public void TestCreateFromIdentifierNestedInMethodCall ()
 
677
                {
 
678
                        // Not 100% correct input code, but should work in that case as well.
 
679
                        Test<CreateMethodDeclarationAction> (@"namespace System {
 
680
        class TestClass
 
681
        {
 
682
                public void FooBar (object test)
 
683
                {
 
684
                        FooBar (new EventHandler ($Foo));
 
685
                }
 
686
        }
 
687
}", @"namespace System {
 
688
        class TestClass
 
689
        {
 
690
                void Foo (object sender, EventArgs e)
 
691
                {
 
692
                        throw new NotImplementedException ();
 
693
                }
 
694
                public void FooBar (object test)
 
695
                {
 
696
                        FooBar (new EventHandler (Foo));
 
697
                }
 
698
        }
 
699
}");
 
700
                }
 
701
 
 
702
                [Test]
 
703
                public void TestEnumCase()
 
704
                {
 
705
                        TestWrongContext<CreateMethodDeclarationAction>(@"
 
706
enum AEnum { A }
 
707
class Foo
 
708
{
 
709
        public void Test ()
 
710
        {
 
711
                AEnum e = AEnum.B$ar ();
 
712
        }
 
713
}
 
714
");
 
715
                }
 
716
 
 
717
                [Test]
 
718
                public void TestPassNullArgument ()
 
719
                {
 
720
                        Test<CreateMethodDeclarationAction> (@"class TestClass
 
721
{
 
722
        void TestMethod ()
 
723
        {
 
724
                $NonExistantMethod (null);
 
725
        }
 
726
}", @"class TestClass
 
727
{
 
728
        void NonExistantMethod (object o)
 
729
        {
 
730
                throw new System.NotImplementedException ();
 
731
        }
 
732
        void TestMethod ()
 
733
        {
 
734
                NonExistantMethod (null);
 
735
        }
 
736
}");
 
737
                }
 
738
 
 
739
 
 
740
 
 
741
        }
 
742
}
 
743