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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/FindReferencesTest.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
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.IO;
 
22
using System.Linq;
 
23
using System.Threading;
 
24
using ICSharpCode.NRefactory.CSharp.TypeSystem;
 
25
using ICSharpCode.NRefactory.TypeSystem;
 
26
using NUnit.Framework;
 
27
 
 
28
namespace ICSharpCode.NRefactory.CSharp.Resolver
 
29
{
 
30
        [TestFixture]
 
31
        public class FindReferencesTest
 
32
        {
 
33
                SyntaxTree syntaxTree;
 
34
                CSharpUnresolvedFile unresolvedFile;
 
35
                ICompilation compilation;
 
36
                FindReferences findReferences;
 
37
                
 
38
                void Init(string code)
 
39
                {
 
40
                        syntaxTree = SyntaxTree.Parse(code, "test.cs");
 
41
                        unresolvedFile = syntaxTree.ToTypeSystem();
 
42
                        compilation = TypeSystemHelper.CreateCompilation(unresolvedFile);
 
43
                        findReferences = new FindReferences();
 
44
                }
 
45
                
 
46
                AstNode[] FindReferences(IEntity entity)
 
47
                {
 
48
                        var result = new List<AstNode>();
 
49
                        var searchScopes = findReferences.GetSearchScopes(entity);
 
50
                        findReferences.FindReferencesInFile(searchScopes, unresolvedFile, syntaxTree, compilation,
 
51
                                                            (node, rr) => result.Add(node), CancellationToken.None);
 
52
                        return result.OrderBy(n => n.StartLocation).ToArray();
 
53
                }
 
54
 
 
55
                AstNode[] FindReferences(INamespace ns)
 
56
                {
 
57
                        var result = new List<AstNode>();
 
58
                        var searchScopes = findReferences.GetSearchScopes(ns);
 
59
                        findReferences.FindReferencesInFile(searchScopes, unresolvedFile, syntaxTree, compilation,
 
60
                                                            (node, rr) => result.Add(node), CancellationToken.None);
 
61
                        return result.OrderBy(n => n.StartLocation).ToArray();
 
62
                }
 
63
 
 
64
                #region Method Group
 
65
                [Test]
 
66
                public void FindMethodGroupReference()
 
67
                {
 
68
                        Init(@"using System;
 
69
class Test {
 
70
  Action<int> x = M;
 
71
  Action<string> y = M;
 
72
  static void M(int a) {}
 
73
  static void M(string a) {}
 
74
}");
 
75
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single();
 
76
                        var m_int = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "Int32");
 
77
                        var m_string = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "String");
 
78
                        Assert.AreEqual(new int[] { 3, 5 }, FindReferences(m_int).Select(n => n.StartLocation.Line).ToArray());
 
79
                        Assert.AreEqual(new int[] { 4, 6 }, FindReferences(m_string).Select(n => n.StartLocation.Line).ToArray());
 
80
                }
 
81
                
 
82
                [Test]
 
83
                public void FindMethodGroupReferenceInOtherMethodCall()
 
84
                {
 
85
                        Init(@"using System;
 
86
class Test {
 
87
 static void T(Action<int> a, Action<string> b) {
 
88
  this.T(M, M);
 
89
 }
 
90
 static void M(int a) {}
 
91
 static void M(string a) {}
 
92
}");
 
93
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single();
 
94
                        var m_int = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "Int32");
 
95
                        var m_string = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "String");
 
96
                        Assert.AreEqual(new [] { new TextLocation(4, 10), new TextLocation(6, 2) },
 
97
                                        FindReferences(m_int).Select(n => n.StartLocation).ToArray());
 
98
                        Assert.AreEqual(new [] { new TextLocation(4, 13), new TextLocation(7, 2) },
 
99
                                        FindReferences(m_string).Select(n => n.StartLocation).ToArray());
 
100
                }
 
101
                
 
102
                [Test]
 
103
                public void FindMethodGroupReferenceInExplicitDelegateCreation()
 
104
                {
 
105
                        Init(@"using System;
 
106
class Test {
 
107
 static void T(Action<int> a, Action<string> b) {
 
108
  this.T(new Action<int>(M), new Action<string>(M));
 
109
 }
 
110
 static void M(int a) {}
 
111
 static void M(string a) {}
 
112
}");
 
113
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single();
 
114
                        var m_int = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "Int32");
 
115
                        var m_string = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "String");
 
116
                        Assert.AreEqual(new [] { new TextLocation(4, 26), new TextLocation(6, 2) },
 
117
                                        FindReferences(m_int).Select(n => n.StartLocation).ToArray());
 
118
                        Assert.AreEqual(new [] { new TextLocation(4, 49), new TextLocation(7, 2) },
 
119
                                        FindReferences(m_string).Select(n => n.StartLocation).ToArray());
 
120
                }
 
121
                #endregion
 
122
                
 
123
                #region GetEnumerator
 
124
                [Test]
 
