Testing improvements (#662)

This commit is contained in:
Techatrix
2022-09-18 22:47:06 +00:00
committed by GitHub
parent b3078c36dd
commit 7f4f002380
10 changed files with 510 additions and 111 deletions

View File

@@ -3,6 +3,8 @@ const zls = @import("zls");
const headerPkg = zls.header;
const Server = zls.Server;
const types = zls.types;
const requests = zls.requests;
const initialize_msg =
\\{"processId":6896,"clientInfo":{"name":"vscode","version":"1.46.1"},"rootPath":null,"rootUri":null,"capabilities":{"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":[{"uri":"file://./tests", "name":"root"}]}
@@ -101,4 +103,58 @@ pub const Context = struct {
try std.testing.expectEqualStrings(expected, result_json);
}
// helper
pub fn requestDidOpen(self: *Context, uri: []const u8, source: []const u8) !void {
const open_document = requests.OpenDocument{
.params = .{
.textDocument = .{
.uri = uri,
// .languageId = "zig",
// .version = 420,
.text = source,
},
},
};
const params = try std.json.stringifyAlloc(allocator, open_document.params, .{});
defer allocator.free(params);
try self.request("textDocument/didOpen", params, null);
}
pub fn Response(comptime Result: type) type {
return struct {
jsonrpc: []const u8,
id: types.RequestId,
result: Result,
pub fn deinit(self: @This()) void {
const parse_options = std.json.ParseOptions{
.allocator = allocator,
.ignore_unknown_fields = true,
};
std.json.parseFree(@This(), self, parse_options);
}
};
}
pub fn requestGetResponse(self: *Context, comptime Result: type, method: []const u8, request_struct: anytype) !Response(Result) {
const params = try std.json.stringifyAlloc(allocator, request_struct.params, .{});
defer allocator.free(params);
const response_bytes = try self.requestAlloc(method, params);
defer allocator.free(response_bytes);
const parse_options = std.json.ParseOptions{
.allocator = allocator,
.ignore_unknown_fields = true,
};
var token_stream = std.json.TokenStream.init(response_bytes);
const response = try std.json.parse(Response(Result), &token_stream, parse_options);
errdefer std.json.parseFree(Response(Result), response, parse_options);
// TODO validate jsonrpc and id
return response;
}
};