prepare testing framework

- allow source files from tests as a package
- use `tests/tests.zig` as the entry point
- add `Context.requestAlloc`
This commit is contained in:
Techatrix 2022-08-26 16:45:22 +02:00
parent 8cf96fe27c
commit f6082e837d
4 changed files with 46 additions and 16 deletions

View File

@ -82,11 +82,12 @@ pub fn build(b: *std.build.Builder) !void {
unit_tests.setTarget(target);
test_step.dependOn(&unit_tests.step);
var session_tests = b.addTest("tests/sessions.zig");
session_tests.use_stage1 = true;
session_tests.addPackage(.{ .name = "header", .source = .{ .path = "src/header.zig" } });
session_tests.addPackage(.{ .name = "server", .source = .{ .path = "src/Server.zig" }, .dependencies = exe.packages.items });
session_tests.setBuildMode(.Debug);
session_tests.setTarget(target);
test_step.dependOn(&session_tests.step);
var tests = b.addTest("tests/tests.zig");
tests.use_stage1 = true;
tests.addPackage(.{ .name = "zls", .source = .{ .path = "src/zls.zig" }, .dependencies = exe.packages.items });
tests.addPackage(.{ .name = "helper", .source = .{ .path = "tests/helper.zig" } });
tests.addPackage(.{ .name = "context", .source = .{ .path = "tests/context.zig" } });
tests.setBuildMode(.Debug);
tests.setTarget(target);
test_step.dependOn(&tests.step);
}

6
src/zls.zig Normal file
View File

@ -0,0 +1,6 @@
// used by tests as a package
pub const header = @import("header.zig");
pub const types = @import("types.zig");
pub const requests = @import("requests.zig");
pub const Server = @import("Server.zig");
pub const translate_c = @import("translate_c.zig");

View File

@ -1,6 +1,8 @@
const std = @import("std");
const headerPkg = @import("header");
const Server = @import("server");
const zls = @import("zls");
const headerPkg = zls.header;
const Server = zls.Server;
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"}]}
@ -32,12 +34,11 @@ pub const Context = struct {
self.server.deinit();
}
pub fn request(
pub fn requestAlloc(
self: *Context,
method: []const u8,
params: []const u8,
expect: ?[]const u8,
) !void {
) ![]const u8 {
var output = std.ArrayListUnmanaged(u8){};
defer output.deinit(allocator);
@ -51,20 +52,31 @@ pub const Context = struct {
// send the request to the server
try self.server.processJsonRpc(output.writer(allocator), req);
// if we don't expect a response ignore it
const expected = expect orelse return;
// get the output from the server
var buffer_stream = std.io.fixedBufferStream(output.items);
const header = try headerPkg.readRequestHeader(allocator, buffer_stream.reader());
defer header.deinit(allocator);
var response_bytes = try allocator.alloc(u8, header.content_length);
defer allocator.free(response_bytes);
errdefer allocator.free(response_bytes);
const content_length = try buffer_stream.reader().readAll(response_bytes);
try std.testing.expectEqual(content_length, header.content_length);
return response_bytes;
}
pub fn request(
self: *Context,
method: []const u8,
params: []const u8,
expect: ?[]const u8,
) !void {
const response_bytes = try self.requestAlloc(method, params);
defer allocator.free(response_bytes);
const expected = expect orelse return;
// parse the response
var parser = std.json.Parser.init(allocator, false);
defer parser.deinit();

11
tests/tests.zig Normal file
View File

@ -0,0 +1,11 @@
test {
_ = @import("sessions.zig");
// TODO Lifecycle Messages
// TODO Document Synchronization
// TODO LSP features
// TODO Language features
}