diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/behaviour/other_files.zig | 229 | ||||
| -rw-r--r-- | test/behaviour/settings.zig | 42 |
2 files changed, 271 insertions, 0 deletions
diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig index ef1fab7e..2d5cb112 100644 --- a/test/behaviour/other_files.zig +++ b/test/behaviour/other_files.zig @@ -80,3 +80,232 @@ 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}))); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index f3ed57de..df70dbe6 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -132,3 +132,45 @@ test "settings: a setting this frontend cannot show says GUI-only, never run as 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 }, + }); +} |
