diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/behaviour.zig | 3 | ||||
| -rw-r--r-- | test/behaviour/addresses.zig | 111 | ||||
| -rw-r--r-- | test/behaviour/builtins.zig | 36 | ||||
| -rw-r--r-- | test/behaviour/columns_and_tags.zig | 61 | ||||
| -rw-r--r-- | test/behaviour/edit.zig | 28 | ||||
| -rw-r--r-- | test/behaviour/event.zig | 16 | ||||
| -rw-r--r-- | test/behaviour/flags.zig | 102 | ||||
| -rw-r--r-- | test/behaviour/from_acme.zig | 28 | ||||
| -rw-r--r-- | test/behaviour/keys.zig | 152 | ||||
| -rw-r--r-- | test/behaviour/limits.zig | 22 | ||||
| -rw-r--r-- | test/behaviour/look_and_exec.zig | 390 | ||||
| -rw-r--r-- | test/behaviour/modes.zig | 382 | ||||
| -rw-r--r-- | test/behaviour/mounts.zig | 7 | ||||
| -rw-r--r-- | test/behaviour/mouse.zig | 65 | ||||
| -rw-r--r-- | test/behaviour/other_files.zig | 246 | ||||
| -rw-r--r-- | test/behaviour/panes.zig | 541 | ||||
| -rw-r--r-- | test/behaviour/placement.zig | 110 | ||||
| -rw-r--r-- | test/behaviour/repls.zig | 11 | ||||
| -rw-r--r-- | test/behaviour/root_ctl.zig | 56 | ||||
| -rw-r--r-- | test/behaviour/rules.zig | 22 | ||||
| -rw-r--r-- | test/behaviour/settings.zig | 168 | ||||
| -rw-r--r-- | test/behaviour/tags.zig | 84 | ||||
| -rw-r--r-- | test/output.zig | 70 | ||||
| -rw-r--r-- | test/panes.zig | 177 | ||||
| -rw-r--r-- | test/pdf.zig | 28 | ||||
| -rw-r--r-- | test/session.zig | 29 |
26 files changed, 2688 insertions, 257 deletions
diff --git a/test/behaviour.zig b/test/behaviour.zig index d9b15183..e19a847f 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -4,10 +4,12 @@ //! so a test sees the session as a 9P client does. test { _ = @import("behaviour/addresses.zig"); + _ = @import("behaviour/builtins.zig"); _ = @import("behaviour/columns_and_tags.zig"); _ = @import("behaviour/edit.zig"); _ = @import("behaviour/event.zig"); _ = @import("behaviour/flags.zig"); + _ = @import("behaviour/from_acme.zig"); _ = @import("behaviour/keys.zig"); _ = @import("behaviour/limits.zig"); _ = @import("behaviour/log.zig"); @@ -18,6 +20,7 @@ test { _ = @import("behaviour/other_files.zig"); _ = @import("behaviour/panes.zig"); _ = @import("behaviour/placement.zig"); + _ = @import("behaviour/repls.zig"); _ = @import("behaviour/root_ctl.zig"); _ = @import("behaviour/rules.zig"); _ = @import("behaviour/settings.zig"); diff --git a/test/behaviour/addresses.zig b/test/behaviour/addresses.zig index a1474c3d..8e57ad9d 100644 --- a/test/behaviour/addresses.zig +++ b/test/behaviour/addresses.zig @@ -63,3 +63,114 @@ test "addresses: sel reads the selection, and a write to sel replaces it and is try s.expect(body, "ab\ntwo\n"); try s.expect(sel, "ab"); } + +test "addresses: a malformed address is bad address syntax; on a pane with no text any address is refused as nothing to address" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const addr = s.at("/pane/{d}/addr", .{try s.focused()}); + for ([_][]const u8{ "1x", "/(/", ",,x" }) |expr| { + try expectEqual(@as(u16, 22), try s.refused(addr, expr)); + try std.testing.expect(s.said.len > 0); + } + _ = try s.refused(addr, "1x"); + try std.testing.expectEqualStrings("bad address syntax", s.said); + // A terminal (as an image or a PDF): addr, dot and limit alike. + var t = try Session.initTerm(std.testing.allocator); + defer t.deinit(); + const term = try t.focused(); + for ([_][]const u8{ "addr", "dot", "limit" }) |file| for ([_][]const u8{ "1", "1x" }) |expr| { + try expectEqual(@as(u16, 22), try t.refused(t.at("/pane/{d}/{s}", .{ term, file }), expr)); + try std.testing.expectEqualStrings("invalid address: this pane has no text to address", t.said); + }; +} + +test "addresses: a 9P write moves dot only as acme's textinsert does" { + // dot is `cd` of `abcdef\n` (2 4); two bytes go in at `at`. + for ([_][3]u32{ + .{ 0, 4, 6 }, // before: shifted + .{ 2, 2, 6 }, // at its start: grown + .{ 3, 2, 6 }, // inside: grown + .{ 4, 2, 4 }, // at its end: left + .{ 6, 2, 4 }, // after: left + }) |c| { + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(dot, "2 4"); + try s.write(s.at("/pane/{d}/addr", .{n}), s.at("#{d}", .{c[0]})); + try s.write(s.at("/pane/{d}/data", .{n}), "XX"); + try expectEqual(.{ c[1], c[2] }, try s.range(dot)); + } + // A body append leaves dot, and a sel write after it replaces dot, not + // the text appended. + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(dot, "2 4"); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); + try expectEqual(.{ @as(u32, 2), @as(u32, 4) }, try s.range(dot)); + try s.write(s.at("/pane/{d}/sel", .{n}), "YY"); + try s.expect(s.at("/pane/{d}/body", .{n}), "abYYef\nmore\n"); +} + +test "addresses: a pair out of order or out of range is refused as the address form is, not clamped" { + var s = try Session.initFile(std.testing.allocator, "abcdef\n"); + defer s.deinit(); + const addr = s.at("/pane/{d}/addr", .{try s.focused()}); + for ([_][2][]const u8{ + .{ "5 2", "addresses out of order" }, + .{ "#5,#2", "addresses out of order" }, + .{ "2 99", "address out of range" }, + .{ "#2,#99", "address out of range" }, + }) |c| { + try expectEqual(@as(u16, 22), try s.refused(addr, c[0])); + try std.testing.expectEqualStrings(c[1], s.said); + } + try s.write(addr, "2 4"); + try expectEqual(.{ @as(u32, 2), @as(u32, 4) }, try s.range(addr)); +} + +test "addresses: every address lands on a rune, and a combining mark and a lone \\r are places of their own" { + // e, U+0301 (two bytes, 1-2), x, \r, \n + var s = try Session.initFile(std.testing.allocator, "e\u{0301}x\r\n"); + defer s.deinit(); + const n = try s.focused(); + const addr = s.at("/pane/{d}/addr", .{n}); + for ([_]struct { []const u8, u32, u32 }{ + .{ "#1,#3", 1, 3 }, // the mark alone + .{ "#2", 1, 1 }, // inside it: back to its start + .{ "#4,#5", 4, 5 }, // the \r alone + .{ "1:2", 1, 1 }, // line:col the same + .{ "1:3", 1, 1 }, + .{ "0/\xCC/", 1, 3 }, // a match inside a rune covers it + }) |c| { + try s.write(addr, c[0]); + try expectEqual(.{ c[1], c[2] }, try s.range(addr)); + } + // Copied to dot, it reads back the same. + const dot = s.at("/pane/{d}/dot", .{n}); + try s.write(addr, "#1,#3"); + try s.write(dot, " 1 3 "); + try s.expect(dot, " 1 3 "); + try s.write(addr, "#4,#5"); + try s.expect(s.at("/pane/{d}/xdata", .{n}), "\r"); +} + +test "addresses: truncating data or xdata deletes only the addressed range, so > data replaces it" { + var s = try Session.initFile(std.testing.allocator, ""); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + for ([_][]const u8{ "data", "xdata" }) |file| { + try s.replace(body, "hello world\nsecond line\n"); + try s.write(s.at("/pane/{d}/addr", .{n}), "#0,#5"); + const h = try s.hold(s.at("/pane/{d}/{s}", .{ n, file }), 1); + try s.truncate(h); + try s.expect(body, " world\nsecond line\n"); + try expectEqual(@as(u16, 0), try s.writeFid(h, "HOWDY")); + s.clunk(h); + try s.expect(body, "HOWDY world\nsecond line\n"); + } +} diff --git a/test/behaviour/builtins.zig b/test/behaviour/builtins.zig new file mode 100644 index 00000000..3c16a4a2 --- /dev/null +++ b/test/behaviour/builtins.zig @@ -0,0 +1,36 @@ +//! docs/typ/builtins.typ, the builtins' glossary. +const std = @import("std"); +const Session = @import("session").Session; + +test "builtins: Mini maps a file of the pane's directory over pardes's own source of that name, in one pane, refreshed only when asked again" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "build.zig", .data = "x\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "a\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/look", s.at("{s}/a.txt\n", .{at})); + const a = try s.focused(); + // The directory's one-line build.zig, one braille row, not pardes's. + try s.write(s.at("/pane/{d}/exec", .{a}), "Mini build.zig\n"); + const mini = try s.focused(); + try std.testing.expect(mini != a); + const body = s.at("/pane/{d}/body", .{mini}); + try s.expect(body, "⠁\n"); + const index = try s.read("/index"); + try s.write(s.at("/pane/{d}/exec", .{mini}), "Mini build.zig\n"); + try std.testing.expectEqual(mini, try s.focused()); + try s.expect("/index", index); + // A snapshot: the file grown on disk shows only at the next Mini. + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "build.zig", .data = "xx\n" }); + try s.expect(body, "⠁\n"); + try s.write(s.at("/pane/{d}/exec", .{mini}), "Mini build.zig\n"); + try s.expect(body, "⠉\n"); + try s.expect("/index", index); + // Save asks for a path; nothing is written for it. + try s.write(s.at("/pane/{d}/exec", .{mini}), "Save\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("ask {d} save path\n", .{mini})) != null); + try std.testing.expectEqual(@as(usize, 0), s.saved); +} diff --git a/test/behaviour/columns_and_tags.zig b/test/behaviour/columns_and_tags.zig index a1add750..b9e3b3f5 100644 --- a/test/behaviour/columns_and_tags.zig +++ b/test/behaviour/columns_and_tags.zig @@ -235,3 +235,64 @@ test "columns-and-tags: a refused Delcol changes nothing: no pane, no focus" { try s.expect("/focus", focus); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unsaved ") != null); } + +/// Column `i`'s x and width, from /layout. +fn columnBox(s: *Session, i: usize) !struct { usize, usize } { + var lines = std.mem.tokenizeScalar(u8, try s.read("/layout"), '\n'); + for (0..i) |_| _ = lines.next(); + var fields = std.mem.tokenizeScalar(u8, lines.next().?, ' '); + _ = fields.next(); // serial + _ = fields.next(); // index + return .{ try std.fmt.parseInt(usize, fields.next().?, 10), try std.fmt.parseInt(usize, fields.next().?, 10) }; +} + +test "columns-and-tags: a column tag lights the word under the mouse, is clicked and swept into, typed at its caret, and scrolls from its column's edge" { + var s = try Session.initFile(std.testing.allocator, "untouched\n"); + defer s.deinit(); + const file = try s.focused(); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/exec", .{try s.column(1)}), "New\n"); + for (0..2) |i| { + const tag = s.at("/col/{d}/tag", .{try s.column(i)}); + const left, const w = try columnBox(&s, i); + // Its text starts two cells into the column. + const x = left + 2; + const width = w - 2; + const plain = try s.bg(x, 1); + try s.write("/mousein", s.at("m {d} 1 0\n", .{x})); + const lit = try s.bg(x, 1); + try std.testing.expect(!std.mem.eql(u8, plain, lit)); + try expectEqualStrings(plain, try s.bg(x + 4, 1)); // Tty, another word + // Pressed, the keyboard is on the tag, its caret where it was pressed. + try s.write("/mousein", s.at("m {d} 1 1\n", .{x})); + try s.expect("/focus", ""); + try std.testing.expectEqual(.{ x, @as(usize, 1) }, (try s.cursor()).?); + // Swept over three cells: those and the caret's, normal mode's + // selection taking the cell under it, in the selection's colour. + try s.write("/mousein", s.at("m {d} 1 1\nm {d} 1 0\n", .{ x + 3, x + 3 })); + for (0..4) |k| try expectEqualStrings(lit, try s.bg(x + k, 1)); + try std.testing.expect(!std.mem.eql(u8, lit, try s.bg(x + 4, 1))); + // A clicked tag is in normal mode: `i` types at its caret. + try s.keys("\u{F00D}iHelp "); + try std.testing.expect(std.mem.startsWith(u8, try s.read(tag), "Help New Tty")); + try std.testing.expectEqual(x + 5, (try s.cursor()).?[0]); + // A tag one short of its room: its caret at the end is drawn on the + // last cell, and one more letter scrolls the text, never the caret. + const as: [80]u8 = @splat('a'); + const row = as[0 .. width - 1]; + try s.replace(tag, row); + try s.keys("\u{F018}"); + try std.testing.expectEqual(x + width - 1, (try s.cursor()).?[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), row) != null); + try s.keys("b"); + try std.testing.expectEqual(x + width - 1, (try s.cursor()).?[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), s.at("{s}b", .{row[1..]})) != null); + try s.keys("\u{F00D}"); + try std.testing.expectEqual(x, (try s.cursor()).?[0]); + // Scrolled back: the text drawn whole from its first cell again. + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), s.at("{s}b", .{row})) != null); + try s.keys("\x1b\x1b"); + try s.write("/mousein", "m 0 23 0\n"); + } + try s.expect(s.at("/pane/{d}/body", .{file}), "untouched\n"); +} diff --git a/test/behaviour/edit.zig b/test/behaviour/edit.zig index 3d1a731e..83b1b464 100644 --- a/test/behaviour/edit.zig +++ b/test/behaviour/edit.zig @@ -29,3 +29,31 @@ test "edit: a bad command inside an Edit block written whole is named, not the } try std.testing.expect(std.mem.indexOf(u8, s.said, "z") != null); try s.expect(s.at("/pane/{d}/body", .{n}), "abc\n"); } + +test "edit: Edit through a pane's ctl renames every foo in one undo step, and a failed one changes nothing" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y foo\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(ctl, "Edit ,x/foo/c/foobar/\n"); + try s.expect(body, "foobar x foobar y foobar\n"); + try std.testing.expectEqual(@as(u16, 5), try s.refused(ctl, "Edit ,s/nothing/x/\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Edit: no substitution")); + try s.expect(body, "foobar x foobar y foobar\n"); + // The whole rename is one undo step. + try s.write(ctl, "Undo"); + try s.expect(body, "foo x foo y foo\n"); +} + +test "edit: Edit's p and = never fail for want of room for +Errors: the text is logged instead" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 80 6\n"); + const index = try s.read("/index"); + try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Edit ,p\n"); + const log = try s.read("/log"); + try std.testing.expect(std.mem.indexOf(u8, log, " one\n") != null and std.mem.indexOf(u8, log, " two\n") != null); + try s.expect("/index", index); // the +Errors that had no room is gone +} diff --git a/test/behaviour/event.zig b/test/behaviour/event.zig index 8aebe8f4..77a80e30 100644 --- a/test/behaviour/event.zig +++ b/test/behaviour/event.zig @@ -53,3 +53,19 @@ test "event: a refused Del written back through event fails that write, EIO, wit try std.testing.expect(has(s.said, "Modified")); _ = try s.read(s.at("/pane/{d}/ctl", .{n})); // still there } + +test "event: a bad event record says what is wrong with it" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const event = try s.hold(s.at("/pane/{d}/event", .{try s.focused()}), 2); + defer s.clunk(event); + for ([_][2][]const u8{ + .{ "MI0 1 0 1 x\n", "only a look or exec" }, + .{ "MX3 1 0 0 \n", "ends before it starts" }, + .{ "MX9 9 0 0 \n", "range past end" }, + .{ "garbage\n", "a record is" }, + }) |c| { + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(event, c[0])); + try std.testing.expect(has(s.said, c[1])); + } +} diff --git a/test/behaviour/flags.zig b/test/behaviour/flags.zig index 656d219f..ead267fe 100644 --- a/test/behaviour/flags.zig +++ b/test/behaviour/flags.zig @@ -40,3 +40,105 @@ test "flags: dirty, mark and scroll read and write the state acme kept behind ct try t.expect(term_dirty, "0\n"); try std.testing.expectEqual(@as(u16, 22), try t.refused(term_dirty, "1")); } + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "flags: Undo and Redo on a pane's ctl step its body through its edits, and say when there is none, counted" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const ctl = s.at("/pane/{d}/ctl", .{n}); + try s.write(body, "two\n"); + try s.expect(body, "one\ntwo\n"); + try s.write(ctl, "Undo"); + try s.expect(body, "one\n"); + try s.write(ctl, "Redo"); + try s.expect(body, "one\ntwo\n"); + // Nothing left: said, and the write still succeeds, as acme's is silent. + try s.write(ctl, "Redo"); + try std.testing.expect(has(try s.read("/log"), "Redo: nothing to redo")); + try s.write(ctl, "Undo"); + try s.write(ctl, "Undo"); + try std.testing.expect(has(try s.read("/log"), "Undo: nothing to undo")); + // Said again word for word, it is that line counted, as err's are. + for (0..3) |_| try s.write(ctl, "Undo"); + try std.testing.expect(has(try s.read("/log"), "Undo: nothing to undo (x4)\n")); +} + +test "flags: dirty goes back to 0 when the text is the saved text again" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + const dirty = s.at("/pane/{d}/dirty", .{n}); + const ctl = s.at("/pane/{d}/ctl", .{n}); + try s.expect(dirty, "0\n"); + try s.write(s.at("/pane/{d}/body", .{n}), "two\n"); + try s.expect(dirty, "1\n"); + try s.write(ctl, "Undo"); + try s.expect(dirty, "0\n"); + try s.write(ctl, "Redo"); + try s.expect(dirty, "1\n"); +} + +test "flags: a > into an empty body is one undo step, as into any other" { + var s = try Session.initFile(std.testing.allocator, ""); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + try s.replace(body, "fresh\n"); + try s.expect(body, "fresh\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, ""); +} + +test "flags: a write that changes nothing, an empty range deleted or a range replaced by itself, is no undo step and no new version" { + var s = try Session.initFile(std.testing.allocator, "abc\ndef\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const addr = s.at("/pane/{d}/addr", .{n}); + const data = s.at("/pane/{d}/data", .{n}); + const ctl = s.at("/pane/{d}/ctl", .{n}); + // One real change, so a step made after it would be the one undone. + try s.write(addr, "1"); + try s.write(data, "abd\n"); + const version = (try s.stat(body)).version; + // Each as a shell does it: `: > data` after an empty address, `echo + // abd > data` over the line it holds, and Edit's `s/d/d/`. + try s.write(addr, "#2,#2"); + const h = try s.hold(data, 1); + try s.truncate(h); + s.clunk(h); + try s.write(addr, "1"); + try s.write(data, "abd\n"); + try s.write(ctl, "Edit ,s/d/d/\n"); + try s.expect(body, "abd\ndef\n"); + try std.testing.expectEqual(version, (try s.stat(body)).version); + try s.write(ctl, "Undo\n"); + try s.expect(body, "abc\ndef\n"); +} + +test "flags: an open's writes in a row go in as one edit, seen by the next request" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const version = (try s.stat(body)).version; + const h = try s.hold(body, 1); + for (0..100) |_| try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h, "0123456789\n")); + try std.testing.expectEqual(@as(usize, 4 + 1100), (try s.read(body)).len); + try std.testing.expectEqual(version + 1, (try s.stat(body)).version); + s.clunk(h); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "one\n"); + // data inserts at its address go on the same way. + try s.write(s.at("/pane/{d}/addr", .{n}), "#0"); + const d = try s.hold(s.at("/pane/{d}/data", .{n}), 1); + _ = try s.writeFid(d, "a"); + _ = try s.writeFid(d, "b"); + s.clunk(d); + try s.expect(body, "abone\n"); +} diff --git a/test/behaviour/from_acme.zig b/test/behaviour/from_acme.zig new file mode 100644 index 00000000..62d4fc55 --- /dev/null +++ b/test/behaviour/from_acme.zig @@ -0,0 +1,28 @@ +//! docs/typ/reference.typ, Coming from acme <from-acme>. +const std = @import("std"); +const Session = @import("session").Session; + +test "from-acme: acme's words run as pardes's (Put is Save, Delete a Del that does not ask) or are refused, never run as commands" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + for ([_][]const u8{ "Snarf\n", "Indent\n", "Local\n", "Abort\n" }) |line| { + for ([_][]const u8{ s.at("/pane/{d}/ctl", .{n}), s.at("/pane/{d}/exec", .{n}) }) |path| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(path, line)); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is not a pardes builtin") != null); + } + } + // Load is Restore: whatever it says, never acme's word refused. + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Load /tmp/pardes-surely-no-dump.zon\n"); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is not a pardes builtin") == null); + // Put saves, as Save does. + try s.write(s.at("/pane/{d}/name", .{n}), "/tmp/pardes-put.txt\n"); + try s.write(s.at("/pane/{d}/exec", .{n}), "Put\n"); + try std.testing.expectEqual(@as(usize, 1), s.saved); + // Delete closes an edited pane without asking. + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{made}), "/tmp/pardes-delete.txt\n"); + try s.write(s.at("/pane/{d}/body", .{made}), "edited\n"); + try s.write(s.at("/pane/{d}/ctl", .{made}), "Delete\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "/tmp/pardes-delete.txt") == null); +} diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig index 3880b116..476504ce 100644 --- a/test/behaviour/keys.zig +++ b/test/behaviour/keys.zig @@ -226,3 +226,155 @@ test "keys: SPC j n and SPC j p walk Themes' rows, whole, never its section head try s.keys("\t"); try std.testing.expectEqualStrings(names.items[brk - 1]["Theme ".len..], try setting(&s, "Theme")); } + +test "keys: Save's prompt on a scratch starts from the scratch's directory, one slash after it" { + for ([_][2][]const u8{ .{ "/dír space/f.txt", " Save /dír space/zq" }, .{ "/f.txt", " Save /zq" } }) |c| { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}\n", .{c[0]})); + // New from the file's pane: a scratch in its directory. + try s.write(s.at("/pane/{d}/exec", .{n}), "New\n"); + try std.testing.expect(try s.focused() != n); + try s.keys(":\tzq"); + _ = try s.where(c[1]); + try s.keys("\x1b"); + } +} + +/// A B1 sweep from cell (x0, y0) to (x1, y1). +fn sweep(s: *Session, x0: usize, y0: usize, x1: usize, y1: usize) !void { + try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 0\n", .{ x0, y0, x1, y1, x1, y1 })); +} + +/// Whether any cell /screen draws is in `style`. +fn drawsIn(s: *Session, style: []const u8) !bool { + for (try s.styles()) |cell| if (std.mem.eql(u8, cell, style)) return true; + return false; +} + +test "keys: Ctrl-o and Ctrl-i jump back and forth, leaving no sweep drawn, a wrapped Unicode and tab one included" { + const ctl = "\u{F017}"; + for ([_]struct { []const u8, []const u8, bool }{ + .{ "alpha\nbravo\n", "alpha", false }, + .{ "界\tabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n", "界", true }, + }) |c| for ([_]bool{ false, true }) |reverse| { + const prefix, const first, const wrap = c; + var buf: [4096]u8 = undefined; + @memcpy(buf[0..prefix.len], prefix); + for (0..80) |i| @memcpy(buf[prefix.len + i * 27 ..][0..27], "unrelated destination text\n"); + var s = try Session.initFile(std.testing.allocator, buf[0 .. prefix.len + 80 * 27]); + defer s.deinit(); + const n = try s.focused(); + s.detach(); + try s.write("/ctl", "size 34 10\n"); + try s.write("/ctl", if (wrap) "Wrap on\n" else "Wrap off\n"); + const dot = s.at("/pane/{d}/dot", .{n}); + const addr = s.at("/pane/{d}/addr", .{n}); + // Two places in the jump list: line 60, then line 1. + try s.keys("60G1G"); + const x, const y = try s.where(first); + const plain = (try s.styles())[0]; + if (reverse) try sweep(&s, x + 3, y + 1, x + 1, y) else try sweep(&s, x + 1, y, x + 3, y + 1); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + const swept = (try s.styles())[(y + 1) * cols + x]; + try std.testing.expect(!std.mem.eql(u8, swept, plain)); + try s.keys(ctl ++ "o"); + try s.write(addr, "60"); + const line60 = (try s.range(addr))[0]; + try std.testing.expectEqual(.{ line60, line60 }, try s.range(dot)); + try std.testing.expectError(error.NotOnScreen, s.where(first)); + try std.testing.expect(!try drawsIn(&s, swept)); + // A sweep made after Back is gone after Forward. + const x2, const y2 = try s.where("unrelated"); + try sweep(&s, x2 + 1, y2, x2 + 3, y2 + 1); + try std.testing.expect(try drawsIn(&s, swept)); + try s.keys(ctl ++ "i"); + // Back where the sweep left the caret, nothing selected. + const q0, const q1 = try s.range(dot); + try std.testing.expect(q0 == q1 and q1 < line60); + _ = try s.where(first); + try std.testing.expect(!try drawsIn(&s, swept)); + }; +} + +// Its rows whole and Tab running one are the Themes walk's test above. +test "keys: SPC j n from the pane a Look gave the keyboard walks back into the command output" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + for ([_][]const u8{ "a.txt", "b.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/look", s.at("{s}/a.txt\n", .{at})); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{a}), "Themes\n"); + try s.keys(" jn"); + const themes = try s.focused(); + try std.testing.expect(themes != a); + const sel = s.at("/pane/{d}/sel", .{themes}); + try s.expect(sel, "Theme orchard"); + try s.write(s.at("/pane/{d}/exec", .{a}), "Look b.txt\n"); + try std.testing.expect(try s.focused() != themes); + try s.keys(" jn"); + try std.testing.expectEqual(themes, try s.focused()); + try std.testing.expect(std.mem.startsWith(u8, try s.read(sel), "Theme ")); +} + +test "keys: SPC j n and SPC j p walk on from the place a Look's +Search listing chose, its keyboard left in the file" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "look-owner.txt", .data = "alpha target\nbeta target\ngamma target\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "LookWord list\n"); // the +Search listing is what is walked + try s.write("/look", s.at("{s}/look-owner.txt\n", .{at})); + const file = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{file}), "Look target\n"); + try std.testing.expectEqual(file, try s.focused()); + // The Look took the first; the walk goes on from it, and back. + try s.keys(" jn"); + const results = try s.focused(); + try std.testing.expect(results != file); + const sel = s.at("/pane/{d}/sel", .{results}); + try s.expect(sel, "look-owner.txt:2:6-11"); + try s.keys(" jp"); + try std.testing.expectEqual(results, try s.focused()); + try s.expect(sel, "look-owner.txt:1:7-12"); +} + +/// Screen row `row` as drawn. +fn screenRow(s: *Session, row: usize) ![]const u8 { + var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); + for (0..row) |_| _ = rows.next(); + return rows.next().?; +} + +test "keys: a PDF in normal mode keeps : (its tag and back), SPC (a leader, cancelled by a key it has not) and / (a search prompt, Esc closing it)" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const body = (try s.cursor()).?; + const tag = (try s.where("docs/design.pdf [1/"))[1]; + try s.keys(":"); + const on_tag = (try s.cursor()).?[1]; + try std.testing.expect(on_tag == tag or on_tag == tag + 1); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal ")); + try s.keys(":"); + try std.testing.expectEqual(body, (try s.cursor()).?); + // The leader shows its keys at the row's end until one it has not. + try s.keys(" "); + try std.testing.expect(std.mem.endsWith(u8, std.mem.trimEnd(u8, try screenRow(&s, body[1]), " "), " SPC")); + try s.keys("!"); + try std.testing.expect(!std.mem.endsWith(u8, std.mem.trimEnd(u8, try screenRow(&s, body[1]), " "), " SPC")); + try s.keys("/"); + try std.testing.expect(!std.meta.eql(body, (try s.cursor()).?)); + try std.testing.expectEqual(n, try s.focused()); + try s.keys("\x1b"); + try std.testing.expectEqual(body, (try s.cursor()).?); + try std.testing.expectEqual(n, try s.focused()); +} diff --git a/test/behaviour/limits.zig b/test/behaviour/limits.zig index 08c864eb..d84d4490 100644 --- a/test/behaviour/limits.zig +++ b/test/behaviour/limits.zig @@ -16,3 +16,25 @@ test "limits: a builtin that needs a pane says so when every slot is taken" { try std.testing.expectEqualStrings("help: no space for a pane: 64 max", s.said); try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "help: no space for a pane") != null); } + +test "limits: a ctl line over 1 MiB is refused once, EINVAL, and its tail is not run as a line of its own" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = try s.hold(s.at("/pane/{d}/ctl", .{n}), 1); + const chunk: [4096]u8 = @splat('z'); + var refused: usize = 0; + for (0..270) |_| { + const errno = try s.writeFid(ctl, &chunk); + if (errno != 0) { + refused += 1; + try std.testing.expectEqual(@as(u16, 22), errno); + } + } + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ctl, "zzz\nEdit ,d\n")); + s.clunk(ctl); + try std.testing.expectEqual(@as(usize, 1), refused); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unknown control message") == null); + // What came after the long line's newline ran. + try s.expect(s.at("/pane/{d}/body", .{n}), ""); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 98cab342..0d8056da 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -323,3 +323,393 @@ test "look-and-exec: a +PdfSections row's first number is the page: a section no _ = try s.refused(look, "design.pdf:1:999\n"); try expectEqual(@as(usize, 2), try errs(&s)); } + +test "look-and-exec: a look's miss repeats the path whole up to 256 bytes, then gives up its middle to an ellipsis" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const long = "some/deeply/nested/directory/that/goes/on/and/on/with/a_rather_long_file_name.txt"; + try s.write("/look", long ++ "\n"); + try std.testing.expect(has(try s.read("/log"), "look: no match for \"" ++ long ++ "\"\n")); + const huge = std.mem.asBytes(&@as([150][2]u8, @splat("é".*))); // 300 bytes: its middle cut to fit 256, between characters + const kept = std.mem.asBytes(&@as([63][2]u8, @splat("é".*))); + try s.write("/look", huge ++ "\n"); + try std.testing.expect(has(try s.read("/log"), "look: no match for \"" ++ kept ++ "…" ++ kept ++ "\"\n")); +} + +test "look-and-exec: @p<serial>:<address> takes any sam address; a miss and an unknown serial are errs" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\nbar\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + const Case = struct { []const u8, u32, u32 }; + for ([_]Case{ + .{ "0/foo/", 0, 3 }, + .{ "/foo/", 6, 9 }, + .{ "#12,#15", 12, 15 }, + .{ "$", 16, 16 }, + .{ "1,2", 0, 16 }, + // Backward, as in addr: from the dot's start, wrapping to the last. + .{ "?foo?", 6, 9 }, + .{ "$-?y?", 10, 11 }, + }) |c| { + try s.write("/look", s.at("@p{d}:{s}\n", .{ n, c[0] })); + try expectEqual(.{ c[1], c[2] }, try s.range(dot)); + } + try s.write("/look", s.at("@p{d}:/zzqq/\n", .{n})); + try std.testing.expect(has(try s.read("/log"), ": no match for regexp")); + try s.expect("/look", ""); + try s.write("/look", "@p999:/foo/\n"); + try std.testing.expect(has(try s.read("/log"), "look: no pane @p999")); + try s.expect("/look", ""); + // A line address to no pane is a miss too, not the active pane. + try s.write("/look", "@p77:1\n"); + try std.testing.expect(has(try s.read("/log"), "look: no pane @p77")); + try s.expect("/look", ""); +} + +test "look-and-exec: a look at file:3:0 is refused as addr 3:0 is, a column counting from 1" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n"); + defer s.deinit(); + const n = try s.focused(); + try expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/addr", .{n}), "3:0")); + const by_addr = s.said; + try expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/look", .{n}), "/test.txt:3:0\n")); + try std.testing.expectEqualStrings(by_addr, s.said); + try s.write(s.at("/pane/{d}/look", .{n}), "/test.txt:3:1\n"); +} + +test "look-and-exec: a look of ./x, x or /abs/x that is not there is a miss: answered, the err logged" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const look = s.at("/pane/{d}/look", .{try s.focused()}); + for ([_][]const u8{ "./zzq-nosuch.txt\n", "zzq-nosuch.txt\n", "/zzq-nosuch.txt\n" }) |line| try s.write(look, line); + try std.testing.expect(has(try s.read("/log"), "look: ./zzq-nosuch.txt: no such file")); +} + +test "look-and-exec: a look at a ./ or ../ name that is not there is a miss naming the file; a plain one is looked for as text" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDirPath(std.testing.io, "many"); + 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.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/many/a.txt\n", .{dir})); + for ([_][]const u8{ "./zz.txt", "../zz.txt" }) |name| { + try s.write(s.at("/pane/{d}/look", .{n}), s.at("{s}\n", .{name})); + try std.testing.expect(has(try s.read("/log"), s.at("look: {s}: no such file", .{name}))); + } + try s.write(s.at("/pane/{d}/look", .{n}), "zz.txt\n"); + try std.testing.expect(has(try s.read("/log"), "look: no match for \"zz.txt\"")); +} + +test "look-and-exec: a look at file:addr whose file is not there is one miss, quoting what was written, in a terminal too" { + var s = try Session.initTerm(std.testing.allocator); + defer s.deinit(); + const look = s.at("/pane/{d}/look", .{try s.focused()}); + const before = try errs(&s); + for ([_][]const u8{ "zzq:#3", "zzq:2", "zzq:/re/" }) |written| { + try s.write(look, s.at("{s}\n", .{written})); + try expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/log"), s.at("look: no match for \"{s}\"\n", .{written}))); + } + try expectEqual(before + 3, try errs(&s)); +} + +test "look-and-exec: a builtin that needs its argument, written bare to an exec, is EINVAL as through ctl" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{try s.focused()}), "/tagexec" }) |path| { + try expectEqual(@as(u16, 22), try s.refused(path, "Mount\n")); + try std.testing.expectEqualStrings("wrong #args in control message \"Mount\"", s.said); + } +} + +test "look-and-exec: a builtin that takes no argument, written with one to an exec, is refused wrong #args, as at a ctl" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{try s.focused()}) }) |path| for ([_][]const u8{ "Config extra\n", "DumpConfig extra\n" }) |line| { + try expectEqual(@as(u16, 22), try s.refused(path, line)); + try std.testing.expect(std.mem.startsWith(u8, s.said, "wrong #args in control message")); + }; +} + +/// A fresh directory's absolute path, kept until deinit. +fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 { + var buf: [4096]u8 = undefined; + return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}); +} + +test "look-and-exec: a look opens spaced paths and locations without editing the source pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "two words.zig", .data = "first\nsecond\nthird\n" }); + var s = try Session.initFile(std.testing.allocator, "source stays intact\n"); + defer s.deinit(); + const path = s.at("{s}/two words.zig", .{try tmpPath(&s, &tmp)}); + const source = try s.focused(); + const ctl = try s.read(s.at("/pane/{d}/ctl", .{source})); + try s.write(s.at("/pane/{d}/dot", .{source}), "3 3"); + try s.write(s.at("/pane/{d}/look", .{source}), s.at("{s}:2:3\n", .{path})); + const opened = try s.focused(); + try std.testing.expect(opened != source); + try s.expect(s.at("/pane/{d}/name", .{opened}), s.at("{s}\n", .{path})); + try s.expect(s.at("/pane/{d}/body", .{opened}), "first\nsecond\nthird\n"); + try expectEqual(@as(u32, 8), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); // line 2, column 3 + // The source: its text, its undo steps and its cursor as they were. + try s.expect(s.at("/pane/{d}/body", .{source}), "source stays intact\n"); + const after = try s.read(s.at("/pane/{d}/ctl", .{source})); + try std.testing.expectEqualStrings(ctl[0..60], after[0..60]); + var fields = std.mem.tokenizeScalar(u8, after, ' '); + for (0..8) |_| _ = fields.next(); + try std.testing.expectEqualStrings("0", fields.next().?); // still no undo step + try expectEqual(.{ @as(u32, 3), @as(u32, 3) }, try s.range(s.at("/pane/{d}/dot", .{source}))); + try s.expect("/look", s.at("{d}\n", .{opened})); + // From the root, with the source focused: the same pane, moved. + try s.write("/focus", s.at("{d}\n", .{source})); + try s.write("/look", s.at("{s}:3:2\n", .{path})); + try expectEqual(opened, try s.focused()); + try expectEqual(@as(u32, 14), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); // line 3, column 2 + try s.expect("/look", s.at("{d}\n", .{opened})); +} + +test "look-and-exec: a look validates the whole batch before opening virtual files" { + var s = try Session.initFile(std.testing.allocator, "source\n"); + defer s.deinit(); + const source = try s.focused(); + const look = s.at("/pane/{d}/look", .{source}); + const index = try s.read("/index"); + for ([_][]const u8{ "/virtual/index\x00ignored", "/virtual/index\n/virtual/\x01index\n" }) |invalid| { + try expectEqual(@as(u16, 22), try s.refused(look, invalid)); + try s.expect("/index", index); + try expectEqual(source, try s.focused()); + try s.expect(s.at("/pane/{d}/body", .{source}), "source\n"); + } + try s.write(look, "/n/self/index\n"); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), "/virtual/index\n"); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/body", .{opened})), "/test.txt")); + try s.expect(s.at("/pane/{d}/body", .{source}), "source\n"); +} + +test "look-and-exec: status reads the editor, and look and exec validate whole batches" { + var s = try Session.initFile(std.testing.allocator, "Msg root\n"); + defer s.deinit(); + const status = try s.read("/status"); + var rows = std.mem.tokenizeScalar(u8, status, '\n'); + try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "pid ")); + try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "version ")); + try std.testing.expectEqualStrings(s.at("panes {d}", .{lines(try s.read("/index"))}), rows.next().?); + try std.testing.expect(rows.next() == null); + const st = try s.stat("/status"); + try expectEqual(@as(u64, status.len), st.size); + try expectEqual(@as(u16, 0o444), st.mode & 0o777); + try expectEqual(@as(u16, 1), try s.refused("/status", "anything\n")); + try s.expect("/exec", ""); + const index = try s.read("/index"); + for ([_][]const u8{ "Msg a\x00b", "Msg x\nMsg \x01y\n" }) |invalid| { + try expectEqual(@as(u16, 22), try s.refused("/exec", invalid)); + try s.expect("/index", index); + } + // An empty write is a no-op, not a command. + try s.write("/exec", "\n \n"); + try s.expect("/exec", ""); + try s.expect("/status", status); + const n = try s.focused(); + try s.write("/exec", "Msg hello there\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} hello there\n", .{n}))); + try s.expect("/exec", s.at("{d}\n", .{n})); + try s.write("/look", "/virtual/index\n"); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), "/virtual/index\n"); + try s.expect("/look", s.at("{d}\n", .{opened})); + try s.write("/exec", "New\n"); + const made = try s.focused(); + try std.testing.expect(made != opened); + try s.expect("/exec", s.at("{d}\n", .{made})); +} + +test "look-and-exec: a look at a file it may not read fails the write naming the file" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "locked.txt", .data = "secret\n", .flags = .{ .permissions = .fromMode(0o000) } }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const path = s.at("{s}/locked.txt", .{try tmpPath(&s, &tmp)}); + if (try s.refused("/look", s.at("{s}\n", .{path})) == 0) return error.SkipZigTest; // root reads anything + try std.testing.expectEqualStrings(s.at("look: {s}: permission denied", .{path}), s.said); +} + +test "look-and-exec: a look line's trailing colon is dropped, and a look whose address fails never leaves its file open" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const before = lines(try s.read("/index")); + // Failing addresses: nothing opened, the write still answered. + for ([_][]const u8{ s.at("{s}/a.txt:/nomatch/\n", .{dir}), s.at("{s}/a.txt:9:\n", .{dir}) }) |line| { + _ = try s.refused("/look", line); + try expectEqual(before, lines(try s.read("/index"))); + } + // `a.txt:2:3:` is `a.txt:2:3`: it opens there. + try s.write("/look", s.at("{s}/a.txt:2:3:\n", .{dir})); + try expectEqual(before + 1, lines(try s.read("/index"))); + const opened = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{opened}), s.at("{s}/a.txt\n", .{dir})); + try expectEqual(@as(u32, 6), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); +} + +test "look-and-exec: a look that misses file:/re/ names the file, and is logged as its pane's" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "alpha\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const first = try s.focused(); + try s.write("/look", s.at("{s}/f.txt\n", .{dir})); + const f = try s.focused(); + _ = try s.refused(s.at("/pane/{d}/look", .{first}), s.at("{s}/f.txt:/zzqq/\n", .{dir})); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} look: {s}/f.txt: no match for regexp", .{ f, dir }))); +} + +test "look-and-exec: a look that goes to another pane leaves the looking pane's dot alone" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f one\nf two\n" }); + var s = try Session.initFile(std.testing.allocator, "alpha bravo\ncharlie\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const source = try s.focused(); + const other = try s.newPane(); + try s.write(s.at("/pane/{d}/body", .{other}), "one\ntwo\nthree\n"); + for ([_][]const u8{ s.at("@p{d}:2\n", .{other}), s.at("@p{d}:/three/\n", .{other}), s.at("{s}/f.txt:2\n", .{dir}) }) |line| { + try s.write(s.at("/pane/{d}/dot", .{source}), "6 11"); + try s.write(s.at("/pane/{d}/look", .{source}), line); + try std.testing.expect(try s.focused() != source); + try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(s.at("/pane/{d}/dot", .{source}))); + } +} + +test "look-and-exec: a looked word is found in its own pane, next place after the dot, wrapping, and opens no pane; LookWord list lists it in +Search" { + var s = try Session.initFile(std.testing.allocator, "foo x foo y\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const index = try s.read("/index"); + for ([_]u32{ 0, 6, 0 }) |at| { + try s.write(look, "foo\n"); + try expectEqual(.{ at, at + 3 }, try s.range(s.at("/pane/{d}/dot", .{n}))); + } + try s.expect("/index", index); + try s.write("/ctl", "LookWord list\n"); + try s.write(look, "foo\n"); + try std.testing.expect(lines(try s.read("/index")) > lines(index)); + // ...and look reads back that +Search pane, where the places are. + const search = try paneNamed(&s, "/+Search"); + try std.testing.expect(has(try s.read(look), s.at("{d}\n", .{search}))); +} + +test "look-and-exec: a second LookWord list look in a directory refills its +Search rather than stacking another" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "words.txt", .data = "alpha beta\nalpha beta\n" }); + 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}/name", .{n}), s.at("{s}/words.txt\n", .{try tmpPath(&s, &tmp)})); + try s.write(s.at("/pane/{d}/exec", .{n}), "Get\n"); + try s.write("/ctl", "LookWord list\n"); + try s.write(s.at("/pane/{d}/look", .{n}), "alpha\n"); + const first = try paneNamed(&s, "/+Search"); + try s.write(s.at("/pane/{d}/look", .{n}), "beta\n"); + try expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/index"), "/+Search ")); + try expectEqual(first, try paneNamed(&s, "/+Search")); + // Refilled with beta's places (columns 7-10), not alpha's. + try std.testing.expect(has(try s.read(s.at("/pane/{d}/body", .{first})), "words.txt:1:7-10\n")); +} + +test "look-and-exec: `@pN:` looks at pane N as `a.txt:` looks at a.txt, and a line past the end is said one way, `:N` or `file:N`" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const look = s.at("/pane/{d}/look", .{n}); + const before = try errs(&s); + try s.write(look, s.at("@p{d}:\n", .{n})); + try expectEqual(before, try errs(&s)); + try s.write(look, ":99999\n"); + try s.write(look, "/test.txt:99999\n"); + try std.testing.expect(has(try s.read("/log"), "/test.txt:99999 has no line 99999 (x2)\n")); +} + +test "look-and-exec: an exec line over the command limit fails the write and says the limit; a builtin's may be longer" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const long = "echo " ++ &@as([1024]u8, @splat('y')) ++ "\n"; + try expectEqual(@as(u16, 22), try s.refused("/exec", long)); + try std.testing.expectEqualStrings("invalid command line: a command line is at most 1024 bytes", s.said); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: invalid command line: a command line is at most 1024 bytes", .{n}))); + _ = try s.refused("/exec", "echo a\x01b\n"); + try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said); + try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: invalid command line: it holds", .{n}))); + try s.write("/exec", "Msg " ++ &@as([1100]u8, @splat('z')) ++ "\n"); +} + +test "look-and-exec: a builtin that fails through exec, tagexec or a column's exec fails the write with one err and no msg" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // Another failure first: the pane's `new` is logged after it. + _ = try s.refused("/ctl", "Unmount yyy\n"); + for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{n}), "/tagexec", s.at("/col/{d}/exec", .{try s.column(0)}) }) |path| { + try std.testing.expect(try s.refused(path, "Unmount zzz\n") != 0); + try std.testing.expect(has(s.said, "Unmount: no such mount zzz")); + } + const log = try s.read("/log"); + try std.testing.expect(!has(log, "msg ")); + // The root's exec ran at the pane, so its err and the pane's exec's are + // the same record, counted. + try expectEqual(@as(usize, 3), std.mem.count(u8, log, "Unmount: no such mount zzz")); + try std.testing.expect(has(log, "mount zzz (x2)")); + // A control character is refused with its reason. + try expectEqual(@as(u16, 22), try s.refused("/exec", "Msg a\x01b\n")); + try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said); + // A look miss: one err, no msg; the same miss again is that record counted. + try s.write("/look", "zzqqnotthere\n"); + try std.testing.expect(has(try s.read("/log"), "look: no match for \"zzqqnotthere\"")); + try std.testing.expect(!has(try s.read("/log"), "msg ")); + try s.write("/look", "zzqqnotthere\n"); + try std.testing.expect(has(try s.read("/log"), "look: no match for \"zzqqnotthere\" (x2)\n")); +} + +test "look-and-exec: a served-tree pane spoils no Grep, and Look . from a gone directory is ENOENT, not /virtual/" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "hit.txt", .data = "needle here\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{first}), s.at("{s}/other.txt\n", .{try tmpPath(&s, &tmp)})); + // A pane of the served tree, and one named its root, whose directory is `/`. + try s.write(s.at("/pane/{d}/name", .{try s.newPane()}), "/virtual/index\n"); + try s.write(s.at("/pane/{d}/name", .{try s.newPane()}), "/virtual\n"); + try s.write(s.at("/pane/{d}/exec", .{first}), "Grep needle\n"); + // `.` from a pane whose directory is gone. + const gone = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{gone}), "/tmp/pardes-no-such-dir-zz/f.txt\n"); + try expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/look", .{gone}), ".\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "look: /tmp/pardes-no-such-dir-zz: no such directory")); +} + +test "look-and-exec: a pane's tagexec runs a word as a click in its tag, and reads back what it touched" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const tagexec = s.at("/pane/{d}/tagexec", .{n}); + try s.write(tagexec, "Msg from the tag\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "from the tag") != null); + try s.expect(tagexec, s.at("{d}\n", .{n})); +} diff --git a/test/behaviour/modes.zig b/test/behaviour/modes.zig index 06bcac98..ba792086 100644 --- a/test/behaviour/modes.zig +++ b/test/behaviour/modes.zig @@ -101,3 +101,385 @@ test "modes: Esc back into a file leaves its view where it was, and a look centr const drawn_at = line - try topLine(&s, "/test.txt"); try std.testing.expect(drawn_at + 1 >= rows / 2 and drawn_at <= rows / 2 + 1); } + +/// Three panes named /a.txt (left column), /b.txt over /c.txt (right), +/// the keyboard on /a.txt; their serials. +fn threePanes(s: *Session) ![3]u32 { + const a = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{a}), "/a.txt\n"); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/exec", .{try s.column(1)}), "New\n"); + const b = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{b}), "New\n"); + const c = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{b}), "/b.txt\n"); + try s.write(s.at("/pane/{d}/name", .{c}), "/c.txt\n"); + try s.write("/focus", s.at("{d}\n", .{a})); + return .{ a, b, c }; +} + +/// The panes whose tag (`name Save`) is drawn in `lit`, the outline's colour. +fn outlined(s: *Session, lit: []const u8, names: []const []const u8) ![]const u8 { + var found: []const u8 = ""; + for (names) |name| { + const x, const y = try s.where(s.at("{s} Save", .{name})); + if (std.mem.eql(u8, lit, try s.bg(x, y))) found = s.at("{s}{s}", .{ found, name }); + } + return found; +} + +test "modes: Ctrl-w outlines a pane, h j k l move the outline, and Enter, Esc, : or a pane's letter gives it the keyboard" { + var s = try Session.initFile(std.testing.allocator, "left\n"); + defer s.deinit(); + const a, const b, const c = try threePanes(&s); + const names = &[_][]const u8{ "/a.txt", "/b.txt", "/c.txt" }; + try s.keys("\x17"); + const x, const y = try s.where("/a.txt Save"); + const lit = try s.bg(x, y); + try std.testing.expectEqualStrings("/a.txt", try outlined(&s, lit, names)); + // Each pane's box shows its letter: the left column's is a. + try std.testing.expectEqualStrings("a /a.txt", (try s.screen())[y * 81 ..][0.."a /a.txt".len]); + try s.keys("l"); + try std.testing.expectEqualStrings("/b.txt", try outlined(&s, lit, names)); + try s.keys("j"); + try std.testing.expectEqualStrings("/c.txt", try outlined(&s, lit, names)); + try expectEqual(a, try s.focused()); + try s.keys("\n"); + try expectEqual(c, try s.focused()); + try std.testing.expectEqualStrings("", try outlined(&s, lit, names)); + // SPC w enters it too, and Esc leaves it on the outlined pane. + try s.keys(" wh\x1b"); + try expectEqual(a, try s.focused()); + // Other keys change nothing. + try s.keys("\x17!"); + try std.testing.expectEqualStrings("/a.txt", try outlined(&s, lit, names)); + try s.keys("\x1b"); + // `:` gives the outlined pane's tag the keyboard. + try s.keys("\x17l:"); + try expectEqual(b, try s.focused()); + try expectEqual((try s.where("/b.txt Save"))[1], (try s.cursor()).?[1]); + try s.keys("\x1b"); + // A letter goes straight there. + try s.keys("\x17a"); + try expectEqual(a, try s.focused()); + try std.testing.expectEqualStrings("", try outlined(&s, lit, names)); +} + +fn listed(s: *Session, n: u32) !bool { + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |row| if (std.mem.startsWith(u8, row, s.at("{d} ", .{n}))) return true; + return false; +} + +test "modes: in window mode d deletes the outlined pane, asking k or j between two, c collapses it, and the mode stays on" { + var s = try Session.initFile(std.testing.allocator, "top\n"); + defer s.deinit(); + // The neighbours below are what is under test: each New right under its asker. + try s.write("/ctl", "Placement pardes\n"); + const top = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); + const mid = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{mid}), "New\n"); + const low = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{mid}), "/mid\n"); + try s.write(s.at("/pane/{d}/name", .{low}), "/low\n"); + const names = &[_][]const u8{ "/test.txt", "/mid", "/low" }; + const row_top = (try s.where("/test.txt Save"))[1]; + const row_mid = (try s.where("/mid Save"))[1]; + const row_low = (try s.where("/low Save"))[1]; + // From the bottom pane, the outline up to the middle one. + const mx, const my = try s.where("/mid Save"); + const plain = try s.bg(mx, my); + try s.keys("\x17k"); + const lit = try s.bg(mx, my); + try std.testing.expect(!std.mem.eql(u8, plain, lit)); + try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); + // c folds it to its tag and back, the mode staying. + try s.keys("c"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{mid})), " collapsed ") != null); + try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); + try s.keys("c"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{mid})), " collapsed ") == null); + try expectEqual(row_low, (try s.where("/low Save"))[1]); + // d between two panes asks, as SPC d does; Esc takes it back, the + // outline where it was. + try s.keys("d"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("ask {d} del k j\n", .{mid})) != null); + try s.keys("\x1b"); + try std.testing.expect(try listed(&s, mid)); + try std.testing.expectEqualStrings("/mid", try outlined(&s, lit, names)); + // j: the pane below takes the rows, and the outline goes with them. + try s.keys("dj"); + try std.testing.expect(!try listed(&s, mid)); + try expectEqual(row_top, (try s.where("/test.txt Save"))[1]); + try expectEqual(row_mid, (try s.where("/low Save"))[1]); + try std.testing.expectEqualStrings("/low", try outlined(&s, lit, &.{ "/test.txt", "/low" })); + // With one neighbour nothing is asked: d closes it, the outline on the + // pane that grew. + try s.keys("d"); + try std.testing.expect(!try listed(&s, low)); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("ask {d} ", .{low})) == null); + try std.testing.expectEqualStrings("/test.txt", try outlined(&s, lit, &.{"/test.txt"})); + // Enter leaves the mode on it. + try s.keys("\n"); + try expectEqual(top, try s.focused()); + try std.testing.expectEqualStrings("", try outlined(&s, lit, &.{"/test.txt"})); +} + +test "modes: window keys walk from a pane up through its column's tag and the workspace's, across, and back down" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const a = try s.focused(); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/exec", .{try s.column(1)}), "New\n"); + try s.write("/focus", s.at("{d}\n", .{a})); + const right = 40; // two columns on 80 cells: the right one's first + // Up from the top pane: its column's tag, in normal mode (w moves). + try s.keys("\x17k"); + try s.expect("/focus", ""); + const x0, const y0 = (try s.cursor()).?; + try expectEqual(@as(usize, 1), y0); + try std.testing.expect(x0 < right); + const tag = s.at("/col/{d}/tag", .{try s.column(0)}); + const words = try s.read(tag); + try s.keys("w"); + try s.expect(tag, words); + const remembered = (try s.cursor()).?; + try std.testing.expect(remembered[0] > x0); + // Across to the right column's tag, and back to where the caret was. + try s.keys("\x17l"); + try std.testing.expect((try s.cursor()).?[0] >= right); + try s.keys("\x17h"); + try expectEqual(remembered, (try s.cursor()).?); + // Up again: the workspace tag; down, the column's, then the pane. + try s.keys("\x17k"); + try expectEqual(@as(usize, 0), (try s.cursor()).?[1]); + try s.keys("\x17j"); + try expectEqual(@as(usize, 1), (try s.cursor()).?[1]); + try s.keys("\x17j"); + try expectEqual(a, try s.focused()); + // `:` in a column's tag is the way back to the body. + try s.keys("\x17k:"); + try expectEqual(a, try s.focused()); + try expectEqual((try s.where("body"))[1], (try s.cursor()).?[1]); + try s.expect(s.at("/pane/{d}/body", .{a}), "body\n"); +} + +/// The jump tests' session: a pane a letter of `order`, A and B each a file +/// of 100 lines, C a Zerox of A, O the +Help output (no file behind it), +/// and the jumplist `order`, a letter a place, each letter's n-th place its +/// line 1 + 40n ("ABABA" is A1 B1 A41 B41 A81): each made by moving its +/// pane's dot there and giving it the keyboard, as a jump does (a new +/// pane's first, where it opens). The shell the session started with is +/// closed after, its places with it. +const Jumps = struct { + s: Session, + ids: [4]u32 = @splat(0), + + const letters = "ABCO"; + + fn init(order: []const u8) !Jumps { + var t: Jumps = .{ .s = try Session.initTerm(std.testing.allocator) }; + errdefer t.s.deinit(); + const s = &t.s; + const shell = try s.focused(); + var seen: [4]u32 = @splat(0); + for (order) |letter| { + const which = std.mem.indexOfScalar(u8, letters, letter).?; + if (t.ids[which] == 0) t.ids[which] = switch (letter) { + 'O' => o: { + try s.write(s.at("/pane/{d}/exec", .{shell}), "Help\n"); + break :o try s.focused(); + }, + else => made: { + // C is A's twin, and A C's when C came first. + const twin = t.ids[if (letter == 'A') 2 else if (letter == 'C') 0 else 1]; + if (letter != 'B' and twin != 0) { + try s.write(s.at("/pane/{d}/exec", .{twin}), "Zerox\n"); + break :made try s.focused(); + } + const n = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{n}), if (letter == 'B') "/b.txt\n" else "/a.txt\n"); + try s.write(s.at("/pane/{d}/body", .{n}), std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); + break :made n; + }, + }; + const n = t.ids[which]; + try s.write(s.at("/pane/{d}/addr", .{n}), s.at("{d}\n", .{1 + 40 * seen[which]})); + try s.write(s.at("/pane/{d}/ctl", .{n}), "dot=addr\n"); + try s.write("/focus", s.at("{d}\n", .{n})); + seen[which] += 1; + } + try s.write(s.at("/pane/{d}/ctl", .{shell}), "delete\n"); + return t; + } + + /// Where the keyboard is: its pane's letter and the dot's line, `A50`. + fn here(t: *Jumps) ![]const u8 { + const s = &t.s; + const n = try s.focused(); + const at, _ = try s.range(s.at("/pane/{d}/dot", .{n})); + const body = try s.read(s.at("/pane/{d}/body", .{n})); + const which = std.mem.indexOfScalar(u32, &t.ids, n).?; + return s.at("{c}{d}", .{ letters[which], std.mem.count(u8, body[0..at], "\n") + 1 }); + } + + const Way = enum { key, leader, builtin }; + + /// Steps back or on until the list ends that way, as it says; the + /// places landed on, "A50 A10 B10". + fn walk(t: *Jumps, way: Way, back: bool) ![]const u8 { + const s = &t.s; + var out: []const u8 = ""; + for (0..64) |_| { + const before = try t.here(); + switch (way) { + .key => try s.keys(if (back) "\x0f" else "\u{F017}i"), + .leader => try s.keys(if (back) " jo" else " ji"), + .builtin => try s.write("/exec", if (back) "Back\n" else "Forward\n"), + } + const now = try t.here(); + if (std.mem.eql(u8, before, now)) break; + out = if (out.len == 0) now else s.at("{s} {s}", .{ out, now }); + } else return error.NoEnd; + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), if (back) "nothing further back" else "nothing further on") != null); + return out; + } +}; + +test "modes: Ctrl-o keeps to the focused file (JumpScope file), A41 then A1, and leaves it only once it runs out that way; Ctrl-i mirrors it" { + var t = try Jumps.init("BABABA"); + defer t.s.deinit(); + try std.testing.expect(std.mem.indexOf(u8, try t.s.read("/ctl"), "\nJumpScope file\n") != null); + // B81, B41 passed over, kept in their places: A has nothing older than + // A1, so the step after it is the nearest place of any file, B1. + try std.testing.expectEqualStrings("A41 A1 B1", try t.walk(.key, true)); + // Forward is the mirror, from B1 in B: B's own places first, then A81, + // the newest, where the Backs started. + try std.testing.expectEqualStrings("B41 B81 A81", try t.walk(.key, false)); +} + +test "modes: Ctrl-i retraces a run of Ctrl-o inside one file step for step, back to where it began" { + var t = try Jumps.init("ABABA"); + defer t.s.deinit(); + // A1 B1 A41 B41 A81 from A81: nothing older than A1 at all. + try std.testing.expectEqualStrings("A41 A1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("A41 A81", try t.walk(.key, false)); + // One back, one on: the same place, whichever were passed over. + try t.s.keys("\x0f"); + try std.testing.expectEqualStrings("A41", try t.here()); + try t.s.keys("\u{F017}i"); + try std.testing.expectEqualStrings("A81", try t.here()); + // And the steps made no places of their own. + try std.testing.expectEqualStrings("A41 A1", try t.walk(.key, true)); +} + +test "modes: with JumpScope all, Ctrl-o and Ctrl-i take every place in the order it was jumped to, and bare JumpScope is file again" { + var t = try Jumps.init("ABABA"); + defer t.s.deinit(); + try t.s.write("/ctl", "JumpScope all\n"); + try std.testing.expectEqualStrings("B41 A41 B1 A1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("B1 A41 B41 A81", try t.walk(.key, false)); + try t.s.write("/ctl", "JumpScope\n"); + try std.testing.expect(std.mem.indexOf(u8, try t.s.read("/ctl"), "\nJumpScope file\n") != null); +} + +test "modes: from a pane with no file, Ctrl-o and Ctrl-i step through every place" { + var t = try Jumps.init("OAOBO"); + defer t.s.deinit(); + // O81 keeps to nothing: B1, the next older of all. From B, nothing of + // B is older, so O41; from O, A1; from A, O1. + try std.testing.expectEqualStrings("B1 O41 A1 O1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("A1 O41 B1 O81", try t.walk(.key, false)); +} + +test "modes: Ctrl-o in a file shown in two panes keeps to its own pane first, then the file in the other" { + // C is A's file in a second pane: C1 B1 A1 C41 A41. + var t = try Jumps.init("CBACA"); + defer t.s.deinit(); + // From A41: A1 in this pane, over the nearer C41; then none of A's own + // is older, so the file's in C, C1, over B1. + try std.testing.expectEqualStrings("A1 C1", try t.walk(.key, true)); + // From C1 in C: C41, its own, then the file's newest, A41. + try std.testing.expectEqualStrings("C41 A41", try t.walk(.key, false)); +} + +test "modes: Back and Forward take the same steps from Ctrl-o and Ctrl-i, SPC j o and SPC j i, and the builtins" { + for ([_]Jumps.Way{ .key, .leader, .builtin }) |way| { + var t = try Jumps.init("BABABA"); + defer t.s.deinit(); + try std.testing.expectEqualStrings("A41 A1 B1", try t.walk(way, true)); + try std.testing.expectEqualStrings("B41 B81 A81", try t.walk(way, false)); + } +} + +test "modes: Ctrl-o and Ctrl-i drop the selections, select mode and the extra cursors, and land where the place was left" { + const rows = std.mem.asBytes(&@as([97][4]u8, @splat("row\n".*))); + var s = try Session.initFile(std.testing.allocator, "alpha beta\ngamma delta\nepsilon zeta\n" ++ rows); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + // A place on line 3, at its eighth column (byte 30), then line 60. + try s.write(s.at("/pane/{d}/addr", .{n}), "#30\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "dot=addr\n"); + try s.keys("60G"); + // Each step lands on the cursor the place was left with. + var back_to: u32 = 30; + for ([_][]const u8{ "\x0f", "\u{F017}i" }) |key| { + // A line selected, a second cursor under it, select mode on. + try s.keys("xCv"); + try std.testing.expectEqualStrings("select", try mode(&s, n)); + const leaving = (try s.range(dot))[1] - 1; // the cursor: its last character + try s.keys(key); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + try std.testing.expectEqual(back_to, (try s.range(dot))[0]); + back_to = leaving; + try s.expect(s.at("/pane/{d}/sel", .{n}), ""); + // One cursor: a Z typed goes in once. + try s.keys("iZ\x1b"); + try expectEqual(@as(usize, 1), std.mem.count(u8, try s.read(s.at("/pane/{d}/body", .{n})), "Z")); + try s.keys("u"); + } + // No place further on: said, and the selection left as it was. + try s.keys("x"); + const sel = try s.read(s.at("/pane/{d}/sel", .{n})); + try s.keys("\u{F017}i"); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "nothing further on") != null); + try s.expect(s.at("/pane/{d}/sel", .{n}), sel); +} + +test "modes: hopping between two panes by Esc, window mode or a click makes no places, so one Ctrl-o goes back across and the next has nowhere to go" { + var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); + defer s.deinit(); + // Two panes of room, not acme's fit to the doc. + try s.write("/ctl", "Placement pardes\n"); + const doc = try s.focused(); + try s.keys("\u{F015}n"); // a shell under the doc + const shell = try s.focused(); + if (std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{shell})), "mode:tty") != null) try s.keys("\x02"); + for (0..8) |_| { + const was = try s.focused(); + try s.keys("\x1b"); + try expectEqual(if (was == doc) shell else doc, try s.focused()); + } + for (0..8) |_| { + const want = if (try s.focused() == doc) shell else doc; + try s.keys(if (want == shell) " wj\n" else " wk\n"); + try expectEqual(want, try s.focused()); + } + const doc_row = (try s.where("row"))[1]; + const shell_row = (try s.where("Tty Save Mode"))[1] + 1; + for (0..8) |_| { + const other = if (try s.focused() == doc) shell else doc; + const row = if (other == doc) doc_row else shell_row; + try s.write("/mousein", s.at("m 10 {d} 1\nm 10 {d} 0\n", .{ row, row })); + try expectEqual(other, try s.focused()); + } + const before = try s.focused(); + try s.keys("\x0f"); + try std.testing.expect(before != try s.focused()); + const after = try s.focused(); + try s.keys("\x0f"); + try expectEqual(after, try s.focused()); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "nothing further back") != null); +} diff --git a/test/behaviour/mounts.zig b/test/behaviour/mounts.zig index 57685a38..c05166bc 100644 --- a/test/behaviour/mounts.zig +++ b/test/behaviour/mounts.zig @@ -23,3 +23,10 @@ test "mounts: Mount with no dial is wrong #args, any other x!y a bad dial addres try std.testing.expect(has(s.said, "no such mount")); } } + +test "mounts: a Mount dial with a network it does not know is a bad dial address, EINVAL" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try expectEqual(@as(u16, 22), try s.refused("/ctl", "Mount peer nowhere!x\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Mount: bad dial address: nowhere!x")); +} diff --git a/test/behaviour/mouse.zig b/test/behaviour/mouse.zig index 0abe15dc..d7da46e8 100644 --- a/test/behaviour/mouse.zig +++ b/test/behaviour/mouse.zig @@ -131,3 +131,68 @@ test "mouse: a B1 sweep over several lines is a stream, as acme's, in a body and 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"); } + +test "mouse: a click on either half of a wide character lands before it, in the text and on a tag's path, and the caret steps over it whole" { + var s = try Session.initFile(std.testing.allocator, "a界e\u{301}z\n"); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + const x, const y = try s.where("a界"); + // 界 is two cells, bytes 1 to 4; é two runes, bytes 4 to 7. + for ([_]usize{ 1, 2 }) |into| { + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + into, y, x + into, y }); + try std.testing.expectEqual(.{ @as(u32, 1), @as(u32, 1) }, try s.range(dot)); + } + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 4, y }); + try std.testing.expectEqual(.{ @as(u32, 7), @as(u32, 7) }, try s.range(dot)); + // On the tag's path, the second half of 界 drafts the name with its + // caret before it; Right steps a whole grapheme, two cells then one. + try s.write(s.at("/pane/{d}/name", .{n}), "/界e\u{301}.txt\n"); + const px, const py = try s.where("/界"); + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ px + 2, py, px + 2, py }); + try std.testing.expectEqual(.{ px + 1, py }, (try s.cursor()).?); + try s.keys("\u{F012}"); + try std.testing.expectEqual(.{ px + 3, py }, (try s.cursor()).?); + try s.keys("\u{F012}"); + try std.testing.expectEqual(.{ px + 4, py }, (try s.cursor()).?); + try s.keys("\x1b\x1b"); + try s.expect(s.at("/pane/{d}/name", .{n}), "/界e\u{301}.txt\n"); + // Typed past the tag's words, a two-byte λ goes in and one backspace + // takes it out whole. + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + try s.click("Collapse Del", "Collapse Del".len + 1, 1); + const caret = (try s.cursor()).?; + // A clicked tag is in normal mode: `i` types at the caret. + try s.keys("iλ"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/tag", .{n})), "λ")); + try std.testing.expectEqual(.{ caret[0] + 1, caret[1] }, (try s.cursor()).?); + try s.keys("\x08"); + try std.testing.expectEqual(caret, (try s.cursor()).?); + try s.expect(s.at("/pane/{d}/tag", .{n}), tag); +} + +test "mouse: a sweep pressed on the line numbers takes only the text: drawn while held and once let go, looked for by a click in it, cut by a chord" { + var s = try Session.initFile(std.testing.allocator, "alpha beta\ngamma delta\nalpha beta\ngamma\n"); + defer s.deinit(); + const n = try s.focused(); + const x, const y = try s.where("alpha"); + const plain = try s.bg(x - 2, y); + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\n", .{ x - 2, y, x + 4, y + 1 }); + for (0..2) |released| { + // The number pressed on, and the room after the line, stay unlit. + try std.testing.expectEqualStrings(plain, try s.bg(x - 2, y)); + try std.testing.expectEqualStrings(plain, try s.bg(x - 2, y + 1)); + const lit = try s.bg(x, y); + try std.testing.expect(!std.mem.eql(u8, plain, lit)); + try std.testing.expectEqualStrings(lit, try s.bg(x + 4, y + 1)); + try std.testing.expectEqualStrings(plain, try s.bg(x + 5, y + 1)); + if (released == 0) try mouse(&s, "m {d} {d} 0\n", .{ x + 4, y + 1 }); + } + // A Look clicked in it looks for the text swept: its next place, the + // third and fourth lines. + try mouse(&s, "m {d} {d} 4\nm {d} {d} 0\n", .{ x + 1, y, x + 1, y }); + try std.testing.expectEqual(.{ @as(u32, 23), @as(u32, 39) }, try s.range(s.at("/pane/{d}/dot", .{n}))); + // A chord cuts what was swept: the text, no number. + 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 - 2, y, x + 4, y + 1, x + 4, y + 1, x + 4, y + 1, x + 4, y + 1 }); + try s.expect(s.at("/pane/{d}/body", .{n}), " delta\nalpha beta\ngamma\n"); +} diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig index ef1fab7e..701f4f14 100644 --- a/test/behaviour/other_files.zig +++ b/test/behaviour/other_files.zig @@ -80,3 +80,249 @@ test "other-files: /recent keeps a PDF as it keeps a file: open, then closed" { 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); } + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// A fresh directory's absolute path, kept until deinit. +fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 { + var buf: [4096]u8 = undefined; + return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}); +} + +/// The serials /index lists with a name ending `suffix`. +fn named(s: *Session, suffix: []const u8) ![]u32 { + var out: std.ArrayList(u32) = .empty; + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |line| { + const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; + if (std.mem.endsWith(u8, name, suffix)) try out.append(s.arena.allocator(), try std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10)); + } + return out.items; +} + +/// The body of the pane a root exec of `word` answers (Recent, Jumplist). +fn listing(s: *Session, word: []const u8) ![]const u8 { + try s.write("/exec", s.at("{s}\n", .{word})); + return s.read(s.at("/pane/{s}/body", .{std.mem.trimEnd(u8, try s.read("/exec"), "\n")})); +} + +test "other-files: Jumplist fills its one +Jumps again, as Recent does, never a second" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + for (0..2) |_| { + try s.write(s.at("/pane/{d}/exec", .{try s.focused()}), "Jumplist\n"); + try std.testing.expectEqual(@as(usize, 1), (try named(&s, "/+Jumps")).len); + } +} + +test "other-files: a Jumplist entry is (closed) only while no pane has its file open" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "two\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + try s.write("/look", s.at("{s}/a.txt\n", .{dir})); + const a = try s.focused(); + try s.write("/look", s.at("{s}/b.txt\n", .{dir})); + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expect(has(try listing(&s, "Jumplist"), "(closed)")); + // Open again, by a look: no entry is closed any more. + try s.write("/look", s.at("{s}/a.txt\n", .{dir})); + try std.testing.expect(!has(try listing(&s, "Jumplist"), "(closed)")); +} + +test "other-files: Recent's row for a file still open is where its dot is now, not where it last closed" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/look", s.at("{s}/a.txt:3\n", .{try tmpPath(&s, &tmp)})); + try s.write(s.at("/pane/{d}/dot", .{try s.focused()}), "6 6"); // line 2, column 3 + // A query pane under the file (line 1 `Recent `), its rows said from + // its directory. + const rows = try listing(&s, "Recent"); + try std.testing.expect(std.mem.startsWith(u8, rows, "Recent \n")); + try std.testing.expect(has(rows, "\na.txt:2:3 open\n")); +} + +test "other-files: a closed file is found again: Recent and /recent list it closed, a look at its row reopens it at its dot, and a jump back does too" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "bee\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const a = s.at("{s}/a.txt", .{dir}); + // Opened, its dot on line 3, then closed. + try s.write("/look", s.at("{s}:3\n", .{a})); + const opened = try s.focused(); + try std.testing.expect(has(try s.read("/recent"), s.at("open {s}\n", .{a}))); + // Another file, so the jumplist has somewhere to come back from. + try s.write("/look", s.at("{s}/b.txt\n", .{dir})); + try s.write(s.at("/pane/{d}/ctl", .{opened}), "Del\n"); + try std.testing.expect(has(try s.read("/recent"), s.at("closed {s}\n", .{a}))); + // Recent lists it, closed, at its dot; a look at the row opens it there. + const rows = try listing(&s, "Recent"); + const recent = std.mem.trimEnd(u8, try s.read("/exec"), "\n"); + const at = std.mem.indexOf(u8, rows, "\na.txt:3:").? + 1; + const row = rows[at..std.mem.indexOfScalarPos(u8, rows, at, '\n').?]; + try std.testing.expect(std.mem.endsWith(u8, row, " (closed)")); + try s.write(s.at("/pane/{s}/look", .{recent}), s.at("{s}\n", .{row[0 .. row.len - " (closed)".len]})); + const back = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{back}), s.at("{s}\n", .{a})); + try std.testing.expectEqual(@as(u32, 8), (try s.range(s.at("/pane/{d}/dot", .{back})))[0]); + // Closed again; the jumplist keeps its entry, closed, and a jump back + // to it opens it again. + try s.write(s.at("/pane/{d}/ctl", .{back}), "Del\n"); + try std.testing.expect(has(try listing(&s, "Jumplist"), "(closed)")); + // Every entry in order (JumpScope all): which file has the focus after + // a Del is the layout's to choose, and Back keeps to it first. + try s.write("/ctl", "JumpScope all\n"); + for (0..8) |_| { + if ((try named(&s, "/a.txt")).len > 0) break; + try s.write("/exec", "Back\n"); + } + try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), s.at("{s}\n", .{a})); +} + +test "other-files: Back and Forward over a closed file's entries: one reopen is that file's pane in every entry, so the next step moves" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "bee\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "sea\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + try s.write("/look", s.at("{s}/b.txt\n", .{dir})); + const b = try s.focused(); + // Two entries for a.txt, far apart in it. + try s.write("/look", s.at("{s}/a.txt:1\n", .{dir})); + const a = try s.focused(); + try s.write("/look", s.at("{s}/a.txt:90\n", .{dir})); + try s.write("/look", s.at("{s}/c.txt\n", .{dir})); + const c = try s.focused(); + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try s.write("/focus", s.at("{d}\n", .{c})); + try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try listing(&s, "Jumplist"), "(closed)")); + try s.write("/focus", s.at("{d}\n", .{c})); + // Back reopens it at line 90: both its entries are that pane now. + try s.write("/exec", "Back\n"); + const re = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{re}), s.at("{s}/a.txt\n", .{dir})); + try std.testing.expectEqual(@as(u32, 89), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4); + try std.testing.expect(!has(try listing(&s, "Jumplist"), "(closed)")); + try s.write("/focus", s.at("{d}\n", .{re})); + // Back again goes to its line 1, in the same pane, then to b. + try s.write("/exec", "Back\n"); + try std.testing.expectEqual(re, try s.focused()); + try std.testing.expectEqual(@as(u32, 0), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4); + try s.write("/exec", "Back\n"); + try std.testing.expectEqual(b, try s.focused()); + // And Forward walks it all again, to c. + try s.write("/exec", "Forward\n"); + try s.write("/exec", "Forward\n"); + try std.testing.expectEqual(@as(u32, 89), (try s.range(s.at("/pane/{d}/dot", .{re})))[0] / 4); + try s.write("/exec", "Forward\n"); + try std.testing.expectEqual(c, try s.focused()); +} + +test "other-files: Recent keeps only files on disk: not a name never saved, nor the served tree's" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "real.txt", .data = "real\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const names = [_][]const u8{ s.at("{s}/c", .{dir}), "/virtual/zz", s.at("{s}/real.txt", .{dir}) }; + for (names) |name| { + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{made}), s.at("{s}\n", .{name})); + // Asked once if it is a file with text to lose; gone either way. + _ = try s.refused(s.at("/pane/{d}/ctl", .{made}), "Del\n"); + _ = s.refused(s.at("/pane/{d}/ctl", .{made}), "Del\n") catch 0; + } + const recent = try s.read("/recent"); + try std.testing.expect(!has(recent, s.at(" {s}\n", .{names[0]}))); + try std.testing.expect(!has(recent, " /virtual/zz\n")); + try std.testing.expect(has(recent, s.at(" {s}\n", .{names[2]}))); +} + +test "other-files: Back over a closed file deleted since skips it, says so, and goes on to the place before" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + for ([_][]const u8{ "a.txt", "gone.txt", "c.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "text\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + try s.write("/look", s.at("{s}/a.txt\n", .{dir})); + const a = try s.focused(); + try s.write("/look", s.at("{s}/gone.txt\n", .{dir})); + const gone = try s.focused(); + try s.write("/look", s.at("{s}/c.txt\n", .{dir})); + const c = try s.focused(); + try s.write(s.at("/pane/{d}/ctl", .{gone}), "Del\n"); + try tmp.dir.deleteFile(std.testing.io, "gone.txt"); + try s.write("/focus", s.at("{d}\n", .{c})); + try s.write("/exec", "Back\n"); + try std.testing.expectEqual(a, try s.focused()); + try std.testing.expect(has(try s.read("/log"), "gone.txt: deleted, skipped")); +} + +test "other-files: a remembered place is clamped to the file as it is now, its selection's two ends kept where they fit" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\nthree\nfour\nfive\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + try s.write("/look", s.at("{s}/c.txt:2\n", .{dir})); + const c = try s.focused(); + // A selection over lines 2 to 4, then closed. + try s.write(s.at("/pane/{d}/dot", .{c}), "4 19"); + try s.write(s.at("/pane/{d}/ctl", .{c}), "Del\n"); + // The file shrinks to two lines while it is closed. + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.txt", .data = "one\ntwo\n" }); + const rows = try listing(&s, "Recent"); + const recent = std.mem.trimEnd(u8, try s.read("/exec"), "\n"); + const at = std.mem.indexOf(u8, rows, "c.txt:").?; + const row = rows[at..std.mem.indexOfScalarPos(u8, rows, at, '\n').?]; + // Both ends are in the row, a range. + try std.testing.expect(std.mem.indexOfScalar(u8, row, '-') != null); + const loc = s.at("{s}/{s}\n", .{ dir, row[0..std.mem.indexOfScalar(u8, row, ' ').?] }); + // A look at it opens the file, never a miss, clamped to its last line. + try s.write(s.at("/pane/{s}/look", .{recent}), loc); + const back = try s.focused(); + try s.expect(s.at("/pane/{d}/name", .{back}), s.at("{s}/c.txt\n", .{dir})); + // Its ends clamped to the last character, `two`'s `o`, not its newline. + const dot = try s.range(s.at("/pane/{d}/dot", .{back})); + try std.testing.expect(dot[0] < dot[1] and dot[1] <= 7); + try std.testing.expect(has(try s.read("/log"), "c.txt has 2 lines now: at its last")); + // The same row with the file open: the same place, said the same way. + try s.write(s.at("/pane/{s}/look", .{recent}), loc); + try std.testing.expectEqual(back, try s.focused()); + try std.testing.expectEqual(dot, try s.range(s.at("/pane/{d}/dot", .{back}))); +} + +test "other-files: /index and /recent show a name's own backslash doubled, so `\\n` written in a name reads back unlike a newline" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "back\\nslash", .data = "x\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/back\\nslash\n", .{dir})); + const index = try s.read("/index"); + try std.testing.expect(has(index, s.at("{s}/back\\\\nslash ", .{dir}))); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, "\n")); + try s.write(s.at("/pane/{d}/exec", .{n}), "Get\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n"); + try std.testing.expect(has(try s.read("/recent"), s.at("closed {s}/back\\\\nslash\n", .{dir}))); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index a46f8432..0c0edbf9 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -363,3 +363,544 @@ test "panes: a Mini that cannot read its file leaves the panes, the last Mini's try s.expect("/index", index); try s.expect(s.at("/pane/{d}/body", .{mini}), body); } + +test "panes: Mini with no argument is wrong #args, EINVAL, as every word that needs one" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{try s.focused()}), "Mini\n")); + try std.testing.expectEqualStrings("wrong #args in control message \"Mini\"", s.said); +} + +test "panes: the ctl word name with nothing after it says it names nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{try s.focused()}), "name \n")); + try std.testing.expectEqualStrings("invalid file name: name takes the name, and there is none", s.said); +} + +test "panes: ctl reads acme's fields in acme's order, undo and redo flags included, then pardes's own" { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const field = struct { + fn at(text: []const u8, i: usize) []const u8 { + var it = std.mem.tokenizeScalar(u8, text, ' '); + for (0..i) |_| _ = it.next(); + return it.next().?; + } + }.at; + try std.testing.expectEqualStrings("0", field(try s.read(ctl), 8)); // nothing to undo + try std.testing.expectEqualStrings("0", field(try s.read(ctl), 9)); // nothing to redo + const current = field(try s.read(ctl), 10); + try std.testing.expect(std.mem.eql(u8, current, "current") or std.mem.eql(u8, current, "notcurrent")); + try s.write(s.at("/pane/{d}/body", .{n}), "two\n"); + try std.testing.expectEqualStrings("1", field(try s.read(ctl), 8)); + try s.write(ctl, "Undo\n"); + try std.testing.expectEqualStrings("1", field(try s.read(ctl), 9)); +} + +/// A fresh directory's absolute path, kept until deinit. +fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 { + var buf: [4096]u8 = undefined; + return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}); +} + +/// The `i`th blank-separated field of a pane's ctl read. +fn ctlField(s: *Session, n: u32, i: usize) ![]const u8 { + var it = std.mem.tokenizeScalar(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), ' '); + for (0..i) |_| _ = it.next(); + return it.next() orelse error.NoField; +} + +test "panes: get reloads the pane from disk and reports a missing file without losing dirty contents" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); + var s = try Session.initFile(std.testing.allocator, "in memory\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir})); + try s.write(ctl, "get\n"); + try s.expect(body, "from disk\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + try std.testing.expectEqualStrings("1", try ctlField(&s, n, 8)); // an undo step + // Renamed but never edited, it was clean: nothing thrown away, nothing + // logged as reloaded over edits. + try std.testing.expect(!has(try s.read("/log"), s.at("changed {d} reloaded\n", .{n}))); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/missing.txt\n", .{dir})); + try s.write(body, "unsaved\n"); + // Unsaved text is asked about once, as acme's get asks winclean. + try std.testing.expectEqual(@as(u16, 5), try s.refused(ctl, "get\n")); + try std.testing.expect(std.mem.endsWith(u8, s.said, ": Modified (get again to discard)")); + try std.testing.expect(has(try s.read("/log"), s.at("unsaved {d} {s}/missing.txt\n", .{ n, dir }))); + try std.testing.expectEqual(@as(u16, 2), try s.refused(ctl, "get\n")); + try s.expect(body, "from disk\nunsaved\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); +} + +test "panes: Get reads the pane's file again, refusing unsaved edits once, and Get file loads that file into the pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "the other\n" }); + var s = try Session.initFile(std.testing.allocator, "in memory\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try s.expect(body, "from disk\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + // Edited: asked once, then read again. + try s.write(body, "unsaved\n"); + try std.testing.expect(try s.refused(exec, "Get\n") != 0); + try s.expect(body, "from disk\nunsaved\n"); + try s.write(exec, "Get\n"); + try s.expect(body, "from disk\n"); + // Get file: that file, by a name from the pane's directory. + try s.write(exec, "Get other.txt\n"); + try s.expect(body, "the other\n"); + try s.expect(s.at("/pane/{d}/name", .{n}), s.at("{s}/other.txt\n", .{dir})); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: Undo after a load under another name (Get file, Edit e) puts the old name back with the old text and its clean state; Putall then writes nothing over the loaded file" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "other\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "keep.txt", .data = "keep\n" }); + for ([_][]const u8{ "Get keep.txt\n", "Edit e keep.txt\n" }) |load| { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const name = s.at("/pane/{d}/name", .{n}); + const dirty = s.at("/pane/{d}/dirty", .{n}); + try s.write(name, s.at("{s}/other.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try s.expect(body, "other\n"); + try s.expect(dirty, "0\n"); + // Loaded: keep.txt's text under its name. + try s.write(exec, load); + try s.expect(body, "keep\n"); + try s.expect(name, s.at("{s}/keep.txt\n", .{dir})); + if (std.mem.startsWith(u8, load, "Get")) try s.expect(dirty, "0\n"); + // Undo: other.txt's text, other.txt's name, clean as it was. + try s.write(exec, "Undo\n"); + try s.expect(body, "other\n"); + try s.expect(name, s.at("{s}/other.txt\n", .{dir})); + try s.expect(dirty, "0\n"); + // Putall saves nothing, and nothing lands on keep.txt. + try s.write("/ctl", "Putall\n"); + try std.testing.expectEqual(@as(usize, 0), s.saved); + // Redo: keep.txt again. + try s.write(exec, "Redo\n"); + try s.expect(name, s.at("{s}/keep.txt\n", .{dir})); + try s.expect(body, "keep\n"); + } +} + +test "panes: a Get file that fails changes nothing: no such file, a directory, or unsaved edits asked about once" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "here.txt", .data = "here\n" }); + try tmp.dir.createDirPath(std.testing.io, "sub"); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const name = s.at("/pane/{d}/name", .{n}); + const here = s.at("{s}/here.txt\n", .{dir}); + try s.write(name, here); + try s.write(exec, "Get\n"); + for ([_][]const u8{ "Get nosuch.txt\n", "Get sub\n" }) |line| { + try std.testing.expect(try s.refused(exec, line) != 0); + try s.expect(name, here); + try s.expect(body, "here\n"); + } + // No undo step left by them: one Undo takes back the first Get. + try s.write(exec, "Undo\n"); + try s.expect(body, "x\n"); + try s.write(exec, "Redo\n"); + // Edited, the first Get asks and renames nothing. + try s.write(body, "more\n"); + try std.testing.expect(try s.refused(exec, "Get other.txt\n") != 0); + try s.expect(name, here); + try s.expect(body, "here\nmore\n"); +} + +/// The serials /index lists with a name ending `suffix`, in its order. +fn panesNamed(s: *Session, suffix: []const u8) ![]u32 { + var out: std.ArrayList(u32) = .empty; + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |line| { + const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; + if (std.mem.endsWith(u8, name, suffix)) try out.append(s.arena.allocator(), try std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10)); + } + return out.items; +} + +test "panes: Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Del of one asks nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{a}), s.at("{s}/z.txt\n", .{dir})); + try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n"); + // Both in the index, each its serial, one name. + const twins = try panesNamed(&s, "/z.txt"); + try std.testing.expectEqual(@as(usize, 2), twins.len); + const b = if (twins[0] == a) twins[1] else twins[0]; + // b's cursor on `three`; a line written above it in a moves it down. + try s.write(s.at("/pane/{d}/dot", .{b}), "9 9"); + try s.write(s.at("/pane/{d}/addr", .{a}), "0"); + try s.write(s.at("/pane/{d}/data", .{a}), "zero\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + try std.testing.expectEqual(.{ @as(u32, 14), @as(u32, 14) }, try s.range(s.at("/pane/{d}/dot", .{b}))); + try s.expect(s.at("/pane/{d}/dirty", .{a}), "1\n"); + try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n"); + // A write through b is a's too; Undo in a takes it back from both. + try s.write(s.at("/pane/{d}/body", .{b}), "four\n"); + try s.expect(s.at("/pane/{d}/body", .{a}), "zero\none\ntwo\nthree\nfour\n"); + try s.write(s.at("/pane/{d}/exec", .{a}), "Undo\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + // Del of a asks nothing: b holds the text. + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt")); + try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n"); + try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n"); + // Alone now, it is no twin; Del asks about it as about any pane. + try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{b}), "Del\n") != 0); + try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt")); +} + +test "panes: a twin renamed logs a rename for each twin" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n"); + const twins = try panesNamed(&s, "/test.txt"); + try std.testing.expectEqual(@as(usize, 2), twins.len); + try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-twin-renamed.txt\n"); + for (twins) |n| try std.testing.expect(has(try s.read("/log"), s.at("rename {d} /tmp/pardes-twin-renamed.txt\n", .{n}))); +} + +test "panes: small refusals: one name an open, small scratches loaded over unasked, twins saved once, and errnos in words a mount reads" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "noread.txt", .data = "secret\n" }); + try tmp.dir.createDirPath(std.testing.io, "sub"); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + const exec = s.at("/pane/{d}/exec", .{n}); + // One name an open: a second line, in the write or after, is refused. + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/pardes-a\nb")); + const f = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "/tmp/pardes-one.txt\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(f, "b")); + s.clunk(f); + try s.expect(name, "/tmp/pardes-one.txt\n"); + // Errnos a mount reads from the words. + try s.write(name, s.at("{s}/f.txt\n", .{dir})); + try s.write(exec, "Get\n"); + try std.testing.expectEqual(@as(u16, 21), try s.refused(exec, "Get sub\n")); // EISDIR + const noread = s.at("{s}/noread.txt\x00", .{dir}); + _ = std.c.chmod(noread[0 .. noread.len - 1 :0], 0); + try std.testing.expectEqual(@as(u16, 13), try s.refused(exec, "Get noread.txt\n")); // EACCES + try std.testing.expectEqual(@as(u16, 20), try s.refused(exec, s.at("Incl {s}/f.txt\n", .{dir}))); // ENOTDIR + _ = try s.refused(s.at("/pane/{d}/look", .{n}), "/dev/zero\n"); + try std.testing.expect(has(try s.read("/log"), "not a regular file")); + // Twins saved once by Putall. + try s.write(s.at("/pane/{d}/body", .{n}), "edited\n"); + try s.write(exec, "Zerox\n"); + const saved = s.saved; + try s.write("/ctl", "Putall\n"); + try std.testing.expectEqual(saved + 1, s.saved); + // A scratch of 2 bytes is loaded over unasked. + try s.write(exec, "New\n"); + const scratch = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{scratch}), "hi"); + try s.write(s.at("/pane/{d}/exec", .{scratch}), s.at("Get {s}/f.txt\n", .{dir})); + try s.expect(s.at("/pane/{d}/body", .{scratch}), "f\n"); +} + +test "panes: Get of a file not there says so without asking about unsaved edits" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" }); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const dir = try tmpPath(&s, &tmp); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{n}), "unsaved\n"); + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Get /tmp/pardes-surely-no-such-file.txt\n"); + const log = try s.read("/log"); + try std.testing.expect(has(log, "no such file")); + try std.testing.expect(!has(log, "again to discard")); + // Not asked, so the next Get of a file there is asked about, not run. + try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{n}), s.at("Get {s}/f.txt\n", .{dir})) != 0); + try s.expect(s.at("/pane/{d}/body", .{n}), "x\nunsaved\n"); +} + +test "panes: a pane's ctl takes acme's words: done by the files that replace them, or refused with why" { + var s = try Session.initFile(std.testing.allocator, "one two\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const dirty = s.at("/pane/{d}/dirty", .{n}); + try s.write(ctl, "name /tmp/pardes-acme-ctl.txt\n"); + try s.expect(s.at("/pane/{d}/name", .{n}), "/tmp/pardes-acme-ctl.txt\n"); + try s.write(ctl, "dirty\n"); + try s.expect(dirty, "1\n"); + try s.write(ctl, "clean\n"); + try s.expect(dirty, "0\n"); + try s.write(s.at("/pane/{d}/addr", .{n}), "/two/"); + try s.write(ctl, "dot=addr\nshow\nnomark\nmark\nlimit=addr\n"); + try std.testing.expectEqual(@as(u32, 4), (try s.range(s.at("/pane/{d}/dot", .{n})))[0]); + try std.testing.expectEqual(.{ @as(u32, 4), @as(u32, 7) }, try s.range(s.at("/pane/{d}/limit", .{n}))); + try s.write(ctl, "cleartag\n"); + for ([_][]const u8{ "menu\n", "nomenu\n", "dump echo hi\n", "dumpdir /tmp\n", "font Go Mono\n" }) |line| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, line)); + try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid: acme's ")); + } + // Checked whole: a refused word anywhere and nothing ran. + try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "dirty\nmenu\n")); + try s.expect(dirty, "0\n"); +} + +test "panes: +Unsaved drops a pane once it has closed" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const a = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-unsaved-a.txt\n"); + try s.write(s.at("/pane/{d}/body", .{a}), "edited\n"); + const b = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{b}), "/tmp/pardes-unsaved-b.txt\n"); + try s.write(s.at("/pane/{d}/body", .{b}), "edited\n"); + try std.testing.expect(try s.refused("/ctl", "Exit\n") != 0); + const list = s.at("/pane/{d}/body", .{(try panesNamed(&s, "/+Unsaved"))[0]}); + try std.testing.expect(has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified")); + // a closes (discarded): its row goes. + _ = try s.refused(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n"); + try std.testing.expectEqual(@as(usize, 0), (try panesNamed(&s, "/pardes-unsaved-a.txt")).len); + try std.testing.expect(!has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified")); +} + +test "panes: name refuses a directory, EISDIR, and leaves the name as it was" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + for ([_][]const u8{ "/\n", "/tmp/\n", "sub/\n", "/tmp\n" }) |dir| { + try std.testing.expectEqual(@as(u16, 21), try s.refused(name, dir)); + try std.testing.expect(std.mem.indexOf(u8, s.said, "is a directory") != null); + } + try s.expect(name, "/test.txt\n"); +} + +test "panes: a write of two lines to name is refused EINVAL, on a held open or not" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/a\n/tmp/b\n")); + const h = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h, "/tmp/a\n/tmp/b\n")); + s.clunk(h); + try s.expect(name, "/test.txt\n"); + // Line by line, as bash writes it: the second line is refused. + const h2 = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h2, "/tmp/pardes-a\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h2, "/tmp/pardes-b\n")); + s.clunk(h2); + try s.expect(name, "/tmp/pardes-a\n"); +} + +test "panes: a name with a component over 255 bytes is refused by the write that holds it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/" ++ &@as([256]u8, @splat('c')) ++ "/f.txt\n")); + try std.testing.expectEqualStrings("invalid file name: a component over 255 bytes", s.said); + const longest = "/tmp/" ++ &@as([255]u8, @splat('c')); + try s.write(name, longest ++ "\n"); + // Held with no newline, it is refused by that write, and the close + // has nothing left to apply. + const held = try s.hold(name, 1); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(held, "/tmp/" ++ &@as([256]u8, @splat('e')))); + s.clunk(held); + try s.expect(name, longest ++ "\n"); +} + +test "panes: a name cut across writes is one name, applied once at its newline or its close" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const name = s.at("/pane/{d}/name", .{try s.focused()}); + const h = try s.hold(name, 1); + _ = try s.writeFid(h, "/tmp/pardes-na"); + try s.expect(name, "/test.txt\n"); + _ = try s.writeFid(h, "me.txt\n"); + try s.expect(name, "/tmp/pardes-name.txt\n"); + s.clunk(h); + // No newline: held, and applied at the close. + const h3 = try s.hold(name, 1); + _ = try s.writeFid(h3, "/tmp/pardes-closed.txt"); + try s.expect(name, "/tmp/pardes-name.txt\n"); + s.clunk(h3); + try s.expect(name, "/tmp/pardes-closed.txt\n"); +} + +test "panes: name reads the file name, and a write promotes a scratch without touching its body or its file" { + const gpa = std.testing.allocator; + var s = try Session.initFile(gpa, "opener\n"); + defer s.deinit(); + const n = try s.newPane(); + const name = s.at("/pane/{d}/name", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + const scratch = try s.read(name); + try std.testing.expect(std.mem.endsWith(u8, scratch, "/+New\n")); + try std.testing.expectEqual(@as(u64, scratch.len), (try s.stat(name)).size); + try s.write(body, "scratch "); + try s.write(body, "work\n"); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "renamed.zig", .data = "existing target\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + const path = s.at("{s}/renamed.zig", .{dir}); + try s.write(name, s.at("{s}\n", .{path})); + try s.expect(body, "scratch work\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); + try s.expect(name, s.at("{s}\n", .{path})); + var target_buf: [64]u8 = undefined; + try std.testing.expectEqualStrings("existing target\n", try tmp.dir.readFile(std.testing.io, "renamed.zig", &target_buf)); + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "\n")); + // One name, and what no file name holds: a second line, a NUL. + for ([_][2][]const u8{ + .{ "two\nlines\n", "invalid file name: one name a write, on one line" }, + .{ "nul\x00.zig\n", "bad character in file name: a NUL" }, + }) |c| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(name, c[0])); + try std.testing.expectEqualStrings(c[1], s.said); + } + // Any other byte a file name holds is taken, and reads back as written: + // blanks at its ends, a tab, a control byte, DEL, C1, bytes not UTF-8. + for ([_][]const u8{ " lead.zig ", "tab\t.zig", "c\x01.zig", "del\x7f.zig", "c1\xc2\x85.zig", "bad\xff.zig" }) |odd| { + try s.write(name, s.at("{s}\n", .{odd})); + const read_back = std.mem.trimEnd(u8, try s.read(name), "\n"); + try std.testing.expect(std.mem.endsWith(u8, read_back, odd)); + try s.write(name, read_back); + try s.expect(name, s.at("{s}\n", .{read_back})); + } + // A name that is one line and UTF-8 is taken, é and all. + try s.write(name, "caf\xc3\xa9.zig\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(name), "/caf\xc3\xa9.zig\n")); + try s.write(name, "two words.zig\n"); + try s.expect(name, s.at("{s}/two words.zig\n", .{dir})); + // A rename is no edit: Undo takes back the last write. + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "scratch "); +} + +test "panes: relative names go from the file's directory and can name a new target" { + var s = try Session.initFile(std.testing.allocator, "retained body\n"); + defer s.deinit(); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from pane directory\n" }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + try s.write(name, s.at("{s}/old.txt\n", .{dir})); + try s.write(name, "child/../note.txt\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "from pane directory\n"); + try s.write(name, "./created.txt\n"); + try s.expect(name, s.at("{s}/created.txt\n", .{dir})); + // A name alone is no edit. + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: relative names follow a pane/new scratch's session directory, and a virtual one" { + var s = try Session.initFile(std.testing.allocator, "source body\n"); + defer s.deinit(); + const source = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{source}), "/elsewhere/source.zig\n"); + // pane/new's scratch is the session's, not the active pane's. + const n = try s.newPane(); + const name = s.at("/pane/{d}/name", .{n}); + const session = std.fs.path.dirname(std.mem.trimEnd(u8, try s.read(name), "\n")).?; + try std.testing.expect(!std.mem.eql(u8, session, "/elsewhere")); + const up = std.fs.path.dirname(session) orelse ""; + try s.write(name, "../out/./notes.txt\n"); + try s.expect(name, s.at("{s}/out/notes.txt\n", .{up})); + try s.write(name, "again.txt\n"); + try s.expect(name, s.at("{s}/out/again.txt\n", .{up})); + try s.write(name, s.at("/virtual/pane/{d}/./body\n", .{source})); + try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "source body\n"); + try s.write(name, "./body\n"); + try s.expect(name, s.at("/virtual/pane/{d}/body\n", .{source})); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); +} + +test "panes: errors appends to one +Errors pane for the pane's directory" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "+Errors") == null); + try s.write(s.at("/pane/{d}/errors", .{n}), "boom\n"); + try s.write(s.at("/pane/{d}/errors", .{n}), "again\n"); + const index = try s.read("/index"); + try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, " /+Errors ")); + const at = std.mem.indexOf(u8, index, " /+Errors ").?; + const line = index[(if (std.mem.lastIndexOfScalar(u8, index[0..at], '\n')) |i| i + 1 else 0)..]; + const serial = line[0..std.mem.indexOfScalar(u8, line, ' ').?]; + try s.expect(s.at("/pane/{s}/body", .{serial}), "boom\nagain\n"); +} + +test "panes: truncating body empties it, and a stat says the length after" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const body = s.at("/pane/{d}/body", .{try s.focused()}); + const h = try s.hold(body, 1); + try s.truncate(h); + try std.testing.expectEqual(@as(u64, 0), (try s.stat(body)).size); + try s.expect(body, ""); + s.clunk(h); + try s.write(body, "new text\n"); + try std.testing.expectEqual(@as(u64, 9), (try s.stat(body)).size); +} + +test "panes: a rename colours a source its new name's language names, its text unchanged" { + if (!@import("pardes_config").syntax_highlighting) return error.SkipZigTest; + var s = try Session.initFile(std.testing.allocator, "fn check() void {}\n"); + defer s.deinit(); + const n = try s.focused(); + const x, const y = try s.where("fn check"); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + const plain = try s.styles(); + try std.testing.expectEqualStrings(plain[y * cols + x], plain[y * cols + x + 3]); + try s.write(s.at("/pane/{d}/name", .{n}), "renamed.zig\n"); + const coloured = try s.styles(); + try std.testing.expectEqualStrings(coloured[y * cols + x], coloured[y * cols + x + 1]); + try std.testing.expect(!std.mem.eql(u8, coloured[y * cols + x], plain[y * cols + x])); + try s.expect(s.at("/pane/{d}/body", .{n}), "fn check() void {}\n"); +} diff --git a/test/behaviour/placement.zig b/test/behaviour/placement.zig index 079b5a5e..3d261808 100644 --- a/test/behaviour/placement.zig +++ b/test/behaviour/placement.zig @@ -59,3 +59,113 @@ test "placement: Placement pardes puts a new pane right under the asking one, an try s.write("/ctl", "Placement\n"); try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement acme\n") != null); } + +test "placement: a look under pardes's placement, from a pane pane/new made after Delcol, opens its file in a column" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "file\n" }); + 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 first = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{first}), "Placement\n"); + try s.write(s.at("/pane/{d}/exec", .{first}), "Delcol\n"); + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/look", .{made}), s.at("{s}/f.txt:#4\n", .{dir})); + const opened = try s.focused(); + try std.testing.expect(opened != made); + // The pane pane/new made is placed, and so is the one its look opened. + const cols = try s.columns(); + for ([_]u32{ made, opened }) |n| try std.testing.expect(std.mem.indexOf(u8, cols, s.at(":{d}", .{n})) != null or std.mem.indexOf(u8, cols, s.at(",{d}", .{n})) != null); +} + +/// Each pane's tag row and its rows down to the next tag, or the screen's +/// end, as /screen draws them; `names` top to bottom in one column. +fn spans(s: *Session, comptime n: usize, names: [n][]const u8) ![n][2]usize { + var out: [n][2]usize = undefined; + for (names, 0..) |name, i| out[i][0] = try tagRow(s, name); + const rows = std.mem.count(u8, try s.screen(), "\n") + 1; + for (0..n) |i| out[i][1] = (if (i + 1 < n) out[i + 1][0] else rows) - out[i][0]; + return out; +} + +test "placement: a look from a pane's text goes under its text when it has many blank rows, else halves the biggest pane" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + for ([_][]const u8{ "second.txt", "third.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n"); + defer s.deinit(); + s.detach(); + // Wide enough that a tag with the test's temporary path stays one row, + // wherever the checkout is. + try s.write("/ctl", "size 200 60\n"); + const top = try s.focused(); + // Most of the pane is blank: the look starts under its three lines + // (makenewwindow, util.c:482-486). + try s.write(s.at("/pane/{d}/look", .{top}), s.at("{s}/second.txt\n", .{at})); + const second = try s.focused(); + const shown = s.at("{s}/second.txt", .{at}); + try std.testing.expectEqual((try tagRow(&s, "/test.txt")) + 1 + 3, try tagRow(&s, shown)); + // Both full of text: no blank space is big, so the biggest is halved. + const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); + for ([_]u32{ top, second }) |n| try s.replace(s.at("/pane/{d}/body", .{n}), full); + const was = try spans(&s, 2, .{ "/test.txt", shown }); + const big: usize = if (was[1][1] >= was[0][1]) 1 else 0; + try s.write(s.at("/pane/{d}/look", .{second}), s.at("{s}/third.txt\n", .{at})); + const third = try s.focused(); + const ids = [_]u32{ top, second }; + try std.testing.expect(std.mem.indexOf(u8, try s.columns(), s.at("{d},{d}", .{ ids[big], third })) != null); + const made = s.at("{s}/third.txt", .{at}); + const now = if (big == 1) try spans(&s, 3, .{ "/test.txt", shown, made }) else try spans(&s, 3, .{ "/test.txt", made, shown }); + try std.testing.expectEqual(was[big][0] + was[big][1] / 2, now[big + 1][0]); + try std.testing.expectEqual(was[big][0] + was[big][1], now[big + 1][0] + now[big + 1][1]); +} + +test "placement: a look from the middle of a column splits only the pane it came from, short ones round it kept short, through a Newcol too" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + for ([_][]const u8{ "mid", "low", "made" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 100 60\n"); + const top = try s.focused(); + try s.write(s.at("/pane/{d}/look", .{top}), s.at("{s}/mid\n", .{at})); + const mid = try s.focused(); + try s.write(s.at("/pane/{d}/look", .{mid}), s.at("{s}/low\n", .{at})); + const low = try s.focused(); + const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*))); + for ([_]u32{ top, mid, low }) |n| try s.replace(s.at("/pane/{d}/body", .{n}), full); + // Short names, so a narrowed column draws each tag on one row. + try s.write(s.at("/pane/{d}/name", .{mid}), "/mid\n"); + try s.write(s.at("/pane/{d}/name", .{low}), "/low\n"); + const names = [_][]const u8{ "/test.txt", "/mid", "/low", "/made" }; + // The top and bottom panes dragged by their boxes to two and three + // rows, under a placement's minimum; the middle one full of text, so + // the one halved. + const rows = std.mem.count(u8, try s.screen(), "\n") + 1; + const r = try spans(&s, 3, names[0..3].*); + try s.write("/mousein", s.at("m 0 {d} 1\nm 0 {d} 1\nm 0 {d} 0\n", .{ r[1][0], r[0][0] + 2, r[0][0] + 2 })); + try s.write("/mousein", s.at("m 0 {d} 1\nm 0 {d} 1\nm 0 {d} 0\n", .{ r[2][0], rows - 3, rows - 3 })); + const was = try spans(&s, 3, names[0..3].*); + try std.testing.expectEqual(@as(usize, 2), was[0][1]); + try std.testing.expectEqual(@as(usize, 3), was[2][1]); + try s.write(s.at("/pane/{d}/look", .{mid}), s.at("{s}/made\n", .{at})); + const made = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{made}), "/made\n"); + try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d},{d}", .{ try s.column(0), top, mid, made, low }), try s.columns()); + const now = try spans(&s, 4, .{ names[0], names[1], names[3], names[2] }); + try std.testing.expectEqual(was[0], now[0]); + try std.testing.expectEqual(was[2], now[3]); + // A new column narrows this one, and the short panes keep their rows. + try s.write(s.at("/pane/{d}/exec", .{made}), "Newcol\n"); + try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try s.columns(), ":")); + const narrowed = try spans(&s, 4, .{ names[0], names[1], names[3], names[2] }); + try std.testing.expectEqual(was[0][1], narrowed[0][1]); + try std.testing.expectEqual(was[2][1], narrowed[3][1]); +} diff --git a/test/behaviour/repls.zig b/test/behaviour/repls.zig new file mode 100644 index 00000000..44c53361 --- /dev/null +++ b/test/behaviour/repls.zig @@ -0,0 +1,11 @@ +//! docs/typ/reference.typ, REPLs <repls>. +const std = @import("std"); +const Session = @import("session").Session; + +test "repls: Repl with no such language is invalid, EINVAL, and names a few whole and how many more" { + var s = try Session.initTerm(std.testing.allocator); + defer s.deinit(); + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{try s.focused()}), "Repl zzlang\n")); + try std.testing.expect(std.mem.indexOf(u8, s.said, "Repl: invalid language \"zzlang\"; - or one like zig ada bash c c_sharp clojure (") != null); + try std.testing.expect(std.mem.endsWith(u8, s.said, " more)")); +} diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index 90838d90..12f2b56a 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -85,3 +85,59 @@ test "root-ctl: the root ctl reads the settings as a write takes them, and takes 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")); } + +test "root-ctl: Kill with a name and nothing running says so and does not fail, as bare Kill does" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Kill zzz\n"); + try std.testing.expect(has(try s.read("/log"), "Kill: nothing running")); +} + +test "root-ctl: a bad Tab width and a bad Incl directory are refused invalid, changing nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const before = try s.read("/ctl"); + // Bare, Incl lists its directories. + try s.write("/ctl", "Incl\n"); + const listed = try lastIncl(&s); + for ([_][]const u8{ "Tab 0\n", "Tab 17\n", "Tab x\n" }) |line| { + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", line)); + // Run from a pane, as a tag click runs it: said invalid too. + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), line)); + try std.testing.expect(has(s.said, "invalid tab width")); + } + try s.expect("/ctl", before); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Incl rel/dir\n")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Incl - /usr/include\n")); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/ctl", "Incl /tmp/pardes-surely-no-such-dir\n")); + // The same list again: none taken. + try s.write("/ctl", "Incl\n"); + try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try s.read("/log"), listed)); + // Bare Tab on the root ctl says the width, as from a pane. + try s.write("/ctl", "Tab\n"); +} + +/// The log's one `msg - Incl:` record, whole. +fn lastIncl(s: *Session) ![]const u8 { + const log = try s.read("/log"); + const at = std.mem.indexOf(u8, log, "\nmsg - Incl:") orelse return error.NoIncl; + return log[at .. std.mem.indexOfScalarPos(u8, log, at + 1, '\n').? + 1]; +} + +test "root-ctl: Joincol with no column to the right and Theme with no such theme say so; a theme's name is any case" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try std.testing.expect(try s.refused("/ctl", "Joincol\n") != 0); + try std.testing.expect(std.mem.startsWith(u8, s.said, "Joincol: no column to the right")); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Theme drak\n")); + try std.testing.expect(std.mem.startsWith(u8, s.said, "bad value in control message; like it: ")); + try std.testing.expect(has(s.said, ": dr")); + try std.testing.expect(std.mem.endsWith(u8, s.said, "\"drak\"")); + // Within the 128 bytes an Rerror carries, the quoted line kept. + try std.testing.expect(s.said.len <= 128); + // A click on the word says it in the log. + _ = try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Theme drak\n"); + try std.testing.expect(has(try s.read("/log"), "no theme \"drak\"")); + try s.write("/ctl", "Theme DRACULA\n"); + try std.testing.expect(has(try s.read("/ctl"), "\nTheme dracula\n")); +} diff --git a/test/behaviour/rules.zig b/test/behaviour/rules.zig index 4b853a40..8f5597ae 100644 --- a/test/behaviour/rules.zig +++ b/test/behaviour/rules.zig @@ -37,3 +37,25 @@ test "rules: body reads at any offset and writes append; a stat says its length, 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); } + +test "rules: every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + for ([_][2][]const u8{ + .{ "/exec", "echo a\x7fb\n" }, + .{ "/look", "a\x7fb\n" }, + }) |c| { + try std.testing.expectEqual(@as(u16, 22), try s.refused(c[0], c[1])); + try std.testing.expect(std.mem.indexOf(u8, s.said, "control character") != null); + } + try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "\n")); + try std.testing.expect(std.mem.indexOf(u8, s.said, "an empty name") != null); + const log = try s.hold("/log", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(log, "bogus\n")); + try std.testing.expect(std.mem.indexOf(u8, s.said, "takes `follow`") != null); + s.clunk(log); + const text = try s.read("/log"); + try std.testing.expect(std.mem.indexOf(u8, text, "Invalid argument") == null); + try std.testing.expect(std.mem.indexOf(u8, text, "an empty name") != null); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index ff24704a..a24a3010 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -101,3 +101,171 @@ test "settings: Config without a configuration directory says so and opens nothi try s.expect("/index", index); try expectHas(try s.read("/log"), "Config: no startup file here: no configuration directory"); } + +test "settings: a bad setting value is quoted, so two different bad values log as two records, never one counted twice" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + _ = try s.refused("/ctl", "Verbose maybe\n"); + _ = try s.refused("/ctl", "Verbose nope\n"); + const log = try s.read("/log"); + try std.testing.expect(has(log, "takes on, off \"maybe\"\n")); + try std.testing.expect(has(log, "takes on, off \"nope\"\n")); + try std.testing.expect(!has(log, "(x2)")); +} + +test "settings: a setting this frontend cannot show says GUI-only, never run as a command, and DumpDir reads back" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + if (comptime @import("pardes_config").platform != .gui) { + _ = try s.refused("/ctl", "Lift on\n"); + try std.testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", s.said); + _ = try s.refused("/ctl", "GripWidth 120\n"); + try std.testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", s.said); + // A builtin the GUI alone has, written to a ctl or an exec, never + // runs as a shell command. + _ = try s.refused("/ctl", "Fonts\n"); + try std.testing.expectEqualStrings("Fonts is GUI-only, invalid here \"Fonts\"", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/exec", "Fonts\n")); + try std.testing.expectEqualStrings("Fonts is GUI-only, invalid here", s.said); + } + _ = try s.refused("/ctl", "Liftt on\n"); + try std.testing.expectEqualStrings("unknown control message \"Liftt\"", s.said); + try std.testing.expect(has(try s.read("/ctl"), "\nDumpDir /")); +} + +test "settings: Back and Forward written to exec keep to the focused file, and JumpScope all, written to ctl, takes every place in order" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) }); + 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(); + // Each a look, as a user's jumps are: b1 a1 b40 a40 b80 a80. + for ([_][]const u8{ "b.txt:1", "a.txt:1", "b.txt:40", "a.txt:40", "b.txt:80", "a.txt:80" }) |place| try s.write("/look", s.at("{s}/{s}\n", .{ dir, place })); + const Step = struct { []const u8, []const u8, u32 }; + const walk = struct { + fn run(q: *Session, steps: []const Step) !void { + for (steps) |st| { + try q.write("/exec", q.at("{s}\n", .{st[0]})); + const n = try q.focused(); + try std.testing.expect(std.mem.endsWith(u8, try q.read(q.at("/pane/{d}/name", .{n})), q.at("{s}\n", .{st[1]}))); + // Each line is `row\n`: the line the dot is on. + try std.testing.expectEqual(st[2] - 1, (try q.range(q.at("/pane/{d}/dot", .{n})))[0] / 4); + } + } + }.run; + // a's places first, b only once a has none older; Forward mirrors it + // from b and ends at a80, where it began. + try walk(&s, &.{ + .{ "Back", "/a.txt", 40 }, + .{ "Back", "/a.txt", 1 }, + .{ "Back", "/b.txt", 1 }, + .{ "Forward", "/b.txt", 40 }, + .{ "Forward", "/b.txt", 80 }, + .{ "Forward", "/a.txt", 80 }, + }); + try s.write("/ctl", "JumpScope all\n"); + try std.testing.expect(has(try s.read("/ctl"), "JumpScope all\n")); + try walk(&s, &.{ + .{ "Back", "/b.txt", 80 }, + .{ "Back", "/a.txt", 40 }, + .{ "Forward", "/b.txt", 80 }, + }); +} + +const syntax = @import("pardes_config").syntax_highlighting; + +/// The style /screen draws the cell `into` cells into the first place +/// `text` is drawn. +fn styleOf(s: *Session, text: []const u8, into: usize) ![]const u8 { + const x, const y = try s.where(text); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + return (try s.styles())[y * cols + x + into]; +} + +/// How many ` fn ` and ` if ` keywords the screen draws, each of whose two +/// cells must be drawn in `want`. +fn keywordsIn(s: *Session, want: []const u8) !usize { + const screen = try s.screen(); + const styles = try s.styles(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + var seen: usize = 0; + var rows = std.mem.splitScalar(u8, screen, '\n'); + var y: usize = 0; + while (rows.next()) |row| : (y += 1) for ([_][]const u8{ " fn ", " if " }) |word| { + var from: usize = 0; + while (std.mem.indexOfPos(u8, row, from, word)) |i| : (from = i + 1) { + const x = try std.unicode.utf8CountCodepoints(row[0..i]) + 1; + for (0..2) |k| try std.testing.expectEqualStrings(want, styles[y * cols + x + k]); + seen += 1; + } + }; + return seen; +} + +test "settings: Colors off draws a source plain, and on again colours its keywords, the layout as it was, an edit made while off included" { + if (!syntax) return error.SkipZigTest; + for ([_]bool{ false, true }) |edit_while_off| { + var s = try Session.initFile(std.testing.allocator, "pub fn first() void { if (true) {} }\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), "/colors.zig\n"); + var ident: []const u8 = "first"; + if (edit_while_off) { + try std.testing.expect(!std.mem.eql(u8, try styleOf(&s, " fn ", 1), try styleOf(&s, "first", 0))); + try s.write("/ctl", "Colors off\n"); + try s.replace(s.at("/pane/{d}/body", .{n}), "pub fn edited() void { if (false) {} }\n"); + ident = "edited"; + } else try s.write("/ctl", "Colors off\n"); + try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, try styleOf(&s, ident, 0))); + const layout = try s.read("/layout"); + try s.write("/ctl", "Colors on\n"); + try s.expect("/layout", layout); + const kw = try styleOf(&s, " fn ", 1); + try std.testing.expect(!std.mem.eql(u8, kw, try styleOf(&s, ident, 0))); + try std.testing.expect(!has(kw, "\"bold\":true")); + try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, kw)); + } +} + +test "settings: SyntaxBold keywords stay coloured and bold in every row a pane grows into" { + if (!syntax) return error.SkipZigTest; + var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*)))); + defer s.deinit(); + try s.write(s.at("/pane/{d}/name", .{try s.focused()}), "/resize.zig\n"); + try s.write("/ctl", "SyntaxBold on\n"); + s.detach(); + try s.write("/ctl", "size 80 8\n"); + const kw = try styleOf(&s, " fn ", 1); + try std.testing.expect(has(kw, "\"bold\":true")); + for ([_]usize{ 50, 12, 70 }) |rows| { + try s.write("/ctl", s.at("size 80 {d}\n", .{rows})); + try std.testing.expect(try keywordsIn(&s, kw) >= 2 * (rows - 4)); + } +} + +test "settings: every on/off setting the root ctl reads flips when its word is written bare, and Theme and Shell take a value" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + var lines = std.mem.tokenizeScalar(u8, try s.read("/ctl"), '\n'); + var flipped: usize = 0; + while (lines.next()) |line| { + const space = std.mem.indexOfScalar(u8, line, ' ') orelse continue; + const word, const was = .{ line[0..space], line[space + 1 ..] }; + const on = std.mem.eql(u8, was, "on"); + if (!on and !std.mem.eql(u8, was, "off")) continue; + try s.write("/ctl", s.at("{s}\n", .{word})); + try std.testing.expectEqualStrings(s.at("{s} {s}", .{ word, if (on) "off" else "on" }), try setting(&s, word)); + flipped += 1; + } + try std.testing.expect(flipped > 20); + for ([_][]const u8{ "Colors off", "Wrap off", "Tagbottom on", "Debug on" }) |want| + try std.testing.expectEqualStrings(want, try setting(&s, want[0..std.mem.indexOfScalar(u8, want, ' ').?])); + try s.write("/ctl", "Theme acme\n"); + try s.write("/ctl", "Shell /bin/sh\n"); + try std.testing.expectEqualStrings("Theme acme", try setting(&s, "Theme")); + try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell")); +} diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig index 7cfa054f..8609e134 100644 --- a/test/behaviour/tags.zig +++ b/test/behaviour/tags.zig @@ -415,3 +415,87 @@ test "tags: a click on a tag puts its cursor there in normal mode: a letter is a try std.testing.expect(std.mem.indexOf(u8, try tag(&s, n), " SaQve ") != null); try s.expect(s.at("/pane/{d}/body", .{n}), "body\n"); } + +test "tags: a pane's tag reads whole, its path first, and a write appends to its own text up to 4096 bytes" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const path = s.at("/pane/{d}/tag", .{n}); + const whole = try tag(&s, n); + try std.testing.expect(std.mem.startsWith(u8, whole, "/test.txt")); + try std.testing.expect(std.mem.indexOf(u8, whole, "Del") != null); + try std.testing.expectEqual(@as(u64, whole.len), (try s.stat(path)).size); + try s.write(path, " Mine"); + try std.testing.expectEqualStrings(s.at("{s} Mine", .{whole}), try tag(&s, n)); + // A tag is a text like the body: a newline written to it stays one. + try s.write(path, "\nNext Line"); + try std.testing.expectEqualStrings(s.at("{s} Mine\nNext Line", .{whole}), try tag(&s, n)); + // Full, it takes not a byte more. + try s.replace(path, &@as([4096]u8, @splat('x'))); + try std.testing.expectEqual(@as(u16, 28), try s.refused(path, "x")); +} + +test "tags: truncating a tag clears its own text, u in the tag undoes it, and > drops the one newline ending it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const path = s.at("/pane/{d}/tag", .{n}); + try s.write(path, " Mine"); + // Done at the first read or write after it (or the close). + const h = try s.hold(path, 1); + try s.truncate(h); + try std.testing.expect(std.mem.indexOf(u8, try tag(&s, n), "Mine") == null); + s.clunk(h); + try std.testing.expectEqualStrings("/test.txt", std.mem.trimEnd(u8, try tag(&s, n), " ")); + // The tag keeps its undo: undoing in it brings back what was cleared. + try s.keys(":u:"); + try std.testing.expect(std.mem.endsWith(u8, try tag(&s, n), " Mine")); + // Written back without a leading space, the text still stands apart + // from the path, and a click on its first letter lands on it. + try s.replace(path, "Get"); + try std.testing.expectEqualStrings("/test.txt Get", try tag(&s, n)); + try s.click("Get", 0, 1); + try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); + // `echo Make > tag`: the newline ending the truncating write's text is + // dropped, one only; a newline between its writes stays. + const m = try s.hold(path, 1); + try s.truncate(m); + _ = try s.writeFid(m, "Make\n"); + _ = try s.writeFid(m, "More\n\n"); + s.clunk(m); + try std.testing.expectEqualStrings("/test.txt Make\nMore\n", try tag(&s, n)); + // `>>` keeps what it is given. + try s.write(path, " Tail\n"); + try std.testing.expectEqualStrings("/test.txt Make\nMore\n Tail\n", try tag(&s, n)); +} + +test "tags: a pane tag's file name is tinted, its directory, dirty marker and words not, the tag at the top or the bottom" { + for ([_]bool{ false, true }) |bottom| { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write("/ctl", "FocusTint off\n"); + if (bottom) try s.write("/ctl", "Tagbottom\n"); + try s.write(s.at("/pane/{d}/name", .{n}), "/project/界file.zig\n"); + try s.write(s.at("/pane/{d}/dirty", .{n}), "1"); + const x0, const y = try s.where("/project/"); + const screen = try s.screen(); + const styles = try s.styles(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + var rows = std.mem.splitScalar(u8, screen, '\n'); + for (0..y) |_| _ = rows.next(); + var cells = (try std.unicode.Utf8View.init(rows.next().?)).iterator(); + var x: usize = 0; + var tinted: ?[]const u8 = null; + var ink: ?[]const u8 = null; + while (cells.nextCodepointSlice()) |cell| : (x += 1) { + if (x < x0 or std.mem.eql(u8, cell, " ")) continue; + // `界file.zig`: the wide glyph's two cells, then eight. + const in_name = x >= x0 + "/project/".len and x < x0 + "/project/".len + 10; + const want = if (in_name) &tinted else &ink; + if (want.* == null) want.* = styles[y * cols + x]; + try std.testing.expectEqualStrings(want.*.?, styles[y * cols + x]); + } + try std.testing.expect(!std.mem.eql(u8, tinted.?, ink.?)); + } +} diff --git a/test/output.zig b/test/output.zig index 3cfece45..876cd8fc 100644 --- a/test/output.zig +++ b/test/output.zig @@ -389,7 +389,9 @@ test "result stepping reaches an unterminated last row" { try std.testing.expectEqual(@as(i32, 1), result.body.cur_row); } -test "Mini uses OS source precedence and refreshes one independent snapshot" { +// What it shows and when it refreshes is test/behaviour/builtins.zig's; +// the watch it never asks for is an effect, unseen through 9P. +test "Mini asks for no watch on its pane" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "build.zig", .data = "x\n" }); @@ -401,27 +403,11 @@ test "Mini uses OS source precedence and refreshes one independent snapshot" { try std.testing.expect(p.executeBuiltinLine(0, "Mini build.zig")); const id = p.active; try std.testing.expect(id != 0); - const mini = p.panes[id].?; - try std.testing.expect(mini.terminal == null); - try std.testing.expectEqualStrings("⠁\n", mini.file.?.content); - try std.testing.expect(std.mem.startsWith(u8, mini.file.?.mini.?.source, dir)); - const next = p.freeSlot(); - const body = mini.file.?.content.ptr; - try std.testing.expect(p.executeBuiltinLine(id, "Mini build.zig")); - try std.testing.expectEqual(id, p.active); - try std.testing.expectEqual(body, mini.file.?.content.ptr); - try std.testing.expectEqual(next, p.freeSlot()); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "build.zig", .data = "xx\n" }); - try std.testing.expectEqualStrings("⠁\n", mini.file.?.content); try std.testing.expect(p.executeBuiltinLine(id, "Mini build.zig")); - try std.testing.expectEqual(id, p.active); - try std.testing.expectEqualStrings("⠉\n", mini.file.?.content); while (p.nextEffect()) |effect| switch (effect) { .watch => |watch| try std.testing.expect(watch.pane != id), else => {}, }; - try std.testing.expect(p.executeBuiltinLine(id, "Save")); - try std.testing.expect(mini.prompt == .save); } test "Mini dump preserves its exact virtual snapshot and body writes detach colors" { @@ -617,7 +603,7 @@ test "EffectCode lists readable virtual sources used by this backend" { try std.testing.expectEqualStrings(pardes.filesystem.sourceBytes(backend_source["/virtual/".len..]).?, source.content); } -test "every enabled setting builtin mutates the State Config reports" { +test "a window's setting builtins (TaglineSize, Font, transitions, scene passes) reach the report DumpConfig writes" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); p.locations_config.layout = .@"inline"; @@ -653,14 +639,8 @@ test "every enabled setting builtin mutates the State Config reports" { try std.testing.expect(p.executeBuiltinLine(p.active, "DumpConfig")); const report = p.panes[p.active].?.file.?.content; - for ([_][]const u8{ - "Colors off\n", - "Wrap off\n", - "Tagbottom on\n", - "Debug on\n", - "Theme acme\n", - "Shell fish\n", - }) |line| try std.testing.expect(std.mem.indexOf(u8, report, line) != null); + // The toggles, Theme and Shell are test/behaviour/settings.zig's, through + // the root ctl; what is left is a window's (fonts, transitions, passes). const transition = if (!builtins.viewCapabilities(p).panel_transitions) "PanelSlide unsupported\n" else if (p.settings.panel_transition == .off) @@ -821,7 +801,7 @@ test "LookNext/LookPrev selects one output location per row and opens nothing" { try std.testing.expectEqual(panes.Output.Grain.word, panes.Output.grain(p.panes[p.active].?.file.?.output)); } -test "LookNext/LookPrev resumes the result output whose Look moved focus away" { +test "LookNext/LookPrev keeps the result output a Look chose through a no-match answer, and a stale serial once it is deleted" { if (platform == .web) return; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); @@ -844,12 +824,8 @@ test "LookNext/LookPrev resumes the result output whose Look moved focus away" { try std.testing.expect(results.look_at != null); try std.testing.expectEqual(results.serial, p.look_walk_owner orelse return error.MissingLookOwner); - lookKey(p, 'n'); - try std.testing.expectEqual(rid, p.active); - try std.testing.expectEqual(first + 1, results.body.cur_row); - lookKey(p, 'N'); - try std.testing.expectEqual(first, results.body.cur_row); - + // The walk on and back from it is test/behaviour/keys.zig's; what is + // left needs answers put in by fillResults. // A later no-match answer has no position to resume and therefore cannot // steal the provenance established by the Look above. const owner = results.serial; @@ -924,34 +900,6 @@ test "LookPrev exactly reverses LookNext across output panes and the ring seam" try std.testing.expect(saw_first and saw_second); } -test "LookNext/LookPrev selects command outputs by whole row" { - if (platform == .web) return; - var cwd_buf: [4096]u8 = undefined; - var path_buf: [4096]u8 = undefined; - const paths = try projectPaths(&cwd_buf, &path_buf); - const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 40, .file = paths.boot }); - defer p.deinit(); - p.update(.{ .resize = .{ .cols = 100, .rows = 40 } }); - - try std.testing.expect(p.executeBuiltinLine(p.active, "Themes")); - const tid = p.panes[p.active].?.search_pane orelse return error.MissingThemeList; - const themes = p.panes[tid].?; - try std.testing.expect(panes.Output.fileTraits(themes.file.?.output).commands); - lookKey(p, 'n'); - const row0 = std.mem.trimEnd(u8, modal.lineSlice(themes.file.?.content, 1), " \t\r"); - try std.testing.expectEqualStrings(row0, selectedOutputText(themes) orelse ""); - const theme_before = p.settings.theme; - lookKey(p, 'n'); - p.update(.{ .key = .{ .cp = Key.tab } }); - try std.testing.expect(p.settings.theme != theme_before); - - try std.testing.expect(p.executeBuiltinLine(0, "Look build.zig")); - p.update(.tick); - try std.testing.expect(p.active != 0); - lookKey(p, 'n'); - try std.testing.expectEqual(@as(usize, 0), p.active); -} - test "identical result refresh keeps warm content caches without allocation" { var allocator: std.testing.FailingAllocator = .init(std.testing.allocator, .{}); const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 }); diff --git a/test/panes.zig b/test/panes.zig index 3b842c59..6b2af760 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -790,75 +790,8 @@ const JumpSelectionTests = struct { p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = col1, .row = row1 } }); } - fn exercise(prefix: []const u8, wrap: bool, reverse: bool, first_col: u16, last_col: u16, expected: ?[]const u8) !void { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 34, .rows = 10 }); - defer p.deinit(); - p.settings.wrap = wrap; - const text = try std.fmt.allocPrint(p.gpa, "{s}{s}", .{ prefix, std.mem.asBytes(&@as([80][27]u8, @splat("unrelated destination text\n".*))) }); - defer p.gpa.free(text); - const pane = try p.setTestFile(text); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - if (wrap) try std.testing.expectEqual(pane.wrap_line[0], pane.wrap_line[1]); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = p.bodyTop(pane, rect); - if (reverse) - drag(p, x + last_col, y + 1, x + first_col, y) - else - drag(p, x + first_col, y, x + last_col, y + 1); - - const selected = try p.gpa.dupe(u8, pardes.exec.heldSelection(p, 0) orelse return error.MissingMouseSelection); - defer p.gpa.free(selected); - if (expected) |want| try std.testing.expectEqualStrings(want, selected); - try std.testing.expect(std.mem.indexOfScalar(u8, selected, '\n') != null); - if (wrap) try std.testing.expect(std.mem.indexOf(u8, selected, "界") != null); - - // Seed two actual locations after the drag, so both history directions - // must scroll and discard the previous selection. - p.njumps = 2; - p.jcur = 1; - p.jumps[0] = .{ .pane = 0, .serial = pane.serial, .line = 60, .col = 1 }; - p.jumps[1] = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; - p.update(.{ .key = .{ .cp = 'o', .ctrl = true } }); - _ = frame.reset(.retain_capacity); - _ = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 0), p.jcur); - try std.testing.expect(pane.file.?.scroll > 20); - try LookResetTests.cleared(pane); - try std.testing.expect(!pane.body.vsel.active); - try std.testing.expect(pardes.exec.heldSelection(p, 0) == null); - try std.testing.expectEqual(@as(i32, 59), pane.body.cur_row); - try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); - - // A selection created after Back must also disappear on Forward. - // Back announces itself, so the body sits under a notice band now. - const y2 = p.bodyTop(pane, p.rects[0]); - drag(p, x + 1, y2, x + 3, y2 + 1); - try std.testing.expect(pardes.exec.heldSelection(p, 0) != null); - p.update(.{ .key = .{ .cp = 'i', .ctrl = true } }); - _ = frame.reset(.retain_capacity); - _ = try p.render(frame.allocator()); - try std.testing.expectEqual(@as(usize, 1), p.jcur); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.scroll); - try LookResetTests.cleared(pane); - try std.testing.expect(!pane.body.vsel.active); - try std.testing.expect(pardes.exec.heldSelection(p, 0) == null); - try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row); - try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); - } - - 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"); - } - - test "jump selection clears wrapped Unicode and tab rectangles" { - for ([_]bool{ false, true }) |reverse| - try exercise("界\tabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n", true, reverse, 0, 8, null); - } - + // One pane's Back and Forward are test/behaviour/keys.zig's; this + // one seeds held selections in two panes, which only internals reach. test "jump selection clears source and destination state across panes" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); @@ -949,7 +882,9 @@ const TagNameTintTests = struct { return visible_name; } - test "tag filename tint excludes directories dirty marker and output commands" { + // A file pane's is test/behaviour/tags.zig's; a search pane's needs + // results put in through fillResults. + test "tag filename tint of a search pane excludes its directory and command" { for ([_]bool{ false, true }) |bottom| { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 24 }); defer p.deinit(); @@ -961,7 +896,6 @@ const TagNameTintTests = struct { configure(p); defer p.custom_theme = null; p.settings.tag_bottom = bottom; - try std.testing.expect((try check(p, 0, pane.file.?.path)) > 0); try panes.Output.fillResults(p, 0, "/project", .search, "tint", try p.gpa.dupe(u8, "no matches\n"), null); const id = pane.search_pane.?; p.update(.tick); @@ -1399,11 +1333,13 @@ test "a prompt that cannot be opened leaves the open one as it was" { } } -test "Save's prompt starts from the pane's directory" { +// A file's directory is test/behaviour/keys.zig's; an owned directory (a +// shell's) ending in a slash, or none, only a real shell gives. +test "Save's prompt starts from an owned directory ending in a slash, or none" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); const pane = try p.setTestFile("one\n"); - for ([_][]const u8{ "/dír space", "/dír space/", "" }) |dir| { + for ([_][]const u8{ "/dír space/", "" }) |dir| { try pane.setOwnedCwd(dir); const slash = if (dir.len == 0 or dir[dir.len - 1] != '/') "/" else ""; p.startPrompt(pane, &pane.body, .save); @@ -3774,101 +3710,6 @@ test "cancelled normal gestures leave no count or subprefix through raw tty mode } } -test "Colors restores syntax after rendering disabled files and disabled edits" { - if (!syntax.enabled) return error.SkipZigTest; - const gpa = std.testing.allocator; - for ([_]bool{ false, true }) |edit_while_disabled| { - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("pub fn first() void { if (true) {} }\n"); - const file = &pane.file.?; - gpa.free(file.path); - file.path = try gpa.dupe(u8, "/colors.zig"); - p.settings.colors = edit_while_disabled; - p.presentation.enabled = false; - var frame = std.heap.ArenaAllocator.init(gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - if (edit_while_disabled) { - try std.testing.expect(file.highlights.len > 0); - p.update(.{ .command = "Colors" }); - panes.File.setContent(p, file, try gpa.dupe(u8, "pub fn edited() void { if (false) {} }\n")); - _ = frame.reset(.retain_capacity); - _ = try p.render(frame.allocator()); - } - try std.testing.expect(!p.settings.colors); - try std.testing.expectEqual(@as(usize, 0), file.highlights.len); - try std.testing.expect(!file.syntax_dirty); - const rect = p.rects[0]; - const cols = pane.cols; - const rows = pane.rows; - p.update(.{ .command = "Colors" }); - try std.testing.expectEqual(rect, p.rects[0]); - try std.testing.expectEqual(cols, pane.cols); - try std.testing.expectEqual(rows, pane.rows); - _ = frame.reset(.retain_capacity); - const surface = try p.render(frame.allocator()); - try std.testing.expect(p.settings.colors); - try std.testing.expect(file.highlights.len > 0); - try std.testing.expect(!file.syntax_dirty); - var seen: usize = 0; - for (surface.cells[0 .. surface.cells.len - 1], 0..) |*first, i| { - const second = &surface.cells[i + 1]; - if (!(std.mem.eql(u8, first.grapheme(), "f") and std.mem.eql(u8, second.grapheme(), "n")) and - !(std.mem.eql(u8, first.grapheme(), "i") and std.mem.eql(u8, second.grapheme(), "f"))) continue; - seen += 1; - for ([_]*pardes.Cell{ first, second }) |cell| { - try std.testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, cell.style.fg); - try std.testing.expect(!cell.style.bold); - } - } - try std.testing.expectEqual(@as(usize, 2), seen); - } -} - -test "growing a file pane colors every newly visible Zig keyword" { - if (!syntax.enabled) return error.SkipZigTest; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 8 }); - defer p.deinit(); - const pane = try p.setTestFile(std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*)))); - const file = &pane.file.?; - gpa.free(file.path); - file.path = try gpa.dupe(u8, "/resize.zig"); - p.update(.{ .command = "SyntaxBold" }); - try std.testing.expect(p.settings.syntax_bold); - p.settings.colors = true; - p.presentation.enabled = false; - var frame = std.heap.ArenaAllocator.init(gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const initial_end = file.highlight_start + file.highlights.len; - for ([_]u16{ 50, 12, 70 }) |rows| { - p.update(.{ .resize = .{ .cols = 80, .rows = rows } }); - _ = frame.reset(.retain_capacity); - const surface = try p.render(frame.allocator()); - const rect = p.rects[0]; - const body_y = bodyY(p, rect); - var seen: usize = 0; - for (body_y..body_y + bodyH(p, rect)) |y| { - for (rect.x + config.GUTTER..rect.x + rect.w - 1) |x| { - const first = surface.at(@intCast(x), @intCast(y)); - const second = surface.at(@intCast(x + 1), @intCast(y)); - if (!(std.mem.eql(u8, first.grapheme(), "f") and std.mem.eql(u8, second.grapheme(), "n")) and - !(std.mem.eql(u8, first.grapheme(), "i") and std.mem.eql(u8, second.grapheme(), "f"))) continue; - seen += 1; - for ([_]*pardes.Cell{ first, second }) |cell| { - try std.testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, cell.style.fg); - try std.testing.expect(cell.style.bold); - } - } - } - try std.testing.expect(seen >= 2 * (rows - 4)); - try std.testing.expect(file.highlight_start + file.highlights.len > initial_end); - try std.testing.expect(!file.syntax_dirty); - } -} - test "Zig keywords keep every rendered byte colored across line-number widths" { if (!syntax.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; diff --git a/test/pdf.zig b/test/pdf.zig index a068fc40..16522af6 100644 --- a/test/pdf.zig +++ b/test/pdf.zig @@ -1146,34 +1146,6 @@ fn copySourceRect( return out; } -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, .{ - .file = "docs/design.pdf", - .cols = 80, - .rows = 24, - }); - defer p.deinit(); - const pane = p.panes[0].?; - p.native_images = false; - - // Cross-pane BODY-NORMAL actions keep their established shared paths. - p.update(.{ .key = .{ .cp = ':' } }); - try std.testing.expect(pane.focus == .tag); - try std.testing.expectEqual(Mode.normal, pane.body.mode); - p.update(.{ .key = .{ .cp = ':' } }); - try std.testing.expect(pane.focus == .body); - p.update(.{ .key = .{ .cp = ' ' } }); - try std.testing.expect(p.leader_on); - p.update(.{ .key = .{ .cp = '!' } }); - try std.testing.expect(!p.leader_on); - p.update(.{ .key = .{ .cp = '/' } }); - try std.testing.expect(pane.prompt == .search and pane.focus == .body); - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(pane.prompt == .none); -} - test "Escape cancels PDF chrome in place, then leaves the pane; Shift-Escape leaves at once" { if (!pdf_enabled or platform == .web) return; diff --git a/test/session.zig b/test/session.zig index 16a131fd..f572a4b1 100644 --- a/test/session.zig +++ b/test/session.zig @@ -314,6 +314,35 @@ pub const Session = struct { var it = std.mem.splitScalar(u8, tag[lo + 2 .. close], '/'); return .{ try std.fmt.parseInt(u32, it.next().?, 10), try std.fmt.parseInt(u32, it.next() orelse return error.NoPage, 10) }; } + + // pane2: + + /// Each cell's style as /screen draws it, as JSON text (compare two + /// cells by it), row-major as `screen` draws the cells: row `y`'s cell + /// `x` is `[y * cols + x]`. + pub fn styles(s: *Session) ![]const []const u8 { + const a = s.arena.allocator(); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{}); + const kinds = json.object.get("styles").?.array.items; + const named = try a.alloc([]const u8, kinds.len); + for (kinds, named) |style, *text| text.* = try std.json.Stringify.valueAlloc(a, style, .{}); + const cells = json.object.get("cells").?.array.items; + const out = try a.alloc([]const u8, cells.len); + for (cells, out) |cell, *style| style.* = named[@intCast(cell.array.items[1].integer)]; + return out; + } + + // core2: + + /// The background /screen draws at a cell, as its JSON (`{"rgb":[255,255,255]}`). + pub fn bg(s: *Session, col: usize, row: usize) ![]const u8 { + const a = s.arena.allocator(); + const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try s.read("/screen"), .{}); + const cols: usize = @intCast(json.object.get("cols").?.integer); + const cell = json.object.get("cells").?.array.items[row * cols + col]; + const style = json.object.get("styles").?.array.items[@intCast(cell.array.items[1].integer)]; + return std.json.Stringify.valueAlloc(a, style.object.get("bg").?, .{}); + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { |
