From 52bfa3f2aa8f8158d4d7ca720d81ea0c654f94bf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:28:16 -0300 Subject: test: three of src/pardes.zig's tests through the 9P tree alone: in test/behaviour/columns_and_tags.zig a column tag lighting the word under the mouse, taking the caret where pressed, sweeping three cells in the selection's colour, typed at its caret (Home, Help) and scrolling its text from the column's edge, never its caret (each column); in test/behaviour/mouse.zig a click on either half of a wide character landing before it in the text (dot) and on a tag's path (a name drafted, Right stepping a grapheme, two cells then one), a two-byte λ typed in a tag and taken out by one backspace, and a sweep pressed on the line numbers taking only the text (unlit numbers, a Look in it finding its next place, a chord cutting it); Session's bg (a cell's background from /screen's styles); each checked to fail against a break (no tag hover, the header scrolled one cell too far, byteAtDisplay rounding a wide glyph's second half past it, Right stepping a byte in insert, the three clamps that keep a sweep's first row off the line numbers dropped) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/session.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'test/session.zig') diff --git a/test/session.zig b/test/session.zig index 16a131fd..5dd506ec 100644 --- a/test/session.zig +++ b/test/session.zig @@ -314,6 +314,18 @@ pub const Session = struct { var it = std.mem.splitScalar(u8, tag[lo + 2 .. close], '/'); return .{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, it.next() orelse return error.NoPage, 10) }; } + + // core2: + + /// The background /screen draws at a cell, as its JSON (`{"rgb":[255,255,255]}`). + pub fn bg(s: *Session, col: usize, row: usize) ![]const u8 { + const a = s.arena.allocator(); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{}); + const cols: usize = @intCast(json.object.get("cols").?.integer); + const cell = json.object.get("cells").?.array.items[row * cols + col]; + const style = json.object.get("styles").?.array.items[@intCast(cell.array.items[1].integer)]; + return std.json.Stringify.valueAlloc(a, style.object.get("bg").?, .{}); + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { -- cgit v1.3 From ff0b8a382de7a1994a6912dc71ba28a8ecdaf474 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 10:45:36 -0300 Subject: test: syntax colours and Save's prompt through the 9P tree alone, Session.styles added (each cell's style as /screen draws it, as JSON text): in test/behaviour/settings.zig Colors off drawing a source plain and on again colouring its fn and if, the layout as it was, an edit made while off included, and SyntaxBold keywords coloured and bold in every row a detached session's size grows a pane into; in panes.zig a rename colouring a source in its new name's language, its text unchanged; in keys.zig Save's prompt on a scratch made by New from a file's pane starting from its directory, one slash after it (/dír space/, and / alone); each checked to fail against a break (the Colors toggle not marking syntax dirty, the painted stretch four rows from the scroll, a rename not marking syntax dirty, the prompt's slash always added, the prompt from the session's directory); test/panes.zig's Colors and growing tests and pane.zig's cached-syntax rename test deleted, and the Save prompt test trimmed to an owned directory ending in a slash or empty (a shell's, T2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/pane.zig | 17 -------- test/behaviour/keys.zig | 15 +++++++ test/behaviour/panes.zig | 17 ++++++++ test/behaviour/settings.zig | 72 +++++++++++++++++++++++++++++++ test/panes.zig | 101 ++------------------------------------------ test/session.zig | 17 ++++++++ 6 files changed, 125 insertions(+), 114 deletions(-) (limited to 'test/session.zig') diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index cfc93ee5..28f1882d 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -1228,23 +1228,6 @@ test "writes under mark 0 are one undo step, even with no history before them" { try testing.expectEqualStrings("one\ntwo\nthree\n", rd(p, body, 0, 64).bytes); } -test "name refreshes cached syntax for unchanged contents" { - if (!pardes.syntax.enabled) return error.SkipZigTest; - pardes.syntax.start(testing.allocator); - defer pardes.syntax.stop(); - const p = try withFile(testing.allocator, "fn check() void {}\n"); - defer p.deinit(); - const pane = p.panes[0].?; - panes.File.refreshHighlights(p); - try testing.expectEqual(@as(usize, 0), pane.file.?.highlights.len); - try testing.expectEqual(Status.ok, wr(p, Node.of(pane.serial, .name), "renamed.zig\n").reply.status); - panes.File.refreshHighlights(p); - try testing.expect(pane.file.?.highlights.len >= 2); - try testing.expectEqual(@backingInt(pardes.syntax.Syn.keyword), pane.file.?.highlights[0]); - try testing.expectEqual(@backingInt(pardes.syntax.Syn.keyword), pane.file.?.highlights[1]); - try testing.expectEqualStrings("fn check() void {}\n", pane.file.?.content); -} - test "owned cwd name promotion releases the former directory" { const p = try th.withTerm(testing.allocator); defer p.deinit(); diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig index 41a2821a..989489d7 100644 --- a/test/behaviour/keys.zig +++ b/test/behaviour/keys.zig @@ -225,3 +225,18 @@ test "keys: SPC j n and SPC j p walk Themes' rows, whole, never its section head try s.keys("\t"); try std.testing.expectEqualStrings(names.items[brk - 1]["Theme ".len..], try setting(&s, "Theme")); } + +test "keys: Save's prompt on a scratch starts from the scratch's directory, one slash after it" { + for ([_][2][]const u8{ .{ "/dír space/f.txt", " Save /dír space/zq" }, .{ "/f.txt", " Save /zq" } }) |c| { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}\n", .{c[0]})); + // New from the file's pane: a scratch in its directory. + try s.write(s.at("/pane/{d}/exec", .{n}), "New\n"); + try std.testing.expect(try s.focused() != n); + try s.keys(":\tzq"); + _ = try s.where(c[1]); + try s.keys("\x1b"); + } +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 553b1ca9..9dfc6abd 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -548,3 +548,20 @@ test "panes: truncating body empties it, and a stat says the length after" { try s.write(body, "new text\n"); try std.testing.expectEqual(@as(u64, 9), (try s.stat(body)).size); } + +test "panes: a rename colours a source its new name's language names, its text unchanged" { + if (!@import("pardes_config").syntax_highlighting) return error.SkipZigTest; + var s = try Session.initFile(std.testing.allocator, "fn check() void {}\n"); + defer s.deinit(); + const n = try s.focused(); + const x, const y = try s.where("fn check"); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + const plain = try s.styles(); + try std.testing.expectEqualStrings(plain[y * cols + x], plain[y * cols + x + 3]); + try s.write(s.at("/pane/{d}/name", .{n}), "renamed.zig\n"); + const coloured = try s.styles(); + try std.testing.expectEqualStrings(coloured[y * cols + x], coloured[y * cols + x + 1]); + try std.testing.expect(!std.mem.eql(u8, coloured[y * cols + x], plain[y * cols + x])); + try s.expect(s.at("/pane/{d}/body", .{n}), "fn check() void {}\n"); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index ff24704a..5ba227bc 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -101,3 +101,75 @@ test "settings: Config without a configuration directory says so and opens nothi try s.expect("/index", index); try expectHas(try s.read("/log"), "Config: no startup file here: no configuration directory"); } + +const syntax = @import("pardes_config").syntax_highlighting; + +/// The style /screen draws the cell `into` cells into the first place +/// `text` is drawn. +fn styleOf(s: *Session, text: []const u8, into: usize) ![]const u8 { + const x, const y = try s.where(text); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + return (try s.styles())[y * cols + x + into]; +} + +/// How many ` fn ` and ` if ` keywords the screen draws, each of whose two +/// cells must be drawn in `want`. +fn keywordsIn(s: *Session, want: []const u8) !usize { + const screen = try s.screen(); + const styles = try s.styles(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + var seen: usize = 0; + var rows = std.mem.splitScalar(u8, screen, '\n'); + var y: usize = 0; + while (rows.next()) |row| : (y += 1) for ([_][]const u8{ " fn ", " if " }) |word| { + var from: usize = 0; + while (std.mem.indexOfPos(u8, row, from, word)) |i| : (from = i + 1) { + const x = try std.unicode.utf8CountCodepoints(row[0..i]) + 1; + for (0..2) |k| try std.testing.expectEqualStrings(want, styles[y * cols + x + k]); + seen += 1; + } + }; + return seen; +} + +test "settings: Colors off draws a source plain, and on again colours its keywords, the layout as it was, an edit made while off included" { + if (!syntax) return error.SkipZigTest; + for ([_]bool{ false, true }) |edit_while_off| { + var s = try Session.initFile(std.testing.allocator, "pub fn first() void { if (true) {} }\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), "/colors.zig\n"); + var ident: []const u8 = "first"; + if (edit_while_off) { + try std.testing.expect(!std.mem.eql(u8, try styleOf(&s, " fn ", 1), try styleOf(&s, "first", 0))); + try s.write("/ctl", "Colors off\n"); + try s.replace(s.at("/pane/{d}/body", .{n}), "pub fn edited() void { if (false) {} }\n"); + ident = "edited"; + } else try s.write("/ctl", "Colors off\n"); + try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, try styleOf(&s, ident, 0))); + const layout = try s.read("/layout"); + try s.write("/ctl", "Colors on\n"); + try s.expect("/layout", layout); + const kw = try styleOf(&s, " fn ", 1); + try std.testing.expect(!std.mem.eql(u8, kw, try styleOf(&s, ident, 0))); + try std.testing.expect(!has(kw, "\"bold\":true")); + try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, kw)); + } +} + +test "settings: SyntaxBold keywords stay coloured and bold in every row a pane grows into" { + if (!syntax) return error.SkipZigTest; + var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*)))); + defer s.deinit(); + try s.write(s.at("/pane/{d}/name", .{try s.focused()}), "/resize.zig\n"); + try s.write("/ctl", "SyntaxBold on\n"); + s.detach(); + try s.write("/ctl", "size 80 8\n"); + const kw = try styleOf(&s, " fn ", 1); + try std.testing.expect(has(kw, "\"bold\":true")); + for ([_]usize{ 50, 12, 70 }) |rows| { + try s.write("/ctl", s.at("size 80 {d}\n", .{rows})); + try std.testing.expect(try keywordsIn(&s, kw) >= 2 * (rows - 4)); + } +} diff --git a/test/panes.zig b/test/panes.zig index db816d41..c996ca00 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -1397,11 +1397,13 @@ test "a prompt that cannot be opened leaves the open one as it was" { } } -test "Save's prompt starts from the pane's directory" { +// A file's directory is test/behaviour/keys.zig's; an owned directory (a +// shell's) ending in a slash, or none, only a real shell gives. +test "Save's prompt starts from an owned directory ending in a slash, or none" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); const pane = try p.setTestFile("one\n"); - for ([_][]const u8{ "/dír space", "/dír space/", "" }) |dir| { + for ([_][]const u8{ "/dír space/", "" }) |dir| { try pane.setOwnedCwd(dir); const slash = if (dir.len == 0 or dir[dir.len - 1] != '/') "/" else ""; p.startPrompt(pane, &pane.body, .save); @@ -3772,101 +3774,6 @@ test "cancelled normal gestures leave no count or subprefix through raw tty mode } } -test "Colors restores syntax after rendering disabled files and disabled edits" { - if (!syntax.enabled) return error.SkipZigTest; - const gpa = std.testing.allocator; - for ([_]bool{ false, true }) |edit_while_disabled| { - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("pub fn first() void { if (true) {} }\n"); - const file = &pane.file.?; - gpa.free(file.path); - file.path = try gpa.dupe(u8, "/colors.zig"); - p.settings.colors = edit_while_disabled; - p.presentation.enabled = false; - var frame = std.heap.ArenaAllocator.init(gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - if (edit_while_disabled) { - try std.testing.expect(file.highlights.len > 0); - p.update(.{ .command = "Colors" }); - panes.File.setContent(p, file, try gpa.dupe(u8, "pub fn edited() void { if (false) {} }\n")); - _ = frame.reset(.retain_capacity); - _ = try p.render(frame.allocator()); - } - try std.testing.expect(!p.settings.colors); - try std.testing.expectEqual(@as(usize, 0), file.highlights.len); - try std.testing.expect(!file.syntax_dirty); - const rect = p.rects[0]; - const cols = pane.cols; - const rows = pane.rows; - p.update(.{ .command = "Colors" }); - try std.testing.expectEqual(rect, p.rects[0]); - try std.testing.expectEqual(cols, pane.cols); - try std.testing.expectEqual(rows, pane.rows); - _ = frame.reset(.retain_capacity); - const surface = try p.render(frame.allocator()); - try std.testing.expect(p.settings.colors); - try std.testing.expect(file.highlights.len > 0); - try std.testing.expect(!file.syntax_dirty); - var seen: usize = 0; - for (surface.cells[0 .. surface.cells.len - 1], 0..) |*first, i| { - const second = &surface.cells[i + 1]; - if (!(std.mem.eql(u8, first.grapheme(), "f") and std.mem.eql(u8, second.grapheme(), "n")) and - !(std.mem.eql(u8, first.grapheme(), "i") and std.mem.eql(u8, second.grapheme(), "f"))) continue; - seen += 1; - for ([_]*pardes.Cell{ first, second }) |cell| { - try std.testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, cell.style.fg); - try std.testing.expect(!cell.style.bold); - } - } - try std.testing.expectEqual(@as(usize, 2), seen); - } -} - -test "growing a file pane colors every newly visible Zig keyword" { - if (!syntax.enabled) return error.SkipZigTest; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 8 }); - defer p.deinit(); - const pane = try p.setTestFile(std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*)))); - const file = &pane.file.?; - gpa.free(file.path); - file.path = try gpa.dupe(u8, "/resize.zig"); - p.update(.{ .command = "SyntaxBold" }); - try std.testing.expect(p.settings.syntax_bold); - p.settings.colors = true; - p.presentation.enabled = false; - var frame = std.heap.ArenaAllocator.init(gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const initial_end = file.highlight_start + file.highlights.len; - for ([_]u16{ 50, 12, 70 }) |rows| { - p.update(.{ .resize = .{ .cols = 80, .rows = rows } }); - _ = frame.reset(.retain_capacity); - const surface = try p.render(frame.allocator()); - const rect = p.rects[0]; - const body_y = bodyY(p, rect); - var seen: usize = 0; - for (body_y..body_y + bodyH(p, rect)) |y| { - for (rect.x + config.GUTTER..rect.x + rect.w - 1) |x| { - const first = surface.at(@intCast(x), @intCast(y)); - const second = surface.at(@intCast(x + 1), @intCast(y)); - if (!(std.mem.eql(u8, first.grapheme(), "f") and std.mem.eql(u8, second.grapheme(), "n")) and - !(std.mem.eql(u8, first.grapheme(), "i") and std.mem.eql(u8, second.grapheme(), "f"))) continue; - seen += 1; - for ([_]*pardes.Cell{ first, second }) |cell| { - try std.testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, cell.style.fg); - try std.testing.expect(cell.style.bold); - } - } - } - try std.testing.expect(seen >= 2 * (rows - 4)); - try std.testing.expect(file.highlight_start + file.highlights.len > initial_end); - try std.testing.expect(!file.syntax_dirty); - } -} - test "Zig keywords keep every rendered byte colored across line-number widths" { if (!syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; diff --git a/test/session.zig b/test/session.zig index 16a131fd..6f36785c 100644 --- a/test/session.zig +++ b/test/session.zig @@ -314,6 +314,23 @@ pub const Session = struct { var it = std.mem.splitScalar(u8, tag[lo + 2 .. close], '/'); return .{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, it.next() orelse return error.NoPage, 10) }; } + + // pane2: + + /// Each cell's style as /screen draws it, as JSON text (compare two + /// cells by it), row-major as `screen` draws the cells: row `y`'s cell + /// `x` is `[y * cols + x]`. + pub fn styles(s: *Session) ![]const []const u8 { + const a = s.arena.allocator(); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{}); + const kinds = json.object.get("styles").?.array.items; + const named = try a.alloc([]const u8, kinds.len); + for (kinds, named) |style, *text| text.* = try std.json.Stringify.valueAlloc(a, style, .{}); + const cells = json.object.get("cells").?.array.items; + const out = try a.alloc([]const u8, cells.len); + for (cells, out) |cell, *style| style.* = named[@intCast(cell.array.items[1].integer)]; + return out; + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { -- cgit v1.3