summaryrefslogtreecommitdiff
path: root/core/fuzz.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/fuzz.zig')
-rw-r--r--core/fuzz.zig98
1 files changed, 98 insertions, 0 deletions
diff --git a/core/fuzz.zig b/core/fuzz.zig
index f324a48e..23d007ac 100644
--- a/core/fuzz.zig
+++ b/core/fuzz.zig
@@ -771,3 +771,101 @@ test "9P: a rows style past the syntax's kinds is refused, never drawn" {
_ = try p.render(p.frame_arena.allocator());
}
}
+
+// ---------------------------------------------------------------------------
+// (v, vi) Numbers in a file's text and in another program's answer: a
+// diff's hunk headers (a `.diff` drawn, the patch view, a look's target)
+// and a language server's rows (each kind's answer landed, stepped, drawn;
+// the same places typed to Look).
+
+const diff = @import("diff.zig");
+const hunks = @import("hunks.zig");
+const lsp = pardes.lsp;
+
+/// Around what an int of each width holds, and past it.
+const numbers = [_][]const u8{ "0", "1", "2", "3", "5", "65535", "2147483647", "2147483648", "3000000000", "4294967295", "4294967296", "9223372036854775807", "9223372036854775808", "18446744073709551615", "18446744073709551616", "99999999999999999999999" };
+
+fn draw(p: *Pardes) !void {
+ p.sync();
+ _ = p.frame_arena.reset(.retain_capacity);
+ _ = try p.render(p.frame_arena.allocator());
+}
+
+fn diffSeed(gpa: std.mem.Allocator, seed: u64) !void {
+ var prng: Random.DefaultPrng = .init(seed);
+ const r = prng.random();
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(gpa);
+ try text.appendSlice(gpa, "diff --git a/x.zig b/x.zig\n--- a/x.zig\n+++ b/x.zig\n");
+ for (0..1 + r.uintLessThan(usize, 3)) |_| {
+ try text.print(gpa, "@@ -{s}", .{pick(r, numbers)});
+ if (chance(r, 0.7)) try text.print(gpa, ",{s}", .{pick(r, numbers)});
+ try text.print(gpa, " +{s}", .{pick(r, numbers)});
+ if (chance(r, 0.7)) try text.print(gpa, ",{s}", .{pick(r, numbers)});
+ try text.appendSlice(gpa, " @@ fn x()\n");
+ const lines = [_][]const u8{ " const a = 1;\n", "-const b = 2;\n", "+const b = 3;\n", "\\ No newline at end of file\n", "\n", "--- y\n", "+++ y\n", "text\n" };
+ for (0..r.uintLessThan(usize, 9)) |_| try text.appendSlice(gpa, pick(r, lines));
+ }
+ // A `.diff` drawn.
+ const p = try th.withFile(gpa, text.items);
+ defer p.deinit();
+ const pane = p.panes[p.view().active].?;
+ gpa.free(pane.file.?.path);
+ pane.file.?.path = try gpa.dupe(u8, "/change.diff");
+ pane.file.?.syntax_dirty = true;
+ try draw(p);
+ // The patch view.
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const ctx: hunks.Ctx = .{ .arena = arena.allocator() };
+ _ = hunks.format(&ctx, "", text.items) catch {};
+ // A look's target, at every row.
+ var rows: std.ArrayList([]const u8) = .empty;
+ defer rows.deinit(gpa);
+ var it = std.mem.splitScalar(u8, text.items, '\n');
+ while (it.next()) |row| try rows.append(gpa, row);
+ for (0..rows.items.len) |row| _ = diff.targetAt(rows.items, row, false);
+}
+
+test "a diff's hunk headers with numbers past any file's: drawn, formatted and looked at, never a panic" {
+ for (seeds) |seed| for (0..4) |k| try diffSeed(testing.allocator, seed *% 31 +% k);
+}
+
+fn lspSeed(gpa: std.mem.Allocator, seed: u64) !void {
+ var prng: Random.DefaultPrng = .init(seed);
+ const r = prng.random();
+ const p = try th.withFile(gpa, "alpha one\n\tbeta\n\t\tgamma \xe6\x97\xa5\n\n");
+ defer p.deinit();
+ var rows: std.ArrayList(u8) = .empty;
+ defer rows.deinit(gpa);
+ for (0..1 + r.uintLessThan(usize, 4)) |_| {
+ // As lsp.sourceSpanRow writes them; the file's own name.
+ try rows.print(gpa, "/test.txt:{s}:{s}", .{ pick(r, numbers), pick(r, numbers) });
+ if (chance(r, 0.5)) try rows.print(gpa, "-{s}", .{pick(r, numbers)});
+ try rows.appendSlice(gpa, " alpha\n");
+ }
+ const kinds = [_]lsp.Kind{ .definition, .declaration, .implementation, .references, .diagnostics, .document_symbols, .workspace_symbols };
+ const kind = pick(r, kinds);
+ if (lsp.supports.contains(kind)) {
+ p.lspRequest(0, kind, "");
+ while (p.nextEffect()) |_| {}
+ if (p.lsp_ask.wait) |wait| p.update(.{ .lsp_resp = .{ .id = wait.id, .rows = rows.items } });
+ try draw(p);
+ for ("nNjk\r") |key| {
+ p.update(.{ .key = .{ .cp = if (key == '\r') pardes.Key.enter else key } });
+ try draw(p);
+ }
+ }
+ // The same places typed.
+ var look: [96]u8 = undefined;
+ p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}:{s}", .{ pick(r, numbers), pick(r, numbers) }) });
+ try draw(p);
+ p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}:{s}-{s}:{s}", .{ pick(r, numbers), pick(r, numbers), pick(r, numbers), pick(r, numbers) }) });
+ try draw(p);
+ p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}-{s}", .{ pick(r, numbers), pick(r, numbers) }) });
+ try draw(p);
+}
+
+test "a language server's rows and a typed Look with numbers past any file's: landed, stepped and drawn, never a panic" {
+ for (seeds) |seed| for (0..4) |k| try lspSeed(testing.allocator, seed *% 31 +% k);
+}