-
Notifications
You must be signed in to change notification settings - Fork 105
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 all 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,306 @@ | ||
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; | ||
using System.Collections.Generic; | ||
using System; | ||
|
||
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 => IsWindows ? @"C:\Foo.txt" : "/Foo.txt"; | ||
|
||
/// <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> | ||
/// Ensure that the language client can successfully request Completions. | ||
/// </summary> | ||
[Fact(DisplayName = "Language client can successfully request completions")] | ||
public async Task Completions_Success() | ||
{ | ||
await Connect(); | ||
|
||
const int line = 5; | ||
const int column = 5; | ||
string expectedDocumentPath = AbsoluteDocumentPath; | ||
Uri expectedDocumentUri = DocumentUri.FromFileSystemPath(expectedDocumentPath); | ||
|
||
var expectedCompletionItems = new CompletionItem[] | ||
{ | ||
new CompletionItem | ||
{ | ||
Kind = CompletionItemKind.Class, | ||
Label = "Class1", | ||
TextEdit = new TextEdit | ||
{ | ||
Range = new Range | ||
{ | ||
Start = new Position | ||
{ | ||
Line = line, | ||
Character = column | ||
}, | ||
End = new Position | ||
{ | ||
Line = line, | ||
Character = column | ||
} | ||
}, | ||
NewText = "Class1", | ||
} | ||
} | ||
}; | ||
|
||
ServerDispatcher.HandleRequest<TextDocumentPositionParams, CompletionList>("textDocument/completion", (request, cancellationToken) => | ||
{ | ||
Assert.NotNull(request.TextDocument); | ||
|
||
Assert.Equal(expectedDocumentUri, request.TextDocument.Uri); | ||
|
||
Assert.Equal(line, request.Position.Line); | ||
Assert.Equal(column, request.Position.Character); | ||
|
||
return Task.FromResult(new CompletionList( | ||
expectedCompletionItems, | ||
isIncomplete: true | ||
)); | ||
}); | ||
|
||
CompletionList actualCompletions = await LanguageClient.TextDocument.Completions(AbsoluteDocumentPath, line, column); | ||
|
||
Assert.True(actualCompletions.IsIncomplete, "completions.IsIncomplete"); | ||
Assert.NotNull(actualCompletions.Items); | ||
|
||
CompletionItem[] actualCompletionItems = actualCompletions.Items.ToArray(); | ||
Assert.Collection(actualCompletionItems, actualCompletionItem => | ||
{ | ||
CompletionItem expectedCompletionItem = expectedCompletionItems[0]; | ||
|
||
Assert.Equal(expectedCompletionItem.Kind, actualCompletionItem.Kind); | ||
Assert.Equal(expectedCompletionItem.Label, actualCompletionItem.Label); | ||
|
||
Assert.NotNull(actualCompletionItem.TextEdit); | ||
Assert.Equal(expectedCompletionItem.TextEdit.NewText, actualCompletionItem.TextEdit.NewText); | ||
|
||
Assert.NotNull(actualCompletionItem.TextEdit.Range); | ||
Assert.NotNull(actualCompletionItem.TextEdit.Range.Start); | ||
Assert.NotNull(actualCompletionItem.TextEdit.Range.End); | ||
Assert.Equal(expectedCompletionItem.TextEdit.Range.Start.Line, actualCompletionItem.TextEdit.Range.Start.Line); | ||
Assert.Equal(expectedCompletionItem.TextEdit.Range.Start.Character, actualCompletionItem.TextEdit.Range.Start.Character); | ||
Assert.Equal(expectedCompletionItem.TextEdit.Range.End.Line, actualCompletionItem.TextEdit.Range.End.Line); | ||
Assert.Equal(expectedCompletionItem.TextEdit.Range.End.Character, actualCompletionItem.TextEdit.Range.End.Character); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Ensure that the language client can successfully receive Diagnostics from the server. | ||
/// </summary> | ||
[Fact(DisplayName = "Language client can successfully receive diagnostics")] | ||
public async Task Diagnostics_Success() | ||
{ | ||
await Connect(); | ||
|
||
string documentPath = AbsoluteDocumentPath; | ||
Uri expectedDocumentUri = DocumentUri.FromFileSystemPath(documentPath); | ||
List<Diagnostic> expectedDiagnostics = new List<Diagnostic> | ||
{ | ||
new Diagnostic | ||
{ | ||
Source = "Test", | ||
Code = new DiagnosticCode(1234), | ||
Message = "This is a diagnostic message.", | ||
Range = new Range | ||
{ | ||
Start = new Position | ||
{ | ||
Line = 2, | ||
Character = 5 | ||
}, | ||
End = new Position | ||
{ | ||
Line = 3, | ||
Character = 7 | ||
} | ||
}, | ||
Severity = DiagnosticSeverity.Warning | ||
} | ||
}; | ||
|
||
TaskCompletionSource<object> receivedDiagnosticsNotification = new TaskCompletionSource<object>(); | ||
|
||
Uri actualDocumentUri = null; | ||
List<Diagnostic> actualDiagnostics = null; | ||
LanguageClient.TextDocument.OnPublishDiagnostics((documentUri, diagnostics) => | ||
{ | ||
actualDocumentUri = documentUri; | ||
actualDiagnostics = diagnostics; | ||
|
||
receivedDiagnosticsNotification.SetResult(null); | ||
}); | ||
|
||
ServerConnection.SendNotification("textDocument/publishDiagnostics", new PublishDiagnosticsParams | ||
{ | ||
Uri = DocumentUri.FromFileSystemPath(documentPath), | ||
Diagnostics = expectedDiagnostics | ||
}); | ||
|
||
// Timeout. | ||
Task winner = await Task.WhenAny( | ||
receivedDiagnosticsNotification.Task, | ||
Task.Delay( | ||
TimeSpan.FromSeconds(2) | ||
) | ||
); | ||
Assert.Same(receivedDiagnosticsNotification.Task, winner); | ||
|
||
Assert.NotNull(actualDocumentUri); | ||
Assert.Equal(expectedDocumentUri, actualDocumentUri); | ||
|
||
Assert.NotNull(actualDiagnostics); | ||
Assert.Equal(1, actualDiagnostics.Count); | ||
|
||
Diagnostic expectedDiagnostic = expectedDiagnostics[0]; | ||
Diagnostic actualDiagnostic = actualDiagnostics[0]; | ||
|
||
Assert.Equal(expectedDiagnostic.Code, actualDiagnostic.Code); | ||
Assert.Equal(expectedDiagnostic.Message, actualDiagnostic.Message); | ||
Assert.Equal(expectedDiagnostic.Range.Start.Line, actualDiagnostic.Range.Start.Line); | ||
Assert.Equal(expectedDiagnostic.Range.Start.Character, actualDiagnostic.Range.Start.Character); | ||
Assert.Equal(expectedDiagnostic.Range.End.Line, actualDiagnostic.Range.End.Line); | ||
Assert.Equal(expectedDiagnostic.Range.End.Character, actualDiagnostic.Range.End.Character); | ||
Assert.Equal(expectedDiagnostic.Severity, actualDiagnostic.Severity); | ||
Assert.Equal(expectedDiagnostic.Source, actualDiagnostic.Source); | ||
} | ||
|
||
/// <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."); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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
LanguageServer
is working correctly.