Skip to content

Commit

Permalink
more DiagnosticsCollection test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Dec 1, 2024
1 parent 9f08c56 commit 892dad3
Showing 1 changed file with 67 additions and 7 deletions.
74 changes: 67 additions & 7 deletions src/DiagnosticsCollection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,49 @@ fn errorBundleSourceLocationToRange(
};
}

test errorBundleSourceLocationToRange {
var eb = try createTestingErrorBundle(&.{
.{
.message = "First Error",
.source_location = .{
.src_path = "",
.line = 2,
.column = 6,
.span_start = 14,
.span_main = 14,
.span_end = 17,
.source_line = "const foo = 5",
},
},
.{
.message = "Second Error",
.source_location = .{
.src_path = "",
.line = 1,
.column = 4,
.span_start = 20,
.span_main = 23,
.span_end = 25,
.source_line = null,
},
},
});
defer eb.deinit(std.testing.allocator);

const src_loc0 = eb.getSourceLocation(eb.getErrorMessage(eb.getMessages()[0]).src_loc);
const src_loc1 = eb.getSourceLocation(eb.getErrorMessage(eb.getMessages()[1]).src_loc);

try std.testing.expectEqual(lsp.types.Range{
.start = .{ .line = 2, .character = 6 },
.end = .{ .line = 2, .character = 9 },
}, errorBundleSourceLocationToRange(eb, src_loc0, .@"utf-8"));

try std.testing.expectEqual(lsp.types.Range{
.start = .{ .line = 1, .character = 1 },
.end = .{ .line = 1, .character = 6 },
}, errorBundleSourceLocationToRange(eb, src_loc1, .@"utf-8"));
}

test {
var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_allocator.deinit();
Expand All @@ -321,13 +364,18 @@ test {
var eb3 = try createTestingErrorBundle(&.{.{ .message = "As" }});
defer eb3.deinit(std.testing.allocator);

const uri = switch (@import("builtin").os.tag) {
.windows => "file:///C:\\sample.zig",
else => "file:///sample.zig",
};

{
try collection.pushErrorBundle(.parse, 1, null, eb1);
try std.testing.expectEqual(1, collection.outdated_files.count());
try std.testing.expectEqualStrings("file:///sample.zig", collection.outdated_files.keys()[0]);
try std.testing.expectEqualStrings(uri, collection.outdated_files.keys()[0]);

var diagnostics: std.ArrayListUnmanaged(lsp.types.Diagnostic) = .{};
try collection.collectLspDiagnosticsForDocument("file:///sample.zig", .@"utf-8", arena, &diagnostics);
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);

try std.testing.expectEqual(1, diagnostics.items.len);
try std.testing.expectEqual(lsp.types.DiagnosticSeverity.Error, diagnostics.items[0].severity);
Expand All @@ -339,7 +387,7 @@ test {
try collection.pushErrorBundle(.parse, 0, null, eb2);

var diagnostics: std.ArrayListUnmanaged(lsp.types.Diagnostic) = .{};
try collection.collectLspDiagnosticsForDocument("file:///sample.zig", .@"utf-8", arena, &diagnostics);
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);

try std.testing.expectEqual(1, diagnostics.items.len);
try std.testing.expectEqualStrings("Living For The City", diagnostics.items[0].message);
Expand All @@ -349,7 +397,7 @@ test {
try collection.pushErrorBundle(.parse, 2, null, eb2);

var diagnostics: std.ArrayListUnmanaged(lsp.types.Diagnostic) = .{};
try collection.collectLspDiagnosticsForDocument("file:///sample.zig", .@"utf-8", arena, &diagnostics);
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);

try std.testing.expectEqual(1, diagnostics.items.len);
try std.testing.expectEqualStrings("You Haven't Done Nothin'", diagnostics.items[0].message);
Expand All @@ -359,10 +407,22 @@ test {
try collection.pushErrorBundle(.parse, 3, null, .empty);

var diagnostics: std.ArrayListUnmanaged(lsp.types.Diagnostic) = .{};
try collection.collectLspDiagnosticsForDocument("file:///sample.zig", .@"utf-8", arena, &diagnostics);
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);

try std.testing.expectEqual(0, diagnostics.items.len);
}

{
try collection.pushErrorBundle(@enumFromInt(16), 4, null, eb2);
try collection.pushErrorBundle(@enumFromInt(17), 4, null, eb3);

var diagnostics: std.ArrayListUnmanaged(lsp.types.Diagnostic) = .{};
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);

try std.testing.expectEqual(2, diagnostics.items.len);
try std.testing.expectEqualStrings("You Haven't Done Nothin'", diagnostics.items[0].message);
try std.testing.expectEqualStrings("As", diagnostics.items[1].message);
}
}

fn createTestingErrorBundle(messages: []const struct {
Expand All @@ -375,7 +435,7 @@ fn createTestingErrorBundle(messages: []const struct {
span_start: u32,
span_main: u32,
span_end: u32,
source_line: []const u8,
source_line: ?[]const u8,
} = .{ .src_path = "/sample.zig", .line = 0, .column = 0, .span_start = 0, .span_main = 0, .span_end = 0, .source_line = "" },
}) error{OutOfMemory}!std.zig.ErrorBundle {
var eb: std.zig.ErrorBundle.Wip = undefined;
Expand All @@ -393,7 +453,7 @@ fn createTestingErrorBundle(messages: []const struct {
.span_start = msg.source_location.span_start,
.span_main = msg.source_location.span_main,
.span_end = msg.source_location.span_end,
.source_line = try eb.addString(msg.source_location.source_line),
.source_line = if (msg.source_location.source_line) |source_line| try eb.addString(source_line) else 0,
}),
});
}
Expand Down

0 comments on commit 892dad3

Please sign in to comment.