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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateClassDeclarationTests.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
// CreateClassDeclarationTests.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.Linq;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.CodeActions
 
32
{
 
33
        [TestFixture]
 
34
        public class CreateClassDeclarationTests : ContextActionTestBase
 
35
        {
 
36
                [Test()]
 
37
                public void TestCreateClass ()
 
38
                {
 
39
                        Test<CreateClassDeclarationAction> (
 
40
@"
 
41
class TestClass
 
42
{
 
43
        void TestMethod ()
 
44
        {
 
45
                $new Foo (0, ""Hello"");
 
46
        }
 
47
}
 
48
", @"
 
49
class Foo
 
50
{
 
51
        public Foo (int i, string hello)
 
52
        {
 
53
                throw new System.NotImplementedException ();
 
54
        }
 
55
}
 
56
class TestClass
 
57
{
 
58
        void TestMethod ()
 
59
        {
 
60
                new Foo (0, ""Hello"");
 
61
        }
 
62
}
 
63
");
 
64
                }
 
65
 
 
66
                [Test]
 
67
                public void TestNestedCreateClass()
 
68
                {
 
69
                        Test<CreateClassDeclarationAction>(
 
70
@"
 
71
class TestClass
 
72
{
 
73
        void TestMethod ()
 
74
        {
 
75
                $new Foo (0);
 
76
        }
 
77
}
 
78
", @"
 
79
class TestClass
 
80
{
 
81
        class Foo
 
82
        {
 
83
                public Foo (int i)
 
84
                {
 
85
                        throw new System.NotImplementedException ();
 
86
                }
 
87
        }
 
88
        void TestMethod ()
 
89
        {
 
90
                new Foo (0);
 
91
        }
 
92
}
 
93
", 1);
 
94
                }
 
95
 
 
96
                [Test]
 
97
                public void TestEmptyConstructor ()
 
98
                {
 
99
                        Test<CreateClassDeclarationAction> (
 
100
@"
 
101
class TestClass
 
102
{
 
103
        void TestMethod ()
 
104
        {
 
105
                $new Foo ();
 
106
        }
 
107
}
 
108
", @"
 
109
class Foo
 
110
{
 
111
}
 
112
class TestClass
 
113
{
 
114
        void TestMethod ()
 
115
        {
 
116
                new Foo ();
 
117
        }
 
118
}
 
119
");
 
120
                }
 
121
 
 
122
                [Test]
 
123
                public void TestCreatePublicEventArgs ()
 
124
                {
 
125
                        Test<CreateClassDeclarationAction> (
 
126
@"
 
127
class TestClass
 
128
{
 
129
        public event EventHandler<$MyEventArgs> evt;
 
130
}
 
131
", @"
 
132
public class MyEventArgs : System.EventArgs
 
133
{
 
134
}
 
135
class TestClass
 
136
{
 
137
        public event EventHandler<MyEventArgs> evt;
 
138
}
 
139
");
 
140
                }
 
141
 
 
142
                [Test]
 
143
                public void TestCreateInternalEventArgs ()
 
144
                {
 
145
                        Test<CreateClassDeclarationAction> (
 
146
@"
 
147
class TestClass
 
148
{
 
149
        internal event EventHandler<$MyEventArgs> evt;
 
150
}
 
151
", @"
 
152
class MyEventArgs : System.EventArgs
 
153
{
 
154
}
 
155
class TestClass
 
156
{
 
157
        internal event EventHandler<MyEventArgs> evt;
 
158
}
 
159
");
 
160
                }
 
161
 
 
162
                [Test]
 
163
                public void TestCreateAttribute ()
 
164
                {
 
165
                        Test<CreateClassDeclarationAction> (
 
166
@"
 
167
[$MyAttribute]
 
168
class TestClass
 
169
{
 
170
}
 
171
", @"
 
172
class MyAttribute : System.Attribute
 
173
{
 
174
}
 
175
[MyAttribute]
 
176
class TestClass
 
177
{
 
178
}
 
179
");
 
180
                }
 
181
 
 
182
                [Test]
 
183
                public void TestCreateAttributeCase2 ()
 
184
                {
 
185
                        Test<CreateClassDeclarationAction> (
 
186
@"
 
187
[$My]
 
188
class TestClass
 
189
{
 
190
}
 
191
", @"
 
192
class MyAttribute : System.Attribute
 
193
{
 
194
}
 
195
[My]
 
196
class TestClass
 
197
{
 
198
}
 
199
");
 
200
                }
 
201
 
 
202
                [Test]
 
203
                public void TestCreateException ()
 
204
                {
 
205
                        Test<CreateClassDeclarationAction> (
 
206
@"
 
207
class TestClass
 
208
{
 
209
        void TestMethod ()
 
210
        {
 
211
                throw $new MyException ();
 
212
        }
 
213
}
 
214
", @"
 
215
class MyException : System.Exception
 
216
{
 
217
}
 
218
class TestClass
 
219
{
 
220
        void TestMethod ()
 
221
        {
 
222
                throw new MyException ();
 
223
        }
 
224
}
 
225
");
 
226
                }
 
227
 
 
228
                [Test]
 
229
                public void TestNotShowInEventTypes()
 
230
                {
 
231
                        TestWrongContext<CreateClassDeclarationAction>(
 
232
@"
 
233
class TestClass
 
234
{
 
235
        event $MyEventHandler evt;
 
236
}
 
237
");
 
238
                }
 
239
 
 
240
                [Test]
 
241
                public void TestCreateClassImplementingInterface()
 
242
                {
 
243
                        Test<CreateClassDeclarationAction>(
 
244
@"
 
245
class TestClass
 
246
{
 
247
        void TestMethod (System.IDisposable d)
 
248
        {
 
249
                TestMethod ($new Foo ());
 
250
        }
 
251
}
 
252
", @"
 
253
class Foo : System.IDisposable
 
254
{
 
255
        public void Dispose ()
 
256
        {
 
257
                throw new System.NotImplementedException ();
 
258
        }
 
259
}
 
260
class TestClass
 
261
{
 
262
        void TestMethod (System.IDisposable d)
 
263
        {
 
264
                TestMethod (new Foo ());
 
265
        }
 
266
}
 
267
");
 
268
                }
 
269
 
 
270
                [Test]
 
271
                public void TestCreateClassExtendingAbstractClass()
 
272
                {
 
273
                        Test<CreateClassDeclarationAction>(
 
274
@"
 
275
class TestClass
 
276
{
 
277
        abstract class FooBar { protected abstract void SomeFoo (); public abstract int MoreFoo { get; } }
 
278
        void TestMethod (FooBar d)
 
279
        {
 
280
                TestMethod ($new Foo ());
 
281
        }
 
282
}
 
283
", @"
 
284
class Foo : FooBar
 
285
{
 
286
        public override int MoreFoo {
 
287
                get;
 
288
        }
 
289
        protected override void SomeFoo ()
 
290
        {
 
291
                throw new System.NotImplementedException ();
 
292
        }
 
293
}
 
294
class TestClass
 
295
{
 
296
        abstract class FooBar { protected abstract void SomeFoo (); public abstract int MoreFoo { get; } }
 
297
        void TestMethod (FooBar d)
 
298
        {
 
299
                TestMethod (new Foo ());
 
300
        }
 
301
}
 
302
");
 
303
                }
 
304
 
 
305
                [Test]
 
306
                public void TestModifierBug ()
 
307
                {
 
308
                        Test<CreateClassDeclarationAction> (
 
309
                                @"
 
310
class TestClass
 
311
{
 
312
        private readonly $Foo _foo;
 
313
}
 
314
", @"
 
315
class Foo
 
316
{
 
317
}
 
318
class TestClass
 
319
{
 
320
        private readonly Foo _foo;
 
321
}
 
322
");
 
323
                }
 
324
 
 
325
 
 
326
                [Test]
 
327
                public void TestCreateClassFromMemberReferenceExpression ()
 
328
                {
 
329
                        Test<CreateClassDeclarationAction> (
 
330
                                @"
 
331
class TestClass
 
332
{
 
333
        void TestMethod ()
 
334
        {
 
335
                $Foo.Bar (1);
 
336
        }
 
337
}
 
338
", @"
 
339
class Foo
 
340
{
 
341
}
 
342
class TestClass
 
343
{
 
344
        void TestMethod ()
 
345
        {
 
346
                Foo.Bar (1);
 
347
        }
 
348
}
 
349
");
 
350
                }
 
351
 
 
352
 
 
353
                /// <summary>
 
354
                /// Bug 10671 - Auto-Fix of Base Class is wrong (generates invalid code) 
 
355
                /// </summary>
 
356
                [Test]
 
357
                public void TestBug10671 ()
 
358
                {
 
359
                        var input = @"
 
360
namespace TestConsole
 
361
{
 
362
    public class Test : $BaseMissing
 
363
    {
 
364
    }
 
365
}
 
366
";
 
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 ());
 
371
                }
 
372
 
 
373
                
 
374
                [Test]
 
375
                public void TestCreateInterface ()
 
376
                {
 
377
                        Test<CreateClassDeclarationAction> (
 
378
                                @"
 
379
class TestClass
 
380
{
 
381
        private readonly $IFoo _foo;
 
382
}
 
383
", @"
 
384
interface IFoo
 
385
{
 
386
}
 
387
class TestClass
 
388
{
 
389
        private readonly IFoo _foo;
 
390
}
 
391
");
 
392
                }
 
393
 
 
394
                /// <summary>
 
395
                /// Bug 10672 - Auto-Fix of Generate Class to fill Generic params does not take in account constraints
 
396
                /// </summary>
 
397
                [Test]
 
398
                public void TestBug10672 ()
 
399
                {
 
400
                        Test<CreateClassDeclarationAction> (
 
401
                                @"
 
402
namespace TestConsole
 
403
{
 
404
    public interface IBase
 
405
    {
 
406
    }
 
407
    public class Test 
 
408
    {
 
409
        public void Generate<S, T>() where T:IBase, new()
 
410
        {
 
411
 
 
412
        }
 
413
    }
 
414
    class MainClass
 
415
    {
 
416
        public static void Main (string[] args)
 
417
        {
 
418
            var testConsole = new Test ();
 
419
            testConsole.Generate<int, $Data> ();
 
420
        }
 
421
    }
 
422
}
 
423
", @"
 
424
public class Data : IBase
 
425
{
 
426
        public Data ()
 
427
        {
 
428
        }
 
429
}
 
430
namespace TestConsole
 
431
{
 
432
    public interface IBase
 
433
    {
 
434
    }
 
435
    public class Test 
 
436
    {
 
437
        public void Generate<S, T>() where T:IBase, new()
 
438
        {
 
439
 
 
440
        }
 
441
    }
 
442
    class MainClass
 
443
    {
 
444
        public static void Main (string[] args)
 
445
        {
 
446
            var testConsole = new Test ();
 
447
            testConsole.Generate<int, Data> ();
 
448
        }
 
449
    }
 
450
}
 
451
");
 
452
                }
 
453
                
 
454
                [Test]
 
455
                public void TestStructConstraint ()
 
456
                {
 
457
                        Test<CreateClassDeclarationAction> (
 
458
                                @"
 
459
public class Test 
 
460
{
 
461
        public void Generate<T> () where T : struct
 
462
        {
 
463
        }
 
464
 
 
465
        public void FooBar ()
 
466
        {
 
467
                Generate<$Data> ();
 
468
        }
 
469
}
 
470
", @"
 
471
public struct Data
 
472
{
 
473
}
 
474
public class Test 
 
475
{
 
476
        public void Generate<T> () where T : struct
 
477
        {
 
478
        }
 
479
 
 
480
        public void FooBar ()
 
481
        {
 
482
                Generate<Data> ();
 
483
        }
 
484
}
 
485
");
 
486
                }
 
487
 
 
488
                [Test]
 
489
                public void TestClassTypeParameter ()
 
490
                {
 
491
                        Test<CreateClassDeclarationAction> (
 
492
                                @"
 
493
public class Test 
 
494
{
 
495
        public class Generate<T> where T : struct {}
 
496
 
 
497
        public void FooBar ()
 
498
        {
 
499
                Generate<$Data> foo;
 
500
        }
 
501
}
 
502
", @"
 
503
public struct Data
 
504
{
 
505
}
 
506
public class Test 
 
507
{
 
508
        public class Generate<T> where T : struct {}
 
509
 
 
510
        public void FooBar ()
 
511
        {
 
512
                Generate<Data> foo;
 
513
        }
 
514
}
 
515
");
 
516
                }
 
517
                [Test]
 
518
                public void TestClassTypeParameterCase2 ()
 
519
                {
 
520
                        Test<CreateClassDeclarationAction> (
 
521
                                @"
 
522
public class Test 
 
523
{
 
524
        public class Generate<T> where T : struct {}
 
525
 
 
526
        public void FooBar ()
 
527
        {
 
528
                new Generate<$Data> ();
 
529
        }
 
530
}
 
531
", @"
 
532
public struct Data
 
533
{
 
534
}
 
535
public class Test 
 
536
{
 
537
        public class Generate<T> where T : struct {}
 
538
 
 
539
        public void FooBar ()
 
540
        {
 
541
                new Generate<Data> ();
 
542
        }
 
543
}
 
544
");
 
545
                }
 
546
 
 
547
 
 
548
 
 
549
        }
 
550
}