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

« back to all changes in this revision

Viewing changes to tests/UnitTests/MonoDevelop.CSharpBinding.Refactoring/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@novell.com>
6
 
// 
7
 
// Copyright (c) 2011 Novell, Inc (http://www.novell.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
 
 
27
 
using System;
28
 
using NUnit.Framework;
29
 
using System.Linq;
30
 
using MonoDevelop.CSharp.Refactoring;
31
 
using MonoDevelop.Projects;
32
 
using ICSharpCode.NRefactory.TypeSystem;
33
 
using MonoDevelop.Ide.TypeSystem;
34
 
 
35
 
namespace MonoDevelop.CSharpBinding.Refactoring
36
 
{
37
 
        [TestFixture()]
38
 
        public class ImplementInterfaceTests : UnitTests.TestBase
39
 
        {
40
 
                static IUnresolvedAssembly Mscorlib  = new CecilLoader().LoadAssemblyFile(typeof(object).Assembly.Location);
41
 
                static IUnresolvedAssembly SystemCore = new CecilLoader().LoadAssemblyFile(typeof(System.Linq.Enumerable).Assembly.Location);
42
 
                
43
 
                void TestCreateInterface (string interfacecode, string outputString, string stubString = null)
44
 
                {
45
 
                        var project = new UnknownProject ();
46
 
                        project.FileName = "test.csproj";
47
 
                        
48
 
                        TypeSystemService.LoadProject (project);
49
 
                        
50
 
                        TypeSystemService.ParseFile (project, "program.cs", "text/x-csharp", interfacecode);
51
 
                        TypeSystemService.ParseFile (project, "stub.cs", "text/x-csharp", "class Stub {\n "+stubString+"}\n");
52
 
                        
53
 
                        var wrapper = TypeSystemService.GetProjectContentWrapper (project);
54
 
                        wrapper.UpdateContent (c => c.AddAssemblyReferences (new [] { Mscorlib, SystemCore }));
55
 
                        
56
 
                        var pctx = TypeSystemService.GetCompilation (project);
57
 
                        
58
 
                        var stubType = pctx.MainAssembly.GetTypeDefinition ("", "Stub", 0);
59
 
                        var iface = pctx.MainAssembly.GetTypeDefinition ("", "ITest", 0);
60
 
                        
61
 
                        var gen = new CSharpCodeGenerator ();
62
 
                        gen.EolMarker = "\n";
63
 
                        gen.Compilation = pctx;
64
 
                        string generated = gen.CreateInterfaceImplementation (stubType, stubType.Parts.First (), iface, false);
65
 
                        Assert.IsNotEmpty (generated);
66
 
                        // crop #region
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);
73
 
                }
74
 
                
75
 
                /// <summary>
76
 
                /// Bug 663842 - Interface implementation does not include constraints
77
 
                /// </summary>
78
 
                [Test()]
79
 
                public void TestBug663842 ()
80
 
                {
81
 
                        TestCreateInterface (@"using System;
82
 
interface ITest {
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 ()
88
 
        {
89
 
                throw new System.NotImplementedException ();
90
 
        }
91
 
 
92
 
        public void MyMethod2<T> (T t) where T : class
93
 
        {
94
 
                throw new System.NotImplementedException ();
95
 
        }
96
 
 
97
 
        public void MyMethod3<T> (T t) where T : struct
98
 
        {
99
 
                throw new System.NotImplementedException ();
100
 
        }
101
 
 
102
 
        public void MyMethod4<T> (T t) where T : System.IDisposable, System.IServiceProvider
103
 
        {
104
 
                throw new System.NotImplementedException ();
105
 
        }");
106
 
                }
107
 
                
108
 
                /// <summary>
109
 
                /// Bug 683007 - "Refactor/Implement implicit" creates explicit implementations of methods with same names
110
 
                /// </summary>
111
 
                [Test()]
112
 
                public void TestBug683007 ()
113
 
                {
114
 
                        TestCreateInterface (@"interface ITest {
115
 
        void M1();
116
 
        void M1(int x);
117
 
}", @"public void M1 ()
118
 
        {
119
 
                throw new System.NotImplementedException ();
120
 
        }
121
 
 
122
 
        public void M1 (int x)
123
 
        {
124
 
                throw new System.NotImplementedException ();
125
 
        }");
126
 
                }
127
 
                
128
 
                /// <summary>
129
 
                /// Bug 243 - Implement implicit interface doesn't handle overloads correctly. 
130
 
                /// </summary>
131
 
                [Test()]
132
 
                public void TestBug243 ()
133
 
                {
134
 
                        TestCreateInterface (@"interface ITest {
135
 
        void Inc (int n);
136
 
        void Inc (string message);
137
 
}", @"public void Inc (int n)
138
 
        {
139
 
                throw new System.NotImplementedException ();
140
 
        }
141
 
 
142
 
        public void Inc (string message)
143
 
        {
144
 
                throw new System.NotImplementedException ();
145
 
        }");
146
 
                }
147
 
                
148
 
                
149
 
                /// <summary>
150
 
                /// Bug 2074 - [Regression] Implement Interface implicitly does not check the methods already exist 
151
 
                /// </summary>
152
 
                [Test()]
153
 
                public void TestBug2074 ()
154
 
                {
155
 
                        TestCreateInterface (@"interface ITest {
156
 
        void Method1 ();
157
 
        void Method2 ();
158
 
}", @"public void Method1 ()
159
 
        {
160
 
                throw new System.NotImplementedException ();
161
 
        }", "public void Method2 () {}");
162
 
                }
163
 
                
164
 
                /// <summary>
165
 
                /// Bug 3365 - MD cannot implement IEnumerable interface correctly  - MD cannot implement IEnumerable interface correctly 
166
 
                /// </summary>
167
 
                [Test()]
168
 
                public void TestBug3365 ()
169
 
                {
170
 
                        TestCreateInterface (@"using System;
171
 
using System.Collections;
172
 
 
173
 
public interface IA
174
 
{
175
 
        bool GetEnumerator ();
176
 
}
177
 
 
178
 
public interface ITest : IA, IEnumerable
179
 
{
180
 
}
181
 
", @"public bool GetEnumerator ()
182
 
        {
183
 
                throw new System.NotImplementedException ();
184
 
        }
185
 
        #endregion
186
 
 
187
 
        #region IEnumerable implementation
188
 
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
189
 
        {
190
 
                throw new System.NotImplementedException ();
191
 
        }");
192
 
                }
193
 
        }
194
 
}
195