125
                public void FindReferenceToGetEnumeratorUsedImplicitlyInForeach()
 
126
                {
 
127
                        Init(@"using System;
 
128
class MyEnumerable {
 
129
 public System.Collections.IEnumerator GetEnumerator();
 
130
}
 
131
class Test {
 
132
 static void T() {
 
133
  var x = new MyEnumerable();
 
134
  foreach (var y in x) {
 
135
  }
 
136
 }
 
137
}");
 
138
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyEnumerable");
 
139
                        var getEnumerator = test.Methods.Single(m => m.Name == "GetEnumerator");
 
140
                        var actual = FindReferences(getEnumerator).ToList();
 
141
                        Assert.AreEqual(2, actual.Count);
 
142
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is MethodDeclaration));
 
143
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is ForeachStatement));
 
144
                }
 
145
                #endregion
 
146
                
 
147
                #region Op_Implicit
 
148
                [Test]
 
149
                public void FindReferencesForOpImplicitInLocalVariableInitialization()
 
150
                {
 
151
                        Init(@"using System;
 
152
class Test {
 
153
 static void T() {
 
154
  int x = new Test();
 
155
 }
 
156
 public static implicit operator int(Test x) { return 0; }
 
157
}");
 
158
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "Test");
 
159
                        var opImplicit = test.Methods.Single(m => m.Name == "op_Implicit");
 
160
                        var actual = FindReferences(opImplicit).ToList();
 
161
                        Assert.AreEqual(2, actual.Count);
 
162
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is ObjectCreateExpression));
 
163
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 6 && r is OperatorDeclaration));
 
164
                }
 
165
                #endregion
 
166
                
 
167
                #region Inheritance
 
168
                const string inheritanceTest = @"using System;
 
169
class A { public virtual void M() {} }
 
170
class B : A { public override void M() {} }
 
171
class C : A { public override void M() {} }
 
172
class Calls {
 
173
        void Test(A a, B b, C c) {
 
174
                a.M();
 
175
                b.M();
 
176
                c.M();
 
177
        }
 
178
}";
 
179
                
 
180
                [Test]
 
181
                public void InheritanceTest1()
 
182
                {
 
183
                        Init(inheritanceTest);
 
184
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "B");
 
185
                        var BM = test.Methods.Single(m => m.Name == "M");
 
186
                        var actual = FindReferences(BM).ToList();
 
187
                        Assert.AreEqual(2, actual.Count);
 
188
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is MethodDeclaration));
 
189
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is InvocationExpression));
 
190
                }
 
191
                
 
192
                [Test]
 
193
                public void InheritanceTest2()
 
194
                {
 
195
                        Init(inheritanceTest);
 
196
                        findReferences.FindCallsThroughVirtualBaseMethod = true;
 
197
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "B");
 
198
                        var BM = test.Methods.Single(m => m.Name == "M");
 
199
                        var actual = FindReferences(BM).ToList();
 
200
                        Assert.AreEqual(3, actual.Count);
 
201
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is MethodDeclaration));
 
202
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 7 && r is InvocationExpression));
 
203
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is InvocationExpression));
 
204
                }
 
205
                
 
206
                [Test]
 
207
                public void InheritanceTest3()
 
208
                {
 
209
                        Init(inheritanceTest);
 
210
                        findReferences.WholeVirtualSlot = true;
 
211
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "B");
 
212
                        var BM = test.Methods.Single(m => m.Name == "M");
 
213
                        var actual = FindReferences(BM).ToList();
 
214
                        Assert.AreEqual(6, actual.Count);
 
215
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 2 && r is MethodDeclaration));
 
216
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is MethodDeclaration));
 
217
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is MethodDeclaration));
 
218
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 7 && r is InvocationExpression));
 
219
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is InvocationExpression));
 
220
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 9 && r is InvocationExpression));
 
221
                }
 
222
                #endregion
 
223
 
 
224
                #region Await
 
225
 
 
226
                #if NET_4_5
 
227
 
 
228
                const string awaitTest = @"using System;
 
229
class MyAwaiter : System.Runtime.CompilerServices.INotifyCompletion {
 
230
        public bool IsCompleted { get { return false; } }
 
231
        public void OnCompleted(Action continuation) {}
 
232
        public int GetResult() { return 0; }
 
233
}
 
234
class MyAwaitable {
 
235
        public MyAwaiter GetAwaiter() { return null; }
 
236
}
 
237
public class C {
 
238
        public async void M() {
 
239
                MyAwaitable x = null;
 
240
                int i = await x;
 
241
        }
 
242
}";
 
243
 
 
244
                [Test]
 
245
                public void GetAwaiterReferenceInAwaitExpressionIsFound() {
 
246
                        Init(awaitTest);
 
247
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaitable");
 
248
                        var method = test.Methods.Single(m => m.Name == "GetAwaiter");
 
249
                        var actual = FindReferences(method).ToList();
 
250
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is MethodDeclaration));
 
251
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 13 && r is UnaryOperatorExpression));
 
252
                }
 
