-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSymbolExplorerTests.cs
113 lines (89 loc) · 4.52 KB
/
SymbolExplorerTests.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
using System.Threading.Tasks;
using CSharpRepl.Services;
using CSharpRepl.Services.Roslyn;
using CSharpRepl.Services.SymbolExploration;
using Xunit;
namespace CSharpRepl.Tests;
[Collection(nameof(RoslynServices))]
public class SymbolExplorerTests : IAsyncLifetime
{
private readonly RoslynServices services;
public SymbolExplorerTests()
{
var (console, _) = FakeConsole.CreateStubbedOutput();
this.services = new RoslynServices(console, new Configuration(), new TestTraceLogger());
}
public Task InitializeAsync() => services.WarmUpAsync([]);
public Task DisposeAsync() => Task.CompletedTask;
[Fact]
public async Task GetSymbolAtIndex_ReturnsFullyQualifiedName()
{
var symbol = await services.GetSymbolAtIndexAsync(@"Console.WriteLine(""howdy"")", "Console.Wri".Length);
Assert.Equal("System.Console.WriteLine", symbol.SymbolDisplay);
}
[Fact]
public async Task GetSymbolAtIndex_ClassInSourceLinkedAssembly_ReturnsSourceLinkUrl()
{
// should return a string like https://www.github.com/dotnet/runtime/blob/208e377a5329ad6eb1db5e5fb9d4590fa50beadd/src/libraries/System.Console/src/System/Console.cs
var symbol = await services.GetSymbolAtIndexAsync(@"Console.WriteLine(""howdy"")", "Conso".Length);
Assert.StartsWith("https://www.github.com/dotnet/runtime/", symbol.Url);
Assert.EndsWith("Console.cs", symbol.Url);
}
[Fact]
public async Task GetSymbolAtIndex_GenericTypeInSourceLinkedAssembly_ReturnsSourceLinkUrl()
{
// should return a string like https://www.github.com/dotnet/runtime/blob/1381d5ebd2ab1f292848d5b19b80cf71ac332508/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs
var symbol = await services.GetSymbolAtIndexAsync(@"List<string>", "Li".Length);
Assert.StartsWith("https://www.github.com/dotnet/runtime/", symbol.Url);
Assert.EndsWith("List.cs", symbol.Url);
}
[Fact]
public async Task GetSymbolAtIndex_MethodInSourceLinkedAssembly_ReturnsSourceLinkUrl()
{
// should return a string like https://www.github.com/dotnet/runtime/blob/208e377a5329ad6eb1db5e5fb9d4590fa50beadd/src/libraries/System.Console/src/System/Console.cs#L635-L636
var symbol = await services.GetSymbolAtIndexAsync(@"Console.WriteLine(""howdy"")", "Console.Wri".Length);
AssertLinkWithLineNumber(symbol);
}
[Fact]
public async Task GetSymbolAtIndex_PropertyInSourceLinkedAssembly_ReturnsSourceLinkUrl()
{
// should return a string like https://www.github.com/dotnet/runtime/blob/208e377a5329ad6eb1db5e5fb9d4590fa50beadd/src/libraries/System.Console/src/System/Console.cs
var symbol = await services.GetSymbolAtIndexAsync(@"Console.Out", "Console.Ou".Length);
AssertLinkWithLineNumber(symbol);
}
[Fact]
public async Task GetSymbolAtIndex_EventInSourceLinkedAssembly_ReturnsSourceLinkUrl()
{
// should return a string like https://www.github.com/dotnet/runtime/blob/208e377a5329ad6eb1db5e5fb9d4590fa50beadd/src/libraries/System.Console/src/System/Console.cs
var symbol = await services.GetSymbolAtIndexAsync(@"Console.CancelKeyPress", "Console.CancelKe".Length);
AssertLinkWithLineNumber(symbol);
}
[Fact]
public async Task GetSymbolAtIndex_InvalidSymbol_NoException()
{
var symbol = await services.GetSymbolAtIndexAsync(@"wow!", 2);
Assert.Equal(SymbolResult.Unknown, symbol);
}
[Fact]
public async Task GetSymbolAtIndex_NonSourceLinkedAssembly_NoException()
{
_ = await services.EvaluateAsync(@"#r ""./Data/DemoLibrary.dll""");
_ = await services.EvaluateAsync("using DemoLibrary;");
var symbol = await services.GetSymbolAtIndexAsync("DemoClass.Multiply", "DemoClass.Multi".Length);
Assert.Equal("DemoLibrary.DemoClass.Multiply", symbol.SymbolDisplay);
Assert.Null(symbol.Url);
}
private static void AssertLinkWithLineNumber(SymbolResult symbol)
{
var urlParts = symbol.Url.Split('#');
Assert.Equal(2, urlParts.Length);
var url = urlParts[0];
Assert.StartsWith("https://www.github.com/dotnet/runtime/", url);
var lineHash = urlParts[1];
const string LinePattern = "L[0-9]+";
Assert.Matches($"^{LinePattern}-{LinePattern}$", lineHash);
}
}