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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ImplementInterfaceTests.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
// ImplementInterfaceTests.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin Inc. (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
 
 
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
 
31
{
 
32
        [TestFixture]
 
33
        public class ImplementInterfaceTests : ContextActionTestBase
 
34
        {
 
35
                [Test]
 
36
                public void TestSimpleInterface()
 
37
                {
 
38
                        Test<ImplementInterfaceAction>(@"using System;
 
39
class Foo : $IDisposable
 
40
{
 
41
}
 
42
", @"using System;
 
43
class Foo : IDisposable
 
44
{
 
45
        #region IDisposable implementation
 
46
        public void Dispose ()
 
47
        {
 
48
                throw new NotImplementedException ();
 
49
        }
 
50
        #endregion
 
51
}
 
52
");
 
53
                }
 
54
 
 
55
 
 
56
                /// <summary>
 
57
                /// Bug 663842 - Interface implementation does not include constraints
 
58
                /// </summary>
 
59
                [Test]
 
60
                public void TestBug663842()
 
61
                {
 
62
                        Test<ImplementInterfaceAction>(@"using System;
 
63
interface ITest {
 
64
        void MyMethod1<T> (T t) where T : new ();
 
65
        void MyMethod2<T> (T t) where T : class;
 
66
        void MyMethod3<T> (T t) where T : struct;
 
67
        void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider;
 
68
}
 
69
 
 
70
class Foo : $ITest
 
71
{
 
72
}
 
73
", @"using System;
 
74
interface ITest {
 
75
        void MyMethod1<T> (T t) where T : new ();
 
76
        void MyMethod2<T> (T t) where T : class;
 
77
        void MyMethod3<T> (T t) where T : struct;
 
78
        void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider;
 
79
}
 
80
 
 
81
class Foo : ITest
 
82
{
 
83
        #region ITest implementation
 
84
        public void MyMethod1<T> (T t) where T : new()
 
85
        {
 
86
                throw new NotImplementedException ();
 
87
        }
 
88
        public void MyMethod2<T> (T t) where T : class
 
89
        {
 
90
                throw new NotImplementedException ();
 
91
        }
 
92
        public void MyMethod3<T> (T t) where T : struct
 
93
        {
 
94
                throw new NotImplementedException ();
 
95
        }
 
96
        public void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider
 
97
        {
 
98
                throw new NotImplementedException ();
 
99
        }
 
100
        #endregion
 
101
}
 
102
");
 
103
                }
 
104
 
 
105
                /// <summary>
 
106
                /// Bug 683007 - "Refactor/Implement implicit" creates explicit implementations of methods with same names
 
107
                /// </summary>
 
108
                [Test]
 
109
                public void TestBug683007()
 
110
                {
 
111
                        Test<ImplementInterfaceAction>(@"interface ITest {
 
112
        void M1();
 
113
        void M1(int x);
 
114
}
 
115
 
 
116
class Foo : $ITest
 
117
{
 
118
}", @"interface ITest {
 
119
        void M1();
 
120
        void M1(int x);
 
121
}
 
122
 
 
123
class Foo : ITest
 
124
{
 
125
        #region ITest implementation
 
126
        public void M1 ()
 
127
        {
 
128
                throw new System.NotImplementedException ();
 
129
        }
 
130
        public void M1 (int x)
 
131
        {
 
132
                throw new System.NotImplementedException ();
 
133
        }
 
134
        #endregion
 
135
}");
 
136
                }
 
137
                
 
138
                /// <summary>
 
139
                /// Bug 243 - Implement implicit interface doesn't handle overloads correctly. 
 
140
                /// </summary>
 
141
                [Test]
 
142
                public void TestBug243()
 
143
                {
 
144
                        Test<ImplementInterfaceAction>(@"interface ITest {
 
145
        void Inc (int n);
 
146
        void Inc (string message);
 
147
}
 
148
 
 
149
class Foo : $ITest
 
150
{
 
151
}
 
152
", @"interface ITest {
 
153
        void Inc (int n);
 
154
        void Inc (string message);
 
155
}
 
156
 
 
157
class Foo : ITest
 
158
{
 
159
        #region ITest implementation
 
160
        public void Inc (int n)
 
161
        {
 
162
                throw new System.NotImplementedException ();
 
163
        }
 
164
        public void Inc (string message)
 
165
        {
 
166
                throw new System.NotImplementedException ();
 
167
        }
 
168
        #endregion
 
169
}
 
170
");
 
171
                }
 
172
                
 
173
                
 
174
                /// <summary>
 
175
                /// Bug 2074 - [Regression] Implement Interface implicitly does not check the methods already exist 
 
176
                /// </summary>
 
177
                [Test]
 
178
                public void TestBug2074()
 
179
                {
 
180
                        Test<ImplementInterfaceAction>(@"interface ITest {
 
181
        void Method1 ();
 
182
        void Method2 ();
 
183
}
 
184
 
 
185
class Foo : $ITest
 
186
{
 
187
        public void Method2 () {}
 
188
}", @"interface ITest {
 
189
        void Method1 ();
 
190
        void Method2 ();
 
191
}
 
192
 
 
193
class Foo : ITest
 
194
{
 
195
        #region ITest implementation
 
196
        public void Method1 ()
 
197
        {
 
198
                throw new System.NotImplementedException ();
 
199
        }
 
200
        #endregion
 
201
        public void Method2 () {}
 
202
}");
 
203
                }
 
204
                
 
205
                /// <summary>
 
206
                /// Bug 3365 - MD cannot implement IEnumerable interface correctly  - MD cannot implement IEnumerable interface correctly 
 
207
                /// </summary>
 
208
                [Test]
 
209
                public void TestBug3365()
 
210
                {
 
211
                        Test<ImplementInterfaceAction>(@"using System;
 
212
using System.Collections;
 
213
 
 
214
public interface IA
 
215
{
 
216
        bool GetEnumerator ();
 
217
}
 
218
 
 
219
public interface ITest : IA, IEnumerable
 
220
{
 
221
}
 
222
 
 
223
class Foo : $ITest
 
224
{
 
225
}", @"using System;
 
226
using System.Collections;
 
227
 
 
228
public interface IA
 
229
{
 
230
        bool GetEnumerator ();
 
231
}
 
232
 
 
233
public interface ITest : IA, IEnumerable
 
234
{
 
235
}
 
236
 
 
237
class Foo : ITest
 
238
{
 
239
        #region IEnumerable implementation
 
240
        public IEnumerator GetEnumerator ()
 
241
        {
 
242
                throw new NotImplementedException ();
 
243
        }
 
244
        #endregion
 
245
        #region IA implementation
 
246
        bool IA.GetEnumerator ()
 
247
        {
 
248
                throw new NotImplementedException ();
 
249
        }
 
250
        #endregion
 
251
}");
 
252
                }
 
253
 
 
254
 
 
255
                /// <summary>
 
256
                /// Bug 4818 - Implement implicit does not handle 'params' types 
 
257
                /// </summary>
 
258
                [Test]
 
259
                public void TestBug4818()
 
260
                {
 
261
                        Test<ImplementInterfaceAction>(@"using System;
 
262
interface ITest {
 
263
        void OnScenesAdded (params ITest[] scenes);
 
264
}
 
265
 
 
266
class Foo : $ITest
 
267
{
 
268
}
 
269
", @"using System;
 
270
interface ITest {
 
271
        void OnScenesAdded (params ITest[] scenes);
 
272
}
 
273
 
 
274
class Foo : ITest
 
275
{
 
276
        #region ITest implementation
 
277
        public void OnScenesAdded (params ITest[] scenes)
 
278
        {
 
279
                throw new NotImplementedException ();
 
280
        }
 
281
        #endregion
 
282
}
 
283
");
 
284
 
 
285
                        TestWrongContext<ImplementInterfaceAction>(@"using System;
 
286
interface ITest {
 
287
        void OnScenesAdded (params ITest[] scenes);
 
288
}
 
289
 
 
290
class Foo : $ITest
 
291
{
 
292
        #region ITest implementation
 
293
        public void OnScenesAdded (params ITest[] scenes)
 
294
        {
 
295
                throw new NotImplementedException ();
 
296
        }
 
297
        #endregion
 
298
}
 
299
");
 
300
                }
 
301
 
 
302
                /// <summary>
 
303
                /// Bug 9117 - [3.0.5] C#: Implementing interfaces inheriting from other interfaces
 
304
                /// </summary>
 
305
                [Test]
 
306
                public void TestBug9117()
 
307
                {
 
308
                        Test<ImplementInterfaceAction>(@"using System;
 
309
 public interface IAncestor
 
310
{
 
311
        string X { get; set; }
 
312
        void DoThings();
 
313
}
 
314
 
 
315
public interface IDescendant: IAncestor
 
316
{
 
317
        string Y { get; set; }
 
318
        string Z { get; set; }
 
319
}
 
320
 
 
321
class Foo : $IDescendant
 
322
{
 
323
}
 
324
", @"using System;
 
325
 public interface IAncestor
 
326
{
 
327
        string X { get; set; }
 
328
        void DoThings();
 
329
}
 
330
 
 
331
public interface IDescendant: IAncestor
 
332
{
 
333
        string Y { get; set; }
 
334
        string Z { get; set; }
 
335
}
 
336
 
 
337
class Foo : IDescendant
 
338
{
 
339
        #region IAncestor implementation
 
340
        public void DoThings ()
 
341
        {
 
342
                throw new NotImplementedException ();
 
343
        }
 
344
        public string X {
 
345
                get {
 
346
                        throw new NotImplementedException ();
 
347
                }
 
348
                set {
 
349
                        throw new NotImplementedException ();
 
350
                }
 
351
        }
 
352
        #endregion
 
353
        #region IDescendant implementation
 
354
        public string Y {
 
355
                get {
 
356
                        throw new NotImplementedException ();
 
357
                }
 
358
                set {
 
359
                        throw new NotImplementedException ();
 
360
                }
 
361
        }
 
362
        public string Z {
 
363
                get {
 
364
                        throw new NotImplementedException ();
 
365
                }
 
366
                set {
 
367
                        throw new NotImplementedException ();
 
368
                }
 
369
        }
 
370
        #endregion
 
371
}
 
372
");
 
373
                        
 
374
                        TestWrongContext<ImplementInterfaceAction>(@"using System;
 
375
interface ITest {
 
376
        void OnScenesAdded (params ITest[] scenes);
 
377
}
 
378
 
 
379
class Foo : $ITest
 
380
{
 
381
        #region ITest implementation
 
382
        public void OnScenesAdded (params ITest[] scenes)
 
383
        {
 
384
                throw new NotImplementedException ();
 
385
        }
 
386
        #endregion
 
387
}
 
388
");
 
389
                }
 
390
 
 
391
                /// <summary>
 
392
                /// Bug 9603 - Implement interface cannot deal with member hidding
 
393
                /// </summary>
 
394
                [Test]
 
395
                public void TestBug9603()
 
396
                {
 
397
                        Test<ImplementInterfaceAction>(@"using System;
 
398
 
 
399
public interface IA
 
400
{
 
401
    string this[int index] { get; set; }
 
402
}
 
403
 
 
404
public interface IB : IA
 
405
{
 
406
    new int this[int index] { get; set; }
 
407
}
 
408
 
 
409
class M : $IB
 
410
{
 
411
}", @"using System;
 
412
 
 
413
public interface IA
 
414
{
 
415
    string this[int index] { get; set; }
 
416
}
 
417
 
 
418
public interface IB : IA
 
419
{
 
420
    new int this[int index] { get; set; }
 
421
}
 
422
 
 
423
class M : IB
 
424
{
 
425
        #region IB implementation
 
426
        public int this [int index] {
 
427
                get {
 
428
                        throw new NotImplementedException ();
 
429
                }
 
430
                set {
 
431
                        throw new NotImplementedException ();
 
432
                }
 
433
        }
 
434
        #endregion
 
435
        #region IA implementation
 
436
        string IA.this [int index] {
 
437
                get {
 
438
                        throw new NotImplementedException ();
 
439
                }
 
440
                set {
 
441
                        throw new NotImplementedException ();
 
442
                }
 
443
        }
 
444
        #endregion
 
445
}");
 
446
                }
 
447
        }
 
448
}
 
449