2
// ImplementInterfaceTests.cs
5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.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
28
using NUnit.Framework;
30
using MonoDevelop.CSharp.Refactoring;
31
using MonoDevelop.Projects;
32
using ICSharpCode.NRefactory.TypeSystem;
33
using MonoDevelop.Ide.TypeSystem;
35
namespace MonoDevelop.CSharpBinding.Refactoring
38
public class ImplementInterfaceTests : UnitTests.TestBase
40
static IUnresolvedAssembly Mscorlib = new CecilLoader().LoadAssemblyFile(typeof(object).Assembly.Location);
41
static IUnresolvedAssembly SystemCore = new CecilLoader().LoadAssemblyFile(typeof(System.Linq.Enumerable).Assembly.Location);
43
void TestCreateInterface (string interfacecode, string outputString, string stubString = null)
45
var project = new UnknownProject ();
46
project.FileName = "test.csproj";
48
TypeSystemService.LoadProject (project);
50
TypeSystemService.ParseFile (project, "program.cs", "text/x-csharp", interfacecode);
51
TypeSystemService.ParseFile (project, "stub.cs", "text/x-csharp", "class Stub {\n "+stubString+"}\n");
53
var wrapper = TypeSystemService.GetProjectContentWrapper (project);
54
wrapper.UpdateContent (c => c.AddAssemblyReferences (new [] { Mscorlib, SystemCore }));
56
var pctx = TypeSystemService.GetCompilation (project);
58
var stubType = pctx.MainAssembly.GetTypeDefinition ("", "Stub", 0);
59
var iface = pctx.MainAssembly.GetTypeDefinition ("", "ITest", 0);
61
var gen = new CSharpCodeGenerator ();
63
gen.Compilation = pctx;
64
string generated = gen.CreateInterfaceImplementation (stubType, stubType.Parts.First (), iface, false);
65
Assert.IsNotEmpty (generated);
67
generated = generated.Substring (generated.IndexOf ("implementation") + "implementation".Length);
68
generated = generated.Substring (0, generated.LastIndexOf ("#"));
69
generated = generated.Trim ();
70
if (outputString != generated)
71
Console.WriteLine (generated);
72
Assert.AreEqual (outputString, generated);
76
/// Bug 663842 - Interface implementation does not include constraints
79
public void TestBug663842 ()
81
TestCreateInterface (@"using System;
83
void MyMethod1<T> (T t) where T : new ();
84
void MyMethod2<T> (T t) where T : class;
85
void MyMethod3<T> (T t) where T : struct;
86
void MyMethod4<T> (T t) where T : IDisposable, IServiceProvider;
87
}", @"public void MyMethod1<T> (T t) where T : new ()
89
throw new System.NotImplementedException ();
92
public void MyMethod2<T> (T t) where T : class
94
throw new System.NotImplementedException ();
97
public void MyMethod3<T> (T t) where T : struct
99
throw new System.NotImplementedException ();
102
public void MyMethod4<T> (T t) where T : System.IDisposable, System.IServiceProvider
104
throw new System.NotImplementedException ();
109
/// Bug 683007 - "Refactor/Implement implicit" creates explicit implementations of methods with same names
112
public void TestBug683007 ()
114
TestCreateInterface (@"interface ITest {
117
}", @"public void M1 ()
119
throw new System.NotImplementedException ();
122
public void M1 (int x)
124
throw new System.NotImplementedException ();
129
/// Bug 243 - Implement implicit interface doesn't handle overloads correctly.
132
public void TestBug243 ()
134
TestCreateInterface (@"interface ITest {
136
void Inc (string message);
137
}", @"public void Inc (int n)
139
throw new System.NotImplementedException ();
142
public void Inc (string message)
144
throw new System.NotImplementedException ();
150
/// Bug 2074 - [Regression] Implement Interface implicitly does not check the methods already exist
153
public void TestBug2074 ()
155
TestCreateInterface (@"interface ITest {
158
}", @"public void Method1 ()
160
throw new System.NotImplementedException ();
161
}", "public void Method2 () {}");
165
/// Bug 3365 - MD cannot implement IEnumerable interface correctly - MD cannot implement IEnumerable interface correctly
168
public void TestBug3365 ()
170
TestCreateInterface (@"using System;
171
using System.Collections;
175
bool GetEnumerator ();
178
public interface ITest : IA, IEnumerable
181
", @"public bool GetEnumerator ()
183
throw new System.NotImplementedException ();
187
#region IEnumerable implementation
188
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
190
throw new System.NotImplementedException ();