add testing infra for textDocument/documentSymbol (#987)

* add testing infra for textDocument/documentSymbol

* add failing test for 986
This commit is contained in:
Alex Kladov 2023-02-11 06:31:06 +00:00 committed by GitHub
parent ebbe455722
commit 0e5e1fdb8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 0 deletions

View File

@ -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);
}

View File

@ -13,6 +13,7 @@ comptime {
// LSP features // LSP features
_ = @import("lsp_features/completion.zig"); _ = @import("lsp_features/completion.zig");
_ = @import("lsp_features/definition.zig"); _ = @import("lsp_features/definition.zig");
_ = @import("lsp_features/document_symbol.zig");
_ = @import("lsp_features/folding_range.zig"); _ = @import("lsp_features/folding_range.zig");
_ = @import("lsp_features/inlay_hints.zig"); _ = @import("lsp_features/inlay_hints.zig");
_ = @import("lsp_features/references.zig"); _ = @import("lsp_features/references.zig");