summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/behaviour/addons.zig146
-rw-r--r--test/behaviour/addons_pdf.zig28
-rw-r--r--test/session.zig8
3 files changed, 182 insertions, 0 deletions
diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig
index ce3c7b02..25b1e523 100644
--- a/test/behaviour/addons.zig
+++ b/test/behaviour/addons.zig
@@ -371,3 +371,149 @@ test "addons: an image pane whose file changes on disk shows the new picture, un
try std.testing.expectEqual(n, try s.focused());
try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,3,1]"));
}
+
+/// A tmp directory's real path, kept in the session's arena.
+fn dirOf(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 {
+ var buf: [4096]u8 = undefined;
+ return s.arena.allocator().dupe(u8, buf[0..try tmp.dir.realPath(std.testing.io, &buf)]);
+}
+
+test "addons: a picker shows a file of an addon's kind through its kind in the pane it lands in, Esc puts that pane back as it was, and Enter lands as a Look does" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seven.fake", .data = "1234567" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ try load(&s, fake.ok);
+ defer unload(&s, "fake");
+ const dir = try dirOf(&s, &tmp);
+ const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
+ const body = s.at("/pane/{d}/body", .{pane});
+ const a = s.arena.allocator();
+ const screen = try a.dupe(u8, try s.screen());
+ const index = try a.dupe(u8, try s.read("/index"));
+ // Its kind's paint in the reading pane's body (the fake counts the
+ // file's 7 bytes), not the pane's own text, nor the file's bytes.
+ try s.keys(" ffseven");
+ s.tick();
+ const shown = try s.screen();
+ try std.testing.expect(has(shown, "count 7"));
+ try std.testing.expect(!has(shown, "the notes") and !has(shown, "1234567"));
+ // Esc: the pane exactly as it was, and no pane more or less.
+ try s.keys("\x1b");
+ s.tick();
+ // (The notice row says the Find ran: the session's, not the pane's.)
+ const after = try s.screen();
+ try std.testing.expectEqualStrings(screen[0..std.mem.lastIndexOfScalar(u8, screen, '\n').?], after[0..std.mem.lastIndexOfScalar(u8, after, '\n').?]);
+ try s.expect(body, "the notes\n");
+ try std.testing.expectEqualStrings(index, try s.read("/index"));
+ // Enter: the kind's pane where a Look puts it; the reading pane keeps
+ // its text.
+ try s.keys(" ffseven\n");
+ s.tick();
+ const opened = try paneOf(&s, "fake");
+ try std.testing.expect(opened != pane);
+ try s.expect(body, "the notes\n");
+ try std.testing.expect(has(try s.screen(), "count 7"));
+}
+
+test "addons: a picker's row of a file no kind takes and not text shows one line naming it, and the row before's file never stays in the body" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "other text\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seven.fake", .data = "1234567" });
+ var blob: [3000]u8 = undefined;
+ for (&blob, 0..) |*b, i| b.* = @truncate(i *% 7);
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "blob.bin", .data = &blob });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ try load(&s, fake.ok);
+ defer unload(&s, "fake");
+ const dir = try dirOf(&s, &tmp);
+ const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
+ try s.keys(" ffother");
+ s.tick();
+ try std.testing.expect(has(try s.screen(), "other text"));
+ // Onto the binary: its one line, never its bytes, nor other.txt.
+ try s.keys("\x08\x08\x08\x08\x08blob");
+ s.tick();
+ var shown = try s.screen();
+ try std.testing.expect(has(shown, "blob.bin · binary · 3.0 kB"));
+ try std.testing.expect(!has(shown, "other text"));
+ // Onto a kind's file: its kind's paint, not the binary's line.
+ try s.keys("\x08\x08\x08\x08seven");
+ s.tick();
+ shown = try s.screen();
+ try std.testing.expect(has(shown, "count 7"));
+ try std.testing.expect(!has(shown, "blob.bin · binary"));
+ // Back onto text, then Esc: the pane's own.
+ try s.keys("\x08\x08\x08\x08\x08other");
+ s.tick();
+ shown = try s.screen();
+ try std.testing.expect(has(shown, "other text") and !has(shown, "count 7"));
+ try s.keys("\x1b");
+ s.tick();
+ try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n");
+ try std.testing.expect(has(try s.screen(), "the notes"));
+}
+
+test "addons: moving the cursor over pictures in a picker draws only the one it rests on, and a draw of one passed by is dropped whenever it comes back" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ const png = "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef\x00\x00\x00\x00IEND\xaeB`\x82";
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic1.png", .data = png });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic2.png", .data = png });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic3.ppm", .data = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ s.graphics();
+ try load(&s, Session.image_addon);
+ defer unload(&s, "image");
+ const dir = try dirOf(&s, &tmp);
+ const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
+ s.holdJobs();
+ try s.keys(" ffpic");
+ s.tick();
+ // Until its picture is drawn, one line names it.
+ const first = try s.screen();
+ try std.testing.expect(has(first, " · image · "));
+ try std.testing.expectEqual(1, s.held.items.len);
+ // Two rows down with no frame between: only the row it rests on draws.
+ try s.keys("\u{F800}\u{F800}");
+ s.tick();
+ _ = try s.screen();
+ try std.testing.expectEqual(2, s.held.items.len);
+ const rested = if (has(try s.screen(), "▶ pic3.ppm")) "\"src\":[0,0,2,1]" else "\"src\":[0,0,1,1]";
+ const passed = if (std.mem.eql(u8, rested, "\"src\":[0,0,2,1]")) "\"src\":[0,0,1,1]" else "\"src\":[0,0,2,1]";
+ // The rested one's answer first, the passed one's after: dropped.
+ std.mem.reverse(@TypeOf(s.held.items[0]), s.held.items);
+ s.releaseJobs();
+ const screen = try s.read("/screen");
+ try std.testing.expect(has(screen, s.at("\"serial\":{d},", .{pane})) and has(screen, rested));
+ try std.testing.expect(!has(screen, passed));
+}
+
+test "addons: Enter on a picture in a picker opens its kind's pane, never its bytes as text in the reading pane (the user's report)" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ s.graphics();
+ try load(&s, Session.image_addon);
+ defer unload(&s, "image");
+ const dir = try dirOf(&s, &tmp);
+ const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
+ try s.keys(" ffpic\n");
+ s.tick();
+ try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n");
+ const image = try paneOf(&s, "image");
+ try std.testing.expect(image != pane);
+ const screen = try s.read("/screen");
+ try std.testing.expect(has(screen, s.at("\"serial\":{d},", .{image})) and has(screen, "\"src\":[0,0,2,1]"));
+ try std.testing.expect(!has(try s.screen(), "1 P6"));
+}
diff --git a/test/behaviour/addons_pdf.zig b/test/behaviour/addons_pdf.zig
index 0c7982d1..d773f4e2 100644
--- a/test/behaviour/addons_pdf.zig
+++ b/test/behaviour/addons_pdf.zig
@@ -299,3 +299,31 @@ test "addons: a PDF whose file changes on disk while a page of it renders: the p
try s.keys("0");
try std.testing.expect((try shown(&s, n)).len >= 1);
}
+
+test "addons: a PDF under a picker's cursor shows through the pdf addon in the pane it lands in, never its bytes as text, and Esc puts the pane's own text back" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ s.graphics();
+ try s.loadPdf();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ const pdf = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "docs/design.pdf", s.arena.allocator(), .limited(4 << 20));
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "paper.pdf", .data = pdf });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
+ try s.keys(" ffpaper");
+ // (Its kind told its view at the frame after the one that opened it.)
+ s.tick();
+ s.tick();
+ const screen = try s.screen();
+ try std.testing.expect(!has(screen, "%PDF") and !has(screen, "the notes"));
+ // Its pages pictures in that pane's body, as a PDF's pane shows them.
+ try std.testing.expect((try shown(&s, pane)).len > 0);
+ try s.keys("\x1b");
+ s.tick();
+ try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n");
+ try std.testing.expectEqual(0, (try shown(&s, pane)).len);
+ try std.testing.expect(has(try s.screen(), "the notes"));
+}
diff --git a/test/session.zig b/test/session.zig
index d437ecf2..13798753 100644
--- a/test/session.zig
+++ b/test/session.zig
@@ -316,6 +316,14 @@ pub const Session = struct {
return true;
}
+ /// A frame of the host's loop (its tick): what waits for the frame
+ /// after the keys that asked it is done (a picker's preview of the row
+ /// its cursor rests on).
+ pub fn tick(s: *Session) void {
+ pardesOf(s).update(.tick);
+ s.drain();
+ }
+
/// Kinds' jobs and draws held from now on (`hold_jobs`), until
/// `releaseJobs`.
pub fn holdJobs(s: *Session) void {