summaryrefslogtreecommitdiff
path: root/test/behaviour/panes.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour/panes.zig')
-rw-r--r--test/behaviour/panes.zig314
1 files changed, 314 insertions, 0 deletions
diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig
index 01820a07..a46f8432 100644
--- a/test/behaviour/panes.zig
+++ b/test/behaviour/panes.zig
@@ -49,3 +49,317 @@ test "panes: Putall saves every pane with unsaved edits to a file, one each, and
try s.write("/ctl", "Putall\n");
try std.testing.expectEqual(@as(usize, 1), s.saved);
}
+
+fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+test "panes: focus reads the pane with the keyboard, and a serial written to it moves the keyboard there, and only that" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const first = try s.focused();
+ const second = try s.newPane();
+ try s.expect("/focus", s.at("{d}\n", .{second}));
+ // A stat says the length a read gives.
+ try std.testing.expectEqual(@as(u64, s.at("{d}\n", .{second}).len), (try s.stat("/focus")).size);
+ try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " notcurrent "));
+ // A column's tag has the keyboard: no pane does, and a write takes it back.
+ try s.write("/ctl", "Newcol\n");
+ try s.expect("/focus", "");
+ try std.testing.expectEqual(@as(u64, 0), (try s.stat("/focus")).size);
+ try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent "));
+ try s.write("/focus", s.at("{d}\n", .{first}));
+ try s.expect("/focus", s.at("{d}\n", .{first}));
+ try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " current "));
+ try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent "));
+ try std.testing.expectEqual(@as(u16, 2), try s.refused("/focus", "99999\n"));
+ try std.testing.expectEqualStrings("no such pane", s.said);
+ for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try std.testing.expectEqual(@as(u16, 22), try s.refused("/focus", bad));
+ try s.expect("/focus", s.at("{d}\n", .{first}));
+ // Focus gives the keyboard only: a folded pane stays folded.
+ try s.write(s.at("/pane/{d}/ctl", .{second}), "Collapse\n");
+ try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed "));
+ try s.write("/focus", s.at("{d}", .{second}));
+ try s.expect("/focus", s.at("{d}\n", .{second}));
+ try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed "));
+}
+
+test "panes: the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" {
+ var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const ctl = s.at("/pane/{d}/ctl", .{n});
+ // A builtin is its own capitalised word: `GET` and `DEL` are none.
+ for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| {
+ try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, s.at("{s}\n", .{bad})));
+ try std.testing.expectEqualStrings(s.at("unknown control message \"{s}\"", .{std.mem.sliceTo(bad, ' ')}), s.said);
+ }
+ _ = try s.refused(ctl, "Newcol\n");
+ try std.testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", s.said);
+ _ = try s.refused(ctl, "Verbose off\n");
+ try std.testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", s.said);
+ _ = try s.refused(ctl, "Collapse now\n");
+ try std.testing.expectEqualStrings("wrong #args in control message \"Collapse\"", s.said);
+ // Checked whole first: a bad line anywhere and nothing ran.
+ try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nbogus\n"));
+ // A pane's builtin runs at that pane.
+ try s.write(ctl, "Msg from ctl\n");
+ try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from ctl\n", .{n})));
+ // One that means nothing bare is refused before anything runs.
+ _ = try s.refused(ctl, "Find\n");
+ try std.testing.expectEqualStrings("wrong #args in control message \"Find\"", s.said);
+ try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nMsg\n"));
+ try std.testing.expect(!has(try s.read("/log"), "first"));
+ // Save with no name on a scratch would ask for one: refused, not asked.
+ const other = try s.newPane();
+ _ = try s.refused(s.at("/pane/{d}/ctl", .{other}), "Save\n");
+ try std.testing.expectEqualStrings("control message needs its argument \"Save\"", s.said);
+ // A line after the one that closed the pane has nowhere to run.
+ try std.testing.expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/ctl", .{other}), "Del\nMsg after\n"));
+ try std.testing.expectError(error.Refused, s.read(s.at("/pane/{d}/ctl", .{other})));
+}
+
+test "panes: a second lock is refused until the holder unlocks or closes, and binds nobody else" {
+ var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const ctl = s.at("/pane/{d}/ctl", .{n});
+ const a = try s.hold(ctl, 2);
+ const b = try s.hold(ctl, 1);
+ // An open that only reads the status line holds nothing to lock with.
+ const reader = try s.hold(ctl, 0);
+ try std.testing.expect(try s.writeFid(reader, "lock\n") != 0);
+ s.clunk(reader);
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n"));
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n"));
+ // Another open's lock is refused at once, and so is its unlock; writes
+ // to the pane's other files are not.
+ try std.testing.expectEqual(@as(u16, 16), try s.writeFid(b, "lock\n"));
+ try std.testing.expectEqualStrings("file in use: another open of this ctl holds the lock", s.said);
+ try std.testing.expectEqual(@as(u16, 22), try s.writeFid(b, "unlock\n"));
+ try std.testing.expectEqualStrings("pane not locked by this open", s.said);
+ try s.write(s.at("/pane/{d}/addr", .{n}), "1");
+ try s.write(s.at("/pane/{d}/data", .{n}), "ONE\n");
+ try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n");
+ // Once the holder unlocks, the other's try takes it.
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "unlock\n"));
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(b, "lock\n"));
+ try std.testing.expectEqual(@as(u16, 16), try s.writeFid(a, "lock\nunlock\n"));
+ // Closing the holder gives it up.
+ s.clunk(b);
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\nunlock\nlock\n"));
+ try std.testing.expectEqual(@as(u16, 22), try s.writeFid(a, "unlock\nunlock\n"));
+ // Still a's: another open is refused it.
+ const c = try s.hold(ctl, 1);
+ try std.testing.expectEqual(@as(u16, 16), try s.writeFid(c, "lock\n"));
+ s.clunk(c);
+ // The lock lives and dies with the pane.
+ const other = try s.newPane();
+ const d = try s.hold(s.at("/pane/{d}/ctl", .{other}), 1);
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, "lock\n"));
+ try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n");
+ try std.testing.expectEqual(@as(u16, 2), try s.writeFid(d, "lock\n"));
+ s.clunk(d);
+ s.clunk(a);
+ // a's close gave its lock up too.
+ const e = try s.hold(ctl, 1);
+ try std.testing.expectEqual(@as(u16, 0), try s.writeFid(e, "lock\n"));
+ s.clunk(e);
+}
+
+test "panes: an unsaved file's tag is its name alone, its dirty file says it is unsaved" {
+ var s = try Session.initFile(std.testing.allocator, "before\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const tag = s.at("/pane/{d}/tag", .{n});
+ const clean = try s.read(tag);
+ try s.write(s.at("/pane/{d}/body", .{n}), "after\n");
+ try std.testing.expectEqualStrings(clean, try s.read(tag));
+ try std.testing.expect(std.mem.indexOf(u8, clean, " *") == null);
+ try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n");
+}
+
+test "panes: a pane closed while its question stands ends the question in the log" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ try s.write("/ctl", "Placement pardes\n");
+ const top = try s.focused();
+ try s.write(s.at("/pane/{d}/exec", .{top}), "New\n");
+ const closing = try s.focused();
+ try s.write(s.at("/pane/{d}/exec", .{closing}), "New\n");
+ // A middle pane: Del from a key asks which side.
+ try s.write("/focus", s.at("{d}\n", .{closing}));
+ try s.keys(" d");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("ask {d} del k j\n", .{closing})) != null);
+ // A bare answer says what this question takes.
+ const ctl = s.at("/pane/{d}/ctl", .{closing});
+ _ = try s.refused(ctl, "answer\n");
+ try std.testing.expectEqualStrings("answer takes k j or -", s.said);
+ try s.write(ctl, "Del\n");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("answer {d} -\ndel {d}", .{ closing, closing })) != null);
+}
+
+test "panes: focus on a closed pane goes to the pane that takes its place, not the one focused before" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const left = try s.focused();
+ try s.write("/ctl", "Newcol\n");
+ try s.write(s.at("/col/{d}/ctl", .{try s.column(1)}), "New\n");
+ const right = try s.focused();
+ try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
+ const low = try s.focused();
+ // `right` was focused just before `low`, but `left` grows into `low`'s
+ // rows, so `left` gets the focus.
+ try s.write(s.at("/pane/{d}/ctl", .{low}), "Del\n");
+ try std.testing.expectEqual(left, try s.focused());
+ // Closing a pane that has no focus leaves the focus where it is.
+ try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
+ const other = try s.focused();
+ try s.write("/focus", s.at("{d}\n", .{right}));
+ try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n");
+ try std.testing.expectEqual(right, try s.focused());
+ // Delcol: the column that takes the width holds, level with the closed
+ // pane, the pane that takes its place; `right` was focused just before.
+ try s.write(s.at("/pane/{d}/exec", .{right}), "New\n");
+ const right_low = try s.focused();
+ try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
+ const left_low = try s.focused();
+ try s.write("/focus", s.at("{d}\n", .{right}));
+ try s.write("/focus", s.at("{d}\n", .{left_low}));
+ try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Delcol\n");
+ try std.testing.expectEqual(right_low, try s.focused());
+}
+
+test "panes: Collapse keeps the text and folds the pane to its tag, and again restores it" {
+ var s = try Session.initFile(std.testing.allocator, "keep this text\n");
+ defer s.deinit();
+ var t = try Session.initTerm(std.testing.allocator);
+ defer t.deinit();
+ const n = try s.focused();
+ for ([_][]const u8{
+ try s.read(s.at("/pane/{d}/tag", .{n})),
+ try s.read(s.at("/pane/{d}/tag", .{try s.newPane()})),
+ try t.read(t.at("/pane/{d}/tag", .{try t.focused()})),
+ }) |tag| try std.testing.expect(std.mem.endsWith(u8, tag, " Collapse Del"));
+ try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n");
+ const ctl = s.at("/pane/{d}/ctl", .{n});
+ try s.write(ctl, "Collapse\n");
+ // Folded to its tag: no text drawn, no cursor in it, the text kept.
+ try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") != null);
+ try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "keep this text") == null);
+ try std.testing.expectEqual(null, try s.cursor());
+ try s.expect(s.at("/pane/{d}/body", .{n}), "keep this text\n");
+ try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
+ // Its tag still takes the keyboard, the cursor on the tag's row.
+ try s.keys(":");
+ try std.testing.expectEqual((try s.where("/test.txt"))[1], (try s.cursor()).?[1]);
+ try s.write(ctl, "Collapse\n");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") == null);
+ try std.testing.expectEqual((try s.where("/test.txt"))[1] + 1, (try s.where("1 keep this text"))[1]);
+}
+
+test "panes: a pane its whole text fits in never scrolls to keep a margin" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\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();
+ const n = try s.focused();
+ // A second pane under it, as acme's placement puts one: right under the
+ // three lines, which leaves the file exactly three body rows.
+ try s.write(s.at("/pane/{d}/look", .{n}), s.at("{s}/f.txt\n", .{at}));
+ try s.write("/focus", s.at("{d}\n", .{n}));
+ try s.keys("jj");
+ const top = (try s.where("/test.txt"))[1];
+ try std.testing.expectEqual(top + 4, (try s.where(at[0..@min(at.len, 20)]))[1]);
+ // On the last line, and line 1 still the first drawn.
+ try std.testing.expectEqual(.{ @as(u32, 8), @as(u32, 8) }, try s.range(s.at("/pane/{d}/dot", .{n})));
+ try std.testing.expectEqual(top + 1, (try s.where("1 one"))[1]);
+}
+
+/// `name` in `dir`: a one-page PDF with no outline and no text.
+fn plainPdf(dir: std.Io.Dir, name: []const u8) !void {
+ const objects = [_][]const u8{
+ "<< /Type /Catalog /Pages 2 0 R >>",
+ "<< /Type /Pages /Count 1 /Kids [3 0 R] >>",
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>",
+ };
+ var buf: [1024]u8 = undefined;
+ var w: std.Io.Writer = .fixed(&buf);
+ var at: [objects.len]usize = undefined;
+ try w.writeAll("%PDF-1.4\n");
+ for (objects, 0..) |object, i| {
+ at[i] = w.end;
+ try w.print("{d} 0 obj\n{s}\nendobj\n", .{ i + 1, object });
+ }
+ const xref = w.end;
+ try w.print("xref\n0 {d}\n0000000000 65535 f \n", .{objects.len + 1});
+ for (at) |off| try w.print("{d:0>10} 00000 n \n", .{off});
+ try w.print("trailer\n<< /Size {d} /Root 1 0 R >>\nstartxref\n{d}\n%%EOF\n", .{ objects.len + 1, xref });
+ try dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() });
+}
+
+test "panes: a PDF's body is the text of the page shown, each line of it a line, never a heading run into the text under it" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const n = try s.lookFile("docs/design.pdf");
+ const body = s.at("/pane/{d}/body", .{n});
+ try std.testing.expect(std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\nArchitecture and implementation of the rewrite\n"));
+ try s.write(s.at("/pane/{d}/look", .{n}), ":2\n");
+ try std.testing.expect(!std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\n"));
+}
+
+test "panes: PdfTint steps a PDF's tint, read in its ctl and never in its tag, and only a PDF pane takes it" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const text = try s.focused();
+ const n = try s.lookFile("docs/design.pdf");
+ const ctl = s.at("/pane/{d}/ctl", .{n});
+ const tag = try s.read(s.at("/pane/{d}/tag", .{n}));
+ try std.testing.expect(std.mem.endsWith(u8, tag, " PdfSections PdfTint PdfFit Collapse Del"));
+ for ([_][]const u8{ "full", "disabled", "filtered" }) |want| {
+ try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), "fit:width ") != null);
+ try s.write(ctl, "PdfTint\n");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null);
+ try s.expect(s.at("/pane/{d}/tag", .{n}), tag);
+ // Another pane's PdfTint is refused and leaves the PDF's alone.
+ try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/ctl", .{text}), "PdfTint\n"));
+ try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null);
+ }
+}
+
+test "panes: PdfSections on a PDF with no outline says so and opens nothing" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try plainPdf(tmp.dir, "plain.pdf");
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const n = try s.lookFile(s.at("{s}/plain.pdf", .{dir}));
+ try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[1]);
+ const index = try s.read("/index");
+ for (0..2) |_| {
+ _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "PdfSections\n");
+ try s.expect("/index", index);
+ }
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "PdfSections: this PDF has no outline") != null);
+}
+
+test "panes: a Mini that cannot read its file leaves the panes, the last Mini's snapshot and the keyboard as they were" {
+ var s = try Session.initFile(std.testing.allocator, "abc\n");
+ defer s.deinit();
+ const text = try s.focused();
+ const exec = s.at("/pane/{d}/exec", .{text});
+ try s.write(exec, s.at("Mini /virtual/pane/{d}/body\n", .{text}));
+ const mini = try s.focused();
+ try std.testing.expect(mini != text);
+ const body = try s.read(s.at("/pane/{d}/body", .{mini}));
+ const index = try s.read("/index");
+ try std.testing.expectEqual(@as(u16, 2), try s.refused(exec, "Mini /virtual/does-not-exist\n"));
+ try std.testing.expectEqual(mini, try s.focused());
+ try s.expect("/index", index);
+ try s.expect(s.at("/pane/{d}/body", .{mini}), body);
+}