|
9 | 9 | using System.Threading.Tasks;
|
10 | 10 | using Xunit;
|
11 | 11 | using Xunit.Abstractions;
|
| 12 | +using System.Collections.Generic; |
| 13 | +using System; |
12 | 14 |
|
13 | 15 | namespace OmniSharp.Extensions.LanguageServerProtocol.Client.Tests
|
14 | 16 | {
|
@@ -103,6 +105,86 @@ public async Task Hover_Success()
|
103 | 105 | );
|
104 | 106 | }
|
105 | 107 |
|
| 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 | + |
106 | 188 | /// <summary>
|
107 | 189 | /// Connect the client and server.
|
108 | 190 | /// </summary>
|
|
0 commit comments