diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/behaviour.zig | 9 | ||||
| -rw-r--r-- | test/behaviour/addresses.zig | 65 | ||||
| -rw-r--r-- | test/behaviour/columns_and_tags.zig | 34 | ||||
| -rw-r--r-- | test/behaviour/edit.zig | 10 | ||||
| -rw-r--r-- | test/behaviour/event.zig | 55 | ||||
| -rw-r--r-- | test/behaviour/flags.zig | 42 | ||||
| -rw-r--r-- | test/behaviour/keys.zig | 227 | ||||
| -rw-r--r-- | test/behaviour/log.zig | 36 | ||||
| -rw-r--r-- | test/behaviour/look_and_exec.zig | 279 | ||||
| -rw-r--r-- | test/behaviour/mounts.zig | 25 | ||||
| -rw-r--r-- | test/behaviour/mouse.zig | 133 | ||||
| -rw-r--r-- | test/behaviour/other_files.zig | 52 | ||||
| -rw-r--r-- | test/behaviour/panes.zig | 202 | ||||
| -rw-r--r-- | test/behaviour/root_ctl.zig | 56 | ||||
| -rw-r--r-- | test/behaviour/rules.zig | 39 | ||||
| -rw-r--r-- | test/behaviour/settings.zig | 78 | ||||
| -rw-r--r-- | test/behaviour/tags.zig | 382 | ||||
| -rw-r--r-- | test/behaviour/tree.zig | 44 | ||||
| -rw-r--r-- | test/output.zig | 141 | ||||
| -rw-r--r-- | test/panes.zig | 1017 | ||||
| -rw-r--r-- | test/pdf.zig | 305 | ||||
| -rw-r--r-- | test/session.zig | 89 |
22 files changed, 1859 insertions, 1461 deletions
diff --git a/test/behaviour.zig b/test/behaviour.zig index 3049f648..d9b15183 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -3,16 +3,25 @@ //! named by the label first. This module imports test/session.zig alone, //! so a test sees the session as a 9P client does. test { + _ = @import("behaviour/addresses.zig"); _ = @import("behaviour/columns_and_tags.zig"); _ = @import("behaviour/edit.zig"); + _ = @import("behaviour/event.zig"); + _ = @import("behaviour/flags.zig"); + _ = @import("behaviour/keys.zig"); _ = @import("behaviour/limits.zig"); + _ = @import("behaviour/log.zig"); _ = @import("behaviour/look_and_exec.zig"); _ = @import("behaviour/modes.zig"); + _ = @import("behaviour/mounts.zig"); + _ = @import("behaviour/mouse.zig"); _ = @import("behaviour/other_files.zig"); _ = @import("behaviour/panes.zig"); _ = @import("behaviour/placement.zig"); _ = @import("behaviour/root_ctl.zig"); + _ = @import("behaviour/rules.zig"); _ = @import("behaviour/settings.zig"); _ = @import("behaviour/tags.zig"); _ = @import("behaviour/terminals.zig"); + _ = @import("behaviour/tree.zig"); } diff --git a/test/behaviour/addresses.zig b/test/behaviour/addresses.zig new file mode 100644 index 00000000..a1474c3d --- /dev/null +++ b/test/behaviour/addresses.zig @@ -0,0 +1,65 @@ +//! docs/typ/reference.typ, Addresses and data <addresses>. +const std = @import("std"); +const Session = @import("session").Session; +const expectEqual = std.testing.expectEqual; + +test "addresses: data and xdata read from addr, data moving it, and a write goes through it" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + const data = s.at("/pane/{d}/data", .{n}); + try s.write(addr, "#0"); + try std.testing.expectEqualStrings("one", try s.readAt(data, 0, 3)); + try expectEqual(.{ @as(u32, 3), @as(u32, 3) }, try s.range(addr)); + try s.write(addr, "1"); + try s.expect(s.at("/pane/{d}/xdata", .{n}), "one\n"); + try s.write(addr, "1"); + try s.expect(data, "one\ntwo\n"); + try s.write(addr, "1"); + try s.write(data, "ONE\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n"); + try expectEqual(@as(u32, 4), (try s.range(addr))[0]); +} + +test "addresses: sel reads the selection, and a write to sel replaces it and is selected" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + const dot = s.at("/pane/{d}/dot", .{n}); + const sel = s.at("/pane/{d}/sel", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + // Copying addr onto dot is the whole of acme's `dot=addr`, and back again + // of `addr=dot`; each range file reads exactly what the other takes. + try s.write(addr, "#0,#3"); + try s.write(dot, " 0 3 "); + try s.expect(dot, " 0 3 "); + try expectEqual(@as(u64, 24), (try s.stat(dot)).size); + try s.expect(sel, "one"); + try expectEqual(@as(u64, 3), (try s.stat(sel)).size); + try s.write(addr, "#0"); + try s.write(addr, " 0 3 "); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(addr)); + // A truncation empties dot once its open goes, nothing written after + // it; an address expression addresses it. + const h = try s.hold(dot, 1); + try s.truncate(h); + try s.expect(sel, "one"); + s.clunk(h); + try s.expect(sel, ""); + try s.write(dot, "#0,#3"); + try s.write(sel, "ONE"); + try s.expect(body, "ONE\ntwo\n"); + // What was written is selected, as acme's wrsel leaves it. + try s.expect(sel, "ONE"); + // A write of another open replaces it; one open's writes run on. + try s.write(sel, "!"); + try s.expect(body, "!\ntwo\n"); + const w = try s.hold(sel, 1); + try expectEqual(@as(u16, 0), try s.writeFid(w, "a")); + try expectEqual(@as(u16, 0), try s.writeFid(w, "b")); + s.clunk(w); + try s.expect(body, "ab\ntwo\n"); + try s.expect(sel, "ab"); +} diff --git a/test/behaviour/columns_and_tags.zig b/test/behaviour/columns_and_tags.zig index a67fa110..d485ee6e 100644 --- a/test/behaviour/columns_and_tags.zig +++ b/test/behaviour/columns_and_tags.zig @@ -199,3 +199,37 @@ test "columns-and-tags: Enter in a tag in normal mode runs the word under its cu try s.keys("\x1b\t"); try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/name", .{try s.focused()})), "/+Tutor\n")); } + +test "columns-and-tags: a tag write refuses NUL and the other control characters, the workspace's, a column's and a pane's" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const col = std.mem.sliceTo(try s.read("/layout"), ' '); + for ([_][]const u8{ "/tag", s.at("/col/{s}/tag", .{col}), s.at("/pane/{d}/tag", .{try s.focused()}) }) |tag| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(tag, " a\x00b")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid tag text")); + try std.testing.expectEqual(@as(u16, 22), try s.refused(tag, " \x7f")); + } +} + +test "columns-and-tags: a refused Delcol changes nothing: no pane, no focus" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Newcol\n"); + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{made}), "/tmp/pardes-delcol-refused.txt\n"); + try s.write(s.at("/pane/{d}/body", .{made}), "edit\n"); + // Its column, /index's last field. + const col = col: { + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |row| if (std.mem.startsWith(u8, row, s.at("{d} ", .{made}))) break :col row[std.mem.lastIndexOfScalar(u8, row, ' ').? + 1 ..]; + return error.NotListed; + }; + const index = try s.read("/index"); + const layout = try s.read("/layout"); + const focus = try s.read("/focus"); + try std.testing.expect(try s.refused(s.at("/col/{s}/ctl", .{col}), "Delcol\n") != 0); + try s.expect("/index", index); + try s.expect("/layout", layout); + try s.expect("/focus", focus); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unsaved ") != null); +} diff --git a/test/behaviour/edit.zig b/test/behaviour/edit.zig index e95471c6..3d1a731e 100644 --- a/test/behaviour/edit.zig +++ b/test/behaviour/edit.zig @@ -19,3 +19,13 @@ test "edit: a write of an Edit block carries it whole, to a pane's ctl, the root try s.expect(body, "<foo] x\n<foo] y\nend\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unmatched") != null); } + +test "edit: a bad command inside an Edit block written whole is named, not the } after it" { + var s = try Session.initFile(std.testing.allocator, "abc\n"); + defer s.deinit(); + const n = try s.focused(); + try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{n}), "Edit {\n,s/a/b/\nzq\n}\n") != 0); + try std.testing.expect(std.mem.indexOf(u8, s.said, "\"}\"") == null); + try std.testing.expect(std.mem.indexOf(u8, s.said, "z") != null); + try s.expect(s.at("/pane/{d}/body", .{n}), "abc\n"); +} diff --git a/test/behaviour/event.zig b/test/behaviour/event.zig new file mode 100644 index 00000000..8aebe8f4 --- /dev/null +++ b/test/behaviour/event.zig @@ -0,0 +1,55 @@ +//! docs/typ/reference.typ, event <event>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "event: a pane's own look and exec go to the event reader holding it, which writes them back to act" { + var s = try Session.initFile(std.testing.allocator, "Msg routed\n"); + defer s.deinit(); + const n = try s.focused(); + const ev = try s.hold(s.at("/pane/{d}/event", .{n}), 2); + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg routed\n"); + // Not done: the reader has it, as it would a middle click. + try std.testing.expect(!has(try s.read("/log"), "routed")); + const record = try s.readFid(ev); + // Flag 1: its first word is a builtin; no 2, no expansion follows. + try std.testing.expectEqualStrings("FX0 0 1 10 Msg routed\n", record); + // Written back, it is done. + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ev, record)); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} routed\n", .{n}))); + try s.write(s.at("/pane/{d}/look", .{n}), "one.txt:3\n"); + try std.testing.expectEqualStrings("FL0 0 4 9 one.txt:3\n", try s.readFid(ev)); + // The root's is a click at the pane with the keyboard, this one. + try s.write("/exec", "Msg rooted\n"); + try std.testing.expect(!has(try s.read("/log"), "rooted")); + try std.testing.expectEqualStrings("FX0 0 1 10 Msg rooted\n", try s.readFid(ev)); + // A click in its tag is the reader's too, an `x` record, as acme's is; + // written back, it is done. + try s.write(s.at("/pane/{d}/tagexec", .{n}), "Msg tagged\n"); + try std.testing.expect(!has(try s.read("/log"), "tagged")); + const tagged = try s.readFid(ev); + try std.testing.expectEqualStrings("Fx0 0 1 10 Msg tagged\n", tagged); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ev, tagged)); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} tagged\n", .{n}))); + s.clunk(ev); + // With no reader, the root's and the pane's act at once. + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg direct\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} direct\n", .{n}))); +} + +test "event: a refused Del written back through event fails that write, EIO, with its err" { + var s = try Session.initFile(std.testing.allocator, "text\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); // unsaved + const ev = try s.hold(s.at("/pane/{d}/event", .{n}), 2); + defer s.clunk(ev); + try s.write(s.at("/pane/{d}/exec", .{n}), "Del\n"); + const record = try s.readFid(ev); + try std.testing.expectEqual(@as(u16, 5), try s.writeFid(ev, record)); + try std.testing.expect(has(s.said, "Modified")); + _ = try s.read(s.at("/pane/{d}/ctl", .{n})); // still there +} diff --git a/test/behaviour/flags.zig b/test/behaviour/flags.zig new file mode 100644 index 00000000..656d219f --- /dev/null +++ b/test/behaviour/flags.zig @@ -0,0 +1,42 @@ +//! docs/typ/reference.typ, Flags and undo <flags>. +const std = @import("std"); +const Session = @import("session").Session; + +test "flags: dirty, mark and scroll read and write the state acme kept behind ctl verbs, 1 or 0" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const dirty = s.at("/pane/{d}/dirty", .{n}); + const mark = s.at("/pane/{d}/mark", .{n}); + const scroll = s.at("/pane/{d}/scroll", .{n}); + for ([_][]const u8{ dirty, mark, scroll }) |flag| { + try std.testing.expectEqual(@as(u64, 2), (try s.stat(flag)).size); + for ([_][]const u8{ "", "yes", "2", "0 1", "true" }) |bad| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(flag, bad)); + try std.testing.expectEqualStrings("bad flag: takes 1 or 0", s.said); + } + } + try s.expect(dirty, "0\n"); + try s.expect(mark, "1\n"); + try s.expect(scroll, "1\n"); + // dirty as /index shows it. + try s.write(dirty, "1\n"); + try s.expect(dirty, "1\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 1 ", .{n})) != null); + try s.write(dirty, "0"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 ", .{n})) != null); + try s.write(mark, "0"); + try s.write(scroll, "0"); + try s.expect(mark, "0\n"); + try s.expect(scroll, "0\n"); + try s.write(mark, "1"); + try s.write(scroll, "1"); + try s.expect(mark, "1\n"); + try s.expect(scroll, "1\n"); + // A terminal has no file, so its dirty flag is readable but not writable. + var t = try Session.initTerm(std.testing.allocator); + defer t.deinit(); + const term_dirty = t.at("/pane/{d}/dirty", .{try t.focused()}); + try t.expect(term_dirty, "0\n"); + try std.testing.expectEqual(@as(u16, 22), try t.refused(term_dirty, "1")); +} diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig new file mode 100644 index 00000000..41a2821a --- /dev/null +++ b/test/behaviour/keys.zig @@ -0,0 +1,227 @@ + + +const std = @import("std"); +const Session = @import("session").Session; + +const alt = "\u{F015}"; +const window = "\x17"; // Ctrl-w + +test "keys: a prompt's entry and cancellation leave the tag, the body and its cursor as they were" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const tag = s.at("/pane/{d}/tag", .{n}); + const dot = s.at("/pane/{d}/dot", .{n}); + try s.replace(tag, " keep"); + for ([_]struct { keys: []const u8, shown: []const u8 }{ + .{ .keys = "s", .shown = " /zq" }, + .{ .keys = "|", .shown = " |zq" }, + .{ .keys = alt ++ "|", .shown = " |-zq" }, + .{ .keys = "!", .shown = " !zq" }, + .{ .keys = alt ++ "!", .shown = " !+zq" }, + }) |case| { + try s.write(dot, "6 6"); + try s.keys(case.keys); + // The answer is typed into the prompt's own line, never the tag. + try s.keys("zq"); + _ = try s.where(case.shown); + try std.testing.expectEqualStrings("/test.txt keep", try s.read(tag)); + try s.keys("\x1b"); + try std.testing.expectError(error.NotOnScreen, s.where(case.shown)); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal ")); + try std.testing.expectEqualStrings("/test.txt keep", try s.read(tag)); + try std.testing.expectEqual(.{ 6, 6 }, try s.range(dot)); + try s.expect(s.at("/pane/{d}/body", .{n}), "one\ntwo\n"); + } + // Save on a scratch asks for a path, starting from its directory. + const scratch = try s.newPane(); + const scratch_tag = try s.read(s.at("/pane/{d}/tag", .{scratch})); + try s.keys(":\tzq"); + _ = try s.where("/zq"); + try s.expect(s.at("/pane/{d}/tag", .{scratch}), scratch_tag); + try s.keys("\x1b"); + try std.testing.expectError(error.NotOnScreen, s.where("/zq")); + try s.expect(s.at("/pane/{d}/tag", .{scratch}), scratch_tag); + try std.testing.expectEqual(@as(usize, 0), s.saved); +} + +test "keys: a prompt opens whatever the tag already holds" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + const tag = s.at("/pane/{d}/tag", .{n}); + try s.replace(tag, &@as([4096]u8, @splat('x'))); + const full = try s.read(tag); + for ([_][]const u8{ "s", "|" }) |key| { + try s.keys(key); + try s.keys("zq"); + _ = try s.where("zq"); + try std.testing.expectEqualStrings(full, try s.read(tag)); + try s.keys("\x1b"); + } +} + +test "keys: generated output refuses every pipe prompt before seeding the tag" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + try s.write("/ctl", "Help\n"); + const n = try s.focused(); + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + const body = try s.read(s.at("/pane/{d}/body", .{n})); + for ([_][]const u8{ "|", alt ++ "|", "!", alt ++ "!" }) |key| { + try s.keys(key); + try std.testing.expectError(error.NotOnScreen, s.where(" |")); + try std.testing.expectError(error.NotOnScreen, s.where(" !")); + try s.expect(s.at("/pane/{d}/tag", .{n}), tag); + try s.expect(s.at("/pane/{d}/body", .{n}), body); + } +} + +test "keys: a cancelled prompt restores the selection its regex previews changed" { + var s = try Session.initFile(std.testing.allocator, "one two one"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + for ([_][]const u8{ "sone", "S " }) |typed| { + try s.write(dot, "0 11"); + try s.keys(typed); + try s.keys("\x1b"); + try std.testing.expectEqual(.{ 0, 11 }, try s.range(dot)); + try s.expect(s.at("/pane/{d}/sel", .{n}), "one two one"); + try s.expect(s.at("/pane/{d}/body", .{n}), "one two one"); + } +} + +test "keys: a counted paste of nothing leaves no count for the next motion" { + var s = try Session.initFile(std.testing.allocator, "abcdefgh\n"); + defer s.deinit(); + const n = try s.focused(); + try s.keys("3pl"); + try std.testing.expectEqual(.{ 1, 1 }, try s.range(s.at("/pane/{d}/dot", .{n}))); + try s.expect(s.at("/pane/{d}/body", .{n}), "abcdefgh\n"); +} + +test "keys: a count or a prefix a click or a change of pane cancels leaves nothing for the next motion" { + const Gesture = enum { body, tag, focus, last }; + for ([_][]const u8{ "3", "3g", "mr(" }) |typed| { + for ([_]Gesture{ .body, .tag, .focus, .last }) |gesture| { + errdefer std.debug.print("\nkeys={s} gesture={s}\n", .{ typed, @tagName(gesture) }); + var s = try Session.initFile(std.testing.allocator, "abcdefghij\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const other = try s.newPane(); + try s.write("/focus", s.at("{d}", .{n})); + try s.keys(typed); + switch (gesture) { + .body => try s.click("abcdefghij", 3, 1), + .tag => { + try s.click("/test.txt Save", "/test.txt ".len + 3, 1); + try s.keys("\x1b:"); + }, + .focus, .last => { + try s.keys(window ++ "l\n"); + try std.testing.expectEqual(other, try s.focused()); + try s.keys(if (gesture == .last) "\x1b" else window ++ "h\n"); + try std.testing.expectEqual(n, try s.focused()); + }, + } + const dot = s.at("/pane/{d}/dot", .{n}); + const at = (try s.range(dot))[0]; + try s.keys("l"); + try std.testing.expectEqual(at + 1, (try s.range(dot))[0]); + try s.expect(s.at("/pane/{d}/body", .{n}), "abcdefghij\n"); + } + } +} + +test "keys: a PDF shown as text turns pages for the moving keys, counted, and the editing keys do nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const pages = (try s.pdfPage(n))[1]; + try std.testing.expect(pages > 3); + // j k, Ctrl-d Ctrl-u, Ctrl-f Ctrl-b: a page each, counted. + for ([_][]const u8{ "2j", "2k", "2\x04", "2\x15", "2\x06", "2\x02" }, 0..) |keys, i| { + try s.keys(keys); + try std.testing.expectEqual(@as(u32, if (i % 2 == 0) 3 else 1), (try s.pdfPage(n))[0]); + } + for ([_]struct { []const u8, u32 }{ .{ "ge", pages }, .{ "gg", 1 }, .{ "3gg", 3 }, .{ "2G", 2 } }) |step| { + try s.keys(step[0]); + try std.testing.expectEqual(step[1], (try s.pdfPage(n))[0]); + } + // Consumed, with their count: the page stays, no selection begins, and + // the next j is one page. + try s.keys("4dv|"); + try std.testing.expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal ")); + try s.keys("j"); + try std.testing.expectEqual(@as(u32, 3), (try s.pdfPage(n))[0]); +} + +test "keys: SPC j n and SPC j p in plain text select its places, never a comment's slashes, round the end, opening nothing" { + var s = try Session.initFile(std.testing.allocator, "// ordinary comment\n/// documentation comment\n(//) [///] punctuation\n/\n// /tmp\n//tmp\n"); + defer s.deinit(); + const n = try s.focused(); + const index = try s.read("/index"); + for ([_]struct { []const u8, []const u8, u32 }{ + .{ " jn", "/", 69 }, .{ " jn", "/tmp", 74 }, .{ " jn", "//tmp", 79 }, .{ " jn", "/", 69 }, + .{ " jp", "//tmp", 79 }, .{ " jp", "/tmp", 74 }, .{ " jp", "/", 69 }, + }) |step| { + try s.keys(step[0]); + try std.testing.expectEqual(n, try s.focused()); + try s.expect(s.at("/pane/{d}/sel", .{n}), step[1]); + try std.testing.expectEqual(step[2], (try s.range(s.at("/pane/{d}/dot", .{n})))[0]); + try s.expect("/index", index); + } +} + +/// The value the root ctl's read gives `word`. +fn setting(s: *Session, word: []const u8) ![]const u8 { + var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n'); + while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line[word.len + 1 ..]; + return error.NoSuchSetting; +} + +test "keys: SPC j n and SPC j p walk Themes' rows, whole, never its section headings, and Tab sets the one selected" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const text = try s.focused(); + // An imported theme's, the list still begins with the natives. + try s.write("/ctl", "Theme dark\n"); + try s.write(s.at("/pane/{d}/exec", .{text}), "Themes\n"); + // Listed beside the text, which keeps the keyboard. + try std.testing.expectEqual(text, try s.focused()); + const list = text + 1; + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+Themes ", .{list})) != null); + const body = try s.read(s.at("/pane/{d}/body", .{list})); + // A heading, then a row a theme; each section after the first a blank + // line and its heading. + var lines = std.mem.splitScalar(u8, body, '\n'); + try std.testing.expectEqualStrings("# Pardes themes", lines.next().?); + var names: std.ArrayList([]const u8) = .empty; + var first_break: ?usize = null; + while (lines.next()) |line| { + if (line.len == 0) { + const heading = lines.next() orelse break; // the newline ending the last row + try std.testing.expect(std.mem.startsWith(u8, heading, "# ")); + first_break = first_break orelse names.items.len; + continue; + } + try std.testing.expect(std.mem.startsWith(u8, line, "Theme ")); + try names.append(s.arena.allocator(), line); + } + const brk = first_break orelse return error.NoSections; + try std.testing.expect(std.mem.indexOf(u8, body[0..std.mem.indexOf(u8, body, "\n\n").?], "\nTheme acme\n") != null); + // Across the first section's end and back, the theme as it was. + for (names.items[0 .. brk + 1]) |row| { + try s.keys(" jn"); + try std.testing.expectEqual(list, try s.focused()); + try s.expect(s.at("/pane/{d}/sel", .{list}), row); + try std.testing.expectEqualStrings("dark", try setting(&s, "Theme")); + } + try s.keys(" jp"); + try s.expect(s.at("/pane/{d}/sel", .{list}), names.items[brk - 1]); + try s.keys("\t"); + try std.testing.expectEqualStrings(names.items[brk - 1]["Theme ".len..], try setting(&s, "Theme")); +} diff --git a/test/behaviour/log.zig b/test/behaviour/log.zig new file mode 100644 index 00000000..21bccdf0 --- /dev/null +++ b/test/behaviour/log.zig @@ -0,0 +1,36 @@ +//! docs/typ/reference.typ, The log <log>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "log: a message from a root ctl write is the session's, and its own words stand for its Verbose announcement" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Kill\n"); + const log = try s.read("/log"); + try std.testing.expect(has(log, "\nmsg - Kill: nothing running\n")); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, log, "Kill")); +} + +test "log: a builtin that fails a ctl write logs only its err, so the same failure again is counted" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + // Another failure first: the pane's `new` is logged after it. + _ = try s.refused("/ctl", "Unmount yyy\n"); + for (0..3) |_| try std.testing.expect(try s.refused("/ctl", "Unmount zzz\n") != 0); + const log = try s.read("/log"); + try std.testing.expect(std.mem.endsWith(u8, log, "err - ctl: Unmount: no such mount zzz (x3)\n")); + try std.testing.expect(!has(log, "msg - Unmount")); +} + +test "log: a root exec that fails logs its err against the pane it ran at" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{n}), "unsaved\n"); + try std.testing.expect(try s.refused("/exec", "Exit\n") != 0); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: ", .{n}))); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 5b8d7e1a..98cab342 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -37,6 +37,190 @@ test "look-and-exec: a bare :N or :N:M look addresses the pane itself, from its try expectEqual(.{ @as(u32, 0), @as(u32, 6) }, try s.range(dot)); } +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +fn lines(text: []const u8) usize { + return std.mem.count(u8, text, "\n"); +} + +/// Whether /index lists the pane `serial`. +fn listed(s: *Session, serial: u32) !bool { + var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (it.next()) |line| { + var words = std.mem.tokenizeScalar(u8, line, ' '); + if (std.mem.eql(u8, words.next() orelse "", s.at("{d}", .{serial}))) return true; + } + return false; +} + +test "look-and-exec: a look takes acme's :addr and file:addr, and a miss changes nothing" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\nbar\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const dot = s.at("/pane/{d}/dot", .{n}); + const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + // :addr is an address in this pane, evaluated from its dot. + try s.write(look, ":/y/\n"); + try expectEqual(.{ @as(u32, 10), @as(u32, 11) }, try s.range(dot)); + try s.write(look, ":#4\n"); + try expectEqual(.{ @as(u32, 4), @as(u32, 4) }, try s.range(dot)); + try s.write(look, ":/foo/\n"); // from #4: the second foo + try expectEqual(.{ @as(u32, 6), @as(u32, 9) }, try s.range(dot)); + // file:addr names the pane's own file here. + try s.write(look, s.at("{s}:/bar/\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot)); + // A miss: no pane made, the selection kept, and it says so. + const panes = lines(try s.read("/index")); + try s.write(look, "zzqq\n"); + try expectEqual(panes, lines(try s.read("/index"))); + try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot)); + try std.testing.expect(has(try s.read("/log"), "no match for \"zzqq\"")); + // A hit answers the pane the text is selected in, not a hits pane. + try s.write(look, "foo\n"); + try s.expect(look, s.at("{d}\n", .{n})); + // A pattern with a blank in it; 0/re/ searches from the top, whatever dot is. + try s.write(look, ":/foo y/\n"); + try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot)); + try s.write(look, s.at("{s}:0/foo/\n", .{path})); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot)); + // file:N selects the line; a line past the end moves nothing and reads back empty. + try s.write(look, s.at("{s}:2\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot)); + try s.write(look, s.at("{s}:99\n", .{path})); + try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot)); + try s.expect(look, ""); + try std.testing.expect(has(try s.read("/log"), s.at("look: {s}:99 has no line 99\n", .{path}))); +} + +test "look-and-exec: the root look's file:/re/ goes on from the dot, and a miss leaves the dot as it was" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\nfoo\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + for ([_]u32{ 0, 6, 12, 0 }) |at| { + try s.write("/look", s.at("{s}:/foo/\n", .{path})); + try expectEqual(.{ at, at + 3 }, try s.range(dot)); + } + try s.write("/look", s.at("{s}:/zzqq/\n", .{path})); + try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot)); +} + +test "look-and-exec: exec runs a builtin at the pane, and look and exec read back the pane it acted on" { + var s = try Session.initFile(std.testing.allocator, "Msg fs-ran\n"); + defer s.deinit(); + const n = try s.focused(); + const own = s.at("{d}\n", .{n}); + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg from exec\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from exec\n", .{n}))); + try s.expect("/exec", own); + try std.testing.expectEqual(@as(u64, own.len), (try s.stat("/exec")).size); + try s.expect(s.at("/pane/{d}/look", .{n}), own); + try s.write(s.at("/pane/{d}/exec", .{n}), "New\n"); + const made = try s.focused(); + try std.testing.expect(made != n); + try s.expect("/exec", s.at("{d}\n", .{made})); + try s.write(s.at("/pane/{d}/exec", .{made}), "Del\n"); + try std.testing.expect(try listed(&s, n) and !try listed(&s, made)); + try s.expect("/exec", s.at("{d}\n", .{made})); +} + +test "look-and-exec: look and exec read back what the last command touched, a ctl write's too" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write("/exec", "Msg first\n"); + try s.expect("/exec", s.at("{d}\n", .{n})); + // A pane made through ctl is what both read next, not the Msg's pane. + try s.write(s.at("/pane/{d}/ctl", .{n}), "Tty\n"); + const made = try s.focused(); + try std.testing.expect(made != n); + try s.expect("/exec", s.at("{d}\n", .{made})); + try s.expect("/look", s.at("{d}\n", .{made})); + // A ctl write that makes nothing names the pane it ran at. + try s.write("/ctl", "Verbose off\n"); + try s.expect("/exec", s.at("{d}\n", .{made})); + // Closed, it is no pane to go to: a fresh read of look is empty. + try s.write(s.at("/pane/{d}/ctl", .{made}), "Del\n"); + try std.testing.expect(!try listed(&s, made)); + try s.expect("/look", ""); +} + +test "look-and-exec: a line starting with # runs as nothing at an exec or a ctl, silently" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // The setup's own announcements are made by the first write. + try s.write("/ctl", "# settle\n"); + const index = try s.read("/index"); + const log = try s.read("/log"); + const col = std.mem.sliceTo(try s.read("/layout"), ' '); + for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{n}), "/ctl", s.at("/pane/{d}/ctl", .{n}), s.at("/col/{s}/ctl", .{col}) }) |path| + try s.write(path, "# Platform: tty\n # indented too\n"); + try s.expect("/index", index); + try s.expect("/log", log); +} + +test "look-and-exec: a look replaces the selection it was made from, for an address, a range and text found" { + var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\nneedle\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + const look = s.at("/pane/{d}/look", .{n}); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(dot, "0 5"); + // A second cursor on the line below, and select mode. + try s.keys("Cv"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:select ")); + try s.write(look, "/test.txt:3:2\n"); + try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dot)); + // Both are gone with it: normal mode, and what is typed goes in once. + try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:normal ")); + try s.keys("iZ\x1b"); + try s.expect(body, "alpha\nbravo\ncZharlie\nneedle\n"); + try s.write(ctl, "Undo\n"); + try s.expect(body, "alpha\nbravo\ncharlie\nneedle\n"); + try s.write(dot, "0 5"); + try s.write(look, "/test.txt:2:2-4\n"); + try s.expect(s.at("/pane/{d}/sel", .{n}), "rav"); + try s.write(s.at("/pane/{d}/name", .{n}), "/tmp/look-selection-source.txt"); + try s.write(dot, "0 5"); + // A count typed before it is gone after it. + try s.keys("5"); + try s.write(look, "needle\n"); + try s.expect(s.at("/pane/{d}/sel", .{n}), "needle"); + try s.keys("l"); + try expectEqual(.{ @as(u32, 26), @as(u32, 26) }, try s.range(dot)); + try expectEqual(n, try s.focused()); +} + +test "look-and-exec: a look into another pane leaves the source's dot and replaces the destination's selection" { + var s = try Session.initFile(std.testing.allocator, "source\nlines\n"); + defer s.deinit(); + const source = try s.focused(); + const dest = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{dest}), "/look-destination.txt"); + try s.write(s.at("/pane/{d}/body", .{dest}), "alpha\nbravo\ncharlie\n"); + const dest_dot = s.at("/pane/{d}/dot", .{dest}); + for ([_]bool{ false, true }) |range| { + try s.write("/focus", s.at("{d}", .{source})); + try s.write(s.at("/pane/{d}/dot", .{source}), "0 6"); + try s.write(dest_dot, "0 5"); + try s.write(s.at("/pane/{d}/look", .{source}), if (range) "/look-destination.txt:2:2-4\n" else "/look-destination.txt:3:2\n"); + try expectEqual(dest, try s.focused()); + // The source keeps its dot, as acme's look leaves it. + try s.expect(s.at("/pane/{d}/sel", .{source}), "source"); + if (range) + try s.expect(s.at("/pane/{d}/sel", .{dest}), "rav") + else + try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dest_dot)); + } +} + test "look-and-exec: a look that finds nothing says so" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); @@ -44,3 +228,98 @@ test "look-and-exec: a look that finds nothing says so" { try s.write(s.at("/pane/{d}/look", .{n}), "/no/such/zzqq\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("err {d} look: no match for \"/no/such/zzqq\"\n", .{n})) != null); } + +/// The log's `err` records, counted. +fn errs(s: *Session) !usize { + return std.mem.count(u8, try s.read("/log"), "\nerr "); +} + +/// The serial of the pane /index names with `suffix`. +fn paneNamed(s: *Session, suffix: []const u8) !u32 { + var rows = std.mem.splitScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |line| { + // serial kind dirty name column + const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; + if (std.mem.endsWith(u8, name, suffix)) return std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10); + } + return error.NoSuchPane; +} + +test "look-and-exec: `:N` on a PDF turns to page N, from the pane and from the root's look, and `@pN:N` logs no err" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const look = s.at("/pane/{d}/look", .{n}); + const pages = (try s.pdfPage(n))[1]; + try std.testing.expect(pages >= 3); + try s.write(look, ":2\n"); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try s.write(look, s.at("@p{d}:1\n", .{n})); + try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); + try s.write("/look", s.at("@p{d}:2\n", .{n})); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try expectEqual(@as(usize, 0), try errs(&s)); + // `:N:H` is hit H of its search on page N, as a search row's + // `file.pdf:N:H` is: with no search there is no hit 3, a miss that turns + // no page. + try s.write(look, ":1\n"); + _ = try s.refused(look, ":2:3\n"); + try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no search hit 3 on page 2") != null); + try s.write(look, ":2\n"); + _ = try s.refused(look, ":0\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 0") != null); + // A byte offset or a pattern is no PDF address: refused with words. + _ = try s.refused(look, ":#3\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "its address is a page, :N") != null); + const before = try errs(&s); + _ = try s.refused(look, s.at(":{d}\n", .{pages + 1})); + try expectEqual(before + 1, try errs(&s)); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); +} + +test "look-and-exec: a look at a PDF's page past its last is a miss, never the last page, and opens nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + try std.Io.Dir.cwd().copyFile("docs/design.pdf", tmp.dir, "other.pdf", std.testing.io, .{}); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const look = s.at("/pane/{d}/look", .{n}); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + // Open: it stays on its page. + try s.write(look, ":2\n"); + _ = try s.refused(look, s.at("{s}:999\n", .{name})); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 999") != null); + // Not open: nothing opens. + const index = try s.read("/index"); + _ = try s.refused(look, s.at("{s}/other.pdf:999\n", .{dir})); + try s.expect("/index", index); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "other.pdf has no page 999") != null); +} + +test "look-and-exec: a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + try s.write(s.at("/pane/{d}/exec", .{n}), "PdfSections\n"); + const out = try paneNamed(&s, "/+PdfSections"); + const body = try s.read(s.at("/pane/{d}/body", .{out})); + const row = body[0..std.mem.indexOfScalar(u8, body, '\n').?]; + const place = row[0..std.mem.indexOfScalar(u8, row, ' ').?]; // design.pdf:P:S + var fields = std.mem.splitScalar(u8, place, ':'); + _ = fields.next(); + const page = try std.fmt.parseInt(u32, fields.next().?, 10); + const look = s.at("/pane/{d}/look", .{out}); + // As listed: found, no miss. + try s.write(look, s.at("{s}\n", .{place})); + try expectEqual(@as(usize, 0), try errs(&s)); + // The section on another page than the row says, and one not there. + _ = try s.refused(look, s.at("design.pdf:{d}:1\n", .{page + 1})); + try expectEqual(@as(usize, 1), try errs(&s)); + _ = try s.refused(look, "design.pdf:1:999\n"); + try expectEqual(@as(usize, 2), try errs(&s)); +} diff --git a/test/behaviour/mounts.zig b/test/behaviour/mounts.zig new file mode 100644 index 00000000..57685a38 --- /dev/null +++ b/test/behaviour/mounts.zig @@ -0,0 +1,25 @@ +//! docs/typ/reference.typ, Paths and mounts <mounts>. +const std = @import("std"); +const Session = @import("session").Session; +const expectEqual = std.testing.expectEqual; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "mounts: Mount with no dial is wrong #args, any other x!y a bad dial address, a socket not there no such socket, and none mounts anything" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount work\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "wrong #args")); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount work tcp!nohost\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Mount: bad dial address: tcp!nohost")); + try expectEqual(@as(u16, 2), try s.refused("/ctl", "Mount peer unix!/tmp/pardes-surely-no-socket\n")); + try std.testing.expect(has(s.said, "no such socket")); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount peer rel/x.sock\n")); + // Nothing was mounted under either name. + for ([_][]const u8{ "Unmount work\n", "Unmount peer\n" }) |line| { + try expectEqual(@as(u16, 2), try s.refused("/ctl", line)); + try std.testing.expect(has(s.said, "no such mount")); + } +} diff --git a/test/behaviour/mouse.zig b/test/behaviour/mouse.zig new file mode 100644 index 00000000..0abe15dc --- /dev/null +++ b/test/behaviour/mouse.zig @@ -0,0 +1,133 @@ +//! docs/typ/guide.typ, The mouse <mouse>. +const std = @import("std"); +const Session = @import("session").Session; + +/// The mouse as `/mousein` lines say it, `m col row buttons` each. +fn mouse(s: *Session, comptime lines: []const u8, args: anytype) !void { + try s.write("/mousein", s.at(lines, args)); +} + +test "mouse: a double-click selects a word, a line, a bracketed or quoted text, as acme's" { + var s = try Session.initFile(std.testing.allocator, "call foo_bar(a, b)\nsay \"hi there\"\n"); + defer s.deinit(); + const n = try s.focused(); + const sel = s.at("/pane/{d}/sel", .{n}); + for ([_]struct { at: []const u8, into: usize, want: []const u8 }{ + .{ .at = "foo_bar", .into = 2, .want = "foo_bar" }, + .{ .at = "(a, b)", .into = 1, .want = "a, b" }, + .{ .at = "call", .into = 0, .want = "call foo_bar(a, b)\n" }, + .{ .at = "\"hi", .into = 1, .want = "hi there" }, + }) |w| { + try s.click(w.at, w.into, 1); + try s.click(w.at, w.into, 1); + try s.expect(sel, w.want); + // A third click is a plain click again: the caret, no selection. + try s.click(w.at, w.into, 1); + try s.expect(sel, ""); + } + // In the tag too: what it selected is what a yank takes. + try s.click("Collapse Del", "Collapse D".len, 1); + try s.click("Collapse Del", "Collapse D".len, 1); + try s.keys("\x1by:ggP"); + try s.expect(s.at("/pane/{d}/body", .{n}), "Delcall foo_bar(a, b)\nsay \"hi there\"\n"); +} + +test "mouse: everything a B1 chord does while B1 is down is one undo step" { + var s = try Session.initFile(std.testing.allocator, "one two three\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const x, const y = try s.where("one two"); + // B1-B2 then B1-B3: cut, then paste back: a copy, one step. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 5\nm {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y }); + try s.expect(body, "one two three\n"); + // Undone in one step, it is the text before the cut, not after it. + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "one two three\n"); + // A cut alone comes back with one Undo. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y }); + try s.expect(body, "one three\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "one two three\n"); +} + +test "mouse: a click past a line's or the file's end puts the caret where insertion types" { + for ([_]bool{ false, true }) |already_insert| { + for ([_]bool{ false, true }) |bottom_tag| { + for ([_]struct { text: []const u8, click_row: usize, dot: u32, col: usize, row: usize, result: []const u8 }{ + .{ .text = "abc\nxy", .click_row = 0, .dot = 3, .col = 3, .row = 0, .result = "abcZ\nxy" }, + .{ .text = "abc\nxy", .click_row = 5, .dot = 6, .col = 2, .row = 1, .result = "abc\nxyZ" }, + .{ .text = "abc\n", .click_row = 5, .dot = 4, .col = 0, .row = 1, .result = "abc\nZ" }, + .{ .text = "", .click_row = 5, .dot = 0, .col = 0, .row = 0, .result = "Z" }, + }) |case| { + var s = try Session.initFile(std.testing.allocator, case.text); + defer s.deinit(); + const n = try s.focused(); + if (bottom_tag) try s.write("/ctl", "Tagbottom on\n"); + if (already_insert) try s.keys("i"); + // The first line's number, then a blank, then its text. + const x, const y = try s.where("1 "); + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2 + 15, y + case.click_row, x + 2 + 15, y + case.click_row }); + try std.testing.expectEqual(.{ case.dot, case.dot }, try s.range(s.at("/pane/{d}/dot", .{n}))); + // The caret is drawn where it is, and insertion draws it there too. + try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?); + if (!already_insert) try s.keys("i"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:insert ")); + try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?); + try s.keys("Z"); + try s.expect(s.at("/pane/{d}/body", .{n}), case.result); + } + } + } +} + +test "mouse: a click on a tab's or a wrapped wide glyph's cells puts the caret on it" { + for ([_]bool{ false, true }) |already_insert| { + for ([_]bool{ false, true }) |wrap| { + const source = if (wrap) "012345678901234567890123456789界tail\n" else "\t界tail\n"; + var s = try Session.initFile(std.testing.allocator, source); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 24 12\n"); + const n = try s.focused(); + if (!wrap) try s.write("/ctl", "Wrap off\n"); + if (already_insert) try s.keys("i"); + const x, const y = try s.where("界"); + if (wrap) try std.testing.expect(y > (try s.where("1 "))[1]); + // The glyph's second cell. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 1, y, x + 1, y }); + const at: u32 = @intCast(try std.unicode.utf8CountCodepoints(source[0..std.mem.indexOf(u8, source, "界").?])); + try std.testing.expectEqual(.{ at, at }, try s.range(s.at("/pane/{d}/dot", .{n}))); + // Drawn on the glyph's first cell, in insert mode too. + try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?); + if (!already_insert) try s.keys("i"); + try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?); + try s.keys("Z"); + const byte = std.mem.indexOf(u8, source, "界").?; + try s.expect(s.at("/pane/{d}/body", .{n}), s.at("{s}Z{s}", .{ source[0..byte], source[byte..] })); + } + } +} + +test "mouse: a B1 sweep over several lines is a stream, as acme's, in a body and a tag" { + var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const x, const y = try s.where("alpha"); + // From the press to the end of its line, the line between whole, the + // last from its start: no line numbers, no rectangle. B2 cuts it. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ x + 2, y, x + 1, y + 2, x + 1, y + 2, x + 1, y + 2 }); + try s.expect(body, "alarlie\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + // Upward, the same text. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ x + 1, y + 2, x + 2, y, x + 2, y, x + 2, y }); + try s.expect(body, "alarlie\n"); + // A tag of several rows sweeps the same way. + const tag = s.at("/pane/{d}/tag", .{n}); + try s.write(tag, "\nLook Edit"); + const tx, const ty = try s.where("Collapse"); + const lx, const ly = try s.where("Look Edit"); + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ tx + 3, ty, lx + 1, ly, lx + 1, ly, lx + 1, ly }); + try s.expect(tag, "/test.txt Save Tty Colok Edit"); +} diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig index c0f9c08f..ef1fab7e 100644 --- a/test/behaviour/other_files.zig +++ b/test/behaviour/other_files.zig @@ -28,3 +28,55 @@ test "other-files: kbdin's Kalt rune is Alt on the key after it alone, and an Al try s.keys("i\u{F015}zy\x1b"); try s.expect(s.at("/pane/{d}/body", .{try s.focused()}), "yx\n"); } + +test "other-files: pager answers its +Pager on the open that asked, whatever another client exec'd meanwhile, and refuses a directory it cannot page in" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const asked = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(asked, "/tmp\n")); + // Another client execs between the request and its read: a pane of its own. + const racer = try s.hold("/exec", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(racer, "New\n")); + s.clunk(racer); + const paged = try s.readFid(asked); + s.clunk(asked); + // A fresh open reads the session's last answer, the racer's pane. + try std.testing.expect(!std.mem.eql(u8, paged, try s.read("/exec"))); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, paged, "\n")}), "/tmp/+Pager\n"); + // Refused, an open answers nothing, not the session's last answer. + const refused = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(refused, "relative\n")); + try std.testing.expectEqualStrings("", try s.readFid(refused)); + s.clunk(refused); + // Resolved: `/tmp/../tmp/.` is /tmp, a +Pager of its own beside the + // one made above, which keeps its page. + const resolved = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(resolved, "/tmp/../tmp/.\n")); + const second = try s.readFid(resolved); + s.clunk(resolved); + try std.testing.expect(!std.mem.eql(u8, paged, second)); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, second, "\n")}), "/tmp/+Pager\n"); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, paged, "\n")}), "/tmp/+Pager\n"); + // One that may not be written is refused, permission denied (Linux's /proc/1). + try std.testing.expectEqual(@as(u16, 1), try s.refused("/pager", "/proc/1\n")); + // One directory an open: one refused is not followed by another, as a + // mount sends two lines in two writes. + const twice = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(twice, "relative\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(twice, "/tmp\n")); + s.clunk(twice); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/pager", "relative\n")); + try std.testing.expectEqualStrings("pager: invalid directory \"relative\": must be absolute", s.said); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/pager", "/tmp/pardes-surely-missing-dir\n")); + try std.testing.expectEqual(@as(u16, 20), try s.refused("/pager", "/etc/passwd\n")); +} + +test "other-files: /recent keeps a PDF as it keeps a file: open, then closed" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/recent"), s.at("open {s}\n", .{name})) != null); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/recent"), s.at("closed {s}\n", .{name})) != null); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 149c8da1..a46f8432 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -50,6 +50,123 @@ test "panes: Putall saves every pane with unsaved edits to a file, one each, and try std.testing.expectEqual(@as(usize, 1), s.saved); } +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "panes: focus reads the pane with the keyboard, and a serial written to it moves the keyboard there, and only that" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + const second = try s.newPane(); + try s.expect("/focus", s.at("{d}\n", .{second})); + // A stat says the length a read gives. + try std.testing.expectEqual(@as(u64, s.at("{d}\n", .{second}).len), (try s.stat("/focus")).size); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " notcurrent ")); + // A column's tag has the keyboard: no pane does, and a write takes it back. + try s.write("/ctl", "Newcol\n"); + try s.expect("/focus", ""); + try std.testing.expectEqual(@as(u64, 0), (try s.stat("/focus")).size); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent ")); + try s.write("/focus", s.at("{d}\n", .{first})); + try s.expect("/focus", s.at("{d}\n", .{first})); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " current ")); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent ")); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/focus", "99999\n")); + try std.testing.expectEqualStrings("no such pane", s.said); + for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try std.testing.expectEqual(@as(u16, 22), try s.refused("/focus", bad)); + try s.expect("/focus", s.at("{d}\n", .{first})); + // Focus gives the keyboard only: a folded pane stays folded. + try s.write(s.at("/pane/{d}/ctl", .{second}), "Collapse\n"); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed ")); + try s.write("/focus", s.at("{d}", .{second})); + try s.expect("/focus", s.at("{d}\n", .{second})); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed ")); +} + +test "panes: the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + // A builtin is its own capitalised word: `GET` and `DEL` are none. + for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, s.at("{s}\n", .{bad}))); + try std.testing.expectEqualStrings(s.at("unknown control message \"{s}\"", .{std.mem.sliceTo(bad, ' ')}), s.said); + } + _ = try s.refused(ctl, "Newcol\n"); + try std.testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", s.said); + _ = try s.refused(ctl, "Verbose off\n"); + try std.testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", s.said); + _ = try s.refused(ctl, "Collapse now\n"); + try std.testing.expectEqualStrings("wrong #args in control message \"Collapse\"", s.said); + // Checked whole first: a bad line anywhere and nothing ran. + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nbogus\n")); + // A pane's builtin runs at that pane. + try s.write(ctl, "Msg from ctl\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from ctl\n", .{n}))); + // One that means nothing bare is refused before anything runs. + _ = try s.refused(ctl, "Find\n"); + try std.testing.expectEqualStrings("wrong #args in control message \"Find\"", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nMsg\n")); + try std.testing.expect(!has(try s.read("/log"), "first")); + // Save with no name on a scratch would ask for one: refused, not asked. + const other = try s.newPane(); + _ = try s.refused(s.at("/pane/{d}/ctl", .{other}), "Save\n"); + try std.testing.expectEqualStrings("control message needs its argument \"Save\"", s.said); + // A line after the one that closed the pane has nowhere to run. + try std.testing.expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/ctl", .{other}), "Del\nMsg after\n")); + try std.testing.expectError(error.Refused, s.read(s.at("/pane/{d}/ctl", .{other}))); +} + +test "panes: a second lock is refused until the holder unlocks or closes, and binds nobody else" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const a = try s.hold(ctl, 2); + const b = try s.hold(ctl, 1); + // An open that only reads the status line holds nothing to lock with. + const reader = try s.hold(ctl, 0); + try std.testing.expect(try s.writeFid(reader, "lock\n") != 0); + s.clunk(reader); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n")); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n")); + // Another open's lock is refused at once, and so is its unlock; writes + // to the pane's other files are not. + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(b, "lock\n")); + try std.testing.expectEqualStrings("file in use: another open of this ctl holds the lock", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(b, "unlock\n")); + try std.testing.expectEqualStrings("pane not locked by this open", s.said); + try s.write(s.at("/pane/{d}/addr", .{n}), "1"); + try s.write(s.at("/pane/{d}/data", .{n}), "ONE\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n"); + // Once the holder unlocks, the other's try takes it. + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "unlock\n")); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(b, "lock\n")); + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(a, "lock\nunlock\n")); + // Closing the holder gives it up. + s.clunk(b); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\nunlock\nlock\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(a, "unlock\nunlock\n")); + // Still a's: another open is refused it. + const c = try s.hold(ctl, 1); + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(c, "lock\n")); + s.clunk(c); + // The lock lives and dies with the pane. + const other = try s.newPane(); + const d = try s.hold(s.at("/pane/{d}/ctl", .{other}), 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, "lock\n")); + try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n"); + try std.testing.expectEqual(@as(u16, 2), try s.writeFid(d, "lock\n")); + s.clunk(d); + s.clunk(a); + // a's close gave its lock up too. + const e = try s.hold(ctl, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(e, "lock\n")); + s.clunk(e); +} + test "panes: an unsaved file's tag is its name alone, its dirty file says it is unsaved" { var s = try Session.initFile(std.testing.allocator, "before\n"); defer s.deinit(); @@ -161,3 +278,88 @@ test "panes: a pane its whole text fits in never scrolls to keep a margin" { try std.testing.expectEqual(.{ @as(u32, 8), @as(u32, 8) }, try s.range(s.at("/pane/{d}/dot", .{n}))); try std.testing.expectEqual(top + 1, (try s.where("1 one"))[1]); } + +/// `name` in `dir`: a one-page PDF with no outline and no text. +fn plainPdf(dir: std.Io.Dir, name: []const u8) !void { + const objects = [_][]const u8{ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Count 1 /Kids [3 0 R] >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>", + }; + var buf: [1024]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + var at: [objects.len]usize = undefined; + try w.writeAll("%PDF-1.4\n"); + for (objects, 0..) |object, i| { + at[i] = w.end; + try w.print("{d} 0 obj\n{s}\nendobj\n", .{ i + 1, object }); + } + const xref = w.end; + try w.print("xref\n0 {d}\n0000000000 65535 f \n", .{objects.len + 1}); + for (at) |off| try w.print("{d:0>10} 00000 n \n", .{off}); + try w.print("trailer\n<< /Size {d} /Root 1 0 R >>\nstartxref\n{d}\n%%EOF\n", .{ objects.len + 1, xref }); + try dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() }); +} + +test "panes: a PDF's body is the text of the page shown, each line of it a line, never a heading run into the text under it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const body = s.at("/pane/{d}/body", .{n}); + try std.testing.expect(std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\nArchitecture and implementation of the rewrite\n")); + try s.write(s.at("/pane/{d}/look", .{n}), ":2\n"); + try std.testing.expect(!std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\n")); +} + +test "panes: PdfTint steps a PDF's tint, read in its ctl and never in its tag, and only a PDF pane takes it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const text = try s.focused(); + const n = try s.lookFile("docs/design.pdf"); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + try std.testing.expect(std.mem.endsWith(u8, tag, " PdfSections PdfTint PdfFit Collapse Del")); + for ([_][]const u8{ "full", "disabled", "filtered" }) |want| { + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), "fit:width ") != null); + try s.write(ctl, "PdfTint\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null); + try s.expect(s.at("/pane/{d}/tag", .{n}), tag); + // Another pane's PdfTint is refused and leaves the PDF's alone. + try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/ctl", .{text}), "PdfTint\n")); + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null); + } +} + +test "panes: PdfSections on a PDF with no outline says so and opens nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try plainPdf(tmp.dir, "plain.pdf"); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile(s.at("{s}/plain.pdf", .{dir})); + try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[1]); + const index = try s.read("/index"); + for (0..2) |_| { + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "PdfSections\n"); + try s.expect("/index", index); + } + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "PdfSections: this PDF has no outline") != null); +} + +test "panes: a Mini that cannot read its file leaves the panes, the last Mini's snapshot and the keyboard as they were" { + var s = try Session.initFile(std.testing.allocator, "abc\n"); + defer s.deinit(); + const text = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{text}); + try s.write(exec, s.at("Mini /virtual/pane/{d}/body\n", .{text})); + const mini = try s.focused(); + try std.testing.expect(mini != text); + const body = try s.read(s.at("/pane/{d}/body", .{mini})); + const index = try s.read("/index"); + try std.testing.expectEqual(@as(u16, 2), try s.refused(exec, "Mini /virtual/does-not-exist\n")); + try std.testing.expectEqual(mini, try s.focused()); + try s.expect("/index", index); + try s.expect(s.at("/pane/{d}/body", .{mini}), body); +} diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index bdf912da..90838d90 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -29,3 +29,59 @@ test "root-ctl: size sets the screen of a session no frontend is attached to, an try std.testing.expect(std.mem.indexOf(u8, s.said, "too small for the panes") != null); try std.testing.expectEqual(.{ @as(usize, 120), @as(usize, 40) }, try size(&s)); } + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +fn columns(s: *Session) !usize { + return std.mem.count(u8, try s.read("/layout"), "\n") - 1; // and `active` +} + +test "root-ctl: the root ctl reads the settings as a write takes them, and takes the session's builtins" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const settings = try s.read("/ctl"); + try std.testing.expectEqual(@as(u64, settings.len), (try s.stat("/ctl")).size); + try std.testing.expect(has(settings, "Verbose on\n")); + try std.testing.expect(has(settings, "LocationsConfig ")); + try std.testing.expect(has(settings, "CallstackDepth 8\n")); + try s.write("/ctl", "CallstackDepth\n"); + // Settings only: nothing /status or the DumpConfig window says. + try std.testing.expect(!has(settings, "Platform")); + try std.testing.expect(!has(settings, "requested")); + // Every line written back is taken, and changes nothing. + try s.write("/ctl", settings); + try s.expect("/ctl", settings); + try s.write("/ctl", "Verbose off\n"); + try std.testing.expect(has(try s.read("/ctl"), "Verbose off\n")); + const before = try columns(&s); + try s.write("/ctl", "Newcol\n"); + try std.testing.expectEqual(before + 1, try columns(&s)); + const refusals = [_]struct { []const u8, []const u8 }{ + .{ "Del", "not a session control message \"Del\": write it to pane/<n>/ctl" }, + .{ "Nonsense 1", "unknown control message \"Nonsense\"" }, + .{ "Verbose maybe", "bad value in control message; takes on, off \"maybe\"" }, + .{ "Placement east", "bad value in control message; takes acme, pardes \"east\"" }, + // A number's refusal names its range. + .{ "InactiveDim 200", "bad value in control message; takes 0-100 (a percentage) \"200\"" }, + .{ "MessageLinger x", "bad value in control message; takes 0-60000 (milliseconds) \"x\"" }, + .{ "Newcol 2", "wrong #args in control message \"Newcol\"" }, + .{ "Theme", "wrong #args in control message \"Theme\"" }, + }; + for (refusals) |r| { + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", s.at("{s}\n", .{r[0]}))); + try std.testing.expectEqualStrings(r[1], s.said); + } + _ = try s.refused("/ctl", "Theme no-such-theme\n"); + try std.testing.expect(std.mem.startsWith(u8, s.said, "bad value in control message; like it: n")); + try std.testing.expect(std.mem.endsWith(u8, s.said, "; Themes lists all \"no-such-theme\"")); + // A bare required word fails the check, so the line before never runs. + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Verbose on\nMount\n")); + try std.testing.expect(has(try s.read("/ctl"), "Verbose off\n")); + // A builtin that fails as it runs fails the write, quoting its error and + // its line; the lines before it have taken effect, as in acme. + try std.testing.expectEqual(@as(u16, 2), try s.refused("/ctl", "Verbose on\nMount x /tmp/pardes-no-such-sock-zz\nVerbose off\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Mount x: /tmp/pardes-no-such-sock-zz: no such socket")); + try std.testing.expect(has(try s.read("/ctl"), "Verbose on\n")); +} diff --git a/test/behaviour/rules.zig b/test/behaviour/rules.zig new file mode 100644 index 00000000..4b853a40 --- /dev/null +++ b/test/behaviour/rules.zig @@ -0,0 +1,39 @@ +//! docs/typ/reference.typ, Rules for every file <rules>. +const std = @import("std"); +const Session = @import("session").Session; + +test "rules: a control character in a write to any ctl refuses the whole write, and a quoted line shows a tab as a blank" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // The first column's serial, /layout's first word. + const col = std.mem.sliceTo(try s.read("/layout"), ' '); + for ([_][]const u8{ "/ctl", s.at("/pane/{d}/ctl", .{n}), s.at("/col/{s}/ctl", .{col}) }) |ctl| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Wrap off\nbo\x01gus\n")); + try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said); + } + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{n}), "bogus\tword\n")); + // The offending word alone is quoted: a tab ends it. + try std.testing.expect(std.mem.indexOf(u8, s.said, "\"bogus\"") != null); + try std.testing.expect(std.mem.indexOfScalar(u8, s.said, '\t') == null); +} + +test "rules: body reads at any offset and writes append; a stat says its length, and its version moves with each edit" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const body = s.at("/pane/{d}/body", .{try s.focused()}); + try std.testing.expectEqualStrings("two\n", try s.readAt(body, 4, 100)); + try std.testing.expectEqualStrings("wo", try s.readAt(body, 5, 2)); + try std.testing.expectEqualStrings("", try s.readAt(body, 999, 2)); + try s.write(body, "three\n"); + try s.expect(body, "one\ntwo\nthree\n"); + // A rune cut at the write's end is not taken. + try s.write(body, "a\xC3"); + try s.expect(body, "one\ntwo\nthree\na"); + const st = try s.stat(body); + try std.testing.expectEqual(@as(u64, "one\ntwo\nthree\na".len), st.size); + try std.testing.expectEqual(@as(u16, 0o666), st.mode); + try s.write(body, "more\n"); + try std.testing.expect((try s.stat(body)).version != st.version); + try std.testing.expectEqual(@as(u32, 0), (try s.stat(s.at("/pane/{d}/tag", .{try s.focused()}))).version); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index e62539e4..ff24704a 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -1,7 +1,48 @@ -//! docs/typ/reference.typ, Settings <settings>. + + const std = @import("std"); const Session = @import("session").Session; +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// The root ctl's line for `word`. +fn setting(s: *Session, word: []const u8) ![]const u8 { + var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n'); + while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line; + return error.NoSuchSetting; +} + +test "settings: Pager is pardes by default, off taken, anything else refused with its choices" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expectEqualStrings("Pager pardes", try setting(&s, "Pager")); + try s.write("/ctl", "Pager off\n"); + try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pager less\n")); + try std.testing.expectEqualStrings("bad value in control message; takes pardes, off \"less\"", s.said); + try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager")); +} + +test "settings: Shell refuses a path that is no executable, and bare it goes back to the default" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const default = try setting(&s, "Shell"); + try std.testing.expect(try s.refused("/ctl", "Shell /nonexistent/zzsh\n") != 0); + try std.testing.expect(has(s.said, "Shell: shell \"/nonexistent/zzsh\" not found")); + try std.testing.expect(has(try s.read("/log"), "shell \"/nonexistent/zzsh\" not found")); + // Its words say a directory, which a mount reads as EISDIR too. + try std.testing.expectEqual(@as(u16, 21), try s.refused("/ctl", "Shell /etc\n")); + try std.testing.expect(has(try s.read("/log"), "Shell: not a shell: /etc is a directory")); + try std.testing.expectEqualStrings(default, try setting(&s, "Shell")); + try s.write("/ctl", "Shell /bin/sh\n"); + try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell")); + try s.write("/ctl", "Shell\n"); + try std.testing.expect(has(try s.read("/log"), "Shell: the default again, ")); + try std.testing.expectEqualStrings(default, try setting(&s, "Shell")); +} + test "settings: Pet is the window's: a terminal's view refuses it, from a pane and the root; a window takes only its pets" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); @@ -25,3 +66,38 @@ test "settings: Pet is the window's: a terminal's view refuses it, from a pane a try std.testing.expectEqualStrings("Pet is GUI-only, invalid here", s.said); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "Pet ") == null); } + +fn expectHas(text: []const u8, part: []const u8) !void { + if (std.mem.indexOf(u8, text, part) == null) { + std.debug.print("not in it: {s}\n", .{part}); + return error.TestExpectedText; + } +} + +test "settings: DumpConfig reports the settings in its one +DumpConfig pane, a second refreshing it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const text = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{text}); + try s.write(exec, "DumpConfig\n"); + const out = try s.focused(); + try std.testing.expect(out != text); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+DumpConfig ", .{out})) != null); + const report = try s.read(s.at("/pane/{d}/body", .{out})); + for ([_][]const u8{ "Theme orchard\n", "Last shell spawned: (none)\n", "Shell change pending: on\n", "\nShell ", "LocationsConfig ", "no per-user config path" }) |line| try expectHas(report, line); + try s.write("/ctl", "Theme acme\n"); + try s.write("/focus", s.at("{d}", .{text})); + try s.write(exec, "DumpConfig\n"); + try std.testing.expectEqual(out, try s.focused()); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/index"), "/+DumpConfig ")); + try expectHas(try s.read(s.at("/pane/{d}/body", .{out})), "Theme acme\n"); +} + +test "settings: Config without a configuration directory says so and opens nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const index = try s.read("/index"); + try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Config\n")); + try s.expect("/index", index); + try expectHas(try s.read("/log"), "Config: no startup file here: no configuration directory"); +} diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig index ab109b78..6c1c6fcd 100644 --- a/test/behaviour/tags.zig +++ b/test/behaviour/tags.zig @@ -1,7 +1,387 @@ -//! docs/typ/guide.typ, Tags <tags>. + + const std = @import("std"); const Session = @import("session").Session; +const home = "\u{F00D}"; +const right = "\u{F012}"; +const window = "\x17"; // Ctrl-w + +/// The pane's tag file. +fn tag(s: *Session, n: u32) ![]const u8 { + return s.read(s.at("/pane/{d}/tag", .{n})); +} + +/// The column of the screen's cursor in the tag drawn starting with +/// `path`, which must be on its row. +fn tagCol(s: *Session, path: []const u8) !usize { + const x, const y = try s.where(path); + const cx, const cy = (try s.cursor()).?; + try std.testing.expectEqual(y, cy); + return cx - x; +} + +test "tags: s and S in a tag answer for the tag's own text; / still searches the body" { + var s = try Session.initFile(std.testing.allocator, "Some body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.replace(s.at("/pane/{d}/tag", .{n}), " Save Del Snarf"); + // The tag's line, its S's selected, then changed: both are the tag's. + try s.keys(":xsS\ncZ\x1b"); + try std.testing.expectEqualStrings("/test.txt Zave Del Znarf", try tag(&s, n)); + try s.expect(s.at("/pane/{d}/body", .{n}), "Some body\n"); + // `/` from the tag is the body's search, as acme's Look from a tag is. + // Its places list the body's match. + try s.keys(",/body\n"); + _ = try s.where("test.txt:1:6-9"); +} + +test "tags: ms and md edit a tag's own text, and the text the edit made is read back whole" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.keys(":miwms("); + try std.testing.expectEqualStrings("/test.txt (Save) Tty Collapse Del", try tag(&s, n)); + try s.keys("md("); + try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del", try tag(&s, n)); +} + +test "tags: the keyboard reaches a tag's path, which it can select and yank but not edit" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + const before = try tag(&s, n); + // `0` is the line's start, as in acme, where the name is ordinary text. + try s.keys(":0"); + try std.testing.expectEqual(@as(usize, 0), try tagCol(&s, "/test.txt")); + // An edit that reaches into it is refused, the cursor with it. + try s.keys("Eyd"); + try std.testing.expectEqualStrings(before, try tag(&s, n)); + try std.testing.expectEqual(@as(usize, "/test.tx".len), try tagCol(&s, "/test.txt")); + // Typed into, the path drafts a name there, as a click on it does. + try s.keys(";0ix"); + _ = try s.where("x/test.txt"); + // Esc keeps the draft pending; a second drops it. + try s.keys("\x1b\x1b"); + try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); + // After the path, the text is the tag's own to edit. + try s.keys(":A!\x1b"); + try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del!", try tag(&s, n)); + // What it yanked was the path. + try s.keys(":P"); + try s.expect(s.at("/pane/{d}/body", .{n}), "/test.txtbody\n"); +} + +test "tags: a tag remembers its cursor, starts on Save, and a click moves it" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const second = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{second}), "/second.txt"); + const focus = s.at("{d}", .{first}); + const other = s.at("{d}", .{second}); + try s.write("/focus", focus); + // Nothing to remember yet: `:` lands on Save. + try s.keys(":"); + try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); + // Home is the line's start, the path's: the keyboard reaches it. + try s.keys(home ++ right ++ right ++ right); + try std.testing.expectEqual(@as(usize, 3), try tagCol(&s, "/test.txt")); + // `:` in the tag's normal mode is the way back to the body. + try s.keys(":"); + try std.testing.expect((try s.cursor()).?[1] != (try s.where("/test.txt"))[1]); + try s.write("/focus", other); + try s.keys(":" ++ home ++ right ++ right ++ right ++ right ++ right ++ ":"); + try s.write("/focus", focus); + try s.keys(":"); + try std.testing.expectEqual(@as(usize, 3), try tagCol(&s, "/test.txt")); + try s.keys(":"); + try s.write("/focus", other); + try s.keys(":"); + try std.testing.expectEqual(@as(usize, 5), try tagCol(&s, "/second.txt")); + try s.keys(":"); + // A click in the tag puts its cursor there, the pane's keyboard with it. + try s.click("Save Tty", 6, 1); + try std.testing.expectEqual(first, try s.focused()); + try std.testing.expectEqual(@as(usize, "/test.txt Save T".len), try tagCol(&s, "/test.txt")); + // A prompt opened and left from the body leaves it there. + try s.keys("\x1b:/\x1b:"); + try std.testing.expectEqual(@as(usize, "/test.txt Save T".len), try tagCol(&s, "/test.txt")); + try s.keys(":"); + try s.expect(s.at("/pane/{d}/body", .{first}), "body\n"); +} + +test "tags: executing a word from the tag runs it and gives the keyboard back to the body" { + for ([_][]const u8{ "\t", "\n" }) |chord| { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.keys("ix\x1b"); + // `:` lands on Save; the execute key runs it, the look key looks + // it up. Either way the tag lets go of the keyboard first. + try s.keys(":"); + try s.keys(chord); + try std.testing.expectEqual(@as(usize, if (chord[0] == '\t') 1 else 0), s.saved); + try s.keys("iQ\x1b"); + try s.expect(s.at("/pane/{d}/body", .{n}), "xQbody\n"); + // ...and the tag still knows where it was. + try s.keys(":"); + try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); + } +} + +test "tags: a renamed path moves the tag's cursor with the text after it and a shorter tag pulls it back" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.keys("::"); // on Save, and back + try s.write(s.at("/pane/{d}/name", .{n}), "/界.zig"); + try s.keys(":"); + try std.testing.expectEqual(@as(usize, "/界.zig ".len - 1), try tagCol(&s, "/界")); + try s.keys(":"); + try s.replace(s.at("/pane/{d}/tag", .{n}), "é"); + try s.keys(":"); + // `/界.zig é`: the cursor is pulled back onto the last character. + try std.testing.expectEqual(@as(usize, "/界.zig ".len - 1), try tagCol(&s, "/界")); + try s.expect(s.at("/pane/{d}/body", .{n}), "body\n"); +} + +test "tags: window keys move between panes from a tag and each tag keeps its cursor" { + for ([_]bool{ false, true }) |bottom| { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + if (bottom) try s.write("/ctl", "Tagbottom on\n"); + const first = try s.focused(); + const second = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{second}), "/second.txt"); + try s.write("/focus", s.at("{d}", .{first})); + try s.keys(":" ++ home ++ "ll" ++ window ++ "j\n"); + try std.testing.expectEqual(second, try s.focused()); + try s.keys(":" ++ home ++ "llll" ++ window ++ "k\n"); + try std.testing.expectEqual(first, try s.focused()); + try s.keys(":"); + try std.testing.expectEqual(@as(usize, 2), try tagCol(&s, "/test.txt")); + try s.keys(window ++ "j\n:"); + try std.testing.expectEqual(@as(usize, 4), try tagCol(&s, "/second.txt")); + } +} + +test "tags: a tag's normal mode is the body's: motions stay in the tag and insert types into it" { + for ([_]bool{ false, true }) |bottom| { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + if (bottom) try s.write("/ctl", "Tagbottom on\n"); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const right_pane = try s.newPane(); + try s.write("/focus", s.at("{d}", .{first})); + try s.keys(":" ++ home ++ "l"); + try std.testing.expectEqual(@as(usize, 1), try tagCol(&s, "/test.txt")); + try s.keys("hkj"); + try std.testing.expectEqual(@as(usize, 0), try tagCol(&s, "/test.txt")); + try std.testing.expectEqual(first, try s.focused()); + try s.keys(window ++ "l\n"); + try std.testing.expectEqual(right_pane, try s.focused()); + try s.keys(window ++ "h\n"); + try std.testing.expectEqual(first, try s.focused()); + try s.keys(":i\u{F018}hjklHJKL:"); + try std.testing.expectEqualStrings("/test.txt Save Tty Collapse DelhjklHJKL:", try tag(&s, first)); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " mode:normal ")); + try s.expect(s.at("/pane/{d}/body", .{first}), "body\n"); + try std.testing.expectEqual(first, try s.focused()); + } +} + +test "tags: a tag with a newline in it is as tall as its lines, and each line is typed into and pointed at" { + for ([_]bool{ false, true }) |bottom| { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + if (bottom) try s.write("/ctl", "Tagbottom on\n"); + // Insert mode in a tag is a body's: Enter is a new line. + try s.keys(":A\nSecond"); + try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del\nSecond", try tag(&s, n)); + const x, const y = try s.where("/test.txt"); + try std.testing.expectEqual(.{ x, y + 1 }, try s.where("Second")); + // The caret is on the second line, after what was typed. + try std.testing.expectEqual(.{ x + 6, y + 1 }, (try s.cursor()).?); + // The body starts under (or over) the whole tag. + const by = (try s.where("1 body"))[1]; + try std.testing.expect(if (bottom) by < y else by == y + 2); + // A click on the second line puts the tag's cursor on it. + try s.keys("\x1b:"); + try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2, y + 1, x + 2, y + 1 })); + try std.testing.expectEqual(.{ x + 2, y + 1 }, (try s.cursor()).?); + try s.keys("X"); + try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del\nSeXcond", try tag(&s, n)); + try s.expect(s.at("/pane/{d}/body", .{n}), "body\n"); + } +} + +test "tags: a tag with more lines than rows scrolls to keep its caret in view" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + var many: std.ArrayList(u8) = .empty; + defer many.deinit(std.testing.allocator); + for (1..21) |i| try many.print(std.testing.allocator, "\nL{d}", .{i}); + try s.write(s.at("/pane/{d}/tag", .{n}), many.items); + const x, const y = try s.where("/test.txt"); + // To the last line: the tag scrolls so it is the bottom row, caret on it. + try s.keys(":ge"); + const lx, const ly = try s.where("L20"); + try std.testing.expectEqual(.{ lx, ly }, (try s.cursor()).?); + // What the pointer sees is what is shown: its first row is line 13. + try std.testing.expectEqual(.{ x, y }, try s.where("L13")); + try std.testing.expectError(error.NotOnScreen, s.where("L12")); + try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 0\n", .{ x + 1, y + 1, x + 1, y + 1 })); + try s.keys("X"); + _ = try s.where("LX14"); + // Back to the first line brings the prefix back into view. + try s.keys("\x1bgg"); + try std.testing.expectEqual(.{ x, y }, try s.where("/test.txt")); + try std.testing.expectEqual(y, (try s.cursor()).?[1]); + // A column tag is one row, as acme's: its lines are joined. + var layout = std.mem.tokenizeScalar(u8, try s.read("/layout"), ' '); + const col_tag = s.at("/col/{s}/tag", .{layout.next().?}); + try s.replace(col_tag, "New\nC1\nC2"); + try std.testing.expect(std.mem.indexOfScalar(u8, try s.read(col_tag), '\n') == null); + try std.testing.expectEqual(y, (try s.where("/test.txt"))[1]); +} + +test "tags: a long pane tag wraps onto rows, and a click on a wrapped row maps to the whole tag's address" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 50 12\n"); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), "/an/especially/deep/project/path/with/a/long/component/filename.zig"); + try s.write(s.at("/pane/{d}/tag", .{n}), " SyntaxBold"); + const before = try tag(&s, n); + try s.keys(":A"); + try std.testing.expect((try s.cursor()).?[0] < 50); + // SyntaxBold is on a row of its own, below the path's. + try std.testing.expect((try s.where("SyntaxBold"))[1] > (try s.where("/an/"))[1]); + // Its last character, which a wrapped row's offset not mapped back misses. + try s.click("SyntaxBold", "SyntaxBol".len, 2); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "SyntaxBold on\n") != null); + try std.testing.expectEqualStrings(before, try tag(&s, n)); +} + +test "tags: a word the tag's wrapping breaks across rows is still one word to a click" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 50 12\n"); + const n = try s.focused(); + // Lengthen the path until the wrap splits the word after "Synt" (a + // rename resets the tag's text, so it is written again each time). + var pad: usize = 1; + while (pad < 80) : (pad += 1) { + try s.write(s.at("/pane/{d}/name", .{n}), s.at("/{s}/f.zig", .{(&@as([80]u8, @splat('a')))[0..pad]})); + try s.write(s.at("/pane/{d}/tag", .{n}), " SyntaxBold"); + var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); + const split = while (rows.next()) |row| { + if (std.mem.endsWith(u8, std.mem.trimEnd(u8, row, " "), "Synt")) break true; + } else false; + if (split) break; + } else return error.NoSplit; + try s.click("Synt", 1, 2); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "SyntaxBold on\n") != null); +} + +/// A click on the tag's path, at its last character: a name drafted +/// there, its caret taken to its end. +fn clickPathEnd(s: *Session, path: []const u8) !void { + try s.click(path, path.len - 1, 1); + try s.keys("\u{F018}"); +} + +test "tags: a name drafted in the tag commits with Enter, cancels with two Escs, and changes neither body nor tag text" { + var s = try Session.initFile(std.testing.allocator, "original body\n"); + defer s.deinit(); + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + try s.write(s.at("/pane/{d}/tag", .{n}), " SyntaxBold"); + for ([_]bool{ false, true }) |commit| { + const path = std.mem.trimEnd(u8, try s.read(name), "\n"); + try clickPathEnd(&s, path); + try s.keys("\x15/new name.zig"); + try s.expect(name, s.at("{s}\n", .{path})); + _ = try s.where("/new name.zig Save Tty Collapse Del SyntaxBold"); + try s.expect(s.at("/pane/{d}/body", .{n}), "original body\n"); + // Esc keeps the draft pending; a second drops it. + try s.keys(if (commit) "\n" else "\x1b\x1b"); + try s.expect(name, if (commit) "/new name.zig\n" else "/test.txt\n"); + try std.testing.expectEqualStrings(s.at("{s} Save Tty Collapse Del SyntaxBold", .{if (commit) "/new name.zig" else "/test.txt"}), try tag(&s, n)); + // The keyboard is the body's again. + try std.testing.expect((try s.cursor()).?[1] != (try s.where("Save Tty"))[1]); + } + try std.testing.expectEqual(@as(usize, 0), s.saved); + // A name alone is no edit: the pane stays clean. + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "tags: a drafted name's edits are by grapheme, and an empty one is not committed" { + var s = try Session.initFile(std.testing.allocator, "untouched\n"); + defer s.deinit(); + const n = try s.focused(); + try clickPathEnd(&s, "/test.txt"); + try s.keys("\x15/café界\x08"); + _ = try s.where("/café Save"); + try s.keys("\x08"); + _ = try s.where("/caf Save"); + try s.keys("\x15\n"); + try std.testing.expectError(error.NotOnScreen, s.where("/test.txt")); + try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n"); + // Esc keeps the draft pending; a second drops it. + try s.keys("\x1b"); + try std.testing.expectError(error.NotOnScreen, s.where("/test.txt")); + try s.keys("\x1b"); + _ = try s.where("/test.txt"); +} + +test "tags: leaving a pane or entering a header drops an uncommitted name" { + for ([_]bool{ false, true }) |header| { + var s = try Session.initFile(std.testing.allocator, "untouched\n"); + defer s.deinit(); + const n = try s.focused(); + try clickPathEnd(&s, "/test.txt"); + try s.keys("\x15/draft"); + _ = try s.where("/draft"); + if (header) try s.click("Newcol", 1, 1) else _ = try s.newPane(); + try std.testing.expectError(error.NotOnScreen, s.where("/draft")); + try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); + } +} + +test "tags: middle clicking Save commits the drafted name before saving" { + var s = try Session.initFile(std.testing.allocator, "untouched\n"); + defer s.deinit(); + const n = try s.focused(); + try clickPathEnd(&s, "/test.txt"); + try s.keys("\x15/renamed.txt"); + try s.click("Save Tty", 0, 2); + try s.expect(s.at("/pane/{d}/name", .{n}), "/renamed.txt\n"); + try std.testing.expectEqualStrings("/renamed.txt Save Tty Collapse Del", try tag(&s, n)); + try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n"); + try std.testing.expectEqual(@as(usize, 1), s.saved); +} + +test "tags: an oversized drafted name is refused at commit and the draft stays to correct" { + var s = try Session.initFile(std.testing.allocator, "untouched\n"); + defer s.deinit(); + const n = try s.focused(); + try clickPathEnd(&s, "/test.txt"); + const excess = s.at("\x15/{s}\n", .{&@as([4999]u8, @splat('x'))}); + try s.keys(excess); + try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n"); + _ = try s.where("/xxxx"); +} + test "tags: a name typed into the tag's path survives Esc, pending, and the next Enter renames; a second Esc drops it" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); diff --git a/test/behaviour/tree.zig b/test/behaviour/tree.zig new file mode 100644 index 00000000..7f9b37a4 --- /dev/null +++ b/test/behaviour/tree.zig @@ -0,0 +1,44 @@ +//! docs/typ/reference.typ, The tree <tree>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// The first column's serial, /layout's first word. +fn firstColumn(s: *Session) ![]const u8 { + return std.mem.sliceTo(try s.read("/layout"), ' '); +} + +test "tree: index lists serial kind dirty name column, and stats 0, a view generated by each read" { + var s = try Session.initFile(std.testing.allocator, "hello\nthere\n"); + defer s.deinit(); + const n = try s.focused(); + const col = try firstColumn(&s); + const clean = s.at("{d} text 0 /test.txt {s}\n", .{ n, col }); + const index = try s.read("/index"); + const at = std.mem.indexOf(u8, index, clean) orelse return error.NotListed; + const st = try s.stat("/index"); + try std.testing.expectEqual(@as(u64, 0), st.size); + try std.testing.expectEqual(@as(u16, 0o444), st.mode); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); + try std.testing.expect(has(try s.read("/index"), s.at("{d} text 1 /test.txt {s}\n", .{ n, col }))); + // Read from an offset, as any file. + const kind = at + std.mem.indexOfScalar(u8, clean, ' ').? + 1; + try std.testing.expectEqualStrings("text", try s.readAt("/index", kind, 4)); + const made = try s.newPane(); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{made})), "\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read("/index"), s.at("\n{d} text 0 {s} {s}\n", .{ made, name, col }))); +} + +test "tree: a terminal is listed as term with its directory as name, which a write to name cannot change" { + var s = try Session.initTerm(std.testing.allocator); + defer s.deinit(); + const n = try s.focused(); + const dir = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + try std.testing.expect(std.mem.startsWith(u8, dir, "/")); + try std.testing.expect(has(try s.read("/index"), s.at("{d} term 0 {s} {s}\n", .{ n, dir, try firstColumn(&s) }))); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "/elsewhere\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid rename: a terminal is named")); +} diff --git a/test/output.zig b/test/output.zig index 2b8bf1f5..3cfece45 100644 --- a/test/output.zig +++ b/test/output.zig @@ -495,60 +495,12 @@ test "Mini syntax colors survive toggles themes and rendering without source acc } } -test "Mini failures leave its existing snapshot and focus unchanged" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const source = try p.setTestFile("abc\n"); - var path_buf: [128]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "/virtual/pane/{d}/body", .{source.serial}); - try panes.mini.open(p, 0, path); - const id = p.active; - const mini = p.panes[id].?; - const body = mini.file.?.content.ptr; - const next = p.freeSlot(); - try std.testing.expectError(error.FileNotFound, panes.mini.open(p, 0, "/virtual/does-not-exist")); - try std.testing.expectEqual(id, p.active); - try std.testing.expectEqual(body, mini.file.?.content.ptr); - try std.testing.expectEqual(next, p.freeSlot()); -} - -test "DumpConfig prints the startup path and refreshes its one output" { +test "DumpConfig prints the startup path" { const path = "/home/pardes-test/.config/pardes/init"; const p = try Pardes.init(std.testing.allocator, .{ .startup_config_path = path }); defer p.deinit(); - - try std.testing.expect(p.executeBuiltinLine(0, "DumpConfig")); - const opened = p.active; - const out = p.panes[opened].?.file.?; - for ([_][]const u8{ - "Startup config: " ++ path ++ "\n", - "Theme orchard\n", - "Last shell spawned: (none)\n", - "Shell change pending: on\n", - "LocationsConfig ", - }) |line| try std.testing.expect(std.mem.indexOf(u8, out.content, line) != null); - var default_line: [256]u8 = undefined; - try std.testing.expect(std.mem.indexOf(u8, out.content, try std.fmt.bufPrint(&default_line, "Shell {s}\n", .{config.defaultShell()})) != null); - try std.testing.expectEqualStrings(config.dump_config_buffer, std.fs.path.basename(out.path)); - try std.testing.expectEqual(panes.Output.Origin{ .cmd = .DumpConfig }, out.output.?.from); - - try std.testing.expect(p.executeBuiltinLine(0, "DumpConfig")); - try std.testing.expectEqual(opened, p.active); - var buffers: usize = 0; - for (p.panes) |slot| { - const f = (slot orelse continue).file orelse continue; - const origin = (f.output orelse continue).from; - buffers += @intFromBool(std.meta.eql(origin, panes.Output.Origin{ .cmd = .DumpConfig })); - } - try std.testing.expectEqual(@as(usize, 1), buffers); -} - -test "DumpConfig reports the absence of a per-user config path" { - const p = try Pardes.init(std.testing.allocator, .{}); - defer p.deinit(); try std.testing.expect(p.executeBuiltinLine(0, "DumpConfig")); - const report = p.panes[p.active].?.file.?.content; - try std.testing.expect(std.mem.indexOf(u8, report, "no per-user config path") != null); + try std.testing.expect(std.mem.indexOf(u8, p.panes[p.active].?.file.?.content, "Startup config: " ++ path ++ "\n") != null); } /// The file Config names, in a fresh temporary directory: `pardes/init` @@ -621,14 +573,6 @@ test "Config with no startup file opens a pane of its name, and Save makes its d try std.testing.expectEqual(std.Io.File.Kind.directory, made.kind); } -test "Config without a configuration directory says so and opens nothing" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const before = p.freeSlot(); - try std.testing.expect(p.executeBuiltinLine(p.active, "Config")); - try std.testing.expectEqual(before, p.freeSlot()); -} - test "EffectCode lists readable virtual sources used by this backend" { if (comptime !builtins.EffectCode.enabled) return; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); @@ -771,31 +715,6 @@ fn selectedOutputText(pane: *const pardes.Pane) ?[]const u8 { return line[lo..@min(line.len, hi + 1)]; } -test "LookNext/LookPrev in ordinary text skips comment slashes and retains real paths" { - if (platform == .web) return error.SkipZigTest; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 20 }); - defer p.deinit(); - const pane = try p.setTestFile("// ordinary comment\n/// documentation comment\n(//) [///] punctuation\n/\n// /tmp\n//tmp\n"); - try std.testing.expect(pane.file.?.output == null); - pardes.look.armLookWalk(p, 0); - const available = p.freeSlot(); - for ([_]struct { key: u21, row: i32, text: []const u8 }{ - .{ .key = 'n', .row = 3, .text = "/" }, - .{ .key = 'n', .row = 4, .text = "/tmp" }, - .{ .key = 'n', .row = 5, .text = "//tmp" }, - .{ .key = 'n', .row = 3, .text = "/" }, - .{ .key = 'N', .row = 5, .text = "//tmp" }, - .{ .key = 'N', .row = 4, .text = "/tmp" }, - .{ .key = 'N', .row = 3, .text = "/" }, - }) |step| { - lookKey(p, step.key); - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqual(step.row, pane.body.cur_row); - try std.testing.expectEqualStrings(step.text, selectedOutputText(pane) orelse ""); - try std.testing.expectEqual(available, p.freeSlot()); - } -} - test "LookNext/LookPrev skips formatted context and selects comment match locations" { if (!pardes.syntax.enabled) return error.SkipZigTest; for ([_]panes.Output.Origin{ .search, .{ .query = .references } }) |origin| { @@ -1005,62 +924,6 @@ test "LookPrev exactly reverses LookNext across output panes and the ring seam" try std.testing.expect(saw_first and saw_second); } -test "Themes lists the ring in sections and skips headings in both directions" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); - defer p.deinit(); - const source = try p.setTestFile("notes\n"); - pardes.colors.setThemeIndex(p, pardes.themes.len - 1); - const original_theme = p.settings.theme; - try std.testing.expect(p.executeBuiltinLine(0, "Themes")); - const tid = source.search_pane orelse return error.MissingThemeList; - const list = p.panes[tid].?; - const content = list.file.?.content; - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqual(@as(i32, 0), list.body.cur_row); - try std.testing.expectEqual(@as(usize, 0), list.file.?.scroll); - try std.testing.expectEqualStrings("# Pardes themes", modal.lineSlice(content, 0)); - // Every theme in ring order, each section's heading after a blank line - // where colors.groupAt says one starts. - var row: usize = 1; - var sections: usize = 1; - var first_break: ?usize = null; - for (pardes.themes, 0..) |theme, i| { - if (i > 0) if (pardes.colors.groupAt(i)) |title| { - try std.testing.expectEqualStrings("", modal.lineSlice(content, row)); - try std.testing.expect(std.mem.startsWith(u8, modal.lineSlice(content, row + 1), "# ")); - try std.testing.expectEqualStrings(title, modal.lineSlice(content, row + 1)["# ".len..]); - row += 2; - sections += 1; - if (first_break == null) first_break = i; - }; - const line = modal.lineSlice(content, row); - try std.testing.expect(std.mem.startsWith(u8, line, "Theme ")); - try std.testing.expectEqualStrings(theme.name, line["Theme ".len..]); - row += 1; - } - try std.testing.expectEqual(pardes.themes.len + 1 + 2 * (sections - 1), std.mem.count(u8, content, "\n")); - // acme is a current theme: it is listed before any break. - const acme = for (pardes.themes, 0..) |t, i| { - if (std.mem.eql(u8, t.name, "acme")) break i; - } else return error.MissingAcme; - const brk = first_break orelse return error.NoSections; - try std.testing.expect(acme < brk); - // Opening an imported theme's selector must still begin with the natives. - // Walk across the section break without selecting or executing a heading. - for (pardes.themes[0 .. brk + 1]) |theme| { - lookKey(p, 'n'); - try std.testing.expectEqual(tid, p.active); - const selected = selectedOutputText(list) orelse return error.MissingThemeSection; - try std.testing.expectEqualStrings(theme.name, selected["Theme ".len..]); - try std.testing.expectEqual(original_theme, p.settings.theme); - } - lookKey(p, 'N'); - const previous = selectedOutputText(list) orelse return error.MissingThemeSection; - try std.testing.expectEqualStrings(pardes.themes[brk - 1].name, previous["Theme ".len..]); - p.update(.{ .key = .{ .cp = Key.tab } }); - try std.testing.expectEqual(brk - 1, p.settings.theme); -} - test "LookNext/LookPrev selects command outputs by whole row" { if (platform == .web) return; var cwd_buf: [4096]u8 = undefined; diff --git a/test/panes.zig b/test/panes.zig index fc3f7804..db816d41 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -182,393 +182,6 @@ fn tagY(p: *Pardes, r: anytype) u16 { return p.tagTop(paneAt(p, r), r); } -test "a tag with a newline in it is as tall as its lines, and each line is typed into and pointed at" { - for ([_]bool{ false, true }) |bottom| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 14 }); - defer p.deinit(); - const pane = try p.setTestFile("body\n"); - p.presentation.enabled = false; - p.settings.tag_bottom = bottom; - p.update(.tick); - try std.testing.expectEqual(@as(u16, 1), pane.tag_rows); - const height = p.rects[0].h; - const body_rows = pane.rows; - // Insert mode in a tag is a body's: Enter is a new line. - p.update(.{ .key = .{ .cp = ':' } }); - p.update(.{ .key = .{ .cp = 'A' } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - p.update(.{ .key = .{ .cp = 'S', .text = "Second" } }); - try std.testing.expect(std.mem.endsWith(u8, pardes.tagline.curTail(pane), "\nSecond")); - try std.testing.expectEqual(@as(u16, 2), pane.tag_rows); - // The pane keeps its height; the body gives the tag its row. - try std.testing.expectEqual(height, p.rects[0].h); - try std.testing.expectEqual(body_rows - 1, pane.rows); - var frame = std.heap.ArenaAllocator.init(std.testing.allocator); - defer frame.deinit(); - const surface = try p.render(frame.allocator()); - const r = p.rects[0]; - const tag_y = p.tagTop(pane, r); - const x = r.x + pardes.TAG_TEXT_INSET; - try std.testing.expectEqualStrings("/", surface.at(x, tag_y).grapheme()); - for ("Second", 0..) |byte, i| try std.testing.expectEqual(byte, surface.at(x + @as(u16, @intCast(i)), tag_y + 1).text[0]); - try std.testing.expectEqual(pardes.FontRole.tagline, surface.at(x, tag_y + 1).style.font_role); - // the caret is on the second line, after what was typed - try std.testing.expectEqual(tag_y + 1, surface.cursor.?.y); - try std.testing.expectEqual(x + 6, surface.cursor.?.x); - // the body starts under (or over) the whole tag - const body_y = p.bodyTop(pane, r); - try std.testing.expectEqual(if (bottom) r.y else r.y + 2, body_y); - try std.testing.expectEqualStrings("b", surface.at(r.x + config.GUTTER + panes.File.gutterWidth(pane), body_y).grapheme()); - // one layer, a row to each line, for a pixel shell - var layers: usize = 0; - for (p.surface.tagLayers()) |*layer| { - if (layer.rows == 0 or layer.kind != .pane or layer.id != 0) continue; - try std.testing.expectEqual(tag_y, layer.viewport.y); - try std.testing.expectEqual(@as(u16, 2), layer.rows); - try std.testing.expectEqual(@as(u16, 2), layer.viewport.h); - layers += 1; - } - try std.testing.expectEqual(@as(usize, 1), layers); - // a click on the second line puts the tag's cursor on it - p.update(.{ .key = .{ .cp = Key.escape } }); - p.update(.{ .key = .{ .cp = ':' } }); - ClickCursorTests.click(p, .left, x + 2, tag_y + 1); - try std.testing.expect(pane.focus == .tag); - try std.testing.expectEqual(@as(i32, 1), pane.tag.cur_row); - try std.testing.expectEqual(@as(i32, 2), pane.tag.cur_col); - // and the pane's text, a row to a screen row, has both of its lines - const text = try pardes.edit.paneText(p, pane); - try std.testing.expect(std.mem.startsWith(u8, modal.lineSlice(text, 0), "/test.txt")); - try std.testing.expectEqualStrings("Second", modal.lineSlice(text, 1)); - try std.testing.expectEqualStrings("body\n", pane.file.?.content); - } -} - -test "a tag with more lines than rows scrolls to keep its caret in view" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 30 }); - defer p.deinit(); - const pane = try p.setTestFile("body\n"); - p.presentation.enabled = false; - var many: std.ArrayList(u8) = .empty; - defer many.deinit(std.testing.allocator); - for (1..21) |n| try many.print(std.testing.allocator, "\nL{d}", .{n}); - try pane.appendTag(many.items); - p.update(.tick); - try std.testing.expectEqual(@as(u16, pardes.MAX_TAG_ROWS), pane.tag_rows); - var frame = std.heap.ArenaAllocator.init(std.testing.allocator); - defer frame.deinit(); - const r = p.rects[0]; - const x = r.x + pardes.TAG_TEXT_INSET; - // To the last line: the tag scrolls so it is the bottom row, caret on it. - p.update(.{ .key = .{ .cp = ':' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'e' } }); - try std.testing.expectEqual(@as(i32, 20), pane.tag.cur_row); - const bottom = try p.render(frame.allocator()); - const tag_y = p.tagTop(pane, r); - const last_row = tag_y + pardes.MAX_TAG_ROWS - 1; - try std.testing.expectEqual(last_row, bottom.cursor.?.y); - try std.testing.expectEqualStrings("L", bottom.at(x, last_row).grapheme()); - try std.testing.expectEqualStrings("2", bottom.at(x + 1, last_row).grapheme()); - try std.testing.expectEqualStrings("0", bottom.at(x + 2, last_row).grapheme()); - // what the pointer sees is what is shown: its first row is line 13 - const text = try pardes.edit.paneText(p, pane); - try std.testing.expectEqualStrings("L13", modal.lineSlice(text, 0)); - ClickCursorTests.click(p, .left, x + 1, tag_y + 1); - try std.testing.expectEqual(@as(i32, 14), pane.tag.cur_row); - try std.testing.expectEqual(@as(i32, 1), pane.tag.cur_col); - // Back to the first line brings the prefix back into view. - p.update(.{ .key = .{ .cp = Key.escape } }); - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - const top = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(u16, 0), pane.tag_top); - try std.testing.expectEqual(tag_y, top.cursor.?.y); - try std.testing.expectEqualStrings("/", top.at(x, tag_y).grapheme()); - - // A column tag is one row, as acme's: its lines are joined. - many.clearRetainingCapacity(); - try many.appendSlice(std.testing.allocator, "New"); - for (1..21) |n| try many.print(std.testing.allocator, "\nC{d}", .{n}); - p.col_tags[0].own = try p.gpa.dupe(u8, many.items); - p.update(.tick); - try std.testing.expectEqual(pardes.COLBAR_H, p.columnBarHeight()); - try std.testing.expect(std.mem.indexOfScalar(u8, p.col_tags[0].own.?, '\n') == null); -} - -const TagNavigationTests = struct { - fn key(p: *Pardes, cp: u21) void { - p.update(.{ .key = .{ .cp = cp } }); - } - - test "s, S and | in a tag answer for the tag's own text; / still searches the body" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 20 }); - defer p.deinit(); - const pane = try p.setTestFile("Some body\n"); - try setTag(pane, " Save Del Snarf"); - key(p, ':'); - key(p, 'x'); // the tag's line - key(p, 's'); - try std.testing.expect(pane.prompt == .search and pane.prompt_for == .tag and pane.focus == .tag); - p.update(.{ .key = .{ .cp = 'S', .text = "S" } }); - key(p, Key.enter); - try std.testing.expect(pane.focus == .tag and pane.prompt == .none); - var ranges: [panes.Text.max_selections]modal.Selection = undefined; - const text = try pardes.edit.flatSurface(p, &pane.tag); - const got = pane.tag.ranges(text, 0, &ranges); - try std.testing.expectEqual(@as(usize, 2), got.n); - for (ranges[0..got.n]) |range| try std.testing.expectEqualStrings("S", text[@min(range.anchor, range.head)..@max(range.anchor, range.head)]); - // the body's selection is its own - try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); - // `/` from the tag is the body's search, as acme's Look from a tag is - key(p, ','); - key(p, '/'); - try std.testing.expect(pane.prompt == .search and pane.prompt_for == .body and pane.focus == .body); - key(p, Key.escape); - try std.testing.expect(pane.prompt == .none and pane.focus == .body); - } - - test "ms and md edit a tag's own text, and the text the edit made is read back whole" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 20 }); - defer p.deinit(); - const pane = try p.setTestFile("body\n"); - p.update(.tick); - key(p, ':'); // the tag, on `Save` the first time - for ("miwms(") |cp| key(p, cp); - try std.testing.expect(std.mem.indexOf(u8, pardes.tagline.curTail(pane), "(Save)") != null); - for ("md(") |cp| key(p, cp); - try std.testing.expect(std.mem.indexOf(u8, pardes.tagline.curTail(pane), " Save ") != null); - try std.testing.expect(std.mem.indexOf(u8, pardes.tagline.curTail(pane), "(") == null); - } - - test "the keyboard reaches a tag's path, which it can select and yank but not edit" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 20 }); - defer p.deinit(); - const pane = try p.setTestFile("body\n"); - p.update(.tick); - key(p, ':'); - // `0` is the line's start, as in acme, where the name is ordinary text - key(p, '0'); - try std.testing.expectEqual(@as(i32, 0), pane.tag.cur_col); - key(p, 'E'); - key(p, 'y'); - try std.testing.expectEqualStrings("/test.txt", p.registers.text(p.gpa, '"').?); - // an edit that reaches into it is refused, the cursor with it - const before = try std.testing.allocator.dupe(u8, try pardes.edit.flatSurface(p, &pane.tag)); - defer std.testing.allocator.free(before); - const cursor = pane.tag.cur_col; - key(p, 'd'); - try std.testing.expectEqualStrings(before, try pardes.edit.flatSurface(p, &pane.tag)); - try std.testing.expectEqual(cursor, pane.tag.cur_col); - try std.testing.expect(pane.tag.vsel.active); - // typed into, the path drafts a name there, as a click on it does - key(p, ';'); - key(p, '0'); - key(p, 'i'); - p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); - try std.testing.expect(pane.prompt == .name); - try std.testing.expectEqualStrings("x/test.txt", pane.input.own.?); - // Esc keeps the draft pending; a second drops it. - key(p, Key.escape); - key(p, Key.escape); - try std.testing.expect(pane.prompt == .none); - try std.testing.expectEqualStrings("/test.txt", pane.file.?.path); - // after the path, the text is the tag's own to edit - key(p, ':'); - key(p, 'A'); - p.update(.{ .key = .{ .cp = '!', .text = "!" } }); - try std.testing.expect(std.mem.endsWith(u8, pardes.tagline.curTail(pane), "!")); - try std.testing.expectEqualStrings("body\n", pane.file.?.content); - } - - fn window(p: *Pardes, letter: u21) void { - p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); - key(p, letter); - key(p, Key.enter); - } - - test "a tag remembers its cursor, starts on Save, and a click moves it" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 20 }); - defer p.deinit(); - const first = try p.setTestFile("body\n"); - p.presentation.enabled = false; - try std.testing.expect(p.executeBuiltinLine(0, "New")); - const second_id = p.active; - const second = p.panes[second_id].?; - try std.testing.expect(layout.splitColumn(p, second_id, second_id, false)); - p.update(.tick); - p.active = 0; - // Nothing to remember yet: `:` lands on Save, in normal mode. - key(p, ':'); - try std.testing.expect(first.focus == .tag and first.tag.mode == .normal); - const shown = try pardes.edit.flatSurface(p, &first.tag); - try std.testing.expectEqualStrings("Save", shown[@intCast(first.tag.cur_col)..][0..4]); - // Home is the line's start, the path's: the keyboard reaches it. - key(p, Key.home); - for (0..3) |_| key(p, Key.right); - try std.testing.expectEqual(@as(i32, 3), first.tag.cur_col); - // `:` in the tag's normal mode is the way back to the body. - key(p, ':'); - try std.testing.expect(first.focus == .body); - p.active = second_id; - key(p, ':'); - key(p, Key.home); - for (0..5) |_| key(p, Key.right); - key(p, ':'); - p.active = 0; - key(p, ':'); - try std.testing.expectEqual(@as(i32, 3), first.tag.cur_col); - key(p, ':'); - p.active = second_id; - key(p, ':'); - try std.testing.expectEqual(@as(i32, 5), second.tag.cur_col); - key(p, ':'); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const y = tagY(p, rect); - const prefix: u16 = @intCast(panes.File.displayWidth(try pardes.tagline.tagPrefix(p, first))); - ClickCursorTests.click(p, .left, rect.x + pardes.TAG_TEXT_INSET + prefix + 6, y); - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expect(first.focus == .tag and first.tag.mode == .insert); - try std.testing.expectEqual(@as(i32, prefix + 6), first.tag.cur_col); - key(p, Key.escape); - key(p, ':'); - p.startPrompt(first, &first.body, .{ .search = config.search_marker }); - key(p, Key.escape); - key(p, ':'); - try std.testing.expectEqual(@as(i32, prefix + 6), first.tag.cur_col); - key(p, ':'); - try std.testing.expectEqualStrings("body\n", first.file.?.content); - } - - test "executing a word from the tag runs it and gives the keyboard back to the body" { - for ([_]u21{ Key.tab, Key.enter }) |chord| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 20 }); - defer p.deinit(); - const pane = try p.setTestFile("body\n"); - p.update(.{ .key = .{ .cp = 'i' } }); - p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); - p.update(.{ .key = .{ .cp = Key.escape } }); - while (p.nextEffect()) |_| {} - // `:` lands on Save; the execute key runs it, the look key looks - // it up. Either way the tag lets go of the keyboard first. - key(p, ':'); - try std.testing.expect(pane.focus == .tag); - key(p, chord); - try std.testing.expect(pane.focus == .body); - try std.testing.expectEqual(.normal, pane.body.mode); - var saved = false; - while (p.nextEffect()) |effect| { - if (effect == .save_file) saved = true; - } - try std.testing.expectEqual(chord == Key.tab, saved); - // ...and the tag still knows where it was. - key(p, ':'); - try std.testing.expectEqualStrings("Save", (try pardes.edit.flatSurface(p, &pane.tag))[@intCast(pane.tag.cur_col)..][0..4]); - } - } - - test "a renamed path moves the tag's cursor with the text after it and a shorter tag pulls it back" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 14 }); - defer p.deinit(); - const pane = try p.setTestFile("body\n"); - key(p, ':'); // on Save - key(p, ':'); - p.gpa.free(pane.file.?.path); - pane.file.?.path = try p.gpa.dupe(u8, "/界.zig"); - p.update(.tick); - key(p, ':'); - const shown = try pardes.edit.flatSurface(p, &pane.tag); - try std.testing.expectEqualStrings("Save", shown[@intCast(pane.tag.cur_col)..][0..4]); - key(p, ':'); - try setTag(pane, "é"); - p.update(.tick); - key(p, ':'); - // `/界.zig é`: the cursor is pulled back onto the last character - try std.testing.expectEqual(@as(i32, "/界.zig ".len), pane.tag.cur_col); - try std.testing.expectEqualStrings("body\n", pane.file.?.content); - } - - test "window keys move between panes from a tag and each tag keeps its cursor" { - for ([_]bool{ false, true }) |bottom| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); - defer p.deinit(); - const first = try p.setTestFile("body\n"); - p.presentation.enabled = false; - p.settings.tag_bottom = bottom; - try std.testing.expect(p.executeBuiltinLine(0, "New")); - const second_id = p.active; - const second = p.panes[second_id].?; - p.update(.tick); - p.active = 0; - key(p, ':'); - key(p, Key.home); - key(p, 'l'); - key(p, 'l'); - window(p, 'j'); - try std.testing.expectEqual(second_id, p.active); - try std.testing.expect(first.focus == .body and second.focus == .body); - key(p, ':'); - key(p, Key.home); - for (0..4) |_| key(p, 'l'); - window(p, 'k'); - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expect(second.focus == .body); - key(p, ':'); - try std.testing.expectEqual(@as(i32, 2), first.tag.cur_col); - window(p, 'j'); - key(p, ':'); - try std.testing.expectEqual(@as(i32, 4), second.tag.cur_col); - } - } - - test "a tag's normal mode is the body's: motions stay in the tag and insert types into it" { - for ([_]bool{ false, true }) |bottom| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 20 }); - defer p.deinit(); - _ = try p.setTestFile("body\n"); - p.presentation.enabled = false; - p.settings.tag_bottom = bottom; - try std.testing.expect(p.executeBuiltinLine(0, "New")); - const right = p.active; - try std.testing.expect(layout.splitColumn(p, right, right, false)); - p.update(.tick); - p.active = 0; - key(p, ':'); - const pane = p.panes[0].?; - key(p, Key.home); - key(p, 'l'); - try std.testing.expectEqual(@as(i32, 1), pane.tag.cur_col); - key(p, 'h'); - key(p, 'k'); - key(p, 'j'); - try std.testing.expectEqual(@as(i32, 0), pane.tag.cur_col); - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expect(pane.focus == .tag and !p.header_focus); - window(p, 'l'); - try std.testing.expectEqual(right, p.active); - try std.testing.expect(pane.focus == .body and p.panes[right].?.focus == .body); - window(p, 'h'); - try std.testing.expectEqual(@as(usize, 0), p.active); - key(p, ':'); - key(p, 'i'); - key(p, Key.end); - const before = pardes.tagline.curTail(pane).len; - try std.testing.expect(pane.focus == .tag and pane.tag.mode == .insert and pane.body.mode == .normal); - for ("hjklHJKL:") |letter| { - const text = [_]u8{letter}; - p.update(.{ .key = .{ .cp = letter, .text = &text } }); - try std.testing.expect(pane.focus == .tag and pane.tag.mode == .insert); - } - try std.testing.expectEqualStrings("hjklHJKL:", pardes.tagline.curTail(pane)[before..]); - try std.testing.expectEqualStrings("body\n", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), p.active); - } - } -}; - const BodyHitTests = struct { fn mouse(layer: *const pardes.Layer, row: u16, col: u16, kind: pardes.Mouse.Kind) pardes.Mouse { const width: f32 = if (row < layer.context_rows) 8 else 10; @@ -1119,159 +732,6 @@ const ClickCursorTests = struct { try std.testing.expect(std.mem.count(u8, got, "\n") > 20); } - test "everything a B1 chord does while B1 is down is one undo step" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("one two three\n"); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = p.bodyTop(pane, rect); - const M = struct { - fn ev(pp: *Pardes, b: pardes.Mouse.Button, k: pardes.Mouse.Kind, cx: u16, cy: u16) void { - pp.update(.{ .mouse = .{ .button = b, .kind = k, .col = cx, .row = cy } }); - } - }; - // B1-B2 then B1-B3: cut, then paste back: a copy, one step. - const before = pane.file.?.history.undo_len; - M.ev(p, .left, .press, x + 4, y); - M.ev(p, .left, .drag, x + 6, y); - M.ev(p, .middle, .press, x + 6, y); - M.ev(p, .middle, .release, x + 6, y); - M.ev(p, .right, .press, x + 6, y); - M.ev(p, .right, .release, x + 6, y); - M.ev(p, .left, .release, x + 6, y); - try std.testing.expectEqualStrings("one two three\n", pane.file.?.content); - try std.testing.expectEqualStrings("two", p.registers.text(p.gpa, '"').?); - try std.testing.expect(pane.file.?.history.undo_len <= before + 1); - // A cut alone comes back with one Undo. - M.ev(p, .left, .press, x + 4, y); - M.ev(p, .left, .drag, x + 6, y); - M.ev(p, .middle, .press, x + 6, y); - M.ev(p, .middle, .release, x + 6, y); - M.ev(p, .left, .release, x + 6, y); - try std.testing.expectEqualStrings("one three\n", pane.file.?.content); - try std.testing.expect(p.executeBuiltinLine(0, "Undo")); - try std.testing.expectEqualStrings("one two three\n", pane.file.?.content); - } - - test "a double-click selects a word, a line, a bracketed or quoted text, as acme's" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("call foo_bar(a, b)\nsay \"hi there\"\n"); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = p.bodyTop(pane, rect); - const Want = struct { col: u16, row: u16, text: []const u8 }; - for ([_]Want{ - .{ .col = 6, .row = 0, .text = "foo_bar" }, - .{ .col = 13, .row = 0, .text = "a, b" }, - .{ .col = 0, .row = 0, .text = "call foo_bar(a, b)\n" }, - .{ .col = 5, .row = 1, .text = "hi there" }, - }) |w| { - click(p, .left, x + w.col, y + w.row); - click(p, .left, x + w.col, y + w.row); - try std.testing.expectEqualStrings(w.text, pardes.edit.currentSelText(p, pane).?); - // A third click is a plain click again: the caret, no selection. - click(p, .left, x + w.col, y + w.row); - try std.testing.expect(!pane.body.vsel.active); - } - // In the tag too. - const tag_text = try pardes.tagline.tagText(p, frame.allocator(), pane); - const del: u16 = @intCast(std.mem.indexOf(u8, tag_text, "Del").? + 1); - const tag_x = rect.x + pardes.TAG_TEXT_INSET + del; - click(p, .left, tag_x, p.tagTop(pane, rect)); - click(p, .left, tag_x, p.tagTop(pane, rect)); - try std.testing.expect(pane.tag.vsel.active); - const tag_now = try pardes.tagline.tagText(p, frame.allocator(), pane); - const a = modal.offsetAt(tag_now, .{ .row = @intCast(pane.tag.vsel.row), .col = @intCast(pane.tag.vsel.col) }); - const h = modal.offsetAt(tag_now, .{ .row = @intCast(pane.tag.cur_row), .col = @intCast(pane.tag.cur_col) }); - try std.testing.expectEqualStrings("Del", tag_now[a .. h + 1]); - } - - test "click cursor and insertion agree beyond line and file ends" { - for ([_]bool{ false, true }) |already_insert| { - for ([_]bool{ false, true }) |bottom_tag| { - for ([_]struct { text: []const u8, click_row: u16, row: i32, col: i32, result: []const u8 }{ - .{ .text = "abc\nxy", .click_row = 0, .row = 0, .col = 3, .result = "abcZ\nxy" }, - .{ .text = "abc\nxy", .click_row = 5, .row = 1, .col = 2, .result = "abc\nxyZ" }, - .{ .text = "abc\n", .click_row = 5, .row = 1, .col = 0, .result = "abc\nZ" }, - .{ .text = "", .click_row = 5, .row = 0, .col = 0, .result = "Z" }, - }) |case| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - p.settings.tag_bottom = bottom_tag; - const pane = try p.setTestFile(case.text); - p.presentation.enabled = false; - if (already_insert) p.update(.{ .key = .{ .cp = 'i' } }); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = bodyY(p, rect); - click(p, .left, x + 15, y + case.click_row); - try std.testing.expectEqual(case.row, pane.body.cur_row); - try std.testing.expectEqual(case.col, pane.body.cur_col); - const clicked = (try p.render(frame.allocator())).cursor orelse return error.MissingClickCursor; - try std.testing.expectEqual(x + @as(u16, @intCast(case.col)), clicked.x); - try std.testing.expectEqual(y + @as(u16, @intCast(case.row)), clicked.y); - if (!already_insert) p.update(.{ .key = .{ .cp = 'i' } }); - try std.testing.expectEqual(panes.Text.Mode.insert, pane.body.mode); - const insert = (try p.render(frame.allocator())).cursor orelse return error.MissingInsertCursor; - try std.testing.expectEqual(clicked.x, insert.x); - try std.testing.expectEqual(clicked.y, insert.y); - p.update(.{ .key = .{ .cp = 'Z', .text = "Z" } }); - try std.testing.expectEqualStrings(case.result, pane.file.?.content); - } - } - } - } - - test "click cursor snaps tab and wrapped wide glyph cells before insertion" { - for ([_]bool{ false, true }) |already_insert| { - for ([_]bool{ false, true }) |wrap| { - const source = if (wrap) "012345678901234567890123456789界tail\n" else "\t界tail\n"; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 24, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile(source); - p.settings.wrap = wrap; - p.presentation.enabled = false; - if (already_insert) p.update(.{ .key = .{ .cp = 'i' } }); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - const surface = try p.render(frame.allocator()); - var target: ?struct { x: u16, y: u16 } = null; - for (0..surface.rows) |row| for (0..surface.cols) |col| { - if (std.mem.eql(u8, surface.at(@intCast(col), @intCast(row)).grapheme(), "界")) - target = .{ .x = @intCast(col), .y = @intCast(row) }; - }; - const glyph = target orelse return error.MissingWideGlyph; - if (wrap) try std.testing.expect(glyph.y > p.rects[0].y + pardes.BOX_H); - click(p, .left, glyph.x + 1, glyph.y); - const byte = std.mem.indexOf(u8, source, "界").?; - try std.testing.expectEqual(@as(i32, @intCast(byte)), pane.body.cur_col); - const clicked = (try p.render(frame.allocator())).cursor orelse return error.MissingClickCursor; - try std.testing.expectEqual(glyph.x, clicked.x); - try std.testing.expectEqual(glyph.y, clicked.y); - if (!already_insert) p.update(.{ .key = .{ .cp = 'i' } }); - try std.testing.expectEqual(panes.Text.Mode.insert, pane.body.mode); - const insert = (try p.render(frame.allocator())).cursor orelse return error.MissingInsertCursor; - try std.testing.expectEqual(clicked.x, insert.x); - try std.testing.expectEqual(clicked.y, insert.y); - p.update(.{ .key = .{ .cp = 'Z', .text = "Z" } }); - const expected = try std.fmt.allocPrint(p.gpa, "{s}Z{s}", .{ source[0..byte], source[byte..] }); - defer p.gpa.free(expected); - try std.testing.expectEqualStrings(expected, pane.file.?.content); - } - } - } - test "click operands and hover follow the scrolled source selection" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 14 }); defer p.deinit(); @@ -1387,39 +847,6 @@ const JumpSelectionTests = struct { try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); } - test "a B1 sweep over several lines is a stream, as acme's, in a body and a tag" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("alpha\nbravo\ncharlie\n"); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = p.bodyTop(pane, rect); - // From the press to the end of its line, the line between whole, the - // last from its start: no line numbers, no rectangle. - drag(p, x + 2, y, x + 1, y + 2); - try std.testing.expectEqualStrings("pha\nbravo\nch", pardes.exec.heldSelection(p, 0).?); - // Upward, the same text. - drag(p, x + 1, y + 2, x + 2, y); - try std.testing.expectEqualStrings("pha\nbravo\nch", pardes.exec.heldSelection(p, 0).?); - // A tag of several rows sweeps the same way. - if (pane.tag.own) |own| p.gpa.free(own); - pane.tag.own = try p.gpa.dupe(u8, "Save Del\nLook Edit"); - p.sync(); - _ = try p.render(frame.allocator()); - p.sync(); - try std.testing.expectEqual(@as(u16, 2), pane.tag_rows); - const sl: panes.Pane.Sel = .{ .state = .done, .r0 = 0, .c0 = 3, .r1 = 1, .c1 = 1 }; - const text = try pardes.edit.paneText(p, pane); - const swept = try pardes.edit.selectionText(p, pane, sl); - const first_line = text[0 .. std.mem.indexOfScalar(u8, text, '\n') orelse text.len]; - const second = std.mem.sliceTo(text[first_line.len + 1 ..], '\n'); - const want = try std.fmt.allocPrint(frame.allocator(), "{s}\n{s}", .{ first_line[3..], second[0..@min(2, second.len)] }); - try std.testing.expectEqualStrings(want, swept); - } - test "jump selection clears copied sweeps in both history directions" { for ([_]bool{ false, true }) |reverse| try exercise("alpha\nbravo\n", false, reverse, 1, 3, "lpha\nbrav"); @@ -1606,138 +1033,6 @@ fn redraft(pane: *Pane, name: []const u8) void { pane.input.cur_col = @intCast(name.len); } -/// Click the tagline at the end of the path: a name draft, caret there. -fn clickPathEnd(p: *Pardes, pane: *Pane) void { - const rect = p.rects[p.active]; - const y = tagY(p, rect); - const x = rect.x + pardes.TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(pane.file.?.path) - 1)); - ClickCursorTests.click(p, .left, x, y); - std.debug.assert(pane.prompt == .name); - pane.input.cur_col = @intCast(pane.input.own.?.len); -} - -test "pane filename drafts commit and cancel without changing body or command tail" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 70, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("original body\n"); - const original = try p.gpa.dupe(u8, pane.file.?.path); - defer p.gpa.free(original); - try pane.appendTag(" SyntaxBold"); - const tail = try p.gpa.dupe(u8, pardes.tagline.curTail(pane)); - defer p.gpa.free(tail); - for ([_]bool{ false, true }) |commit| { - clickPathEnd(p, pane); - p.update(.{ .key = .{ .cp = 'u', .ctrl = true } }); - p.update(.{ .paste = "/new name.zig" }); - try std.testing.expectEqualStrings(original, pane.file.?.path); - try std.testing.expectEqualStrings("/new name.zig", pane.input.own.?); - try std.testing.expectEqualStrings(tail, pardes.tagline.curTail(pane)); - try std.testing.expectEqualStrings("original body\n", pane.file.?.content); - // Esc keeps the draft pending; a second drops it. - p.update(.{ .key = .{ .cp = if (commit) Key.enter else Key.escape } }); - if (!commit) p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(pane.focus == .body); - try std.testing.expect(pane.prompt == .none); - try std.testing.expectEqualStrings(if (commit) "/new name.zig" else original, pane.file.?.path); - try std.testing.expectEqualStrings(tail, pardes.tagline.curTail(pane)); - while (p.nextEffect()) |effect| try std.testing.expect(std.meta.activeTag(effect) != .save_file); - } - // A name alone is no edit: the pane stays clean. - try std.testing.expect(pane.file.?.revision == pane.file.?.saved_revision); -} - -test "pane filename grapheme edits and invalid empty commits preserve the buffer" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const pane = try p.setTestFile("untouched\n"); - clickPathEnd(p, pane); - p.update(.{ .key = .{ .cp = 'u', .ctrl = true } }); - p.update(.{ .paste = "/café界" }); - p.update(.{ .key = .{ .cp = Key.backspace } }); - try std.testing.expectEqualStrings("/café", pane.input.own.?); - p.update(.{ .key = .{ .cp = Key.backspace } }); - try std.testing.expectEqualStrings("/caf", pane.input.own.?); - p.update(.{ .key = .{ .cp = 'u', .ctrl = true } }); - try std.testing.expectEqualStrings("", pane.input.own.?); - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expect(pane.prompt == .name); - try std.testing.expectEqualStrings("/test.txt", pane.file.?.path); - try std.testing.expectEqualStrings("untouched\n", pane.file.?.content); - // Esc keeps the draft pending; a second drops it. - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(pane.prompt == .name); - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(pane.prompt == .none); -} - -test "leaving a pane or entering a header cancels an uncommitted filename" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const pane = try p.setTestFile("untouched\n"); - for ([_]bool{ false, true }) |header| { - p.active = 0; - pardes.tagline.exitHeader(p); - p.update(.tick); - clickPathEnd(p, pane); - p.update(.{ .paste = "/draft" }); - try std.testing.expect(pane.prompt == .name); - if (header) { - pardes.tagline.enterHeader(p, null); - p.update(.tick); - } else { - try std.testing.expect(p.executeBuiltinLine(0, "New")); - p.update(.tick); - } - try std.testing.expect(pane.prompt == .none); - try std.testing.expect(pane.focus == .body); - try std.testing.expectEqualStrings("/test.txt", pane.file.?.path); - } -} - -test "middle clicking Save commits the drafted filename before issuing a save" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 70, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("untouched\n"); - clickPathEnd(p, pane); - p.update(.{ .key = .{ .cp = 'u', .ctrl = true } }); - p.update(.{ .paste = "/renamed.txt" }); - var arena = std.heap.ArenaAllocator.init(p.gpa); - defer arena.deinit(); - _ = try p.render(arena.allocator()); - const text = try pardes.tagline.tagText(p, arena.allocator(), pane); - const save_at = std.mem.indexOf(u8, text, "Save").?; - const x: u16 = @intCast(p.rects[0].x + pardes.TAG_TEXT_INSET + save_at - pane.tag_scroll); - const y = p.rects[0].y; - while (p.nextEffect()) |_| {} - p.update(.{ .mouse = .{ .col = x, .row = y, .button = config.exec_button, .kind = .press } }); - p.update(.{ .mouse = .{ .col = x, .row = y, .button = config.exec_button, .kind = .release } }); - try std.testing.expectEqualStrings("/renamed.txt", pane.file.?.path); - try std.testing.expect(pane.prompt == .none); - try std.testing.expectEqualStrings("untouched\n", pane.file.?.content); - var saves: usize = 0; - while (p.nextEffect()) |effect| if (effect == .save_file) { - try std.testing.expectEqual(@as(u8, 0), effect.save_file.pane); - saves += 1; - }; - try std.testing.expectEqual(@as(usize, 1), saves); -} - -test "an oversized drafted name is refused at commit and the draft stays to correct" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const pane = try p.setTestFile("untouched\n"); - clickPathEnd(p, pane); - const excess = try p.gpa.alloc(u8, 5000); - defer p.gpa.free(excess); - @memset(excess, 'x'); - excess[0] = '/'; - p.update(.{ .paste = excess }); - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expect(pane.prompt == .name); - try std.testing.expectEqualStrings("/test.txt", pane.file.?.path); - try std.testing.expectEqualStrings("untouched\n", pane.file.?.content); -} - test "paste into pane commands and prompts never inserts into the body" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); @@ -1783,60 +1078,6 @@ test "a tag that exactly fills its row stays one row, its caret at the edge as a try std.testing.expectEqual(p.rects[0].x + p.rects[0].w - 1, surface.cursor.?.x); } -test "a long pane tag wraps onto rows, and a click on a wrapped row maps to the whole tag's address" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 50, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("body\n"); - p.gpa.free(pane.file.?.path); - pane.file.?.path = try p.gpa.dupe(u8, "/an/especially/deep/project/path/with/a/long/component/filename.zig"); - try pane.appendTag(" SyntaxBold"); - p.update(.{ .key = .{ .cp = ':' } }); - p.update(.{ .key = .{ .cp = 'A' } }); - var arena = std.heap.ArenaAllocator.init(p.gpa); - defer arena.deinit(); - const surface = try p.render(arena.allocator()); - try std.testing.expect(pane.tag_rows > 1); - try std.testing.expect(surface.cursor.?.x < p.rects[0].x + p.rects[0].w); - const text = try pardes.tagline.tagText(p, p.gpa, pane); - defer p.gpa.free(text); - const shown = try pardes.tagline.shownTag(p, arena.allocator(), pane); - const at = shown.fromReal(std.mem.indexOf(u8, text, "SyntaxBold").?); - const pos = modal.positionAt(shown.text, at); - const x: u16 = @intCast(p.rects[0].x + pardes.TAG_TEXT_INSET + panes.File.rawDisplayCol(modal.lineSlice(shown.text, pos.row), pos.col)); - const y = p.tagTop(pane, p.rects[0]) + @as(u16, @intCast(pos.row)); - const before = p.settings.syntax_bold; - p.update(.{ .mouse = .{ .col = x, .row = y, .button = config.exec_button, .kind = .press } }); - p.update(.{ .mouse = .{ .col = x, .row = y, .button = config.exec_button, .kind = .release } }); - try std.testing.expectEqual(!before, p.settings.syntax_bold); - try std.testing.expectEqualStrings(text, try pardes.tagline.tagText(p, p.scratch.allocator(), pane)); -} - -test "a word the tag's wrapping breaks across rows is still one word to a click" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 50, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("body\n"); - try pane.appendTag(" SyntaxBold"); - var arena = std.heap.ArenaAllocator.init(p.gpa); - defer arena.deinit(); - // Lengthen the path until the wrap splits the word after "Synt". - var pad: usize = 1; - const shown = while (pad < 80) : (pad += 1) { - p.gpa.free(pane.file.?.path); - const path = try std.fmt.allocPrint(arena.allocator(), "/{s}/f.zig", .{(&@as([80]u8, @splat('a')))[0..pad]}); - pane.file.?.path = try p.gpa.dupe(u8, path); - p.sync(); - const s = try pardes.tagline.shownTag(p, arena.allocator(), pane); - if (std.mem.indexOf(u8, s.text, "Synt\n") != null) break s; - } else return error.NoSplit; - const pos = modal.positionAt(shown.text, std.mem.indexOf(u8, shown.text, "Synt").?); - const x: u16 = @intCast(p.rects[0].x + pardes.TAG_TEXT_INSET + pos.col + 1); - const y = p.tagTop(pane, p.rects[0]) + @as(u16, @intCast(pos.row)); - const before = p.settings.syntax_bold; - p.update(.{ .mouse = .{ .col = x, .row = y, .button = config.exec_button, .kind = .press } }); - p.update(.{ .mouse = .{ .col = x, .row = y, .button = config.exec_button, .kind = .release } }); - try std.testing.expectEqual(!before, p.settings.syntax_bold); -} - test "terminal overlay recoloring never materializes unrelated history" { const testing = std.testing; const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 32, .rows = 8 }); @@ -2107,86 +1348,6 @@ test "terminal eviction and lower-region scrolling in one read preserve a surviv try std.testing.expectEqualStrings("kept edit", pane.ovl.?.text); } -test "a prompt's entry and cancellation leave the tag, the body and its cursor as they were" { - const Case = struct { key: ?Key = null, save: bool = false, marker: []const u8 }; - const cases = [_]Case{ - .{ .marker = config.search_marker }, - .{ .save = true, .marker = config.save_marker ++ "/dír space/" }, - .{ .key = .{ .cp = '|' }, .marker = config.pipe_marker }, - .{ .key = .{ .cp = '|', .alt = true }, .marker = config.pipe_marker_to }, - .{ .key = .{ .cp = '!' }, .marker = config.pipe_marker_insert }, - .{ .key = .{ .cp = '!', .alt = true }, .marker = config.pipe_marker_append }, - }; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("one\ntwo\n"); - try pane.setOwnedCwd("/dír space"); - const tail = " keep"; - for (cases) |case| { - try setTag(pane, tail); - pane.body.cur_row = 1; - pane.body.cur_col = 2; - pane.body.cur_pinned = true; - if (case.key) |key| { - p.update(.{ .key = key }); - } else if (case.save) { - p.startPrompt(pane, &pane.body, .save); - } else { - p.startPrompt(pane, &pane.body, .{ .search = config.search_marker }); - } - // The answer is typed into the prompt's own line, never the tag. - try std.testing.expect(pane.focus == .body); - try std.testing.expectEqual(.insert, pane.input.mode); - try std.testing.expectEqualStrings(case.marker, pane.promptText().?); - try std.testing.expectEqual(@as(i32, @intCast(case.marker.len)), pane.input.cur_col); - try std.testing.expectEqualStrings(tail, pane.tag.own.?); - try std.testing.expectEqualDeep(modal.Normal.State{}, pane.body.normal); - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(pane.prompt == .none); - try std.testing.expect(pane.input.own == null); - try std.testing.expectEqual(.normal, pane.body.mode); - try std.testing.expectEqualStrings(tail, pane.tag.own.?); - try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); - try std.testing.expectEqual(@as(i32, 2), pane.body.cur_col); - try std.testing.expectEqualStrings("one\ntwo\n", pane.file.?.content); - } -} - -test "a prompt opens whatever the tag already holds" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const pane = try p.setTestFile("one\n"); - const full = try p.gpa.alloc(u8, pardes.memory.limits.max_tag_tail); - defer p.gpa.free(full); - @memset(full, 'x'); - try setTag(pane, full); - for (0..3) |case| { - switch (case) { - 0 => p.startPrompt(pane, &pane.body, .{ .search = config.search_marker }), - 1 => p.startPrompt(pane, &pane.body, .save), - else => p.update(.{ .key = .{ .cp = '|' } }), - } - try std.testing.expect(pane.prompt != .none); - try std.testing.expectEqualStrings(full, pane.tag.own.?); - p.update(.{ .key = .{ .cp = Key.escape } }); - } -} - -test "generated output refuses every pipe prompt before seeding the tag" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const content = try p.gpa.dupe(u8, "generated\n"); - const pane = try panes.Output.open(p, p.freeSlot().?, "/", .{ .cmd = .Help }, "", content); - p.active = 1; - for ([_]Key{ .{ .cp = '|' }, .{ .cp = '|', .alt = true }, .{ .cp = '!' }, .{ .cp = '!', .alt = true } }) |key| { - p.update(.{ .key = key }); - try std.testing.expect(pane.prompt == .none); - try std.testing.expect(pane.tag.own == null); - try std.testing.expect(pane.focus == .body); - try std.testing.expectEqualStrings("generated\n", pane.file.?.content); - } -} - test "a prompt owns the pipe behavior through cancellation and submission" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); @@ -2214,34 +1375,6 @@ test "a prompt owns the pipe behavior through cancellation and submission" { } } -test "prompt cancellation restores the selection before regex previews" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const pane = try p.setTestFile("one two one"); - const cases = [_]struct { key: u21, pattern: []const u8, secondary: u8 }{ - .{ .key = 's', .pattern = "one", .secondary = 1 }, - .{ .key = 'S', .pattern = " ", .secondary = 2 }, - }; - for (cases) |case| { - pane.body.cur_row = 0; - pane.body.cur_col = 10; - pane.body.cur_pinned = true; - pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - pane.body.nsel = 0; - p.update(.{ .key = .{ .cp = case.key } }); - try std.testing.expect(pane.prompt == .search); - p.update(.{ .key = .{ .cp = case.pattern[0], .text = case.pattern } }); - try std.testing.expectEqual(case.secondary, pane.body.nsel); - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); - try std.testing.expectEqual(@as(u16, 0), pane.nsel_snap); - try std.testing.expectEqual(@as(i32, 10), pane.body.cur_col); - try std.testing.expectEqual(@as(i32, 0), pane.body.vsel.col); - try std.testing.expect(pane.body.vsel.active and pane.body.vsel.explicit); - try std.testing.expectEqualStrings("one two one", pane.file.?.content); - } -} - test "a prompt that cannot be opened leaves the open one as it was" { const Request = @typeInfo(@TypeOf(Pardes.startPrompt)).@"fn".param_types[3].?; for ([_]Request{ .{ .search = config.search_marker }, .save, .{ .pipe = .replace }, .{ .pipe = .ignore }, .{ .pipe = .insert }, .{ .pipe = .append } }) |request| { @@ -4600,7 +3733,6 @@ const TerminalTests = struct { }; test { - _ = TagNavigationTests; _ = TagLayerTests; _ = TagNameTintTests; _ = TreeContextTests; @@ -4622,83 +3754,6 @@ test { _ = TerminalTests; } -test "an empty counted paste leaves no count for the next motion" { - for ([_]bool{ false, true }) |empty_register| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("abcdefgh\n"); - if (empty_register) p.registers.put(p.gpa, '"', "", 0, 1, true); - for ("3p") |cp| p.update(.{ .key = .{ .cp = cp } }); - try std.testing.expectEqual(modal.Normal.State{}, pane.body.normal); - p.update(.{ .key = .{ .cp = 'l' } }); - try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - try std.testing.expectEqualStrings("abcdefgh\n", pane.file.?.content); - } -} - -test "cancelled normal gestures leave no count or subprefix after mouse and focus changes" { - const Gesture = enum { body, tag, focus, last }; - for ([_][]const u8{ "3", "3g", "mr(" }) |keys| { - for ([_]Gesture{ .body, .tag, .focus, .last }) |gesture| { - errdefer std.debug.print("\nkeys={s} gesture={s}\n", .{ keys, @tagName(gesture) }); - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 16 }); - defer p.deinit(); - const pane = try p.setTestFile("abcdefghij\n"); - p.settings.colors = false; - p.presentation.enabled = false; - try std.testing.expect(p.executeBuiltinLine(0, "New")); - const other = p.active; - try std.testing.expect(layout.splitColumn(p, other, other, false)); - p.active = 0; - p.update(.tick); - var frame = std.heap.ArenaAllocator.init(std.testing.allocator); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - for (keys) |cp| p.update(.{ .key = .{ .cp = cp } }); - try std.testing.expect(!std.meta.eql(modal.Normal.State{}, pane.body.normal)); - switch (gesture) { - .body, .tag => { - const rect = p.rects[0]; - const tag_y = tagY(p, rect); - const body_y = p.bodyTop(pane, rect); - const x = if (gesture == .tag) - rect.x + pardes.TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(try pardes.tagline.tagPrefix(p, pane)))) + 3 - else - rect.x + config.GUTTER + @as(u16, @intCast(panes.File.gutterWidth(pane))) + 3; - const y = if (gesture == .tag) tag_y else body_y; - p.update(.{ .mouse = .{ .button = config.select_button, .kind = .press, .col = x, .row = y } }); - p.update(.{ .mouse = .{ .button = config.select_button, .kind = .release, .col = x, .row = y } }); - if (gesture == .tag) { - try std.testing.expect(pane.focus == .tag); - p.update(.{ .key = .{ .cp = Key.escape } }); - p.update(.{ .key = .{ .cp = ':' } }); - try std.testing.expect(pane.focus == .body); - } - }, - .focus, .last => { - p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); - p.update(.{ .key = .{ .cp = 'l' } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expectEqual(other, p.active); - if (gesture == .last) { - p.update(.{ .key = .{ .cp = Key.escape } }); - } else { - p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); - p.update(.{ .key = .{ .cp = 'h' } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - } - try std.testing.expectEqual(@as(usize, 0), p.active); - }, - } - try std.testing.expectEqual(modal.Normal.State{}, pane.body.normal); - const col = pane.body.cur_col; - p.update(.{ .key = .{ .cp = 'l' } }); - try std.testing.expectEqual(col + 1, pane.body.cur_col); - try std.testing.expectEqualStrings("abcdefghij\n", pane.file.?.content); - } - } -} - test "cancelled normal gestures leave no count or subprefix through raw tty mode" { for ([_][]const u8{ "3", "3g", "mr(" }) |keys| { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 12 }); @@ -4963,78 +4018,6 @@ const LookResetTests = struct { for (pane.sel) |selection| try std.testing.expectEqual(.none, selection.state); for (pane.pointer_selections) |selection| try std.testing.expect(selection == null); } - - fn selected(pane: *const Pane) []const u8 { - if (!pane.body.vsel.active or pane.body.cur_row != pane.body.vsel.row) return ""; - const line = modal.lineSlice(pane.file.?.content, @intCast(pane.body.cur_row)); - const lo: usize = @intCast(@min(pane.body.cur_col, pane.body.vsel.col)); - const hi: usize = @intCast(@max(pane.body.cur_col, pane.body.vsel.col)); - return line[lo..modal.nextGrapheme(line, hi)]; - } - - test "Look replaces old selection for same-pane addresses ranges and text fallback" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); - defer p.deinit(); - const pane = try p.setTestFile("alpha\nbravo\ncharlie\nneedle\n"); - try seed(pane, "/test.txt:3:2"); - // This operand is released by Look's selection cleanup. - pardes.look.lookAt(p, 0, pane.pointer_selections[0].?.raw_text.?); - try cleared(pane); - try std.testing.expectEqual(@as(usize, 0), p.active); - try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); - try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - try std.testing.expect(!pane.body.vsel.active); - - try seed(pane, "/test.txt:2:2-4"); - pardes.look.lookAt(p, 0, pane.pointer_selections[0].?.raw_text.?); - try cleared(pane); - try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); - try std.testing.expectEqualStrings("rav", selected(pane)); - - p.gpa.free(pane.file.?.path); - pane.file.?.path = try p.gpa.dupe(u8, "/tmp/look-selection-source.txt"); - try seed(pane, "needle"); - pardes.look.lookAt(p, 0, pane.pointer_selections[0].?.raw_text.?); - try cleared(pane); - try std.testing.expectEqual(@as(i32, 3), pane.body.cur_row); - try std.testing.expectEqualStrings("needle", selected(pane)); - } - - test "Look clears the source's click highlight, not its dot, and the destination's selections before focus" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); - defer p.deinit(); - const source = try p.setTestFile("source\nlines\n"); - const id = p.freeSlot().?; - const destination = try p.newDocPane(id); - destination.file = .{ - .path = try p.gpa.dupe(u8, "/look-destination.txt"), - .content = try p.gpa.dupe(u8, "alpha\nbravo\ncharlie\n"), - .history = try panes.File.History.create(p.gpa), - }; - layout.insert(p, 0, 1, id); - layout.compute(p); - for ([_]bool{ false, true }) |range| { - p.active = 0; - try seed(source, if (range) "/look-destination.txt:2:2-4" else "/look-destination.txt:3:2"); - try seed(destination, "previous destination"); - pardes.look.lookAt(p, 0, source.pointer_selections[0].?.raw_text.?); - try std.testing.expectEqual(id, p.active); - // The source keeps its dot (acme's look leaves it); the click's - // highlight goes. - for (source.sel) |selection| try std.testing.expectEqual(.none, selection.state); - for (source.pointer_selections) |selection| try std.testing.expect(selection == null); - try std.testing.expect(source.body.vsel.active); - try cleared(destination); - if (range) { - try std.testing.expectEqual(@as(i32, 1), destination.body.cur_row); - try std.testing.expectEqualStrings("rav", selected(destination)); - } else { - try std.testing.expectEqual(@as(i32, 2), destination.body.cur_row); - try std.testing.expectEqual(@as(i32, 1), destination.body.cur_col); - try std.testing.expect(!destination.body.vsel.active); - } - } - } }; test { diff --git a/test/pdf.zig b/test/pdf.zig index fca0c240..a068fc40 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -278,56 +278,6 @@ test "PDF feature gates keep argv and builtin behavior coherent" { } } -test "a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "outline.pdf", .data = fixture }); - var path_buf: [256]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/outline.pdf", .{tmp.sub_path}); - const p = try Pardes.init(gpa, .{ .file = path, .cols = 80, .rows = 28 }); - defer p.deinit(); - panes.Pdf.openSections(p, 0); - const out_id = p.panes[0].?.search_pane orelse return error.MissingPdfSectionsOutput; - const out = p.panes[out_id].?; - const row = std.mem.sliceTo(out.file.?.content, '\n'); - const place = row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len]; // outline.pdf:P:1 - var fields = std.mem.splitScalar(u8, place, ':'); - _ = fields.next(); - const page = try std.fmt.parseInt(usize, fields.next().?, 10); - var line: [128]u8 = undefined; - // As listed: found, no miss. - p.look_missed = false; - pardes.look.lookAt(p, out_id, place); - try std.testing.expect(!p.look_missed); - // The section on another page than the row says, and one not there. - for ([_][]const u8{ try std.fmt.bufPrint(&line, "outline.pdf:{d}:1", .{page + 1}), "outline.pdf:1:999" }) |bad| { - var copy: [128]u8 = undefined; - @memcpy(copy[0..bad.len], bad); - p.look_missed = false; - const failures = p.fs.failures; - pardes.look.lookAt(p, out_id, copy[0..bad.len]); - try std.testing.expect(p.look_missed); - try std.testing.expect(p.fs.failures != failures); - } -} - -test "a PDF's body puts each line of its text on a line of its own, never a heading run into the text under it" { - if (!pdf_enabled or platform == .web) return; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const p = try PdfLinkTests.init(tmp); - defer p.deinit(); - const text = p.panes[0].?.pdf.?.ensureText(p.pdf_gpa); - // The page's three text runs, each its own line. - try std.testing.expect(std.mem.indexOf(u8, text, "target.txt\n") != null); - try std.testing.expect(std.mem.indexOf(u8, text, "https://example.com/same\n") != null); - try std.testing.expect(std.mem.indexOf(u8, text, "label\n") != null); -} - test "PdfSections Look follows the exact owning PDF, not an equal path" { if (!pdf_enabled or platform == .web) return; const gpa = std.testing.allocator; @@ -377,144 +327,6 @@ test "PdfSections Look follows the exact owning PDF, not an equal path" { try std.testing.expectEqual(first_output_id, p.active); } -test "PdfSections on a PDF with no outline says so and opens nothing" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeNoOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plain.pdf", .data = fixture }); - var path_buf: [256]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/plain.pdf", .{tmp.sub_path}); - const p = try Pardes.init(gpa, .{ .file = path }); - defer p.deinit(); - const pane = p.panes[0].?; - for (0..2) |_| { - panes.Pdf.openSections(p, 0); - try std.testing.expect(pane.search_pane == null); - try std.testing.expectEqualStrings("PdfSections: this PDF has no outline", pane.msg[0..pane.msg_len]); - } -} - -test "Recent keeps a PDF as it keeps a file: open, then closed at its page" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeNoOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plain.pdf", .data = fixture }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/plain.pdf", .{dir}); - const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 28 }); - defer p.deinit(); - pardes.look.lookAt(p, p.active, path); - p.update(.{ .resize = .{ .cols = 80, .rows = 28 } }); // an update's end announces it - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - try panes.Output.recentText(p, &out.writer, true); - var want: [4300]u8 = undefined; - try std.testing.expect(std.mem.indexOf(u8, out.written(), try std.fmt.bufPrint(&want, "{s}:1:1 open\n", .{path})) != null); -} - -test "`:N` on a PDF turns to page N, from the pane and from the root's look, and `@pN:N` logs no err" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "outline.pdf", .data = fixture }); - var path_buf: [256]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/outline.pdf", .{tmp.sub_path}); - const p = try Pardes.init(gpa, .{ .file = path, .cols = 80, .rows = 28 }); - defer p.deinit(); - const doc = p.panes[0].?; - const pages = doc.pdf.?.page_count; - try std.testing.expect(pages >= 2); - const failures = p.fs.failures; - pardes.look.lookAt(p, 0, ":2"); - try std.testing.expect(!p.look_missed); - try std.testing.expectEqual(@as(usize, 1), doc.pdf.?.page); - var line: [64]u8 = undefined; - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&line, "@p{d}:1", .{doc.serial})); - try std.testing.expect(!p.look_missed); - try std.testing.expectEqual(@as(usize, 0), doc.pdf.?.page); - try std.testing.expectEqual(failures, p.fs.failures); - // Over 9P too: the root's look at `@pN:2` answers ok and logs no err. - const ctlfs = pardes.ctlfs; - const written = p.serveFs(.{ .tag = 1, .op = .write, .node = @backingInt(ctlfs.TopFile.look), .data = try std.fmt.bufPrint(&line, "@p{d}:2\n", .{doc.serial}) }); - try std.testing.expectEqual(ctlfs.Status.ok, written.status); - try std.testing.expectEqual(@as(usize, 1), doc.pdf.?.page); - const log = @backingInt(ctlfs.TopFile.log); - const h = p.serveFs(.{ .tag = 2, .op = .open, .node = log }).handle; - const record = p.fsPayload(p.serveFs(.{ .tag = 3, .op = .read, .node = log, .handle = h, .size = 1 << 16 })); - try std.testing.expect(std.mem.indexOf(u8, record, "\nerr ") == null); - _ = p.serveFs(.{ .tag = 4, .op = .release, .node = log, .handle = h }); - while (p.nextEffect()) |_| {} - // `:N:H` on the pane is hit H of its search on page N, as `file.pdf:N:H` - // (a search row) is: with no search there is no hit 3, a miss that - // turns no page. - p.look_missed = false; - pardes.look.lookAt(p, 0, ":1"); - pardes.look.lookAt(p, 0, ":2:3"); - try std.testing.expect(p.look_missed); - try std.testing.expect(std.mem.indexOf(u8, doc.msg[0..doc.msg_len], "has no search hit 3 on page 2") != null); - try std.testing.expectEqual(@as(usize, 0), doc.pdf.?.page); - pardes.look.lookAt(p, 0, ":2"); - // Page 0 is no page: the has-no-page miss. - p.look_missed = false; - pardes.look.lookAt(p, 0, ":0"); - try std.testing.expect(p.look_missed); - try std.testing.expect(std.mem.indexOf(u8, doc.msg[0..doc.msg_len], "has no page 0") != null); - p.look_missed = false; - // A byte offset or a pattern is no PDF address: refused with words. - pardes.look.lookAt(p, 0, ":#3"); - try std.testing.expect(p.look_missed); - try std.testing.expect(std.mem.indexOf(u8, doc.msg[0..doc.msg_len], "its address is a page, :N") != null); - p.look_missed = false; - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&line, ":{d}", .{pages + 1})); - try std.testing.expect(p.look_missed); - try std.testing.expectEqual(@as(usize, 1), doc.pdf.?.page); // where the 9P look left it -} - -test "a look at a PDF's page past its last is a miss, never the last page" { - if (!pdf_enabled or platform == .web) return; - const gpa = std.testing.allocator; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - const fixture = try pdf_impl.makeNoOutlineTestPdf(gpa); - defer gpa.free(fixture); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plain.pdf", .data = fixture }); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.pdf", .data = fixture }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/plain.pdf", .{dir}); - const p = try Pardes.init(gpa, .{ .file = path, .cols = 80, .rows = 28 }); - defer p.deinit(); - const doc = p.panes[0].?; - try std.testing.expect(doc.hasPdf()); - var line: [4300]u8 = undefined; - // Open: it stays on its page. - const page = doc.pdf.?.page; - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&line, "{s}:999", .{path})); - try std.testing.expect(p.look_missed); - try std.testing.expectEqual(page, doc.pdf.?.page); - // Not open: nothing opens. - p.look_missed = false; - var before: usize = 0; - for (p.panes) |slot| before += @intFromBool(slot != null); - pardes.look.lookAt(p, 0, try std.fmt.bufPrint(&line, "{s}/other.pdf:999", .{dir})); - try std.testing.expect(p.look_missed); - var after: usize = 0; - for (p.panes) |slot| after += @intFromBool(slot != null); - try std.testing.expectEqual(before, after); -} - test "SDL PDF raster policy is materially denser than Kitty" { try std.testing.expect( pardes.sdl_pdf_raster_policy.dpi >= pardes.kitty_pdf_raster_policy.dpi * 2, @@ -1334,7 +1146,7 @@ fn copySourceRect( return out; } -test "PDF normal adapter consumes unsupported actions and navigates page fallback" { +test "PDF normal mode keeps `:`, SPC and `/` on their shared paths" { if (!pdf_enabled or platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ @@ -1344,61 +1156,8 @@ test "PDF normal adapter consumes unsupported actions and navigates page fallbac }); defer p.deinit(); const pane = p.panes[0].?; - const pv = &pane.pdf.?; - try std.testing.expect(pv.page_count > 3); p.native_images = false; - // Every vertical vocabulary falls back to counted page changes when the - // shell cannot place native pixels. - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'j' } }); - try std.testing.expectEqual(@as(usize, 2), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'k' } }); - try std.testing.expectEqual(@as(usize, 0), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'd', .ctrl = true } }); - try std.testing.expectEqual(@as(usize, 2), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'u', .ctrl = true } }); - try std.testing.expectEqual(@as(usize, 0), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'f', .ctrl = true } }); - try std.testing.expectEqual(@as(usize, 2), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'b', .ctrl = true } }); - try std.testing.expectEqual(@as(usize, 0), pv.page); - - // Prefix actions and the counted text goto-line action map to pages. - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'e' } }); - try std.testing.expectEqual(pv.page_count - 1, pv.page); - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - try std.testing.expectEqual(@as(usize, 0), pv.page); - p.update(.{ .key = .{ .cp = '3' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - try std.testing.expectEqual(@as(usize, 2), pv.page); - p.update(.{ .key = .{ .cp = '2' } }); - p.update(.{ .key = .{ .cp = 'G' } }); - try std.testing.expectEqual(@as(usize, 1), pv.page); - - // Editing/selection actions are consumed no-ops: parser state clears, - // placeholder cells never acquire a range, and PDF state stays intact. - const page_before_noop = pv.page; - const revision_before_noop = pv.next_raster_revision; - p.update(.{ .key = .{ .cp = '4' } }); - p.update(.{ .key = .{ .cp = 'd' } }); - p.update(.{ .key = .{ .cp = 'v' } }); - p.update(.{ .key = .{ .cp = '|' } }); - try std.testing.expectEqual(page_before_noop, pv.page); - try std.testing.expectEqual(revision_before_noop, pv.next_raster_revision); - try std.testing.expectEqual(@as(u32, 0), pane.body.normal.count); - try std.testing.expectEqual(.none, pane.body.normal.prefix); - try std.testing.expect(!pane.body.vsel.active and !pane.body.msel.active and pane.body.nsel == 0); - try std.testing.expect(pane.focus == .body); - // Cross-pane BODY-NORMAL actions keep their established shared paths. p.update(.{ .key = .{ .cp = ':' } }); try std.testing.expect(pane.focus == .tag); @@ -1552,7 +1311,7 @@ test "PDF continuous strip renders every intersecting short page" { } } -test "PdfTint cycles pane-local state without adding tag clutter" { +test "PDF tint resolves a theme's null page colors to its chrome" { if (!pdf_enabled or platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ @@ -1561,66 +1320,6 @@ test "PdfTint cycles pane-local state without adding tag clutter" { .rows = 24, }); defer p.deinit(); - const pane = p.panes[0].?; - const pv = &pane.pdf.?; - const tint_builtin = std.meta.stringToEnum(Builtin, "PdfTint") orelse - return error.MissingPdfTintBuiltin; - - try std.testing.expectEqual(PdfTintMode.filtered, pv.tint); - const initial_tag = try pardes.tagline.tagText(p, p.scratch.allocator(), pane); - try std.testing.expect(std.mem.startsWith(u8, initial_tag, pv.path)); - try std.testing.expect(std.mem.endsWith(u8, initial_tag, " PdfSections PdfTint PdfFit Collapse Del")); - try std.testing.expect(std.mem.indexOf(u8, initial_tag, "filtered") == null); - - try std.testing.expect(pardes.test_api.runBuiltin( - p, - @tagName(tint_builtin), - 0, - "", - null, - )); - try std.testing.expectEqual(PdfTintMode.full, pv.tint); - const full_tag = try pardes.tagline.tagText(p, p.scratch.allocator(), pane); - try std.testing.expectEqualStrings(initial_tag, full_tag); - try std.testing.expect(std.mem.indexOf(u8, full_tag, "full") == null); - - // Running the same pane-scoped word in a terminal cannot mutate the PDF - // next to it. A FILE boot is one pane now, so that terminal is asked for - // here rather than inherited from init. - _ = try p.newShell(1, ""); - _ = layout.splitColumn(p, 0, 1, false); - p.sync(); // rects for the new column, exactly as the two-pane boot did - try std.testing.expect(!hasPdf(p.panes[1].?)); - try std.testing.expect(pardes.test_api.runBuiltin( - p, - @tagName(tint_builtin), - 1, - "", - null, - )); - try std.testing.expectEqual(PdfTintMode.full, pv.tint); - - try std.testing.expect(pardes.test_api.runBuiltin( - p, - @tagName(tint_builtin), - 0, - "", - null, - )); - try std.testing.expectEqual(PdfTintMode.disabled, pv.tint); - const disabled_tag = try pardes.tagline.tagText(p, p.scratch.allocator(), pane); - try std.testing.expectEqualStrings(initial_tag, disabled_tag); - try std.testing.expect(std.mem.indexOf(u8, disabled_tag, "disabled") == null); - try std.testing.expect(pardes.test_api.runBuiltin( - p, - @tagName(tint_builtin), - 0, - "", - null, - )); - try std.testing.expectEqual(PdfTintMode.filtered, pv.tint); - try std.testing.expectEqual(PdfFitMode.width, pv.fit); - // `dark` intentionally leaves page bg/fg null. PDF tint resolves those // deterministically to its chrome colors rather than host defaults. const dark_index = for (themes, 0..) |th, i| { diff --git a/test/session.zig b/test/session.zig index 6d214874..16a131fd 100644 --- a/test/session.zig +++ b/test/session.zig @@ -183,6 +183,72 @@ pub const Session = struct { return @ptrCast(@alignCast(s.core)); } + // ninep: + + /// An open kept until `clunk`, as an event reader or a lock holder + /// keeps one. + pub const Fid = struct { node: u64, handle: u32 }; + + /// `path` opened (omode 0 read, 1 write, 2 both) and held. + pub fn hold(s: *Session, path: []const u8, omode: u8) !Fid { + const node, const h = try s.open(path, omode); + return .{ .node = node, .handle = h }; + } + + /// One read on a held open (an event record, an answer). + pub fn readFid(s: *Session, f: Fid) ![]const u8 { + const r = s.serve(.{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .size = 1 << 16 }); + if (r.status != .ok) return refusal("a held open", r); + return s.arena.allocator().dupe(u8, pardesOf(s).fsPayload(r)); + } + + /// One write on a held open: 0 when taken, else its errno, its words in `said`. + pub fn writeFid(s: *Session, f: Fid, data: []const u8) !u16 { + const r = s.serve(.{ .tag = 0, .op = .write, .node = f.node, .handle = f.handle, .data = data }); + if (r.status == .ok) return 0; + s.said = try s.arena.allocator().dupe(u8, r.ename); + return r.errno; + } + + pub fn clunk(s: *Session, f: Fid) void { + _ = s.serve(.{ .tag = 0, .op = .release, .node = f.node, .handle = f.handle, .opened = true }); + } + + /// A held open's file truncated, as an OTRUNC open (`> data`) does. + pub fn truncate(s: *Session, f: Fid) !void { + const r = s.serve(.{ .tag = 0, .op = .setattr, .node = f.node, .truncate = true }); + if (r.status != .ok) return refusal("truncate", r); + } + + /// One read of `size` bytes at `off`, on an open of its own. + pub fn readAt(s: *Session, path: []const u8, off: u64, size: u32) ![]const u8 { + const f = try s.hold(path, 0); + defer s.clunk(f); + const r = s.serve(.{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .off = off, .size = size }); + if (r.status != .ok) return refusal(path, r); + return s.arena.allocator().dupe(u8, pardesOf(s).fsPayload(r)); + } + + /// What a stat of `path` answers: its size, mode and qid version. + pub fn stat(s: *Session, path: []const u8) !struct { size: u64, mode: u16, version: u32 } { + const f = try s.hold(path, 0); + defer s.clunk(f); + const r = s.serve(.{ .tag = 0, .op = .getattr, .node = f.node }); + if (r.status != .ok) return refusal(path, r); + return .{ .size = r.attr.size, .mode = r.attr.mode, .version = r.attr.version }; + } + + // panes: + /// `data` written to `path` truncated first, as a shell's `>` does. + pub fn replace(s: *Session, path: []const u8, data: []const u8) !void { + const node, const h = try s.open(path, 1); + defer _ = s.serve(.{ .tag = 0, .op = .release, .node = node, .handle = h, .opened = true }); + const t = s.serve(.{ .tag = 0, .op = .setattr, .node = node, .handle = h, .truncate = true }); + if (t.status != .ok) return refusal(path, t); + const w = s.serve(.{ .tag = 0, .op = .write, .node = node, .handle = h, .data = data }); + if (w.status != .ok) return refusal(path, w); + } + // core: /// /layout as its columns, left to right, `column:pane,pane` each and @@ -225,6 +291,29 @@ pub const Session = struct { pub fn where(s: *Session, text: []const u8) !struct { usize, usize } { return s.find(text); } + + // pdf: + + /// A Look of `path` at the root (one relative to the working directory + /// made absolute first); the serial of the pane with the keyboard after. + /// A PDF in a build without them (-Dmupdf=false: no PdfTint in + /// /commands) skips the test. + pub fn lookFile(s: *Session, path: []const u8) !u32 { + if (std.mem.endsWith(u8, path, ".pdf") and std.mem.indexOf(u8, try s.read("/commands"), "PdfTint") == null) return error.SkipZigTest; + var buf: [4096]u8 = undefined; + const abs = if (std.fs.path.isAbsolute(path)) path else s.at("{s}/{s}", .{ buf[0..try std.Io.Dir.cwd().realPath(std.testing.io, &buf)], path }); + try s.write("/look", s.at("{s}\n", .{abs})); + return s.focused(); + } + + /// The page a PDF pane shows and its pages, from its tag's `[page/pages]`. + pub fn pdfPage(s: *Session, n: u32) !struct { u32, u32 } { + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + const lo = std.mem.indexOf(u8, tag, " [") orelse return error.NoPage; + const close = std.mem.indexOfScalarPos(u8, tag, lo, ']') orelse return error.NoPage; + 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) }; + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { |
