From f0b71f3861df8901ad66ec5a6704187060ac39b4 Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Fri, 26 Aug 2022 16:51:43 +0200 Subject: [PATCH] add tests for semantic tokens --- tests/lsp_features/semantic_tokens.zig | 55 ++++++++++++++++++++++++++ tests/sessions.zig | 15 ------- tests/tests.zig | 5 ++- 3 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 tests/lsp_features/semantic_tokens.zig diff --git a/tests/lsp_features/semantic_tokens.zig b/tests/lsp_features/semantic_tokens.zig new file mode 100644 index 0000000..67cecbb --- /dev/null +++ b/tests/lsp_features/semantic_tokens.zig @@ -0,0 +1,55 @@ +const std = @import("std"); +const zls = @import("zls"); + +const Context = @import("context").Context; + +const requests = zls.requests; + +const allocator: std.mem.Allocator = std.testing.allocator; + +test "semantic tokens - empty" { + try testSemanticTokens("", &.{}); +} + +test "semantic tokens" { + try testSemanticTokens( + \\const std = @import("std"); + , + &.{ 0, 0, 5, 7, 0, 0, 6, 3, 0, 33, 0, 4, 1, 11, 0, 0, 2, 7, 12, 0, 0, 8, 5, 9, 0 }, + ); + // TODO more tests +} + +fn testSemanticTokens(source: []const u8, expected: []const u32) !void { + var ctx = try Context.init(); + defer ctx.deinit(); + + ctx.server.config.enable_semantic_tokens = true; + + const open_document = requests.OpenDocument{ + .params = .{ + .textDocument = .{ + .uri = "file:///test.zig", + // .languageId = "zig", + // .version = 420, + .text = source, + }, + }, + }; + + const did_open_method = try std.json.stringifyAlloc(allocator, open_document.params, .{}); + defer allocator.free(did_open_method); + + try ctx.request("textDocument/didOpen", did_open_method, null); + + const Response = struct { + data: []const u32, + }; + + const expected_bytes = try std.json.stringifyAlloc(allocator, Response{ .data = expected }, .{}); + defer allocator.free(expected_bytes); + + try ctx.request("textDocument/semanticTokens/full", + \\{"textDocument":{"uri":"file:///test.zig"}} + , expected_bytes); +} diff --git a/tests/sessions.zig b/tests/sessions.zig index 6baf734..bf13279 100644 --- a/tests/sessions.zig +++ b/tests/sessions.zig @@ -3,21 +3,6 @@ const Context = @import("context.zig").Context; const allocator = std.testing.allocator; -test "Open file, ask for semantic tokens" { - var ctx = try Context.init(); - defer ctx.deinit(); - - try ctx.request("textDocument/didOpen", - \\{"textDocument":{"uri":"file:///test.zig","languageId":"zig","version":420,"text":"const std = @import(\"std\");"}} - , null); - - try ctx.request("textDocument/semanticTokens/full", - \\{"textDocument":{"uri":"file:///test.zig"}} - , - \\{"data":[0,0,5,7,0,0,6,3,0,33,0,4,1,11,0,0,2,7,12,0,0,8,5,9,0]} - ); -} - test "Request completion in an empty file" { var ctx = try Context.init(); defer ctx.deinit(); diff --git a/tests/tests.zig b/tests/tests.zig index c1c0cfb..6a305fe 100644 --- a/tests/tests.zig +++ b/tests/tests.zig @@ -5,7 +5,8 @@ test { // TODO Document Synchronization - // TODO LSP features + // LSP features + _ = @import("lsp_features/semantic_tokens.zig"); // TODO Language features -} \ No newline at end of file +}