17
17
using OmniSharp . Extensions . LanguageServer . Server ;
18
18
using OmniSharp . Extensions . LanguageServer . Server . Abstractions ;
19
19
using OmniSharp . Extensions . LanguageServer . Server . Handlers ;
20
+ using OmniSharp . Extensions . LanguageServer . Server . Matchers ;
20
21
using Xunit ;
21
22
using Xunit . Abstractions ;
22
23
using Xunit . Sdk ;
@@ -27,17 +28,11 @@ namespace Lsp.Tests
27
28
public class LspRequestRouterTests
28
29
{
29
30
private readonly TestLoggerFactory _testLoggerFactory ;
30
- private readonly IHandlerMatcherCollection _handlerMatcherCollection = new HandlerMatcherCollection ( ) ;
31
+ // private readonly IHandlerMatcherCollection handlerMatcherCollection = new HandlerMatcherCollection();
31
32
32
33
public LspRequestRouterTests ( ITestOutputHelper testOutputHelper )
33
34
{
34
35
_testLoggerFactory = new TestLoggerFactory ( testOutputHelper ) ;
35
- var logger = Substitute . For < ILogger > ( ) ;
36
- var matcher = Substitute . For < IHandlerMatcher > ( ) ;
37
- matcher . FindHandler ( Arg . Any < object > ( ) , Arg . Any < IEnumerable < ILspHandlerDescriptor > > ( ) )
38
- . Returns ( new List < HandlerDescriptor > ( ) ) ;
39
-
40
- _handlerMatcherCollection . Add ( matcher ) ;
41
36
}
42
37
43
38
[ Fact ]
@@ -47,7 +42,10 @@ public async Task ShouldRouteToCorrect_Notification()
47
42
textDocumentSyncHandler . Handle ( Arg . Any < DidSaveTextDocumentParams > ( ) ) . Returns ( Task . CompletedTask ) ;
48
43
49
44
var collection = new HandlerCollection { textDocumentSyncHandler } ;
50
- var mediator = new LspRequestRouter ( collection , _testLoggerFactory , _handlerMatcherCollection , new Serializer ( ) ) ;
45
+ var handlerMatcherCollection = new HandlerMatcherCollection {
46
+ new TextDocumentMatcher ( _testLoggerFactory . CreateLogger < TextDocumentMatcher > ( ) , collection . TextDocumentSyncHandlers )
47
+ } ;
48
+ var mediator = new LspRequestRouter ( collection , _testLoggerFactory , handlerMatcherCollection , new Serializer ( ) ) ;
51
49
52
50
var @params = new DidSaveTextDocumentParams ( ) {
53
51
TextDocument = new TextDocumentIdentifier ( new Uri ( "file:///c:/test/123.cs" ) )
@@ -69,7 +67,10 @@ public async Task ShouldRouteToCorrect_Notification_WithManyHandlers()
69
67
textDocumentSyncHandler2 . Handle ( Arg . Any < DidSaveTextDocumentParams > ( ) ) . Returns ( Task . CompletedTask ) ;
70
68
71
69
var collection = new HandlerCollection { textDocumentSyncHandler , textDocumentSyncHandler2 } ;
72
- var mediator = new LspRequestRouter ( collection , _testLoggerFactory , _handlerMatcherCollection , new Serializer ( ) ) ;
70
+ var handlerMatcherCollection = new HandlerMatcherCollection {
71
+ new TextDocumentMatcher ( _testLoggerFactory . CreateLogger < TextDocumentMatcher > ( ) , collection . TextDocumentSyncHandlers )
72
+ } ;
73
+ var mediator = new LspRequestRouter ( collection , _testLoggerFactory , handlerMatcherCollection , new Serializer ( ) ) ;
73
74
74
75
var @params = new DidSaveTextDocumentParams ( ) {
75
76
TextDocument = new TextDocumentIdentifier ( new Uri ( "file:///c:/test/123.cake" ) )
@@ -79,8 +80,8 @@ public async Task ShouldRouteToCorrect_Notification_WithManyHandlers()
79
80
80
81
await mediator . RouteNotification ( mediator . GetDescriptor ( request ) , request ) ;
81
82
82
- await textDocumentSyncHandler . Received ( 1 ) . Handle ( Arg . Any < DidSaveTextDocumentParams > ( ) ) ;
83
- await textDocumentSyncHandler2 . Received ( 0 ) . Handle ( Arg . Any < DidSaveTextDocumentParams > ( ) ) ;
83
+ await textDocumentSyncHandler . Received ( 0 ) . Handle ( Arg . Any < DidSaveTextDocumentParams > ( ) ) ;
84
+ await textDocumentSyncHandler2 . Received ( 1 ) . Handle ( Arg . Any < DidSaveTextDocumentParams > ( ) ) ;
84
85
}
85
86
86
87
[ Fact ]
@@ -96,7 +97,10 @@ public async Task ShouldRouteToCorrect_Request()
96
97
. Returns ( new CommandContainer ( ) ) ;
97
98
98
99
var collection = new HandlerCollection { textDocumentSyncHandler , codeActionHandler } ;
99
- var mediator = new LspRequestRouter ( collection , _testLoggerFactory , _handlerMatcherCollection , new Serializer ( ) ) ;
100
+ var handlerMatcherCollection = new HandlerMatcherCollection {
101
+ new TextDocumentMatcher ( _testLoggerFactory . CreateLogger < TextDocumentMatcher > ( ) , collection . TextDocumentSyncHandlers )
102
+ } ;
103
+ var mediator = new LspRequestRouter ( collection , _testLoggerFactory , handlerMatcherCollection , new Serializer ( ) ) ;
100
104
101
105
var id = Guid . NewGuid ( ) . ToString ( ) ;
102
106
var @params = new DidSaveTextDocumentParams ( ) {
@@ -131,19 +135,61 @@ public async Task ShouldRouteToCorrect_Request_WithManyHandlers()
131
135
. Returns ( new CommandContainer ( ) ) ;
132
136
133
137
var collection = new HandlerCollection { textDocumentSyncHandler , textDocumentSyncHandler2 , codeActionHandler , codeActionHandler2 } ;
134
- var mediator = new LspRequestRouter ( collection , _testLoggerFactory , _handlerMatcherCollection , new Serializer ( ) ) ;
138
+ var handlerMatcherCollection = new HandlerMatcherCollection {
139
+ new TextDocumentMatcher ( _testLoggerFactory . CreateLogger < TextDocumentMatcher > ( ) , collection . TextDocumentSyncHandlers )
140
+ } ;
141
+ var mediator = new LspRequestRouter ( collection , _testLoggerFactory , handlerMatcherCollection , new Serializer ( ) ) ;
135
142
136
143
var id = Guid . NewGuid ( ) . ToString ( ) ;
137
- var @params = new DidSaveTextDocumentParams ( ) {
144
+ var @params = new CodeActionParams ( ) {
138
145
TextDocument = new TextDocumentIdentifier ( new Uri ( "file:///c:/test/123.cake" ) )
139
146
} ;
140
147
141
148
var request = new Request ( id , DocumentNames . CodeAction , JObject . Parse ( JsonConvert . SerializeObject ( @params , new Serializer ( ClientVersion . Lsp3 ) . Settings ) ) ) ;
142
149
143
150
await mediator . RouteRequest ( mediator . GetDescriptor ( request ) , request ) ;
144
151
145
- await codeActionHandler . Received ( 1 ) . Handle ( Arg . Any < CodeActionParams > ( ) , Arg . Any < CancellationToken > ( ) ) ;
146
- await codeActionHandler2 . Received ( 0 ) . Handle ( Arg . Any < CodeActionParams > ( ) , Arg . Any < CancellationToken > ( ) ) ;
152
+ await codeActionHandler . Received ( 0 ) . Handle ( Arg . Any < CodeActionParams > ( ) , Arg . Any < CancellationToken > ( ) ) ;
153
+ await codeActionHandler2 . Received ( 1 ) . Handle ( Arg . Any < CodeActionParams > ( ) , Arg . Any < CancellationToken > ( ) ) ;
154
+ }
155
+
156
+ [ Fact ]
157
+ public async Task ShouldRouteToCorrect_Request_WithManyHandlers_CodeLensHandler ( )
158
+ {
159
+ var textDocumentSyncHandler = TextDocumentSyncHandlerExtensions . With ( DocumentSelector . ForPattern ( "**/*.cs" ) ) ;
160
+ var textDocumentSyncHandler2 = TextDocumentSyncHandlerExtensions . With ( DocumentSelector . ForPattern ( "**/*.cake" ) ) ;
161
+ textDocumentSyncHandler . Handle ( Arg . Any < DidSaveTextDocumentParams > ( ) ) . Returns ( Task . CompletedTask ) ;
162
+ textDocumentSyncHandler2 . Handle ( Arg . Any < DidSaveTextDocumentParams > ( ) ) . Returns ( Task . CompletedTask ) ;
163
+
164
+ var codeActionHandler = Substitute . For < ICodeLensHandler > ( ) ;
165
+ codeActionHandler . GetRegistrationOptions ( ) . Returns ( new CodeLensRegistrationOptions ( ) { DocumentSelector = DocumentSelector . ForPattern ( "**/*.cs" ) } ) ;
166
+ codeActionHandler
167
+ . Handle ( Arg . Any < CodeLensParams > ( ) , Arg . Any < CancellationToken > ( ) )
168
+ . Returns ( new CodeLensContainer ( ) ) ;
169
+
170
+ var codeActionHandler2 = Substitute . For < ICodeLensHandler > ( ) ;
171
+ codeActionHandler2 . GetRegistrationOptions ( ) . Returns ( new CodeLensRegistrationOptions ( ) { DocumentSelector = DocumentSelector . ForPattern ( "**/*.cake" ) } ) ;
172
+ codeActionHandler2
173
+ . Handle ( Arg . Any < CodeLensParams > ( ) , Arg . Any < CancellationToken > ( ) )
174
+ . Returns ( new CodeLensContainer ( ) ) ;
175
+
176
+ var collection = new HandlerCollection { textDocumentSyncHandler , textDocumentSyncHandler2 , codeActionHandler , codeActionHandler2 } ;
177
+ var handlerMatcherCollection = new HandlerMatcherCollection {
178
+ new TextDocumentMatcher ( _testLoggerFactory . CreateLogger < TextDocumentMatcher > ( ) , collection . TextDocumentSyncHandlers )
179
+ } ;
180
+ var mediator = new LspRequestRouter ( collection , _testLoggerFactory , handlerMatcherCollection , new Serializer ( ) ) ;
181
+
182
+ var id = Guid . NewGuid ( ) . ToString ( ) ;
183
+ var @params = new CodeLensParams ( ) {
184
+ TextDocument = new TextDocumentIdentifier ( new Uri ( "file:///c:/test/123.cs" ) )
185
+ } ;
186
+
187
+ var request = new Request ( id , DocumentNames . CodeLens , JObject . Parse ( JsonConvert . SerializeObject ( @params , new Serializer ( ClientVersion . Lsp3 ) . Settings ) ) ) ;
188
+
189
+ await mediator . RouteRequest ( mediator . GetDescriptor ( request ) , request ) ;
190
+
191
+ await codeActionHandler2 . Received ( 0 ) . Handle ( Arg . Any < CodeLensParams > ( ) , Arg . Any < CancellationToken > ( ) ) ;
192
+ await codeActionHandler . Received ( 1 ) . Handle ( Arg . Any < CodeLensParams > ( ) , Arg . Any < CancellationToken > ( ) ) ;
147
193
}
148
194
149
195
[ Fact ]
@@ -155,7 +201,10 @@ public async Task ShouldRouteTo_CorrectRequestWhenGivenNullParams()
155
201
. Returns ( Task . CompletedTask ) ;
156
202
157
203
var collection = new HandlerCollection { handler } ;
158
- var mediator = new LspRequestRouter ( collection , _testLoggerFactory , _handlerMatcherCollection , new Serializer ( ) ) ;
204
+ var handlerMatcherCollection = new HandlerMatcherCollection {
205
+ new TextDocumentMatcher ( _testLoggerFactory . CreateLogger < TextDocumentMatcher > ( ) , collection . TextDocumentSyncHandlers )
206
+ } ;
207
+ var mediator = new LspRequestRouter ( collection , _testLoggerFactory , handlerMatcherCollection , new Serializer ( ) ) ;
159
208
160
209
var id = Guid . NewGuid ( ) . ToString ( ) ;
161
210
var request = new Request ( id , GeneralNames . Shutdown , new JObject ( ) ) ;
@@ -177,7 +226,10 @@ public async Task ShouldHandle_Request_WithNullParameters()
177
226
} ;
178
227
179
228
var collection = new HandlerCollection { shutdownHandler } ;
180
- var mediator = new LspRequestRouter ( collection , _testLoggerFactory , _handlerMatcherCollection , new Serializer ( ) ) ;
229
+ var handlerMatcherCollection = new HandlerMatcherCollection {
230
+ new TextDocumentMatcher ( _testLoggerFactory . CreateLogger < TextDocumentMatcher > ( ) , collection . TextDocumentSyncHandlers )
231
+ } ;
232
+ var mediator = new LspRequestRouter ( collection , _testLoggerFactory , handlerMatcherCollection , new Serializer ( ) ) ;
181
233
182
234
JToken @params = JValue . CreateNull ( ) ; // If the "params" property present but null, this will be JTokenType.Null.
183
235
@@ -201,7 +253,10 @@ public async Task ShouldHandle_Request_WithMissingParameters()
201
253
} ;
202
254
203
255
var collection = new HandlerCollection { shutdownHandler } ;
204
- var mediator = new LspRequestRouter ( collection , _testLoggerFactory , _handlerMatcherCollection , new Serializer ( ) ) ;
256
+ var handlerMatcherCollection = new HandlerMatcherCollection {
257
+ new TextDocumentMatcher ( _testLoggerFactory . CreateLogger < TextDocumentMatcher > ( ) , collection . TextDocumentSyncHandlers )
258
+ } ;
259
+ var mediator = new LspRequestRouter ( collection , _testLoggerFactory , handlerMatcherCollection , new Serializer ( ) ) ;
205
260
206
261
JToken @params = null ; // If the "params" property was missing entirely, this will be null.
207
262
0 commit comments