Skip to content

Commit 892dad3

Browse files
committed
more DiagnosticsCollection test coverage
1 parent 9f08c56 commit 892dad3

File tree

1 file changed

+67
-7
lines changed

1 file changed

+67
-7
lines changed

src/DiagnosticsCollection.zig

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,49 @@ fn errorBundleSourceLocationToRange(
303303
};
304304
}
305305

306+
test errorBundleSourceLocationToRange {
307+
var eb = try createTestingErrorBundle(&.{
308+
.{
309+
.message = "First Error",
310+
.source_location = .{
311+
.src_path = "",
312+
.line = 2,
313+
.column = 6,
314+
.span_start = 14,
315+
.span_main = 14,
316+
.span_end = 17,
317+
.source_line = "const foo = 5",
318+
},
319+
},
320+
.{
321+
.message = "Second Error",
322+
.source_location = .{
323+
.src_path = "",
324+
.line = 1,
325+
.column = 4,
326+
.span_start = 20,
327+
.span_main = 23,
328+
.span_end = 25,
329+
.source_line = null,
330+
},
331+
},
332+
});
333+
defer eb.deinit(std.testing.allocator);
334+
335+
const src_loc0 = eb.getSourceLocation(eb.getErrorMessage(eb.getMessages()[0]).src_loc);
336+
const src_loc1 = eb.getSourceLocation(eb.getErrorMessage(eb.getMessages()[1]).src_loc);
337+
338+
try std.testing.expectEqual(lsp.types.Range{
339+
.start = .{ .line = 2, .character = 6 },
340+
.end = .{ .line = 2, .character = 9 },
341+
}, errorBundleSourceLocationToRange(eb, src_loc0, .@"utf-8"));
342+
343+
try std.testing.expectEqual(lsp.types.Range{
344+
.start = .{ .line = 1, .character = 1 },
345+
.end = .{ .line = 1, .character = 6 },
346+
}, errorBundleSourceLocationToRange(eb, src_loc1, .@"utf-8"));
347+
}
348+
306349
test {
307350
var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
308351
defer arena_allocator.deinit();
@@ -321,13 +364,18 @@ test {
321364
var eb3 = try createTestingErrorBundle(&.{.{ .message = "As" }});
322365
defer eb3.deinit(std.testing.allocator);
323366

367+
const uri = switch (@import("builtin").os.tag) {
368+
.windows => "file:///C:\\sample.zig",
369+
else => "file:///sample.zig",
370+
};
371+
324372
{
325373
try collection.pushErrorBundle(.parse, 1, null, eb1);
326374
try std.testing.expectEqual(1, collection.outdated_files.count());
327-
try std.testing.expectEqualStrings("file:///sample.zig", collection.outdated_files.keys()[0]);
375+
try std.testing.expectEqualStrings(uri, collection.outdated_files.keys()[0]);
328376

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

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

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

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

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

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

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

364412
try std.testing.expectEqual(0, diagnostics.items.len);
365413
}
414+
415+
{
416+
try collection.pushErrorBundle(@enumFromInt(16), 4, null, eb2);
417+
try collection.pushErrorBundle(@enumFromInt(17), 4, null, eb3);
418+
419+
var diagnostics: std.ArrayListUnmanaged(lsp.types.Diagnostic) = .{};
420+
try collection.collectLspDiagnosticsForDocument(uri, .@"utf-8", arena, &diagnostics);
421+
422+
try std.testing.expectEqual(2, diagnostics.items.len);
423+
try std.testing.expectEqualStrings("You Haven't Done Nothin'", diagnostics.items[0].message);
424+
try std.testing.expectEqualStrings("As", diagnostics.items[1].message);
425+
}
366426
}
367427

368428
fn createTestingErrorBundle(messages: []const struct {
@@ -375,7 +435,7 @@ fn createTestingErrorBundle(messages: []const struct {
375435
span_start: u32,
376436
span_main: u32,
377437
span_end: u32,
378-
source_line: []const u8,
438+
source_line: ?[]const u8,
379439
} = .{ .src_path = "/sample.zig", .line = 0, .column = 0, .span_start = 0, .span_main = 0, .span_end = 0, .source_line = "" },
380440
}) error{OutOfMemory}!std.zig.ErrorBundle {
381441
var eb: std.zig.ErrorBundle.Wip = undefined;
@@ -393,7 +453,7 @@ fn createTestingErrorBundle(messages: []const struct {
393453
.span_start = msg.source_location.span_start,
394454
.span_main = msg.source_location.span_main,
395455
.span_end = msg.source_location.span_end,
396-
.source_line = try eb.addString(msg.source_location.source_line),
456+
.source_line = if (msg.source_location.source_line) |source_line| try eb.addString(source_line) else 0,
397457
}),
398458
});
399459
}

0 commit comments

Comments
 (0)