253
 
 
254
                [Test]
 
255
                public void GetResultReferenceInAwaitExpressionIsFound() {
 
256
                        Init(awaitTest);
 
257
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaiter");
 
258
                        var method = test.Methods.Single(m => m.Name == "GetResult");
 
259
                        var actual = FindReferences(method).ToList();
 
260
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 5 && r is MethodDeclaration));
 
261
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 13 && r is UnaryOperatorExpression));
 
262
                }
 
263
 
 
264
                [Test]
 
265
                public void OnCompletedReferenceInAwaitExpressionIsFound() {
 
266
                        Init(awaitTest);
 
267
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaiter");
 
268
                        var method = test.Methods.Single(m => m.Name == "OnCompleted");
 
269
                        var actual = FindReferences(method).ToList();
 
270
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is MethodDeclaration));
 
271
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 13 && r is UnaryOperatorExpression));
 
272
                }
 
273
 
 
274
                [Test]
 
275
                public void IsCompletedReferenceInAwaitExpressionIsFound() {
 
276
                        Init(awaitTest);
 
277
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaiter");
 
278
                        var property = test.Properties.Single(m => m.Name == "IsCompleted");
 
279
                        var actual = FindReferences(property).ToList();
 
280
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is PropertyDeclaration));
 
281
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 13 && r is UnaryOperatorExpression));
 
282
                }
 
283
 
 
284
                [Test]
 
285
                public void UnsafeOnCompletedReferenceInAwaitExpressionIsFound() {
 
286
                        Init(@"using System;
 
287
class MyAwaiter : System.Runtime.CompilerServices.ICriticalNotifyCompletion {
 
288
        public bool IsCompleted { get { return false; } }
 
289
        public void OnCompleted(Action continuation) {}
 
290
        public void UnsafeOnCompleted(Action continuation) {}
 
291
        public int GetResult() { return 0; }
 
292
}
 
293
class MyAwaitable {
 
294
        public MyAwaiter GetAwaiter() { return null; }
 
295
}
 
296
public class C {
 
297
        public async void M() {
 
298
                MyAwaitable x = null;
 
299
                int i = await x;
 
300
        }
 
301
}");
 
302
                        var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaiter");
 
303
                        var method = test.Methods.Single(m => m.Name == "UnsafeOnCompleted");
 
304
                        var actual = FindReferences(method).ToList();
 
305
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 5 && r is MethodDeclaration));
 
306
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 14 && r is UnaryOperatorExpression));
 
307
                }
 
308
 
 
309
                #endif // NET_4_5
 
310
 
 
311
                #endregion
 
312
        
 
313
                #region Namespaces
 
314
                [Test]
 
315
                public void FindNamespaceTest()
 
316
                {
 
317
                        Init(@"using System;
 
318
using Foo.Bar;
 
319
 
 
320
namespace Foo.Bar {
 
321
        class MyTest { }
 
322
}
 
323
 
 
324
namespace Other.Bar {
 
325
        class OtherTest {}
 
326
}
 
327
 
 
328
namespace Foo 
 
329
{
 
330
        class Test 
 
331
        {
 
332
                static void T()
 
333
                {
 
334
                        Bar.MyTest test;
 
335
                        Other.Bar.OtherTest test2;
 
336
                }
 
337
        }
 
338
}
 
339
 
 
340
namespace B
 
341
{
 
342
        using f = Foo.Bar;
 
343
        class Test2
 
344
        {
 
345
                Foo.Bar.MyTest a;
 
346
        }
 
347
}
 
348
");
 
349
                        var test = compilation.MainAssembly.RootNamespace.GetChildNamespace("Foo").GetChildNamespace ("Bar");
 
350
                        var actual = FindReferences(test).ToList();
 
351
                        Assert.AreEqual(5, actual.Count);
 
352
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 2 && r is MemberType));
 
353
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is NamespaceDeclaration));
 
354
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 18 && r is SimpleType));
 
355
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 26 && r is MemberType));
 
356
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 29 && r is MemberType));
 
357
                }
 
358
 
 
359
                [Test]
 
360
                public void FindSub()
 
361
                {
 
362
                        Init(@"using System;
 
363
using Foo.Bar;
 
364
 
 
365
namespace Foo.Bar {
 
366
        class MyTest { }
 
367
}
 
368
 
 
369
namespace Foo 
 
370
{
 
371
        class Test 
 
372
        {
 
373
                Foo.Bar.MyTest t;
 
374
        }
 
375
}
 
376
");
 
377
                        var test = compilation.MainAssembly.RootNamespace.GetChildNamespace("Foo");
 
378
                        var actual = FindReferences(test).ToList();
 
379
                        Assert.AreEqual(4, actual.Count);
 
380
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 2 && r is SimpleType));
 
381
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is NamespaceDeclaration));
 
382
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is NamespaceDeclaration));
 
383
                        Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 12 && r is SimpleType));
 
384
                }
 
385
                #endregion
 
386
        }
 
387
}