summaryrefslogtreecommitdiff
path: root/test/behaviour/find_grep.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour/find_grep.zig')
-rw-r--r--test/behaviour/find_grep.zig63
1 files changed, 63 insertions, 0 deletions
diff --git a/test/behaviour/find_grep.zig b/test/behaviour/find_grep.zig
new file mode 100644
index 00000000..49bf64aa
--- /dev/null
+++ b/test/behaviour/find_grep.zig
@@ -0,0 +1,63 @@
+//! docs/typ/reference.typ, Find and Grep <find-grep>.
+const std = @import("std");
+const Session = @import("session").Session;
+
+fn has(haystack: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, haystack, needle) != null;
+}
+
+/// A project in `tmp`: a `.jj` at its top, `top.txt` there and `x.txt`
+/// two directories below, in `a/b`, each holding `needle`; its path.
+fn project(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 {
+ try tmp.dir.createDirPath(std.testing.io, ".jj");
+ try tmp.dir.createDirPath(std.testing.io, "a/b");
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "top.txt", .data = "needle at the top\n" });
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a/b/x.txt", .data = "needle below\n" });
+ var buf: [4096]u8 = undefined;
+ return s.arena.allocator().dupe(u8, buf[0..try tmp.dir.realPath(std.testing.io, &buf)]);
+}
+
+test "find-grep: SPC f f lists the files of the pane's directory, SPC f F those of the project's root above it, its head `Find root`" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const root = try project(&s, &tmp);
+ _ = try s.lookFile(s.at("{s}/a/b/x.txt", .{root}));
+ try s.keys(" ff");
+ s.tick();
+ _ = try s.where("▶ x.txt");
+ try std.testing.expect(!has(try s.screen(), "top.txt"));
+ try std.testing.expect(!has(try s.screen(), "Find root"));
+ try s.keys("\x1b");
+ // From the root: its files by their paths from it, `.jj` passed over.
+ try s.keys(" fF");
+ s.tick();
+ _ = try s.where("␣ f Find root");
+ _ = try s.where(" a/b/x.txt");
+ _ = try s.where(" top.txt");
+ try std.testing.expect(!has(try s.screen(), ".jj"));
+}
+
+test "find-grep: SPC f G greps from the project's root above the pane's directory, its head `Grep root`, a leading directory narrowing it from the root" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const root = try project(&s, &tmp);
+ _ = try s.lookFile(s.at("{s}/a/b/x.txt", .{root}));
+ try s.keys(" fGneedle");
+ s.advance(100 * std.time.ns_per_ms);
+ s.tick();
+ _ = try s.where("␣ f Grep root needle");
+ // Each row its place from the root, the places' column padded.
+ _ = try s.where("a/b/x.txt:1 needle below");
+ _ = try s.where("top.txt:1 needle at the top");
+ try s.keys("\x1b");
+ // `a needle`: the root's a/, which the pane's directory has not.
+ try s.keys(" fGa needle");
+ s.advance(100 * std.time.ns_per_ms);
+ s.tick();
+ _ = try s.where("a/b/x.txt:1 needle below");
+ try std.testing.expect(!has(try s.screen(), "top.txt:1"));
+}