Skip to content

Commit 3d8b595

Browse files
committed
Add client test for "textDocument/publishDiagnostics".
1 parent bae0291 commit 3d8b595

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/Client.Tests/ClientTests.cs

+82
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using System.Threading.Tasks;
1010
using Xunit;
1111
using Xunit.Abstractions;
12+
using System.Collections.Generic;
13+
using System;
1214

1315
namespace OmniSharp.Extensions.LanguageServerProtocol.Client.Tests
1416
{
@@ -103,6 +105,86 @@ public async Task Hover_Success()
103105
);
104106
}
105107

108+
/// <summary>
109+
/// Ensure that the language client can successfully receive Diagnostics from the server.
110+
/// </summary>
111+
[Fact(DisplayName = "Language client can successfully receive diagnostics")]
112+
public async Task Diagnostics_Success()
113+
{
114+
await Connect();
115+
116+
string documentPath = AbsoluteDocumentPath;
117+
Uri expectedDocumentUri = DocumentUri.FromFileSystemPath(documentPath);
118+
List<Diagnostic> expectedDiagnostics = new List<Diagnostic>
119+
{
120+
new Diagnostic
121+
{
122+
Source = "Test",
123+
Code = new DiagnosticCode(1234),
124+
Message = "This is a diagnostic message.",
125+
Range = new Range
126+
{
127+
Start = new Position
128+
{
129+
Line = 2,
130+
Character = 5
131+
},
132+
End = new Position
133+
{
134+
Line = 3,
135+
Character = 7
136+
}
137+
},
138+
Severity = DiagnosticSeverity.Warning
139+
}
140+
};
141+
142+
TaskCompletionSource<object> receivedDiagnosticsNotification = new TaskCompletionSource<object>();
143+
144+
Uri actualDocumentUri = null;
145+
List<Diagnostic> actualDiagnostics = null;
146+
LanguageClient.TextDocument.OnPublishDiagnostics((documentUri, diagnostics) =>
147+
{
148+
actualDocumentUri = documentUri;
149+
actualDiagnostics = diagnostics;
150+
151+
receivedDiagnosticsNotification.SetResult(null);
152+
});
153+
154+
ServerConnection.SendNotification("textDocument/publishDiagnostics", new PublishDiagnosticsParams
155+
{
156+
Uri = DocumentUri.FromFileSystemPath(documentPath),
157+
Diagnostics = expectedDiagnostics
158+
});
159+
160+
// Timeout.
161+
Task winner = await Task.WhenAny(
162+
receivedDiagnosticsNotification.Task,
163+
Task.Delay(
164+
TimeSpan.FromSeconds(2)
165+
)
166+
);
167+
Assert.Same(receivedDiagnosticsNotification.Task, winner);
168+
169+
Assert.NotNull(actualDocumentUri);
170+
Assert.Equal(expectedDocumentUri, actualDocumentUri);
171+
172+
Assert.NotNull(actualDiagnostics);
173+
Assert.Equal(1, actualDiagnostics.Count);
174+
175+
Diagnostic expectedDiagnostic = expectedDiagnostics[0];
176+
Diagnostic actualDiagnostic = actualDiagnostics[0];
177+
178+
Assert.Equal(expectedDiagnostic.Code, actualDiagnostic.Code);
179+
Assert.Equal(expectedDiagnostic.Message, actualDiagnostic.Message);
180+
Assert.Equal(expectedDiagnostic.Range.Start.Line, actualDiagnostic.Range.Start.Line);
181+
Assert.Equal(expectedDiagnostic.Range.Start.Character, actualDiagnostic.Range.Start.Character);
182+
Assert.Equal(expectedDiagnostic.Range.End.Line, actualDiagnostic.Range.End.Line);
183+
Assert.Equal(expectedDiagnostic.Range.End.Character, actualDiagnostic.Range.End.Character);
184+
Assert.Equal(expectedDiagnostic.Severity, actualDiagnostic.Severity);
185+
Assert.Equal(expectedDiagnostic.Source, actualDiagnostic.Source);
186+
}
187+
106188
/// <summary>
107189
/// Connect the client and server.
108190
/// </summary>

0 commit comments

Comments
 (0)