summaryrefslogtreecommitdiff
path: root/test/behaviour/look_and_exec.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour/look_and_exec.zig')
-rw-r--r--test/behaviour/look_and_exec.zig230
1 files changed, 230 insertions, 0 deletions
diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig
index 3c76988e..98cab342 100644
--- a/test/behaviour/look_and_exec.zig
+++ b/test/behaviour/look_and_exec.zig
@@ -37,6 +37,133 @@ test "look-and-exec: a bare :N or :N:M look addresses the pane itself, from its
try expectEqual(.{ @as(u32, 0), @as(u32, 6) }, try s.range(dot));
}
+fn has(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+fn lines(text: []const u8) usize {
+ return std.mem.count(u8, text, "\n");
+}
+
+/// Whether /index lists the pane `serial`.
+fn listed(s: *Session, serial: u32) !bool {
+ var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
+ while (it.next()) |line| {
+ var words = std.mem.tokenizeScalar(u8, line, ' ');
+ if (std.mem.eql(u8, words.next() orelse "", s.at("{d}", .{serial}))) return true;
+ }
+ return false;
+}
+
+test "look-and-exec: a look takes acme's :addr and file:addr, and a miss changes nothing" {
+ var s = try Session.initFile(std.testing.allocator, "foo x foo y\nbar\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const look = s.at("/pane/{d}/look", .{n});
+ const dot = s.at("/pane/{d}/dot", .{n});
+ const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
+ // :addr is an address in this pane, evaluated from its dot.
+ try s.write(look, ":/y/\n");
+ try expectEqual(.{ @as(u32, 10), @as(u32, 11) }, try s.range(dot));
+ try s.write(look, ":#4\n");
+ try expectEqual(.{ @as(u32, 4), @as(u32, 4) }, try s.range(dot));
+ try s.write(look, ":/foo/\n"); // from #4: the second foo
+ try expectEqual(.{ @as(u32, 6), @as(u32, 9) }, try s.range(dot));
+ // file:addr names the pane's own file here.
+ try s.write(look, s.at("{s}:/bar/\n", .{path}));
+ try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot));
+ // A miss: no pane made, the selection kept, and it says so.
+ const panes = lines(try s.read("/index"));
+ try s.write(look, "zzqq\n");
+ try expectEqual(panes, lines(try s.read("/index")));
+ try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot));
+ try std.testing.expect(has(try s.read("/log"), "no match for \"zzqq\""));
+ // A hit answers the pane the text is selected in, not a hits pane.
+ try s.write(look, "foo\n");
+ try s.expect(look, s.at("{d}\n", .{n}));
+ // A pattern with a blank in it; 0/re/ searches from the top, whatever dot is.
+ try s.write(look, ":/foo y/\n");
+ try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot));
+ try s.write(look, s.at("{s}:0/foo/\n", .{path}));
+ try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot));
+ // file:N selects the line; a line past the end moves nothing and reads back empty.
+ try s.write(look, s.at("{s}:2\n", .{path}));
+ try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot));
+ try s.write(look, s.at("{s}:99\n", .{path}));
+ try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot));
+ try s.expect(look, "");
+ try std.testing.expect(has(try s.read("/log"), s.at("look: {s}:99 has no line 99\n", .{path})));
+}
+
+test "look-and-exec: the root look's file:/re/ goes on from the dot, and a miss leaves the dot as it was" {
+ var s = try Session.initFile(std.testing.allocator, "foo x foo y\nfoo\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const dot = s.at("/pane/{d}/dot", .{n});
+ const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
+ for ([_]u32{ 0, 6, 12, 0 }) |at| {
+ try s.write("/look", s.at("{s}:/foo/\n", .{path}));
+ try expectEqual(.{ at, at + 3 }, try s.range(dot));
+ }
+ try s.write("/look", s.at("{s}:/zzqq/\n", .{path}));
+ try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot));
+}
+
+test "look-and-exec: exec runs a builtin at the pane, and look and exec read back the pane it acted on" {
+ var s = try Session.initFile(std.testing.allocator, "Msg fs-ran\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const own = s.at("{d}\n", .{n});
+ try s.write(s.at("/pane/{d}/exec", .{n}), "Msg from exec\n");
+ try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from exec\n", .{n})));
+ try s.expect("/exec", own);
+ try std.testing.expectEqual(@as(u64, own.len), (try s.stat("/exec")).size);
+ try s.expect(s.at("/pane/{d}/look", .{n}), own);
+ try s.write(s.at("/pane/{d}/exec", .{n}), "New\n");
+ const made = try s.focused();
+ try std.testing.expect(made != n);
+ try s.expect("/exec", s.at("{d}\n", .{made}));
+ try s.write(s.at("/pane/{d}/exec", .{made}), "Del\n");
+ try std.testing.expect(try listed(&s, n) and !try listed(&s, made));
+ try s.expect("/exec", s.at("{d}\n", .{made}));
+}
+
+test "look-and-exec: look and exec read back what the last command touched, a ctl write's too" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const n = try s.focused();
+ try s.write("/exec", "Msg first\n");
+ try s.expect("/exec", s.at("{d}\n", .{n}));
+ // A pane made through ctl is what both read next, not the Msg's pane.
+ try s.write(s.at("/pane/{d}/ctl", .{n}), "Tty\n");
+ const made = try s.focused();
+ try std.testing.expect(made != n);
+ try s.expect("/exec", s.at("{d}\n", .{made}));
+ try s.expect("/look", s.at("{d}\n", .{made}));
+ // A ctl write that makes nothing names the pane it ran at.
+ try s.write("/ctl", "Verbose off\n");
+ try s.expect("/exec", s.at("{d}\n", .{made}));
+ // Closed, it is no pane to go to: a fresh read of look is empty.
+ try s.write(s.at("/pane/{d}/ctl", .{made}), "Del\n");
+ try std.testing.expect(!try listed(&s, made));
+ try s.expect("/look", "");
+}
+
+test "look-and-exec: a line starting with # runs as nothing at an exec or a ctl, silently" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const n = try s.focused();
+ // The setup's own announcements are made by the first write.
+ try s.write("/ctl", "# settle\n");
+ const index = try s.read("/index");
+ const log = try s.read("/log");
+ const col = std.mem.sliceTo(try s.read("/layout"), ' ');
+ for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{n}), "/ctl", s.at("/pane/{d}/ctl", .{n}), s.at("/col/{s}/ctl", .{col}) }) |path|
+ try s.write(path, "# Platform: tty\n # indented too\n");
+ try s.expect("/index", index);
+ try s.expect("/log", log);
+}
+
test "look-and-exec: a look replaces the selection it was made from, for an address, a range and text found" {
var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\nneedle\n");
defer s.deinit();
@@ -93,3 +220,106 @@ test "look-and-exec: a look into another pane leaves the source's dot and replac
try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dest_dot));
}
}
+
+test "look-and-exec: a look that finds nothing says so" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const n = try s.newPane();
+ try s.write(s.at("/pane/{d}/look", .{n}), "/no/such/zzqq\n");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("err {d} look: no match for \"/no/such/zzqq\"\n", .{n})) != null);
+}
+
+/// The log's `err` records, counted.
+fn errs(s: *Session) !usize {
+ return std.mem.count(u8, try s.read("/log"), "\nerr ");
+}
+
+/// The serial of the pane /index names with `suffix`.
+fn paneNamed(s: *Session, suffix: []const u8) !u32 {
+ var rows = std.mem.splitScalar(u8, try s.read("/index"), '\n');
+ while (rows.next()) |line| {
+ // serial kind dirty name column
+ const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue];
+ if (std.mem.endsWith(u8, name, suffix)) return std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10);
+ }
+ return error.NoSuchPane;
+}
+
+test "look-and-exec: `:N` on a PDF turns to page N, from the pane and from the root's look, and `@pN:N` logs no err" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const n = try s.lookFile("docs/design.pdf");
+ const look = s.at("/pane/{d}/look", .{n});
+ const pages = (try s.pdfPage(n))[1];
+ try std.testing.expect(pages >= 3);
+ try s.write(look, ":2\n");
+ try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
+ try s.write(look, s.at("@p{d}:1\n", .{n}));
+ try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
+ try s.write("/look", s.at("@p{d}:2\n", .{n}));
+ try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
+ try expectEqual(@as(usize, 0), try errs(&s));
+ // `:N:H` is hit H of its search on page N, as a search row's
+ // `file.pdf:N:H` is: with no search there is no hit 3, a miss that turns
+ // no page.
+ try s.write(look, ":1\n");
+ _ = try s.refused(look, ":2:3\n");
+ try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no search hit 3 on page 2") != null);
+ try s.write(look, ":2\n");
+ _ = try s.refused(look, ":0\n");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 0") != null);
+ // A byte offset or a pattern is no PDF address: refused with words.
+ _ = try s.refused(look, ":#3\n");
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "its address is a page, :N") != null);
+ const before = try errs(&s);
+ _ = try s.refused(look, s.at(":{d}\n", .{pages + 1}));
+ try expectEqual(before + 1, try errs(&s));
+ try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
+}
+
+test "look-and-exec: a look at a PDF's page past its last is a miss, never the last page, and opens nothing" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ try std.Io.Dir.cwd().copyFile("docs/design.pdf", tmp.dir, "other.pdf", std.testing.io, .{});
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const n = try s.lookFile("docs/design.pdf");
+ const look = s.at("/pane/{d}/look", .{n});
+ const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
+ // Open: it stays on its page.
+ try s.write(look, ":2\n");
+ _ = try s.refused(look, s.at("{s}:999\n", .{name}));
+ try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 999") != null);
+ // Not open: nothing opens.
+ const index = try s.read("/index");
+ _ = try s.refused(look, s.at("{s}/other.pdf:999\n", .{dir}));
+ try s.expect("/index", index);
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "other.pdf has no page 999") != null);
+}
+
+test "look-and-exec: a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const n = try s.lookFile("docs/design.pdf");
+ try s.write(s.at("/pane/{d}/exec", .{n}), "PdfSections\n");
+ const out = try paneNamed(&s, "/+PdfSections");
+ const body = try s.read(s.at("/pane/{d}/body", .{out}));
+ const row = body[0..std.mem.indexOfScalar(u8, body, '\n').?];
+ const place = row[0..std.mem.indexOfScalar(u8, row, ' ').?]; // design.pdf:P:S
+ var fields = std.mem.splitScalar(u8, place, ':');
+ _ = fields.next();
+ const page = try std.fmt.parseInt(u32, fields.next().?, 10);
+ const look = s.at("/pane/{d}/look", .{out});
+ // As listed: found, no miss.
+ try s.write(look, s.at("{s}\n", .{place}));
+ try expectEqual(@as(usize, 0), try errs(&s));
+ // The section on another page than the row says, and one not there.
+ _ = try s.refused(look, s.at("design.pdf:{d}:1\n", .{page + 1}));
+ try expectEqual(@as(usize, 1), try errs(&s));
+ _ = try s.refused(look, "design.pdf:1:999\n");
+ try expectEqual(@as(usize, 2), try errs(&s));
+}