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