- Notifications
You must be signed in to change notification settings - Fork 110
Add request-level tests for LspConnection #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
d24f464 Add request-level tests for LspConnection.
tintoy c6b0393 Add client test for "textDocument/hover".
tintoy bae0291 Tests now use correct path style on Windows vs Unix.
tintoy 3d8b595 Add client test for "textDocument/publishDiagnostics".
tintoy ba583fd Add client test for "textDocument/completions".
tintoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using OmniSharp.Extensions.LanguageServer.Capabilities.Server; | ||
| using OmniSharp.Extensions.LanguageServer.Models; | ||
| using OmniSharp.Extensions.LanguageServerProtocol.Client.Dispatcher; | ||
| using OmniSharp.Extensions.LanguageServerProtocol.Client.Protocol; | ||
| using OmniSharp.Extensions.LanguageServerProtocol.Client.Utilities; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
| | ||
| namespace OmniSharp.Extensions.LanguageServerProtocol.Client.Tests | ||
| { | ||
| /// <summary> | ||
| /// Tests for <see cref="LanguageClient"/>. | ||
| /// </summary> | ||
| public class ClientTests | ||
| : PipeServerTestBase | ||
| { | ||
| /// <summary> | ||
| /// Create a new <see cref="LanguageClient"/> test suite. | ||
| /// </summary> | ||
| /// <param name="testOutput"> | ||
| /// Output for the current test. | ||
| /// </param> | ||
| public ClientTests(ITestOutputHelper testOutput) | ||
| : base(testOutput) | ||
| { | ||
| } | ||
| | ||
| /// <summary> | ||
| /// Get an absolute document path for use in tests. | ||
| /// </summary> | ||
| string AbsoluteDocumentPath => Path.DirectorySeparatorChar + "Foo.txt"; // Absolute path, cross-platform compatible. | ||
| | ||
| /// <summary> | ||
| /// The <see cref="LanguageClient"/> under test. | ||
| /// </summary> | ||
| LanguageClient LanguageClient { get; set; } | ||
| | ||
| /// <summary> | ||
| /// The server-side dispatcher. | ||
| /// </summary> | ||
| LspDispatcher ServerDispatcher { get; } = new LspDispatcher(); | ||
| | ||
| /// <summary> | ||
| /// The server-side connection. | ||
| /// </summary> | ||
| LspConnection ServerConnection { get; set; } | ||
| | ||
| /// <summary> | ||
| /// Ensure that the language client can successfully request Hover information. | ||
| /// </summary> | ||
| [Fact(DisplayName = "Language client can successfully request hover info")] | ||
| public async Task Hover_Success() | ||
| { | ||
| await Connect(); | ||
| | ||
| const int line = 5; | ||
| const int column = 5; | ||
| var expectedHoverContent = new MarkedStringContainer("123", "456", "789"); | ||
| | ||
| ServerDispatcher.HandleRequest<TextDocumentPositionParams, Hover>("textDocument/hover", (request, cancellationToken) => | ||
| { | ||
| Assert.NotNull(request.TextDocument); | ||
| | ||
| Assert.Equal(AbsoluteDocumentPath, | ||
| DocumentUri.GetFileSystemPath(request.TextDocument.Uri) | ||
| ); | ||
| | ||
| Assert.Equal(line, request.Position.Line); | ||
| Assert.Equal(column, request.Position.Character); | ||
| | ||
| return Task.FromResult(new Hover | ||
| { | ||
| Contents = expectedHoverContent, | ||
| Range = new Range | ||
| { | ||
| Start = request.Position, | ||
| End = request.Position | ||
| } | ||
| }); | ||
| }); | ||
| | ||
| Hover hover = await LanguageClient.TextDocument.Hover(AbsoluteDocumentPath, line, column); | ||
| | ||
| Assert.NotNull(hover.Range); | ||
| Assert.NotNull(hover.Range.Start); | ||
| Assert.NotNull(hover.Range.End); | ||
| | ||
| Assert.Equal(line, hover.Range.Start.Line); | ||
| Assert.Equal(column, hover.Range.Start.Character); | ||
| | ||
| Assert.Equal(line, hover.Range.End.Line); | ||
| Assert.Equal(column, hover.Range.End.Character); | ||
| | ||
| Assert.NotNull(hover.Contents); | ||
| Assert.Equal(expectedHoverContent.Select(markedString => markedString.Value), | ||
| hover.Contents.Select( | ||
| markedString => markedString.Value | ||
| ) | ||
| ); | ||
| } | ||
| | ||
| /// <summary> | ||
| /// Connect the client and server. | ||
| /// </summary> | ||
| /// <param name="handleServerInitialize"> | ||
| /// Add standard handlers for server initialisation? | ||
| /// </param> | ||
| async Task Connect(bool handleServerInitialize = true) | ||
| { | ||
| ServerConnection = await CreateServerConnection(); | ||
| ServerConnection.Connect(ServerDispatcher); | ||
| | ||
| if (handleServerInitialize) | ||
| HandleServerInitialize(); | ||
| | ||
| LanguageClient = await CreateClient(initialize: true); | ||
| } | ||
| | ||
| /// <summary> | ||
| /// Add standard handlers for sever initialisation. | ||
| /// </summary> | ||
| void HandleServerInitialize() | ||
| { | ||
| ServerDispatcher.HandleRequest<InitializeParams, InitializeResult>("initialize", (request, cancellationToken) => | ||
| { | ||
| return Task.FromResult(new InitializeResult | ||
| { | ||
| Capabilities = new ServerCapabilities | ||
| { | ||
| HoverProvider = true | ||
| } | ||
| }); | ||
| }); | ||
| ServerDispatcher.HandleEmptyNotification("initialized", () => | ||
| { | ||
| Log.LogInformation("Server initialized."); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -32,8 +32,8 @@ public async Task Client_HandleEmptyNotification_Success() | |
| { | ||
| var testCompletion = new TaskCompletionSource<object>(); | ||
| | ||
| LspConnection serverConnection = await CreateServerConnection(); | ||
| LspConnection clientConnection = await CreateClientConnection(); | ||
| LspConnection serverConnection = await CreateServerConnection(); | ||
| | ||
| var serverDispatcher = new LspDispatcher(); | ||
| serverDispatcher.HandleEmptyNotification("test", () => | ||
| | @@ -85,5 +85,165 @@ public async Task Server_HandleEmptyNotification_Success() | |
| | ||
| await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected); | ||
| } | ||
| | ||
| /// <summary> | ||
| /// Verify that a client <see cref="LspConnection"/> can handle a request from a server <see cref="LspConnection"/>. | ||
| /// </summary> | ||
| [Fact(DisplayName = "Client connection can handle request from server")] | ||
| public async Task Server_HandleRequest_Success() | ||
| { | ||
| LspConnection clientConnection = await CreateClientConnection(); | ||
| Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nitpick, if we make However that can be changed later. | ||
| LspConnection serverConnection = await CreateServerConnection(); | ||
| | ||
| var clientDispatcher = new LspDispatcher(); | ||
| clientDispatcher.HandleRequest<TestRequest, TestResponse>("test", (request, cancellationToken) => | ||
| { | ||
| Log.LogInformation("Got request: {@Request}", request); | ||
| | ||
| return Task.FromResult(new TestResponse | ||
| { | ||
| Value = request.Value.ToString() | ||
| }); | ||
| }); | ||
| clientConnection.Connect(clientDispatcher); | ||
| | ||
| serverConnection.Connect(new LspDispatcher()); | ||
| TestResponse response = await serverConnection.SendRequest<TestResponse>("test", new TestRequest | ||
| { | ||
| Value = 1234 | ||
| }); | ||
| | ||
| Assert.Equal("1234", response.Value); | ||
| | ||
| Log.LogInformation("Got response: {@Response}", response); | ||
| | ||
| serverConnection.Disconnect(flushOutgoing: true); | ||
| clientConnection.Disconnect(); | ||
| | ||
| await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected); | ||
| } | ||
| | ||
| /// <summary> | ||
| /// Verify that a server <see cref="LspConnection"/> can handle a request from a client <see cref="LspConnection"/>. | ||
| /// </summary> | ||
| [Fact(DisplayName = "Server connection can handle request from client")] | ||
| public async Task Client_HandleRequest_Success() | ||
| { | ||
| LspConnection clientConnection = await CreateClientConnection(); | ||
| LspConnection serverConnection = await CreateServerConnection(); | ||
| | ||
| var serverDispatcher = new LspDispatcher(); | ||
| serverDispatcher.HandleRequest<TestRequest, TestResponse>("test", (request, cancellationToken) => | ||
| { | ||
| Log.LogInformation("Got request: {@Request}", request); | ||
| | ||
| return Task.FromResult(new TestResponse | ||
| { | ||
| Value = request.Value.ToString() | ||
| }); | ||
| }); | ||
| serverConnection.Connect(serverDispatcher); | ||
| | ||
| clientConnection.Connect(new LspDispatcher()); | ||
| TestResponse response = await clientConnection.SendRequest<TestResponse>("test", new TestRequest | ||
| { | ||
| Value = 1234 | ||
| }); | ||
| | ||
| Assert.Equal("1234", response.Value); | ||
| | ||
| Log.LogInformation("Got response: {@Response}", response); | ||
| | ||
| clientConnection.Disconnect(flushOutgoing: true); | ||
| serverConnection.Disconnect(); | ||
| | ||
| await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected); | ||
| } | ||
| | ||
| /// <summary> | ||
| /// Verify that a client <see cref="LspConnection"/> can handle a command-style request (i.e. no response body) from a server <see cref="LspConnection"/>. | ||
| /// </summary> | ||
| [Fact(DisplayName = "Client connection can handle command request from server")] | ||
| public async Task Server_HandleCommandRequest_Success() | ||
| { | ||
| LspConnection clientConnection = await CreateClientConnection(); | ||
| LspConnection serverConnection = await CreateServerConnection(); | ||
| | ||
| var clientDispatcher = new LspDispatcher(); | ||
| clientDispatcher.HandleRequest<TestRequest>("test", (request, cancellationToken) => | ||
| { | ||
| Log.LogInformation("Got request: {@Request}", request); | ||
| | ||
| Assert.Equal(1234, request.Value); | ||
| | ||
| return Task.CompletedTask; | ||
| }); | ||
| clientConnection.Connect(clientDispatcher); | ||
| | ||
| serverConnection.Connect(new LspDispatcher()); | ||
| await serverConnection.SendRequest("test", new TestRequest | ||
| { | ||
| Value = 1234 | ||
| }); | ||
| | ||
| serverConnection.Disconnect(flushOutgoing: true); | ||
| clientConnection.Disconnect(); | ||
| | ||
| await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected); | ||
| } | ||
| | ||
| /// <summary> | ||
| /// Verify that a server <see cref="LspConnection"/> can handle a command-style request (i.e. no response body) from a client <see cref="LspConnection"/>. | ||
| /// </summary> | ||
| [Fact(DisplayName = "Server connection can handle command request from client")] | ||
| public async Task Client_HandleCommandRequest_Success() | ||
| { | ||
| LspConnection clientConnection = await CreateClientConnection(); | ||
| LspConnection serverConnection = await CreateServerConnection(); | ||
| | ||
| var serverDispatcher = new LspDispatcher(); | ||
| serverDispatcher.HandleRequest<TestRequest>("test", (request, cancellationToken) => | ||
| { | ||
| Log.LogInformation("Got request: {@Request}", request); | ||
| | ||
| Assert.Equal(1234, request.Value); | ||
| | ||
| return Task.CompletedTask; | ||
| }); | ||
| serverConnection.Connect(serverDispatcher); | ||
| | ||
| clientConnection.Connect(new LspDispatcher()); | ||
| await clientConnection.SendRequest("test", new TestRequest | ||
| { | ||
| Value = 1234 | ||
| }); | ||
| | ||
| clientConnection.Disconnect(flushOutgoing: true); | ||
| serverConnection.Disconnect(); | ||
| | ||
| await Task.WhenAll(clientConnection.HasHasDisconnected, serverConnection.HasHasDisconnected); | ||
| } | ||
| } | ||
| | ||
| /// <summary> | ||
| /// A test request. | ||
| /// </summary> | ||
| class TestRequest | ||
| { | ||
| /// <summary> | ||
| /// A test value for the request. | ||
| /// </summary> | ||
| public int Value { get; set; } | ||
| } | ||
| | ||
| /// <summary> | ||
| /// A test response. | ||
| /// </summary> | ||
| class TestResponse | ||
| { | ||
| /// <summary> | ||
| /// A test value for the response. | ||
| /// </summary> | ||
| public string Value { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should makes things so much easier to integration test... and we can finally make sure
LanguageServeris working correctly.