2
// ImplementInterfaceTests.cs
5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (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;
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
33
public class ImplementInterfaceTests : ContextActionTestBase
36
public void TestSimpleInterface()
38
Test<ImplementInterfaceAction>(@"using System;
39
class Foo : $IDisposable
43
class Foo : IDisposable
45
#region IDisposable implementation
46
public void Dispose ()
48
throw new NotImplementedException ();
57
/// Bug 663842 - Interface implementation does not include constraints
60
public void TestBug663842()
62
Test<ImplementInterfaceAction>(@"using System;
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;
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;
83
#region ITest implementation
84
public void MyMethod1<T> (T t) where T : new()
86
throw new NotImplementedException ();
88
public void MyMethod2<T> (T t) where T : class
90
throw new NotImplementedException ();
92
public void MyMethod3<T> (T t) where T : struct
94
throw new NotImplementedException ();
96
public void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider
98
throw new NotImplementedException ();
106
/// Bug 683007 - "Refactor/Implement implicit" creates explicit implementations of methods with same names
109
public void TestBug683007()
111
Test<ImplementInterfaceAction>(@"interface ITest {
118
}", @"interface ITest {
125
#region ITest implementation
128
throw new System.NotImplementedException ();
130
public void M1 (int x)
132
throw new System.NotImplementedException ();
139
/// Bug 243 - Implement implicit interface doesn't handle overloads correctly.
142
public void TestBug243()
144
Test<ImplementInterfaceAction>(@"interface ITest {
146
void Inc (string message);
152
", @"interface ITest {
154
void Inc (string message);
159
#region ITest implementation
160
public void Inc (int n)
162
throw new System.NotImplementedException ();
164
public void Inc (string message)
166
throw new System.NotImplementedException ();
175
/// Bug 2074 - [Regression] Implement Interface implicitly does not check the methods already exist
178
public void TestBug2074()
180
Test<ImplementInterfaceAction>(@"interface ITest {
187
public void Method2 () {}
188
}", @"interface ITest {
195
#region ITest implementation
196
public void Method1 ()
198
throw new System.NotImplementedException ();
201
public void Method2 () {}
206
/// Bug 3365 - MD cannot implement IEnumerable interface correctly - MD cannot implement IEnumerable interface correctly
209
public void TestBug3365()
211
Test<ImplementInterfaceAction>(@"using System;
212
using System.Collections;
216
bool GetEnumerator ();
219
public interface ITest : IA, IEnumerable
226
using System.Collections;
230
bool GetEnumerator ();
233
public interface ITest : IA, IEnumerable
239
#region IEnumerable implementation
240
public IEnumerator GetEnumerator ()
242
throw new NotImplementedException ();
245
#region IA implementation
246
bool IA.GetEnumerator ()
248
throw new NotImplementedException ();
256
/// Bug 4818 - Implement implicit does not handle 'params' types
259
public void TestBug4818()
261
Test<ImplementInterfaceAction>(@"using System;
263
void OnScenesAdded (params ITest[] scenes);
271
void OnScenesAdded (params ITest[] scenes);
276
#region ITest implementation
277
public void OnScenesAdded (params ITest[] scenes)
279
throw new NotImplementedException ();
285
TestWrongContext<ImplementInterfaceAction>(@"using System;
287
void OnScenesAdded (params ITest[] scenes);
292
#region ITest implementation
293
public void OnScenesAdded (params ITest[] scenes)
295
throw new NotImplementedException ();
303
/// Bug 9117 - [3.0.5] C#: Implementing interfaces inheriting from other interfaces
306
public void TestBug9117()
308
Test<ImplementInterfaceAction>(@"using System;
309
public interface IAncestor
311
string X { get; set; }
315
public interface IDescendant: IAncestor
317
string Y { get; set; }
318
string Z { get; set; }
321
class Foo : $IDescendant
325
public interface IAncestor
327
string X { get; set; }
331
public interface IDescendant: IAncestor
333
string Y { get; set; }
334
string Z { get; set; }
337
class Foo : IDescendant
339
#region IAncestor implementation
340
public void DoThings ()
342
throw new NotImplementedException ();
346
throw new NotImplementedException ();
349
throw new NotImplementedException ();
353
#region IDescendant implementation
356
throw new NotImplementedException ();
359
throw new NotImplementedException ();
364
throw new NotImplementedException ();
367
throw new NotImplementedException ();
374
TestWrongContext<ImplementInterfaceAction>(@"using System;
376
void OnScenesAdded (params ITest[] scenes);
381
#region ITest implementation
382
public void OnScenesAdded (params ITest[] scenes)
384
throw new NotImplementedException ();
392
/// Bug 9603 - Implement interface cannot deal with member hidding
395
public void TestBug9603()
397
Test<ImplementInterfaceAction>(@"using System;
401
string this[int index] { get; set; }
404
public interface IB : IA
406
new int this[int index] { get; set; }
415
string this[int index] { get; set; }
418
public interface IB : IA
420
new int this[int index] { get; set; }
425
#region IB implementation
426
public int this [int index] {
428
throw new NotImplementedException ();
431
throw new NotImplementedException ();
435
#region IA implementation
436
string IA.this [int index] {
438
throw new NotImplementedException ();
441
throw new NotImplementedException ();