This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
FindReferencesTest.cs
273 lines (255 loc) · 10.6 KB
/
FindReferencesTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using ICSharpCode.NRefactory.CSharp.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
[TestFixture]
public class FindReferencesTest
{
SyntaxTree syntaxTree;
CSharpUnresolvedFile unresolvedFile;
ICompilation compilation;
FindReferences findReferences;
void Init(string code)
{
syntaxTree = SyntaxTree.Parse(code, "test.cs");
unresolvedFile = syntaxTree.ToTypeSystem();
compilation = TypeSystemHelper.CreateCompilation(unresolvedFile);
findReferences = new FindReferences();
}
AstNode[] FindReferences(IEntity entity)
{
var result = new List<AstNode>();
var searchScopes = findReferences.GetSearchScopes(entity);
findReferences.FindReferencesInFile(searchScopes, unresolvedFile, syntaxTree, compilation,
(node, rr) => result.Add(node), CancellationToken.None);
return result.OrderBy(n => n.StartLocation).ToArray();
}
#region Method Group
[Test]
public void FindMethodGroupReference()
{
Init(@"using System;
class Test {
Action<int> x = M;
Action<string> y = M;
static void M(int a) {}
static void M(string a) {}
}");
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single();
var m_int = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "Int32");
var m_string = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "String");
Assert.AreEqual(new int[] { 3, 5 }, FindReferences(m_int).Select(n => n.StartLocation.Line).ToArray());
Assert.AreEqual(new int[] { 4, 6 }, FindReferences(m_string).Select(n => n.StartLocation.Line).ToArray());
}
[Test]
public void FindMethodGroupReferenceInOtherMethodCall()
{
Init(@"using System;
class Test {
static void T(Action<int> a, Action<string> b) {
this.T(M, M);
}
static void M(int a) {}
static void M(string a) {}
}");
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single();
var m_int = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "Int32");
var m_string = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "String");
Assert.AreEqual(new [] { new TextLocation(4, 10), new TextLocation(6, 2) },
FindReferences(m_int).Select(n => n.StartLocation).ToArray());
Assert.AreEqual(new [] { new TextLocation(4, 13), new TextLocation(7, 2) },
FindReferences(m_string).Select(n => n.StartLocation).ToArray());
}
[Test]
public void FindMethodGroupReferenceInExplicitDelegateCreation()
{
Init(@"using System;
class Test {
static void T(Action<int> a, Action<string> b) {
this.T(new Action<int>(M), new Action<string>(M));
}
static void M(int a) {}
static void M(string a) {}
}");
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single();
var m_int = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "Int32");
var m_string = test.Methods.Single(m => m.Name == "M" && m.Parameters.Single().Type.Name == "String");
Assert.AreEqual(new [] { new TextLocation(4, 26), new TextLocation(6, 2) },
FindReferences(m_int).Select(n => n.StartLocation).ToArray());
Assert.AreEqual(new [] { new TextLocation(4, 49), new TextLocation(7, 2) },
FindReferences(m_string).Select(n => n.StartLocation).ToArray());
}
#endregion
#region GetEnumerator
[Test]
public void FindReferenceToGetEnumeratorUsedImplicitlyInForeach()
{
Init(@"using System;
class MyEnumerable {
public System.Collections.IEnumerator GetEnumerator();
}
class Test {
static void T() {
var x = new MyEnumerable();
foreach (var y in x) {
}
}
}");
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyEnumerable");
var getEnumerator = test.Methods.Single(m => m.Name == "GetEnumerator");
var actual = FindReferences(getEnumerator).ToList();
Assert.AreEqual(2, actual.Count);
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is ForeachStatement));
}
#endregion
#region Op_Implicit
[Test]
public void FindReferencesForOpImplicitInLocalVariableInitialization()
{
Init(@"using System;
class Test {
static void T() {
int x = new Test();
}
public static implicit operator int(Test x) { return 0; }
}");
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "Test");
var opImplicit = test.Methods.Single(m => m.Name == "op_Implicit");
var actual = FindReferences(opImplicit).ToList();
Assert.AreEqual(2, actual.Count);
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is ObjectCreateExpression));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 6 && r is OperatorDeclaration));
}
#endregion
#region Inheritance
const string inheritanceTest = @"using System;
class A { public virtual void M() {} }
class B : A { public override void M() {} }
class C : A { public override void M() {} }
class Calls {
void Test(A a, B b, C c) {
a.M();
b.M();
c.M();
}
}";
[Test]
public void InheritanceTest1()
{
Init(inheritanceTest);
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "B");
var BM = test.Methods.Single(m => m.Name == "M");
var actual = FindReferences(BM).ToList();
Assert.AreEqual(2, actual.Count);
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is InvocationExpression));
}
[Test]
public void InheritanceTest2()
{
Init(inheritanceTest);
findReferences.FindCallsThroughVirtualBaseMethod = true;
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "B");
var BM = test.Methods.Single(m => m.Name == "M");
var actual = FindReferences(BM).ToList();
Assert.AreEqual(3, actual.Count);
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 7 && r is InvocationExpression));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is InvocationExpression));
}
[Test]
public void InheritanceTest3()
{
Init(inheritanceTest);
findReferences.WholeVirtualSlot = true;
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "B");
var BM = test.Methods.Single(m => m.Name == "M");
var actual = FindReferences(BM).ToList();
Assert.AreEqual(6, actual.Count);
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 2 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 7 && r is InvocationExpression));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is InvocationExpression));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 9 && r is InvocationExpression));
}
#endregion
#region Await
const string awaitTest = @"using System;
class MyAwaiter {
public bool IsCompleted { get { return false; } }
public void OnCompleted(Action continuation) {}
public int GetResult() { return 0; }
}
class MyAwaitable {
public MyAwaiter GetAwaiter() { return null; }
}
public class C {
public async void M() {
MyAwaitable x = null;
int i = await x;
}
}";
[Test]
public void GetAwaiterReferenceInAwaitExpressionIsFound() {
Init(awaitTest);
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaitable");
var method = test.Methods.Single(m => m.Name == "GetAwaiter");
var actual = FindReferences(method).ToList();
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 8 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 13 && r is UnaryOperatorExpression));
}
[Test]
public void GetResultReferenceInAwaitExpressionIsFound() {
Init(awaitTest);
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaiter");
var method = test.Methods.Single(m => m.Name == "GetResult");
var actual = FindReferences(method).ToList();
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 5 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 13 && r is UnaryOperatorExpression));
}
[Test]
public void OnCompletedReferenceInAwaitExpressionIsFound() {
Init(awaitTest);
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaiter");
var method = test.Methods.Single(m => m.Name == "OnCompleted");
var actual = FindReferences(method).ToList();
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is MethodDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 13 && r is UnaryOperatorExpression));
}
[Test]
public void IsCompletedReferenceInAwaitExpressionIsFound() {
Init(awaitTest);
var test = compilation.MainAssembly.TopLevelTypeDefinitions.Single(t => t.Name == "MyAwaiter");
var property = test.Properties.Single(m => m.Name == "IsCompleted");
var actual = FindReferences(property).ToList();
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 3 && r is PropertyDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 13 && r is UnaryOperatorExpression));
}
#endregion
}
}