2
// CreateClassDeclarationTests.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 CreateClassDeclarationTests : ContextActionTestBase
37
public void TestCreateClass ()
39
Test<CreateClassDeclarationAction> (
45
$new Foo (0, ""Hello"");
51
public Foo (int i, string hello)
53
throw new System.NotImplementedException ();
60
new Foo (0, ""Hello"");
67
public void TestNestedCreateClass()
69
Test<CreateClassDeclarationAction>(
85
throw new System.NotImplementedException ();
97
public void TestEmptyConstructor ()
99
Test<CreateClassDeclarationAction> (
123
public void TestCreatePublicEventArgs ()
125
Test<CreateClassDeclarationAction> (
129
public event EventHandler<$MyEventArgs> evt;
132
public class MyEventArgs : System.EventArgs
137
public event EventHandler<MyEventArgs> evt;
143
public void TestCreateInternalEventArgs ()
145
Test<CreateClassDeclarationAction> (
149
internal event EventHandler<$MyEventArgs> evt;
152
class MyEventArgs : System.EventArgs
157
internal event EventHandler<MyEventArgs> evt;
163
public void TestCreateAttribute ()
165
Test<CreateClassDeclarationAction> (
172
class MyAttribute : System.Attribute
183
public void TestCreateAttributeCase2 ()
185
Test<CreateClassDeclarationAction> (
192
class MyAttribute : System.Attribute
203
public void TestCreateException ()
205
Test<CreateClassDeclarationAction> (
211
throw $new MyException ();
215
class MyException : System.Exception
222
throw new MyException ();
229
public void TestNotShowInEventTypes()
231
TestWrongContext<CreateClassDeclarationAction>(
235
event $MyEventHandler evt;
241
public void TestCreateClassImplementingInterface()
243
Test<CreateClassDeclarationAction>(
247
void TestMethod (System.IDisposable d)
249
TestMethod ($new Foo ());
253
class Foo : System.IDisposable
255
public void Dispose ()
257
throw new System.NotImplementedException ();
262
void TestMethod (System.IDisposable d)
264
TestMethod (new Foo ());
271
public void TestCreateClassExtendingAbstractClass()
273
Test<CreateClassDeclarationAction>(
277
abstract class FooBar { protected abstract void SomeFoo (); public abstract int MoreFoo { get; } }
278
void TestMethod (FooBar d)
280
TestMethod ($new Foo ());
286
public override int MoreFoo {
289
protected override void SomeFoo ()
291
throw new System.NotImplementedException ();
296
abstract class FooBar { protected abstract void SomeFoo (); public abstract int MoreFoo { get; } }
297
void TestMethod (FooBar d)
299
TestMethod (new Foo ());
306
public void TestModifierBug ()
308
Test<CreateClassDeclarationAction> (
312
private readonly $Foo _foo;
320
private readonly Foo _foo;
327
public void TestCreateClassFromMemberReferenceExpression ()
329
Test<CreateClassDeclarationAction> (
354
/// Bug 10671 - Auto-Fix of Base Class is wrong (generates invalid code)
357
public void TestBug10671 ()
360
namespace TestConsole
362
public class Test : $BaseMissing
367
// action allowed to create a nested class
368
var context = TestRefactoringContext.Create (input, false);
369
var actions = new CreateClassDeclarationAction().GetActions (context);
370
Assert.AreEqual (1, actions.Count ());
375
public void TestCreateInterface ()
377
Test<CreateClassDeclarationAction> (
381
private readonly $IFoo _foo;
389
private readonly IFoo _foo;
395
/// Bug 10672 - Auto-Fix of Generate Class to fill Generic params does not take in account constraints
398
public void TestBug10672 ()
400
Test<CreateClassDeclarationAction> (
402
namespace TestConsole
404
public interface IBase
409
public void Generate<S, T>() where T:IBase, new()
416
public static void Main (string[] args)
418
var testConsole = new Test ();
419
testConsole.Generate<int, $Data> ();
424
public class Data : IBase
430
namespace TestConsole
432
public interface IBase
437
public void Generate<S, T>() where T:IBase, new()
444
public static void Main (string[] args)
446
var testConsole = new Test ();
447
testConsole.Generate<int, Data> ();
455
public void TestStructConstraint ()
457
Test<CreateClassDeclarationAction> (
461
public void Generate<T> () where T : struct
465
public void FooBar ()
476
public void Generate<T> () where T : struct
480
public void FooBar ()
489
public void TestClassTypeParameter ()
491
Test<CreateClassDeclarationAction> (
495
public class Generate<T> where T : struct {}
497
public void FooBar ()
508
public class Generate<T> where T : struct {}
510
public void FooBar ()
518
public void TestClassTypeParameterCase2 ()
520
Test<CreateClassDeclarationAction> (
524
public class Generate<T> where T : struct {}
526
public void FooBar ()
528
new Generate<$Data> ();
537
public class Generate<T> where T : struct {}
539
public void FooBar ()
541
new Generate<Data> ();