From 0e5e1fdb8ae5b2da813fd10937b12174854ee903 Mon Sep 17 00:00:00 2001 From: Alex Kladov Date: Sat, 11 Feb 2023 06:31:06 +0000 Subject: [PATCH] add testing infra for textDocument/documentSymbol (#987) * add testing infra for textDocument/documentSymbol * add failing test for 986 --- tests/lsp_features/document_symbol.zig | 83 ++++++++++++++++++++++++++ tests/tests.zig | 1 + 2 files changed, 84 insertions(+) create mode 100644 tests/lsp_features/document_symbol.zig diff --git a/tests/lsp_features/document_symbol.zig b/tests/lsp_features/document_symbol.zig new file mode 100644 index 0000000..26c0a89 --- /dev/null +++ b/tests/lsp_features/document_symbol.zig @@ -0,0 +1,83 @@ +const std = @import("std"); +const zls = @import("zls"); +const builtin = @import("builtin"); + +const tres = @import("tres"); + +const Context = @import("../context.zig").Context; + +const types = zls.types; +const requests = zls.requests; + +const allocator: std.mem.Allocator = std.testing.allocator; + +test "documentSymbol - smoke" { + try testDocumentSymbol( + \\const S = struct { + \\ fn f() void {} + \\}; + , + \\Variable S + \\ Function f + ); +} + +// FIXME: https://github.com/zigtools/zls/issues/986 +test "documentSymbol - nested struct with self" { + try testDocumentSymbol( + \\const Foo = struct { + \\ const Self = @This(); + \\ pub fn foo() !Self {} + \\ const Bar = struct {}; + \\}; + , + \\Variable Foo + ); +} + +fn testDocumentSymbol(source: []const u8, want: []const u8) !void { + var ctx = try Context.init(); + defer ctx.deinit(); + + const test_uri: []const u8 = switch (builtin.os.tag) { + .windows => "file:///C:\\test.zig", + else => "file:///test.zig", + }; + + try ctx.requestDidOpen(test_uri, source); + + const params = types.DocumentSymbolParams{ + .textDocument = .{ .uri = test_uri }, + }; + + const response = try ctx.requestGetResponse([]types.DocumentSymbol, "textDocument/documentSymbol", params); + + var got = std.ArrayListUnmanaged(u8){}; + defer got.deinit(allocator); + + var stack: [16][]const types.DocumentSymbol = undefined; + var stack_len: usize = 0; + + stack[stack_len] = response.result; + stack_len += 1; + + var writer = got.writer(allocator); + while (stack_len > 0) { + const top = &stack[stack_len - 1]; + if (top.len > 0) { + try std.fmt.format(writer, "{[space]s:[width]}", .{ .space = "", .width = (stack_len - 1) * 2 }); + try std.fmt.format(writer, "{s} {s}\n", .{ @tagName(top.*[0].kind), top.*[0].name }); + if (top.*[0].children) |children| { + std.debug.assert(stack_len < stack.len); + stack[stack_len] = children; + stack_len += 1; + } + top.* = top.*[1..]; + } else { + stack_len -= 1; + } + } + _ = got.pop(); // Final \n + + try std.testing.expectEqualStrings(want, got.items); +} diff --git a/tests/tests.zig b/tests/tests.zig index c968928..b7719af 100644 --- a/tests/tests.zig +++ b/tests/tests.zig @@ -13,6 +13,7 @@ comptime { // LSP features _ = @import("lsp_features/completion.zig"); _ = @import("lsp_features/definition.zig"); + _ = @import("lsp_features/document_symbol.zig"); _ = @import("lsp_features/folding_range.zig"); _ = @import("lsp_features/inlay_hints.zig"); _ = @import("lsp_features/references.zig");