예제 #1
0
        public async Task ShouldRouteTo_CorrectRequestWhenGivenNullParams()
        {
            var handler = Substitute.For <IShutdownHandler>();

            handler
            .Handle(Arg.Any <ShutdownParams>(), Arg.Any <CancellationToken>())
            .Returns(Unit.Value);

            var collection =
                new SharedHandlerCollection(
                    SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                    new LspHandlerTypeDescriptorProvider(
                        new[] {
                typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly,
                typeof(LspRequestRouter).Assembly
            }
                        )
                    )
            {
                handler
            };

            AutoSubstitute.Provide <IHandlerCollection>(collection);
            AutoSubstitute.Provide <IEnumerable <ILspHandlerDescriptor> >(collection);
            var mediator = AutoSubstitute.Resolve <LspRequestRouter>();

            var id      = Guid.NewGuid().ToString();
            var request = new Request(id, GeneralNames.Shutdown, new JObject());

            await mediator.RouteRequest(mediator.GetDescriptors(request), request, CancellationToken.None);

            await handler.Received(1).Handle(Arg.Any <ShutdownParams>(), Arg.Any <CancellationToken>());
        }
        public void ShouldHandleRequest()
        {
            var inputStream           = new MemoryStream(Encoding.ASCII.GetBytes("Content-Length: 2\r\n\r\n{}"));
            var outputHandler         = Substitute.For <IOutputHandler>();
            var reciever              = Substitute.For <IReceiver>();
            var incomingRequestRouter = Substitute.For <IRequestRouter <IHandlerDescriptor> >();

            var req = new Request(1, "abc", null);

            reciever.IsValid(Arg.Any <JToken>()).Returns(true);
            reciever.GetRequests(Arg.Any <JToken>())
            .Returns(c => (new Renor[] { req }, false));

            var response = new Response(1, req);

            incomingRequestRouter.RouteRequest(Arg.Any <IHandlerDescriptor>(), req, CancellationToken.None)
            .Returns(response);

            using (NewHandler(
                       inputStream,
                       outputHandler,
                       reciever,
                       Substitute.For <IRequestProcessIdentifier>(),
                       incomingRequestRouter,
                       Substitute.For <IResponseRouter>(),
                       cts => {
                outputHandler.When(x => x.Send(Arg.Any <object>()))
                .Do(x => {
                    cts.Cancel();
                });
            }))
            {
                outputHandler.Received().Send(Arg.Is <object>(x => x == response));
            }
        }
        public async Task ShouldRouteToCorrect_Request_WithManyHandlers_CodeLensHandler()
        {
            var textDocumentSyncHandler =
                TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"), "csharp");
            var textDocumentSyncHandler2 =
                TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cake"), "csharp");

            textDocumentSyncHandler.Handle(Arg.Any <DidSaveTextDocumentParams>(), Arg.Any <CancellationToken>())
            .Returns(Unit.Value);
            textDocumentSyncHandler2.Handle(Arg.Any <DidSaveTextDocumentParams>(), Arg.Any <CancellationToken>())
            .Returns(Unit.Value);

            var codeActionHandler = Substitute.For <ICodeLensHandler>();

            codeActionHandler.GetRegistrationOptions().Returns(new CodeLensRegistrationOptions {
                DocumentSelector = DocumentSelector.ForPattern("**/*.cs")
            });
            codeActionHandler
            .Handle(Arg.Any <CodeLensParams>(), Arg.Any <CancellationToken>())
            .Returns(new CodeLensContainer());

            var codeActionHandler2 = Substitute.For <ICodeLensHandler>();

            codeActionHandler2.GetRegistrationOptions().Returns(new CodeLensRegistrationOptions {
                DocumentSelector = DocumentSelector.ForPattern("**/*.cake")
            });
            codeActionHandler2
            .Handle(Arg.Any <CodeLensParams>(), Arg.Any <CancellationToken>())
            .Returns(new CodeLensContainer());

            var tdi        = new TextDocumentIdentifiers();
            var collection =
                new SharedHandlerCollection(SupportedCapabilitiesFixture.AlwaysTrue, tdi, new ServiceCollection().BuildServiceProvider())
            {
                textDocumentSyncHandler, textDocumentSyncHandler2, codeActionHandler, codeActionHandler2
            };

            AutoSubstitute.Provide <IHandlerCollection>(collection);
            AutoSubstitute.Provide <IEnumerable <ILspHandlerDescriptor> >(collection);
            AutoSubstitute.Provide <IHandlerMatcher>(new TextDocumentMatcher(LoggerFactory.CreateLogger <TextDocumentMatcher>(), tdi));
            var mediator = AutoSubstitute.Resolve <LspRequestRouter>();

            var id      = Guid.NewGuid().ToString();
            var @params = new CodeLensParams {
                TextDocument = new TextDocumentIdentifier(new Uri("file:///c:/test/123.cs"))
            };

            var request = new Request(
                id, TextDocumentNames.CodeLens,
                JObject.Parse(JsonConvert.SerializeObject(@params, new Serializer(ClientVersion.Lsp3).Settings))
                );

            await mediator.RouteRequest(mediator.GetDescriptors(request), request, CancellationToken.None);

            await codeActionHandler2.Received(0).Handle(Arg.Any <CodeLensParams>(), Arg.Any <CancellationToken>());

            await codeActionHandler.Received(1).Handle(Arg.Any <CodeLensParams>(), Arg.Any <CancellationToken>());
        }
        public async Task ShouldRouteToCorrect_Request()
        {
            var textDocumentSyncHandler =
                TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.cs"), "csharp");

            textDocumentSyncHandler.Handle(Arg.Any <DidSaveTextDocumentParams>(), Arg.Any <CancellationToken>())
            .Returns(Unit.Value);

            var codeActionHandler = Substitute.For <ICodeActionHandler>();

            codeActionHandler.GetRegistrationOptions().Returns(new CodeActionRegistrationOptions {
                DocumentSelector = DocumentSelector.ForPattern("**/*.cs")
            });
            codeActionHandler
            .Handle(Arg.Any <CodeActionParams>(), Arg.Any <CancellationToken>())
            .Returns(new CommandOrCodeActionContainer());

            var collection =
                new SharedHandlerCollection(
                    SupportedCapabilitiesFixture.AlwaysTrue, new TextDocumentIdentifiers(), Substitute.For <IResolverContext>(),
                    new LspHandlerTypeDescriptorProvider(
                        new[] {
                typeof(FoundationTests).Assembly, typeof(LanguageServer).Assembly, typeof(LanguageClient).Assembly, typeof(IRegistrationManager).Assembly,
                typeof(LspRequestRouter).Assembly
            }
                        )
                    )
            {
                textDocumentSyncHandler, codeActionHandler
            };

            AutoSubstitute.Provide <IHandlerCollection>(collection);
            AutoSubstitute.Provide <IEnumerable <ILspHandlerDescriptor> >(collection);
            var mediator = AutoSubstitute.Resolve <LspRequestRouter>();

            var id      = Guid.NewGuid().ToString();
            var @params = new DidSaveTextDocumentParams {
                TextDocument = new TextDocumentIdentifier(new Uri("file:///c:/test/123.cs"))
            };

            var request = new Request(
                id, TextDocumentNames.CodeAction,
                JObject.Parse(JsonConvert.SerializeObject(@params, new Serializer(ClientVersion.Lsp3).Settings))
                );

            await mediator.RouteRequest(mediator.GetDescriptors(request), request, CancellationToken.None);

            await codeActionHandler.Received(1).Handle(Arg.Any <CodeActionParams>(), Arg.Any <CancellationToken>());
        }
        public async Task ShouldCancelRequest()
        {
            var inputStream           = new MemoryStream(Encoding.ASCII.GetBytes("Content-Length: 2\r\n\r\n{}"));
            var outputHandler         = Substitute.For <IOutputHandler>();
            var reciever              = Substitute.For <IReceiver>();
            var incomingRequestRouter = Substitute.For <IRequestRouter <IHandlerDescriptor> >();
            var requestDescription    = Substitute.For <IHandlerDescriptor>();

            requestDescription.Method.Returns("abc");
            var cancelDescription = Substitute.For <IHandlerDescriptor>();

            cancelDescription.Method.Returns(JsonRpcNames.CancelRequest);

            var req    = new Request(1, "abc", null);
            var cancel = new Notification(JsonRpcNames.CancelRequest, JObject.Parse("{\"id\":1}"));

            reciever.IsValid(Arg.Any <JToken>()).Returns(true);
            reciever.GetRequests(Arg.Any <JToken>())
            .Returns(c => (new Renor[] { req, cancel }, false));

            incomingRequestRouter.When(z => z.CancelRequest(Arg.Any <object>()));
            incomingRequestRouter.GetDescriptor(cancel).Returns(cancelDescription);
            incomingRequestRouter.GetDescriptor(req).Returns(requestDescription);

            incomingRequestRouter.RouteRequest(requestDescription, req, CancellationToken.None)
            .Returns(new Response(1, req));

            incomingRequestRouter.RouteNotification(cancelDescription, cancel, CancellationToken.None)
            .Returns(Task.CompletedTask);

            using (NewHandler(
                       inputStream,
                       outputHandler,
                       reciever,
                       Substitute.For <IRequestProcessIdentifier>(),
                       incomingRequestRouter,
                       Substitute.For <IResponseRouter>(),
                       cts => {
                outputHandler.When(x => x.Send(Arg.Any <object>()))
                .Do(x => {
                    cts.Cancel();
                });
            }))
            {
                incomingRequestRouter.Received().CancelRequest(1L);
                // incomingRequestRouter.Received().RouteNotification(cancelDescription, cancel, CancellationToken.None);
            }
        }