Remove some boilerplate from test (#988)
This commit is contained in:
parent
0e5e1fdb8a
commit
0d3b0e9965
@ -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 {
|
||||||
|
@ -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 },
|
||||||
|
@ -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 },
|
||||||
|
@ -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 },
|
||||||
|
@ -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 } };
|
||||||
|
|
||||||
|
@ -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 },
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
@ -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,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user