diff --git a/tests/sessions.zig b/tests/sessions.zig index 33aa815..9ba88f3 100644 --- a/tests/sessions.zig +++ b/tests/sessions.zig @@ -18,7 +18,8 @@ fn sendRequest(req: []const u8, process: *std.ChildProcess) !void { try process.stdin.?.writeAll(req); } -fn readResponses(process: *std.ChildProcess) !void { +fn readResponses(process: *std.ChildProcess, expected_responses: var) !void { + var seen = std.mem.zeroes([expected_responses.len]bool); while (true) { const header = headerPkg.readRequestHeader(allocator, process.stdout.?.reader()) catch |err| { switch(err) { @@ -32,8 +33,22 @@ fn readResponses(process: *std.ChildProcess) !void { defer allocator.free(stdout_mem); const stdout_bytes = stdout_mem[0..try process.stdout.?.reader().readAll(stdout_mem)]; + inline for (expected_responses) |resp, idx| { + if (std.mem.eql(u8, resp, stdout_bytes)) { + if (seen[idx]) @panic("Expected response already received."); + seen[idx] = true; + } + } std.debug.print("GOT MESSAGE: {}\n", .{stdout_bytes}); } + + comptime var idx = 0; + inline while (idx < expected_responses.len) : (idx += 1) { + if (!seen[idx]) { + std.debug.print("Response `{}` not received.", .{expected_responses[idx]}); + return error.ExpectedResponse; + } + } } fn startZls() !*std.ChildProcess { @@ -61,10 +76,10 @@ fn waitNoError(process: *std.ChildProcess) !void { return error.ShutdownWithError; } -fn consumeOutputAndWait(process: *std.ChildProcess) !void { +fn consumeOutputAndWait(process: *std.ChildProcess, expected_responses: var) !void { process.stdin.?.close(); process.stdin = null; - try readResponses(process); + try readResponses(process, expected_responses); try waitNoError(process); process.deinit(); } @@ -81,7 +96,9 @@ test "Open file, ask for semantic tokens" { ; try sendRequest(sem_toks_req, process); try sendRequest(shutdown_message, process); - try consumeOutputAndWait(process); + try consumeOutputAndWait(process, .{ + \\{"jsonrpc":"2.0","id":2,"result":{"data":[0,0,5,11,0,0,6,3,0,1,0,4,1,15,0,0,2,7,16,0,0,8,5,13,0]}} + }); } test "Requesting a completion in an empty file" { @@ -96,7 +113,7 @@ test "Requesting a completion in an empty file" { ; try sendRequest(completion_req, process); try sendRequest(shutdown_message, process); - try consumeOutputAndWait(process); + try consumeOutputAndWait(process, .{}); } test "Requesting a completion with no trailing whitespace" { @@ -111,7 +128,9 @@ test "Requesting a completion with no trailing whitespace" { ; try sendRequest(completion_req, process); try sendRequest(shutdown_message, process); - try consumeOutputAndWait(process); + try consumeOutputAndWait(process, .{ + \\{"jsonrpc":"2.0","id":2,"result":{"isIncomplete":false,"items":[]}} + }); } const initialize_message_offs = \\{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":6896,"clientInfo":{"name":"vscode","version":"1.46.1"},"rootPath":null,"rootUri":null,"capabilities":{"offsetEncoding":["utf-16", "utf-8"],"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true,"resourceOperations":["create","rename","delete"],"failureHandling":"textOnlyTransactional"},"didChangeConfiguration":{"dynamicRegistration":true},"didChangeWatchedFiles":{"dynamicRegistration":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"tagSupport":{"valueSet":[1]}},"executeCommand":{"dynamicRegistration":true},"configuration":true,"workspaceFolders":true},"textDocument":{"publishDiagnostics":{"relatedInformation":true,"versionSupport":false,"tagSupport":{"valueSet":[1,2]},"complexDiagnosticCodeSupport":true},"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"contextSupport":true,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true,"tagSupport":{"valueSet":[1]},"insertReplaceSupport":true},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]}},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":true,"signatureInformation":{"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true}},"contextSupport":true},"definition":{"dynamicRegistration":true,"linkSupport":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"hierarchicalDocumentSymbolSupport":true,"tagSupport":{"valueSet":[1]}},"codeAction":{"dynamicRegistration":true,"isPreferredSupport":true,"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"codeLens":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"prepareSupport":true},"documentLink":{"dynamicRegistration":true,"tooltipSupport":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"colorProvider":{"dynamicRegistration":true},"foldingRange":{"dynamicRegistration":true,"rangeLimit":5000,"lineFoldingOnly":true},"declaration":{"dynamicRegistration":true,"linkSupport":true},"selectionRange":{"dynamicRegistration":true},"semanticTokens":{"dynamicRegistration":true,"tokenTypes":["comment","keyword","number","regexp","operator","namespace","type","struct","class","interface","enum","typeParameter","function","member","macro","variable","parameter","property","label"],"tokenModifiers":["declaration","documentation","static","abstract","deprecated","readonly"]}},"window":{"workDoneProgress":true}},"trace":"off","workspaceFolders":null}} @@ -123,5 +142,7 @@ test "Requesting utf-8 offset encoding" { try sendRequest(initialized_message, process); try sendRequest(shutdown_message, process); - try consumeOutputAndWait(process); + try consumeOutputAndWait(process, .{ + \\{"jsonrpc":"2.0","id":0,"result": {"offsetEncoding":"utf-8","capabilities": {"signatureHelpProvider": {"triggerCharacters": ["(",","]},"textDocumentSync": 1,"renameProvider":true,"completionProvider": {"resolveProvider": false,"triggerCharacters": [".",":","@"]},"documentHighlightProvider": false,"hoverProvider": true,"codeActionProvider": false,"declarationProvider": true,"definitionProvider": true,"typeDefinitionProvider": true,"implementationProvider": false,"referencesProvider": false,"documentSymbolProvider": true,"colorProvider": false,"documentFormattingProvider": true,"documentRangeFormattingProvider": false,"foldingRangeProvider": false,"selectionRangeProvider": false,"workspaceSymbolProvider": false,"rangeProvider": false,"documentProvider": true,"workspace": {"workspaceFolders": {"supported": true,"changeNotifications": true}},"semanticTokensProvider": {"documentProvider": true,"legend": {"tokenTypes": ["namespace","type","struct","enum","union","parameter","variable","tagField","field","errorTag","function","keyword","comment","string","number","operator","builtin","label"],"tokenModifiers": ["definition","async","documentation", "generic"]}}}}} + }); }