-
Notifications
You must be signed in to change notification settings - Fork 420
/
FindSymbolsFacts.cs
460 lines (395 loc) · 15.9 KB
/
FindSymbolsFacts.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
using System.Linq;
using System.Threading.Tasks;
using OmniSharp.Models;
using OmniSharp.Models.FindSymbols;
using OmniSharp.Roslyn.CSharp.Services.Navigation;
using Microsoft.CodeAnalysis;
using TestUtility;
using Xunit;
using Xunit.Abstractions;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Diagnostics;
using System;
using OmniSharp.Models.v1.SourceGeneratedFile;
namespace OmniSharp.Roslyn.CSharp.Tests
{
public class FindSymbolsFacts : AbstractSingleRequestHandlerTestFixture<FindSymbolsService>
{
public FindSymbolsFacts(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
}
protected override string EndpointName => OmniSharpEndpoints.FindSymbols;
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Can_find_symbols(string filename)
{
string code = @"
public class Foo
{
private string _field = 0;
private string AutoProperty { get; }
private string Property
{
get { return _field; }
set { _field = value; }
}
private string Method() {}
private string Method(string param) {}
private class Nested
{
private string NestedMethod() {}
}
}";
code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsAsync(code, filename);
var symbols = usages.QuickFixes.Select(q => q.Text);
var expected = new[]
{
"Foo",
"_field",
"AutoProperty",
"Property",
"Method()",
"Method(string param)",
"Nested",
"NestedMethod()"
};
Assert.Equal(expected, symbols);
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Does_not_return_event_keyword(string filename)
{
const string code = @"
public static class Game
{
public static event GameEvent GameResumed;
}";
var usages = await FindSymbolsAsync(code, filename);
var symbols = usages.QuickFixes.Select(q => q.Text);
var expected = new[]
{
"Game",
"GameResumed"
};
Assert.Equal(expected, symbols);
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Can_find_symbols_kinds(string filename)
{
string code = @"
public class Foo
{
private string _field = 0;
private string AutoProperty { get; }
private string Property
{
get { return _field; }
set { _field = value; }
}
private string Method() {}
private string Method(string param) {}
private class Nested
{
private string NestedMethod() {}
}
}";
code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsAsync(code, filename);
var symbols = usages.QuickFixes.Cast<SymbolLocation>().Select(q => q.Kind);
var expected = new[]
{
"Class",
"Field",
"Property",
"Property",
"Method",
"Method",
"Class",
"Method"
};
Assert.Equal(expected, symbols);
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Returns_interface_kind(string filename)
{
const string code = @"public interface Foo {}";
var usages = await FindSymbolsAsync(code, filename);
var symbols = usages.QuickFixes.Cast<SymbolLocation>().Select(q => q.Kind);
Assert.Equal("Interface", symbols.First());
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Returns_enum_kind(string filename)
{
const string code = @"public enum Foo {}";
var usages = await FindSymbolsAsync(code, filename);
var symbols = usages.QuickFixes.Cast<SymbolLocation>().Select(q => q.Kind);
Assert.Equal("Enum", symbols.First());
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Returns_struct_kind(string filename)
{
const string code = @"public struct Foo {}";
var usages = await FindSymbolsAsync(code, filename);
var symbols = usages.QuickFixes.Cast<SymbolLocation>().Select(q => q.Kind);
Assert.Contains(symbols.First(), new[] { "Struct", "Structure" });
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Returns_delegate_kind(string filename)
{
const string code = @"public delegate void Foo();";
var usages = await FindSymbolsAsync(code, filename);
var symbols = usages.QuickFixes.Cast<SymbolLocation>().Select(q => q.Kind);
Assert.Equal("Delegate", symbols.First());
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Finds_partial_method_with_body(string filename)
{
const string code = @"
public partial class MyClass
{
partial void Method();
}
public partial class MyClass
{
partial void Method()
{
// do stuff
}
}";
var usages = await FindSymbolsAsync(code, filename);
var methodSymbol = usages.QuickFixes.Cast<SymbolLocation>().First(x => x.Kind == SymbolKind.Method.ToString());
// should find the occurrance with body
Assert.Equal(8, methodSymbol.Line);
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Can_find_symbols_using_filter(string filename)
{
string code = @"
public class Foo
{
private string _field = 0;
private string AutoProperty { get; }
private string Property
{
get { return _field; }
set { _field = value; }
}
private void Method() {}
private string Method(string param) {}
private class Nested
{
private string NestedMethod() {}
}
}";
code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "meth", minFilterLength: null, maxItemsToReturn: null, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);
var expected = new[]
{
"Method()",
"Method(string param)",
"NestedMethod()"
};
Assert.Equal(expected, symbols);
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Can_find_symbols_using_filter_with_subset_match(string filename)
{
string code = @"
public class Options {}
public class Opossum {}
public interface IConfigurationOptions { }
public class ConfigurationOptions : IConfigurationOptions { }";
code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "opti", minFilterLength: 0, maxItemsToReturn: 0, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);
var expected = new[]
{
"Options",
"IConfigurationOptions",
"ConfigurationOptions"
};
Assert.Equal(expected, symbols);
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Only_returns_symbols_matching_symbolfilter(string filename)
{
string code = @"
public class Options {
public bool Property { get; }
}
public interface IConfigurationOptions {
public bool InterfaceProperty { get; set; }
}
public enum WeekDays { Monday }";
code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, string.Empty, minFilterLength: 0, maxItemsToReturn: 0, OmniSharpSymbolFilter.Type);
var symbols = usages.QuickFixes.Select(q => q.Text);
var expected = new[]
{
"Options",
"IConfigurationOptions",
"WeekDays"
};
Assert.Equal(expected, symbols);
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task no_symbols_returned_when_filter_too_short(string filename)
{
string code = @"
public class Options {}";
code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "op", minFilterLength: 3, maxItemsToReturn: 0, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);
Assert.Empty(symbols);
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task limit_number_of_returned_symbols(string filename)
{
string code = @"
public class Options1 {}
public class Options2 {}
public class Options3 {}";
code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "op", minFilterLength: 0, maxItemsToReturn: 2, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);
Assert.Equal(2, symbols.Count());
}
[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task fuzzy_search(string filename)
{
string code = @"
public class ProjectManager {}
public class CoolProjectManager {}
public class ProbabilityManager {}";
code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "ProjMana", minFilterLength: 0, maxItemsToReturn: 0, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);
Assert.Contains("ProjectManager", symbols);
Assert.Contains("CoolProjectManager", symbols);
Assert.DoesNotContain("ProbabilityManager", symbols);
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/63375")]
public async Task SymbolsReturnedForGeneratedTypes()
{
const string Source = @"
public class Generated
{
public int Property { get; set; }
}
";
TestHelpers.AddProjectToWorkspace(SharedOmniSharpTestHost.Workspace,
"project.csproj",
new[] { "net6.0" },
Array.Empty<TestFile>(),
analyzerRefs: ImmutableArray.Create<AnalyzerReference>(new TestGeneratorReference(
context => context.AddSource("GeneratedFile", Source))));
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
var response = await requestHandler.Handle(new FindSymbolsRequest
{
Filter = "Property"
});
var result = (SymbolLocation)response.QuickFixes.Single();
Assert.NotNull(result);
}
[Fact]
public async Task SymbolsReturnedForGeneratedTypes_Partial()
{
const string Source = @"
public partial class Generated
{
public int Property { get; set; }
}
";
const string FileName = "real.cs";
var testFile = new TestFile(FileName, @"
partial Generated
{
}
");
TestHelpers.AddProjectToWorkspace(SharedOmniSharpTestHost.Workspace,
"project.csproj",
new[] { "net6.0" },
new[] { testFile },
analyzerRefs: ImmutableArray.Create<AnalyzerReference>(new TestGeneratorReference(
context => context.AddSource("GeneratedFile", Source))));
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
var response = await requestHandler.Handle(new FindSymbolsRequest
{
Filter = "Generated"
});
var result = response.QuickFixes.Cast<SymbolLocation>().Single(s => s.GeneratedFileInfo is not null);
Assert.NotNull(result);
var sourceGeneratedFileHandler = SharedOmniSharpTestHost.GetRequestHandler<SourceGeneratedFileService>(OmniSharpEndpoints.SourceGeneratedFile);
var sourceGeneratedRequest = new SourceGeneratedFileRequest
{
DocumentGuid = result.GeneratedFileInfo.DocumentGuid,
ProjectGuid = result.GeneratedFileInfo.ProjectGuid
};
var sourceGeneratedFileResponse = await sourceGeneratedFileHandler.Handle(sourceGeneratedRequest);
Assert.NotNull(sourceGeneratedFileResponse);
Assert.Equal(Source, sourceGeneratedFileResponse.Source);
Assert.Equal(@"OmniSharp.Roslyn.CSharp.Tests\OmniSharp.Roslyn.CSharp.Tests.TestSourceGenerator\GeneratedFile.cs", sourceGeneratedFileResponse.SourceName.Replace("/", @"\"));
}
private async Task<QuickFixResponse> FindSymbolsAsync(string code, string filename)
{
var testFile = new TestFile(filename, code);
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
return await requestHandler.Handle(null);
}
private async Task<QuickFixResponse> FindSymbolsWithFilterAsync(string code, string filename, string filter, int? minFilterLength, int? maxItemsToReturn, OmniSharpSymbolFilter? symbolFilter)
{
var testFile = new TestFile(filename, code);
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
return await requestHandler.Handle(new FindSymbolsRequest
{
Filter = filter,
MinFilterLength = minFilterLength,
MaxItemsToReturn = maxItemsToReturn,
SymbolFilter = symbolFilter
});
}
private string WrapInNamespaceIfNeeded(string code, string filename)
{
if (filename.EndsWith(".cs"))
{
code = @"
namespace Some.Long.Namespace
{
" + code + @"
}";
}
return code;
}
}
}