Remove some boilerplate from test (#988)
This commit is contained in:
parent
0e5e1fdb8a
commit
0d3b0e9965
@ -1,5 +1,6 @@
|
||||
const std = @import("std");
|
||||
const zls = @import("zls");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const tres = @import("tres");
|
||||
|
||||
@ -8,7 +9,6 @@ const Config = zls.Config;
|
||||
const Server = zls.Server;
|
||||
const types = zls.types;
|
||||
|
||||
|
||||
/// initialize request taken from Visual Studio Code with the following changes:
|
||||
/// - removed locale, rootPath, rootUri, trace, workspaceFolders
|
||||
/// - removed capabilities.workspace.configuration
|
||||
@ -149,7 +149,12 @@ pub const Context = struct {
|
||||
}
|
||||
|
||||
// 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{
|
||||
.textDocument = .{
|
||||
.uri = uri,
|
||||
@ -160,7 +165,9 @@ pub const Context = struct {
|
||||
};
|
||||
const params = try std.json.stringifyAlloc(allocator, open_document, .{});
|
||||
defer allocator.free(params);
|
||||
|
||||
try self.notification("textDocument/didOpen", params);
|
||||
return uri;
|
||||
}
|
||||
|
||||
pub fn Response(comptime Result: type) type {
|
||||
|
@ -400,12 +400,7 @@ fn testCompletion(source: []const u8, expected_completions: []const Completion)
|
||||
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, text);
|
||||
const test_uri = try ctx.addDocument(text);
|
||||
|
||||
const params = types.CompletionParams{
|
||||
.textDocument = .{ .uri = test_uri },
|
||||
|
@ -53,12 +53,7 @@ fn testDefinition(source: []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, phr.new_source);
|
||||
const test_uri = try ctx.addDocument(phr.new_source);
|
||||
|
||||
const params = types.TextDocumentPositionParams{
|
||||
.textDocument = .{ .uri = test_uri },
|
||||
|
@ -39,12 +39,7 @@ 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 test_uri = try ctx.addDocument(source);
|
||||
|
||||
const params = types.DocumentSymbolParams{
|
||||
.textDocument = .{ .uri = test_uri },
|
||||
|
@ -205,12 +205,7 @@ fn testFoldingRange(source: []const u8, expect: []const types.FoldingRange) !voi
|
||||
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 test_uri = try ctx.addDocument(source);
|
||||
|
||||
const params = types.FoldingRangeParams{ .textDocument = .{ .uri = test_uri } };
|
||||
|
||||
|
@ -73,12 +73,7 @@ fn testInlayHints(source: []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, phr.new_source);
|
||||
const test_uri = try ctx.addDocument(phr.new_source);
|
||||
|
||||
const range = types.Range{
|
||||
.start = types.Position{ .line = 0, .character = 0 },
|
||||
|
@ -115,10 +115,6 @@ test "references - label" {
|
||||
}
|
||||
|
||||
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";
|
||||
|
||||
var phr = try helper.collectReplacePlaceholders(allocator, source, new_name);
|
||||
@ -127,7 +123,7 @@ fn testReferences(source: []const u8) !void {
|
||||
var ctx = try Context.init();
|
||||
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);
|
||||
|
||||
|
@ -31,12 +31,7 @@ fn testSelectionRange(source: []const u8, want: []const []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, phr.new_source);
|
||||
const test_uri = try ctx.addDocument(phr.new_source);
|
||||
|
||||
const position = offsets.locToRange(phr.new_source, phr.locations.items(.new)[0], .@"utf-16").start;
|
||||
|
||||
|
@ -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 {
|
||||
var ctx = try Context.init();
|
||||
defer ctx.deinit();
|
||||
|
||||
try ctx.requestDidOpen(file_uri, source);
|
||||
const file_uri = try ctx.addDocument(source);
|
||||
|
||||
const Response = struct {
|
||||
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 }, .{});
|
||||
defer allocator.free(expected_bytes);
|
||||
|
||||
const params = try std.json.stringifyAlloc(allocator, .{ .textDocument = .{ .uri = file_uri } }, .{});
|
||||
defer allocator.free(params);
|
||||
|
||||
try ctx.request(
|
||||
"textDocument/semanticTokens/full",
|
||||
"{\"textDocument\":{\"uri\":\"" ++ file_uri ++ "\"}}",
|
||||
params,
|
||||
expected_bytes,
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user