Remove some boilerplate from test (#988)

This commit is contained in:
Alex Kladov 2023-02-11 19:19:37 +00:00 committed by GitHub
parent 0e5e1fdb8a
commit 0d3b0e9965
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 27 additions and 56 deletions

View File

@ -44,7 +44,7 @@ pub fn printDocumentScope(doc_scope: analysis.DocumentScope) void {
if (!std.debug.runtime_safety) @compileError("this function should only be used in debug mode!"); if (!std.debug.runtime_safety) @compileError("this function should only be used in debug mode!");
var index: usize = 0; var index: usize = 0;
while(index < doc_scope.scopes.len) : (index += 1) { while (index < doc_scope.scopes.len) : (index += 1) {
const scope = doc_scope.scopes.get(index); const scope = doc_scope.scopes.get(index);
if (index != 0) std.debug.print("\n\n", .{}); if (index != 0) std.debug.print("\n\n", .{});
std.debug.print( std.debug.print(

View File

@ -844,7 +844,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
// Maybe we can hook into it insead? Also applies to Identifier and VarDecl // Maybe we can hook into it insead? Also applies to Identifier and VarDecl
var bound_type_params = analysis.BoundTypeParams{}; var bound_type_params = analysis.BoundTypeParams{};
defer bound_type_params.deinit(builder.store.allocator); defer bound_type_params.deinit(builder.store.allocator);
const lhs_type = try analysis.resolveFieldAccessLhsType( const lhs_type = try analysis.resolveFieldAccessLhsType(
builder.store, builder.store,
(try analysis.resolveTypeOfNodeInternal( (try analysis.resolveTypeOfNodeInternal(

View File

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const zls = @import("zls"); const zls = @import("zls");
const builtin = @import("builtin");
const tres = @import("tres"); const tres = @import("tres");
@ -8,7 +9,6 @@ const Config = zls.Config;
const Server = zls.Server; const Server = zls.Server;
const types = zls.types; const types = zls.types;
/// initialize request taken from Visual Studio Code with the following changes: /// initialize request taken from Visual Studio Code with the following changes:
/// - removed locale, rootPath, rootUri, trace, workspaceFolders /// - removed locale, rootPath, rootUri, trace, workspaceFolders
/// - removed capabilities.workspace.configuration /// - removed capabilities.workspace.configuration
@ -149,7 +149,12 @@ pub const Context = struct {
} }
// helper // helper
pub fn requestDidOpen(self: *Context, uri: []const u8, source: []const u8) !void { pub fn addDocument(self: *Context, source: []const u8) ![]const u8 {
const uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
const open_document = types.DidOpenTextDocumentParams{ const open_document = types.DidOpenTextDocumentParams{
.textDocument = .{ .textDocument = .{
.uri = uri, .uri = uri,
@ -160,7 +165,9 @@ pub const Context = struct {
}; };
const params = try std.json.stringifyAlloc(allocator, open_document, .{}); const params = try std.json.stringifyAlloc(allocator, open_document, .{});
defer allocator.free(params); defer allocator.free(params);
try self.notification("textDocument/didOpen", params); try self.notification("textDocument/didOpen", params);
return uri;
} }
pub fn Response(comptime Result: type) type { pub fn Response(comptime Result: type) type {

View File

@ -400,12 +400,7 @@ fn testCompletion(source: []const u8, expected_completions: []const Completion)
var ctx = try Context.init(); var ctx = try Context.init();
defer ctx.deinit(); defer ctx.deinit();
const test_uri: []const u8 = switch (builtin.os.tag) { const test_uri = try ctx.addDocument(text);
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
try ctx.requestDidOpen(test_uri, text);
const params = types.CompletionParams{ const params = types.CompletionParams{
.textDocument = .{ .uri = test_uri }, .textDocument = .{ .uri = test_uri },

View File

@ -53,12 +53,7 @@ fn testDefinition(source: []const u8) !void {
var ctx = try Context.init(); var ctx = try Context.init();
defer ctx.deinit(); defer ctx.deinit();
const test_uri: []const u8 = switch (builtin.os.tag) { const test_uri = try ctx.addDocument(phr.new_source);
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
try ctx.requestDidOpen(test_uri, phr.new_source);
const params = types.TextDocumentPositionParams{ const params = types.TextDocumentPositionParams{
.textDocument = .{ .uri = test_uri }, .textDocument = .{ .uri = test_uri },

View File

@ -39,12 +39,7 @@ fn testDocumentSymbol(source: []const u8, want: []const u8) !void {
var ctx = try Context.init(); var ctx = try Context.init();
defer ctx.deinit(); defer ctx.deinit();
const test_uri: []const u8 = switch (builtin.os.tag) { const test_uri = try ctx.addDocument(source);
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
try ctx.requestDidOpen(test_uri, source);
const params = types.DocumentSymbolParams{ const params = types.DocumentSymbolParams{
.textDocument = .{ .uri = test_uri }, .textDocument = .{ .uri = test_uri },

View File

@ -183,7 +183,7 @@ test "foldingRange - call" {
\\extern fn foo(a: bool, b: ?usize) void; \\extern fn foo(a: bool, b: ?usize) void;
\\const result = foo( \\const result = foo(
\\ false, \\ false,
\\ null, \\ null,
\\); \\);
, &.{ , &.{
.{ .startLine = 1, .startCharacter = 19, .endLine = 4, .endCharacter = 0 }, .{ .startLine = 1, .startCharacter = 19, .endLine = 4, .endCharacter = 0 },
@ -205,12 +205,7 @@ fn testFoldingRange(source: []const u8, expect: []const types.FoldingRange) !voi
var ctx = try Context.init(); var ctx = try Context.init();
defer ctx.deinit(); defer ctx.deinit();
const test_uri: []const u8 = switch (builtin.os.tag) { const test_uri = try ctx.addDocument(source);
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
try ctx.requestDidOpen(test_uri, source);
const params = types.FoldingRangeParams{ .textDocument = .{ .uri = test_uri } }; const params = types.FoldingRangeParams{ .textDocument = .{ .uri = test_uri } };

View File

@ -73,12 +73,7 @@ fn testInlayHints(source: []const u8) !void {
var ctx = try Context.init(); var ctx = try Context.init();
defer ctx.deinit(); defer ctx.deinit();
const test_uri: []const u8 = switch (builtin.os.tag) { const test_uri = try ctx.addDocument(phr.new_source);
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
try ctx.requestDidOpen(test_uri, phr.new_source);
const range = types.Range{ const range = types.Range{
.start = types.Position{ .line = 0, .character = 0 }, .start = types.Position{ .line = 0, .character = 0 },
@ -120,10 +115,10 @@ fn testInlayHints(source: []const u8) !void {
for (hints) |hint| { for (hints) |hint| {
if (position.line != hint.position.line or position.character != hint.position.character) continue; if (position.line != hint.position.line or position.character != hint.position.character) continue;
if(!std.mem.endsWith(u8, hint.label, ":")) { if (!std.mem.endsWith(u8, hint.label, ":")) {
try error_builder.msgAtLoc("label `{s}` must end with a colon!", new_loc, .err, .{ hint.label }); try error_builder.msgAtLoc("label `{s}` must end with a colon!", new_loc, .err, .{hint.label});
} }
const actual_label = hint.label[0..hint.label.len - 1]; const actual_label = hint.label[0 .. hint.label.len - 1];
if (!std.mem.eql(u8, expected_label, actual_label)) { if (!std.mem.eql(u8, expected_label, actual_label)) {
try error_builder.msgAtLoc("expected label `{s}` here but got `{s}`!", new_loc, .err, .{ expected_label, actual_label }); try error_builder.msgAtLoc("expected label `{s}` here but got `{s}`!", new_loc, .err, .{ expected_label, actual_label });

View File

@ -115,10 +115,6 @@ test "references - label" {
} }
fn testReferences(source: []const u8) !void { fn testReferences(source: []const u8) !void {
const file_uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
const new_name = "placeholder"; const new_name = "placeholder";
var phr = try helper.collectReplacePlaceholders(allocator, source, new_name); var phr = try helper.collectReplacePlaceholders(allocator, source, new_name);
@ -127,7 +123,7 @@ fn testReferences(source: []const u8) !void {
var ctx = try Context.init(); var ctx = try Context.init();
defer ctx.deinit(); defer ctx.deinit();
try ctx.requestDidOpen(file_uri, phr.new_source); const file_uri = try ctx.addDocument(phr.new_source);
try std.testing.expect(phr.locations.len != 0); try std.testing.expect(phr.locations.len != 0);

View File

@ -31,12 +31,7 @@ fn testSelectionRange(source: []const u8, want: []const []const u8) !void {
var ctx = try Context.init(); var ctx = try Context.init();
defer ctx.deinit(); defer ctx.deinit();
const test_uri: []const u8 = switch (builtin.os.tag) { const test_uri = try ctx.addDocument(phr.new_source);
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
try ctx.requestDidOpen(test_uri, phr.new_source);
const position = offsets.locToRange(phr.new_source, phr.locations.items(.new)[0], .@"utf-16").start; const position = offsets.locToRange(phr.new_source, phr.locations.items(.new)[0], .@"utf-16").start;

View File

@ -44,16 +44,11 @@ test "semantic tokens - string literals" {
); );
} }
const file_uri = switch (builtin.os.tag) {
.windows => "file:///C:/test.zig",
else => "file:///test.zig",
};
fn testSemanticTokens(source: []const u8, expected: []const u32) !void { fn testSemanticTokens(source: []const u8, expected: []const u32) !void {
var ctx = try Context.init(); var ctx = try Context.init();
defer ctx.deinit(); defer ctx.deinit();
try ctx.requestDidOpen(file_uri, source); const file_uri = try ctx.addDocument(source);
const Response = struct { const Response = struct {
data: []const u32, data: []const u32,
@ -62,9 +57,12 @@ fn testSemanticTokens(source: []const u8, expected: []const u32) !void {
const expected_bytes = try std.json.stringifyAlloc(allocator, Response{ .data = expected }, .{}); const expected_bytes = try std.json.stringifyAlloc(allocator, Response{ .data = expected }, .{});
defer allocator.free(expected_bytes); defer allocator.free(expected_bytes);
const params = try std.json.stringifyAlloc(allocator, .{ .textDocument = .{ .uri = file_uri } }, .{});
defer allocator.free(params);
try ctx.request( try ctx.request(
"textDocument/semanticTokens/full", "textDocument/semanticTokens/full",
"{\"textDocument\":{\"uri\":\"" ++ file_uri ++ "\"}}", params,
expected_bytes, expected_bytes,
); );
} }