-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathTextDocumentMatcherTests.cs
200 lines (172 loc) · 8.6 KB
/
TextDocumentMatcherTests.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
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NSubstitute;
using OmniSharp.Extensions.LanguageServer;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using OmniSharp.Extensions.LanguageServer.Server;
using OmniSharp.Extensions.LanguageServer.Server.Abstractions;
using OmniSharp.Extensions.LanguageServer.Server.Matchers;
using Xunit;
using Xunit.Abstractions;
namespace Lsp.Tests.Matchers
{
public class TextDocumentMatcherTests : AutoTestBase
{
public TextDocumentMatcherTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}
[Fact]
public void Should_Not_Return_Null()
{
// Given
var handlerDescriptors = Enumerable.Empty<ILspHandlerDescriptor>();
var handlerMatcher = AutoSubstitute.Resolve<TextDocumentMatcher>();
// When
var result = handlerMatcher.FindHandler(1, handlerDescriptors);
// Then
result.Should().NotBeNull();
}
[Fact]
public void Should_Return_Empty_Descriptor()
{
// Given
var handlerDescriptors = Enumerable.Empty<ILspHandlerDescriptor>();
var handlerMatcher = AutoSubstitute.Resolve<TextDocumentMatcher>();
// When
var result = handlerMatcher.FindHandler(1, handlerDescriptors);
// Then
result.Should().BeEmpty();
}
[Fact]
public void Should_Return_Did_Open_Text_Document_Handler_Descriptor()
{
// Given
var textDocumentSyncHandler =
TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler };
AutoSubstitute.Provide<IHandlerCollection>(collection);
var handlerMatcher = AutoSubstitute.Resolve<TextDocumentMatcher>();
// When
var result = handlerMatcher.FindHandler(new DidOpenTextDocumentParams() {
TextDocument = new TextDocumentItem {
Uri = new Uri("file:///abc/123/d.cs")
}
},
collection.Where(x => x.Method == DocumentNames.DidOpen));
// Then
result.Should().NotBeNullOrEmpty();
result.Should().Contain(x => x.Method == DocumentNames.DidOpen);
}
[Fact]
public void Should_Return_Did_Open_Text_Document_Handler_Descriptor_With_Sepcial_Character()
{
// Given
var textDocumentSyncHandler =
TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cshtml"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler };
AutoSubstitute.Provide<IHandlerCollection>(collection);
AutoSubstitute.Provide<IEnumerable<ILspHandlerDescriptor>>(collection);
var handlerMatcher = AutoSubstitute.Resolve<TextDocumentMatcher>();
// When
var result = handlerMatcher.FindHandler(new DidOpenTextDocumentParams() {
TextDocument = new TextDocumentItem {
Uri = new Uri("file://c:/users/myøasdf/d.cshtml")
}
},
collection.Where(x => x.Method == DocumentNames.DidOpen));
// Then
result.Should().NotBeNullOrEmpty();
result.Should().Contain(x => x.Method == DocumentNames.DidOpen);
}
[Fact]
public void Should_Return_Did_Change_Text_Document_Descriptor()
{
// Given
var textDocumentSyncHandler =
TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler };
AutoSubstitute.Provide<IHandlerCollection>(collection);
var handlerMatcher = AutoSubstitute.Resolve<TextDocumentMatcher>();
// When
var result = handlerMatcher.FindHandler(new DidChangeTextDocumentParams() {
TextDocument = new VersionedTextDocumentIdentifier { Uri = new Uri("file:///abc/123/d.cs"), Version = 1 }
},
collection.Where(x => x.Method == DocumentNames.DidChange));
// Then
result.Should().NotBeNullOrEmpty();
result.Should().Contain(x => x.Method == DocumentNames.DidChange);
}
[Fact]
public void Should_Return_Did_Save_Text_Document_Descriptor()
{
// Given
var textDocumentSyncHandler =
TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler };
AutoSubstitute.Provide<IHandlerCollection>(collection);
var handlerMatcher = AutoSubstitute.Resolve<TextDocumentMatcher>();
// When
var result = handlerMatcher.FindHandler(new DidChangeTextDocumentParams() {
TextDocument = new VersionedTextDocumentIdentifier { Uri = new Uri("file:///abc/123/d.cs"), Version = 1 }
},
collection.Where(x => x.Method == DocumentNames.DidSave));
// Then
result.Should().NotBeNullOrEmpty();
result.Should().Contain(x => x.Method == DocumentNames.DidSave);
}
[Fact]
public void Should_Return_Did_Close_Text_Document_Descriptor()
{
// Given
var textDocumentSyncHandler =
TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler };
AutoSubstitute.Provide<IHandlerCollection>(collection);
var handlerMatcher = AutoSubstitute.Resolve<TextDocumentMatcher>();
// When
var result = handlerMatcher.FindHandler(new DidCloseTextDocumentParams() {
TextDocument = new VersionedTextDocumentIdentifier { Uri = new Uri("file:///abc/123/d.cs"), Version = 1 }
},
collection.Where(x => x.Method == DocumentNames.DidClose));
// Then
result.Should().NotBeNullOrEmpty();
result.Should().Contain(x => x.Method == DocumentNames.DidClose);
}
[Fact]
public void Should_Return_Code_Lens_Descriptor()
{
// Given
var textDocumentSyncHandler =
TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"));
var collection = new HandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue) { textDocumentSyncHandler };
AutoSubstitute.Provide<IHandlerCollection>(collection);
var handlerMatcher = AutoSubstitute.Resolve<TextDocumentMatcher>();
var codeLensHandler = Substitute.For(new Type[] { typeof(ICodeLensHandler), typeof(ICodeLensResolveHandler) }, new object[0]) as ICodeLensHandler;
codeLensHandler.GetRegistrationOptions()
.Returns(new CodeLensRegistrationOptions() {
DocumentSelector = new DocumentSelector(new DocumentFilter { Pattern = "**/*.cs" })
});
var codeLensHandler2 = Substitute.For(new Type[] { typeof(ICodeLensHandler), typeof(ICodeLensResolveHandler) }, new object[0]) as ICodeLensHandler;
codeLensHandler2.GetRegistrationOptions()
.Returns(new CodeLensRegistrationOptions() {
DocumentSelector = new DocumentSelector(new DocumentFilter { Pattern = "**/*.cake" })
});
collection.Add(codeLensHandler, codeLensHandler2);
// When
var result = handlerMatcher.FindHandler(new CodeLensParams() {
TextDocument = new VersionedTextDocumentIdentifier { Uri = new Uri("file:///abc/123/d.cs"), Version = 1 }
},
collection.Where(x => x.Method == DocumentNames.CodeLens));
// Then
result.Should().NotBeNullOrEmpty();
result.Should().Contain(x => x.Method == DocumentNames.CodeLens);
result.Should().Contain(x => ((HandlerDescriptor)x).Key == "[**/*.cs]");
}
}
}