/
GoToDefinitionFacts.cs
601 lines (516 loc) · 20.5 KB
/
GoToDefinitionFacts.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using OmniSharp.Models.GotoDefinition;
using OmniSharp.Roslyn.CSharp.Services.Navigation;
using OmniSharp.Models.Metadata;
using TestUtility;
using Xunit;
using Xunit.Abstractions;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
namespace OmniSharp.Roslyn.CSharp.Tests
{
public class GoToDefinitionFacts : AbstractSingleRequestHandlerTestFixture<GotoDefinitionService>
{
public GoToDefinitionFacts(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
}
protected override string EndpointName => OmniSharpEndpoints.GotoDefinition;
[Theory]
[InlineData("foo.cs")]
[InlineData("foo.csx")]
public async Task ReturnsDefinitionInSameFile(string filename)
{
var testFile = new TestFile(filename, @"
class {|def:Foo|} {
private F$$oo foo;
}");
await TestGoToSourceAsync(testFile);
}
[Theory]
[InlineData("foo.cs")]
[InlineData("foo.csx")]
public async Task DoesNotReturnOnPropertAccessorGet(string filename)
{
var testFile = new TestFile(filename, @"
class Test {
public string Foo{ g$$et; set; }
}");
await TestGoToSourceAsync(testFile);
}
[Theory]
[InlineData("foo.cs")]
[InlineData("foo.csx")]
public async Task DoesNotReturnOnPropertAccessorSet(string filename)
{
var testFile = new TestFile(filename, @"
class Test {
public int Foo{ get; s$$et; }
}");
await TestGoToSourceAsync(testFile);
}
[Theory]
[InlineData("foo.cs")]
[InlineData("foo.csx")]
public async Task DoesNotReturnOnPropertyAccessorPropertyDef(string filename)
{
var testFile = new TestFile(filename, @"
class Test {
public int |def:Foo| Fo$$o{ get; set; }
}");
await TestGoToSourceAsync(testFile);
}
[Theory]
[InlineData("foo.cs")]
[InlineData("foo.csx")]
public async Task ReturnsOnPropertyAccessorPropertySetting(string filename)
{
var testFile = new TestFile(filename, @"
class Test {
public int |def:Foo|{ get; set; }
public static void main()
{
F$$oo = 3;
}
}");
await TestGoToSourceAsync(testFile);
}
[Theory]
[InlineData("foo.cs")]
[InlineData("foo.csx")]
public async Task ReturnsOnPropertyAccessorField1(string filename)
{
var testFile = new TestFile(filename, @"
class Test {
public int |def:foo|;
public int Foo
{
get => f$$oo;
set => foo = value;
}
}");
await TestGoToSourceAsync(testFile);
}
[Theory]
[InlineData("foo.cs")]
[InlineData("foo.csx")]
public async Task ReturnsOnPropertyAccessorField2(string filename)
{
var testFile = new TestFile(filename, @"
class Test {
public int |def:foo|;
public int Foo
{
get => foo;
set => f$$oo = value;
}
}");
await TestGoToSourceAsync(testFile);
}
[Theory]
[InlineData("foo.cs")]
[InlineData("foo.csx")]
public async Task ReturnsOnPropertyAccessorPropertyGetting(string filename)
{
var testFile = new TestFile(filename, @"
class Test {
public int |def:Foo|{ get; set; }
public static void main()
{
Foo = 3;
Console.WriteLine(F$$oo);
}
}");
await TestGoToSourceAsync(testFile);
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsPartialMethodDefinitionWithBody(string filename)
{
var testFile = new TestFile(filename, @"
public partial class MyClass
{
public MyClass()
{
Met$$hod();
}
partial void {|def:Method|}()
{
//do stuff
}
}
public partial class MyClass
{
partial void {|def:Method|}();
}");
await TestGoToSourceAsync(testFile);
}
[Fact]
public async Task ReturnsDefinitionInDifferentFile()
{
var testFile1 = new TestFile("foo.cs", @"
using System;
class {|def:Foo|} {
}");
var testFile2 = new TestFile("bar.cs", @"
class Bar {
private F$$oo foo;
}");
await TestGoToSourceAsync(testFile1, testFile2);
}
[Fact]
public async Task ReturnsEmptyResultWhenDefinitionIsNotFound()
{
var testFile1 = new TestFile("foo.cs", @"
using System;
class Foo {
}");
var testFile2 = new TestFile("bar.cs", @"
class Bar {
private B$$az foo;
}");
await TestGoToSourceAsync(testFile1, testFile2);
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDefinitionInMetadata_WhenSymbolIsStaticMethod(string filename)
{
var testFile = new TestFile(filename, @"
using System;
class Bar {
public void Baz() {
Guid.NewG$$uid();
}
}");
await TestGoToMetadataAsync(testFile,
expectedAssemblyName: AssemblyHelpers.CorLibName,
expectedTypeName: "System.Guid");
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDecompiledDefinition_WhenSymbolIsStaticMethod(string filename)
{
var testFile = new TestFile(filename, @"
using System;
class Bar {
public void Baz() {
Guid.NewG$$uid();
}
}");
await TestDecompilationAsync(testFile,
expectedAssemblyName: AssemblyHelpers.CorLibName,
expectedTypeName: "System.Guid");
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDefinitionInMetadata_WhenSymbolIsInstanceMethod(string filename)
{
var testFile = new TestFile(filename, @"
using System.Collections.Generic;
class Bar {
public void Baz() {
var foo = new List<string>();
foo.ToAr$$ray();
}
}");
await TestGoToMetadataAsync(testFile,
expectedAssemblyName: AssemblyHelpers.CorLibName,
expectedTypeName: "System.Collections.Generic.List`1");
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDecompiledDefinition_WhenSymbolIsInstanceMethod(string filename)
{
var testFile = new TestFile(filename, @"
using System.Collections.Generic;
class Bar {
public void Baz() {
var foo = new List<string>();
foo.ToAr$$ray();
}
}");
await TestDecompilationAsync(testFile,
expectedAssemblyName: AssemblyHelpers.CorLibName,
expectedTypeName: "System.Collections.Generic.List`1");
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDefinitionInMetadata_WhenSymbolIsGenericType(string filename)
{
var testFile = new TestFile(filename, @"
using System.Collections.Generic;
class Bar {
public void Baz() {
var foo = new Li$$st<string>();
foo.ToArray();
}
}");
await TestGoToMetadataAsync(testFile,
expectedAssemblyName: AssemblyHelpers.CorLibName,
expectedTypeName: "System.Collections.Generic.List`1");
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDecompiledDefinition_WhenSymbolIsGenericType(string filename)
{
var testFile = new TestFile(filename, @"
using System.Collections.Generic;
class Bar {
public void Baz() {
var foo = new Li$$st<string>();
foo.ToArray();
}
}");
await TestDecompilationAsync(testFile,
expectedAssemblyName: AssemblyHelpers.CorLibName,
expectedTypeName: "System.Collections.Generic.List`1");
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDefinitionInMetadata_WhenSymbolIsType(string filename)
{
var testFile = new TestFile(filename, @"
using System;
class Bar {
public void Baz() {
var str = Stri$$ng.Empty;
}
}");
await TestGoToMetadataAsync(testFile,
expectedAssemblyName: AssemblyHelpers.CorLibName,
expectedTypeName: "System.String");
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDecompiledDefinition_WhenSymbolIsType(string filename)
{
var testFile = new TestFile(filename, @"
using System;
class Bar {
public void Baz() {
var str = Stri$$ng.Empty;
}
}");
await TestDecompilationAsync(testFile,
expectedAssemblyName: AssemblyHelpers.CorLibName,
expectedTypeName: "System.String");
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDefinitionInMetadata_FromMetadata_WhenSymbolIsType(string filename)
{
var testFile = new TestFile(filename, @"
using System;
class Bar {
public void Baz() {
var number = in$$t.MaxValue;
}
}");
using (var host = CreateOmniSharpHost(testFile))
{
var point = testFile.Content.GetPointFromPosition();
// 1. start by asking for definition of "int"
var gotoDefinitionRequest = new GotoDefinitionRequest
{
FileName = testFile.FileName,
Line = point.Line,
Column = point.Offset,
WantMetadata = true,
Timeout = 60000
};
var gotoDefinitionRequestHandler = GetRequestHandler(host);
var gotoDefinitionResponse = await gotoDefinitionRequestHandler.Handle(gotoDefinitionRequest);
// 2. now, based on the response information
// go to the metadata endpoint, and ask for "int" specific metadata
var metadataRequest = new MetadataRequest
{
AssemblyName = gotoDefinitionResponse.MetadataSource.AssemblyName,
TypeName = gotoDefinitionResponse.MetadataSource.TypeName,
ProjectName = gotoDefinitionResponse.MetadataSource.ProjectName,
Language = gotoDefinitionResponse.MetadataSource.Language
};
var metadataRequestHandler = host.GetRequestHandler<MetadataService>(OmniSharpEndpoints.Metadata);
var metadataResponse = await metadataRequestHandler.Handle(metadataRequest);
// 3. the metadata response contains SourceName (metadata "file") and SourceText (syntax tree)
// use the source to locate "IComparable" which is an interface implemented by Int32 struct
var metadataTree = CSharpSyntaxTree.ParseText(metadataResponse.Source);
var iComparable = metadataTree.GetCompilationUnitRoot().
DescendantNodesAndSelf().
OfType<BaseTypeDeclarationSyntax>().First().
BaseList.Types.FirstOrDefault(x => x.Type.ToString() == "IComparable");
var relevantLineSpan = iComparable.GetLocation().GetLineSpan();
// 4. now ask for the definition of "IComparable"
// pass in the SourceName (metadata "file") as FileName - since it's not a regular file in our workspace
var metadataNavigationRequest = new GotoDefinitionRequest
{
FileName = metadataResponse.SourceName,
Line = relevantLineSpan.StartLinePosition.Line,
Column = relevantLineSpan.StartLinePosition.Character,
WantMetadata = true
};
var metadataNavigationResponse = await gotoDefinitionRequestHandler.Handle(metadataNavigationRequest);
// 5. validate the response to be matching the expected IComparable meta info
Assert.NotNull(metadataNavigationResponse.MetadataSource);
Assert.Equal(AssemblyHelpers.CorLibName, metadataNavigationResponse.MetadataSource.AssemblyName);
Assert.Equal("System.IComparable", metadataNavigationResponse.MetadataSource.TypeName);
Assert.NotEqual(0, metadataNavigationResponse.Line);
Assert.NotEqual(0, metadataNavigationResponse.Column);
}
}
[Theory]
[InlineData("bar.cs")]
[InlineData("bar.csx")]
public async Task ReturnsDecompiledDefinition_FromMetadata_WhenSymbolIsType(string filename)
{
var testFile = new TestFile(filename, @"
using System;
class Bar {
public void Baz() {
var number = in$$t.MaxValue;
}
}");
using var host = CreateOmniSharpHost(new[] { testFile }, new Dictionary<string, string>
{
["RoslynExtensionsOptions:EnableDecompilationSupport"] = "true"
});
var point = testFile.Content.GetPointFromPosition();
// 1. start by asking for definition of "int"
var gotoDefinitionRequest = new GotoDefinitionRequest
{
FileName = testFile.FileName,
Line = point.Line,
Column = point.Offset,
WantMetadata = true,
Timeout = 60000
};
var gotoDefinitionRequestHandler = GetRequestHandler(host);
var gotoDefinitionResponse = await gotoDefinitionRequestHandler.Handle(gotoDefinitionRequest);
// 2. now, based on the response information
// go to the metadata endpoint, and ask for "int" specific decompiled source
var metadataRequest = new MetadataRequest
{
AssemblyName = gotoDefinitionResponse.MetadataSource.AssemblyName,
TypeName = gotoDefinitionResponse.MetadataSource.TypeName,
ProjectName = gotoDefinitionResponse.MetadataSource.ProjectName,
Language = gotoDefinitionResponse.MetadataSource.Language,
Timeout = 60000
};
var metadataRequestHandler = host.GetRequestHandler<MetadataService>(OmniSharpEndpoints.Metadata);
var metadataResponse = await metadataRequestHandler.Handle(metadataRequest);
// 3. the response contains SourceName ("file") and SourceText (syntax tree)
// use the source to locate "IComparable" which is an interface implemented by Int32 struct
var decompiledTree = CSharpSyntaxTree.ParseText(metadataResponse.Source);
var compilationUnit = decompiledTree.GetCompilationUnitRoot();
// second comment should indicate we have decompiled
var comments = compilationUnit.DescendantTrivia().Where(t => t.Kind() == SyntaxKind.SingleLineCommentTrivia).ToArray();
Assert.NotNull(comments);
Assert.Equal("// Decompiled with ICSharpCode.Decompiler 7.0.0.6488", comments[1].ToString());
// contrary to regular metadata, we should have methods with full bodies
// this condition would fail if decompilation wouldn't work
var methods = compilationUnit.
DescendantNodesAndSelf().
OfType<MethodDeclarationSyntax>().
Where(m => m.Body != null);
Assert.NotEmpty(methods);
var iComparable = compilationUnit.
DescendantNodesAndSelf().
OfType<BaseTypeDeclarationSyntax>().First().
BaseList.Types.FirstOrDefault(x => x.Type.ToString() == "IComparable");
var relevantLineSpan = iComparable.GetLocation().GetLineSpan();
// 4. now ask for the definition of "IComparable"
// pass in the SourceName (metadata "file") as FileName - since it's not a regular file in our workspace
var metadataNavigationRequest = new GotoDefinitionRequest
{
FileName = metadataResponse.SourceName,
Line = relevantLineSpan.StartLinePosition.Line,
Column = relevantLineSpan.StartLinePosition.Character,
WantMetadata = true
};
var metadataNavigationResponse = await gotoDefinitionRequestHandler.Handle(metadataNavigationRequest);
// 5. validate the response to be matching the expected IComparable meta info
Assert.NotNull(metadataNavigationResponse.MetadataSource);
Assert.Equal(AssemblyHelpers.CorLibName, metadataNavigationResponse.MetadataSource.AssemblyName);
Assert.Equal("System.IComparable", metadataNavigationResponse.MetadataSource.TypeName);
Assert.NotEqual(0, metadataNavigationResponse.Line);
Assert.NotEqual(0, metadataNavigationResponse.Column);
}
[Fact]
public async Task ReturnsNoResultsButDoesNotThrowForNamespaces()
{
var testFile = new TestFile("foo.cs", "namespace F$$oo {}");
var response = await GetResponseAsync(new[] { testFile }, wantMetadata: false);
Assert.Null(response.FileName);
}
private async Task TestGoToSourceAsync(params TestFile[] testFiles)
{
var response = await GetResponseAsync(testFiles, wantMetadata: false);
var target = testFiles.FirstOrDefault(tf => tf.Content.GetSpans("def").Count > 0);
if (target != null)
{
var definitionSpan = target.Content.GetSpans("def").First();
var definitionRange = target.Content.GetRangeFromSpan(definitionSpan);
Assert.Equal(target.FileName, response.FileName);
Assert.Equal(definitionRange.Start.Line, response.Line);
Assert.Equal(definitionRange.Start.Offset, response.Column);
}
else
{
Assert.Null(response.FileName);
Assert.Equal(0, response.Line);
Assert.Equal(0, response.Column);
}
}
private async Task TestDecompilationAsync(TestFile testFile, string expectedAssemblyName, string expectedTypeName)
{
using var host = CreateOmniSharpHost(new[] { testFile }, new Dictionary<string, string>
{
["RoslynExtensionsOptions:EnableDecompilationSupport"] = "true"
});
var response = await GetResponseAsync(new[] { testFile }, wantMetadata: true);
Assert.NotNull(response.MetadataSource);
Assert.False(response.IsEmpty);
Assert.Equal(expectedAssemblyName, response.MetadataSource.AssemblyName);
Assert.Equal(expectedTypeName, response.MetadataSource.TypeName);
}
private async Task TestGoToMetadataAsync(TestFile testFile, string expectedAssemblyName, string expectedTypeName)
{
var response = await GetResponseAsync(new[] { testFile }, wantMetadata: true);
Assert.NotNull(response.MetadataSource);
Assert.False(response.IsEmpty);
Assert.Equal(expectedAssemblyName, response.MetadataSource.AssemblyName);
Assert.Equal(expectedTypeName, response.MetadataSource.TypeName);
// We probably shouldn't hard code metadata locations (they could change randomly)
Assert.NotEqual(0, response.Line);
Assert.NotEqual(0, response.Column);
}
private async Task<GotoDefinitionResponse> GetResponseAsync(TestFile[] testFiles, bool wantMetadata)
{
SharedOmniSharpTestHost.AddFilesToWorkspace(testFiles);
var source = testFiles.Single(tf => tf.Content.HasPosition);
var point = source.Content.GetPointFromPosition();
var request = new GotoDefinitionRequest
{
FileName = source.FileName,
Line = point.Line,
Column = point.Offset,
Timeout = 60000,
WantMetadata = wantMetadata
};
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
return await requestHandler.Handle(request);
}
}
}