2
// CreateMethodDeclarationTests.cs
5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin <http://xamarin.com>
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
27
using NUnit.Framework;
28
using ICSharpCode.NRefactory.CSharp.Refactoring;
31
namespace ICSharpCode.NRefactory.CSharp.CodeActions
34
public class CreateMethodDeclarationTests : ContextActionTestBase
37
public void TestPrivateSimpleCreateMethod ()
39
Test<CreateMethodDeclarationAction> (@"class TestClass
42
string Test { get; set; }
46
$NonExistantMethod (member, Test, 5);
51
string Test { get; set; }
53
void NonExistantMethod (int member, string test, int i)
55
throw new System.NotImplementedException ();
59
NonExistantMethod (member, Test, 5);
65
public void TestStaticSimpleCreateMethod ()
67
Test<CreateMethodDeclarationAction> (@"class TestClass
69
public static void TestMethod ()
72
$NonExistantMethod (testLocalVar);
76
static void NonExistantMethod (int testLocalVar)
78
throw new System.NotImplementedException ();
80
public static void TestMethod ()
83
NonExistantMethod (testLocalVar);
89
public void TestGuessAssignmentReturnType ()
91
Test<CreateMethodDeclarationAction> (@"class TestClass
93
static void TestMethod ()
95
int testLocalVar = $NonExistantMethod ();
99
static int NonExistantMethod ()
101
throw new System.NotImplementedException ();
103
static void TestMethod ()
105
int testLocalVar = NonExistantMethod ();
111
public void TestGuessAssignmentReturnTypeCase2 ()
113
Test<CreateMethodDeclarationAction> (@"class TestClass
115
static void TestMethod ()
118
testLocalVar = $NonExistantMethod ();
120
}", @"class TestClass
122
static int NonExistantMethod ()
124
throw new System.NotImplementedException ();
126
static void TestMethod ()
129
testLocalVar = NonExistantMethod ();
135
public void TestGuessAssignmentReturnTypeCase3 ()
137
Test<CreateMethodDeclarationAction> (@"class TestClass
139
static void TestMethod ()
141
var testLocalVar = (string)$NonExistantMethod ();
143
}", @"class TestClass
145
static string NonExistantMethod ()
147
throw new System.NotImplementedException ();
149
static void TestMethod ()
151
var testLocalVar = (string)NonExistantMethod ();
158
public void TestGuessParameterType ()
160
Test<CreateMethodDeclarationAction> (@"class TestClass
164
Test ($NonExistantMethod ());
168
}", @"class TestClass
170
int NonExistantMethod ()
172
throw new System.NotImplementedException ();
176
Test (NonExistantMethod ());
185
public void TestCreateDelegateDeclarationIdentifierCase ()
187
Test<CreateMethodDeclarationAction> (@"class TestClass
189
public event MyDelegate MyEvent;
193
MyEvent += $NonExistantMethod;
197
public delegate string MyDelegate (int a, object b);
200
public event MyDelegate MyEvent;
202
string NonExistantMethod (int a, object b)
204
throw new System.NotImplementedException ();
208
MyEvent += NonExistantMethod;
212
public delegate string MyDelegate (int a, object b);
217
public void TestCreateDelegateDeclarationMemberReferenceCase ()
219
Test<CreateMethodDeclarationAction> (@"class TestClass
221
public event MyDelegate MyEvent;
225
MyEvent += $this.NonExistantMethod;
229
public delegate string MyDelegate (int a, object b);
232
public event MyDelegate MyEvent;
234
string NonExistantMethod (int a, object b)
236
throw new System.NotImplementedException ();
240
MyEvent += this.NonExistantMethod;
244
public delegate string MyDelegate (int a, object b);
249
public void TestCreateDelegateDeclarationInOtherClassMemberReferenceCase ()
251
Test<CreateMethodDeclarationAction> (@"class Foo {
256
public event MyDelegate MyEvent;
260
MyEvent += $new Foo ().NonExistantMethod;
264
public delegate string MyDelegate (int a, object b);
266
public string NonExistantMethod (int a, object b)
268
throw new System.NotImplementedException ();
274
public event MyDelegate MyEvent;
278
MyEvent += new Foo ().NonExistantMethod;
282
public delegate string MyDelegate (int a, object b);
287
public void TestRefOutCreateMethod ()
289
Test<CreateMethodDeclarationAction> (@"class TestClass
294
$NonExistantMethod (ref a, out b);
296
}", @"class TestClass
298
void NonExistantMethod (ref int a, out int b)
300
throw new System.NotImplementedException ();
305
NonExistantMethod (ref a, out b);
311
public void TestExternMethod ()
313
Test<CreateMethodDeclarationAction> (
323
var fb = new FooBar ();
324
fb.$NonExistantMethod ();
330
public void NonExistantMethod ()
332
throw new System.NotImplementedException ();
340
var fb = new FooBar ();
341
fb.NonExistantMethod ();
348
public void TestCreateInterfaceMethod ()
350
Test<CreateMethodDeclarationAction> (
361
fb.$NonExistantMethod ();
367
void NonExistantMethod ();
375
fb.NonExistantMethod ();
382
public void TestCreateInStaticClassMethod ()
384
Test<CreateMethodDeclarationAction> (
394
FooBar.$NonExistantMethod ();
400
public static void NonExistantMethod ()
402
throw new System.NotImplementedException ();
410
FooBar.NonExistantMethod ();
419
/// Bug 677522 - "Create Method" creates at wrong indent level
422
public void TestBug677522 ()
424
Test<CreateMethodDeclarationAction> (
430
$NonExistantMethod ();
434
", @"namespace Test {
437
void NonExistantMethod ()
439
throw new System.NotImplementedException ();
443
NonExistantMethod ();
451
/// Bug 677527 - "Create Method" uses fully qualified namespace when "using" statement exists
454
public void TestBug677527 ()
456
Test<CreateMethodDeclarationAction> (
464
StringBuilder sb = new StringBuilder ();
465
$NonExistantMethod (sb);
469
", @"using System.Text;
474
void NonExistantMethod (StringBuilder sb)
476
throw new System.NotImplementedException ();
480
StringBuilder sb = new StringBuilder ();
481
NonExistantMethod (sb);
490
/// Bug 693949 - Create method uses the wrong type for param
494
public void TestBug693949 ()
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> (
503
string test(string a)
507
public static void Main(string[] args)
509
Type a = $M(test(""a""));
513
", @"using System.Text;
518
string test(string a)
522
static Type M (string par1)
524
throw new System.NotImplementedException ();
527
public static void Main(string[] args)
529
Type a = $M(test(""a""));
537
/// Bug 469 - CreateMethod created a method incorrectly
540
public void TestBug469 ()
542
Test<CreateMethodDeclarationAction> (
545
public override string ToString ()
547
$BeginDownloadingImage (this);
552
void BeginDownloadingImage (Test test)
554
throw new System.NotImplementedException ();
556
public override string ToString ()
558
BeginDownloadingImage (this);
565
public void TestTestGuessReturnReturnType ()
567
Test<CreateMethodDeclarationAction> (
570
public override string ToString ()
572
return $BeginDownloadingImage (this);
577
string BeginDownloadingImage (Test test)
579
throw new System.NotImplementedException ();
581
public override string ToString ()
583
return BeginDownloadingImage (this);
590
public void TestStringParameterNameGuessing ()
592
Test<CreateMethodDeclarationAction> (@"class TestClass
594
static void TestMethod ()
596
$NonExistantMethod (""Hello World!"");
598
}", @"class TestClass
600
static void NonExistantMethod (string helloWorld)
602
throw new System.NotImplementedException ();
604
static void TestMethod ()
606
NonExistantMethod (""Hello World!"");
612
public void TestMethodInFrameworkClass ()
614
TestWrongContext<CreateMethodDeclarationAction> (
619
$System.Console.ImprovedWriteLine (""Think of it"");
626
public void TestCreateMethodOutOfDelegateCreation ()
628
Test<CreateMethodDeclarationAction> (
634
new System.EventHandler<System.EventArgs>($BeginDownloadingImage);
640
void BeginDownloadingImage (object sender, EventArgs e)
642
throw new NotImplementedException ();
646
new System.EventHandler<System.EventArgs>(BeginDownloadingImage);
653
public void TestStaticClassMethod ()
655
// Not 100% correct input code, but should work in that case as well.
656
Test<CreateMethodDeclarationAction> (@"static class TestClass
662
}", @"static class TestClass
664
static void Foo (int i)
666
throw new System.NotImplementedException ();
676
public void TestCreateFromIdentifierNestedInMethodCall ()
678
// Not 100% correct input code, but should work in that case as well.
679
Test<CreateMethodDeclarationAction> (@"namespace System {
682
public void FooBar (object test)
684
FooBar (new EventHandler ($Foo));
687
}", @"namespace System {
690
void Foo (object sender, EventArgs e)
692
throw new NotImplementedException ();
694
public void FooBar (object test)
696
FooBar (new EventHandler (Foo));
703
public void TestEnumCase()
705
TestWrongContext<CreateMethodDeclarationAction>(@"
711
AEnum e = AEnum.B$ar ();
718
public void TestPassNullArgument ()
720
Test<CreateMethodDeclarationAction> (@"class TestClass
724
$NonExistantMethod (null);
726
}", @"class TestClass
728
void NonExistantMethod (object o)
730
throw new System.NotImplementedException ();
734
NonExistantMethod (null);