summaryrefslogtreecommitdiff
path: root/test/behaviour
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour')
-rw-r--r--test/behaviour/find_grep.zig63
-rw-r--r--test/behaviour/look_and_exec.zig21
2 files changed, 84 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"));
+}
diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig
index 3e6137b1..387c28a7 100644
--- a/test/behaviour/look_and_exec.zig
+++ b/test/behaviour/look_and_exec.zig
@@ -442,6 +442,27 @@ fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 {
return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]});
}
+test "look-and-exec: SPC : runs its line in a command pane at the project's root, the nearest directory at or above the pane's holding a .jj or a .git, where `:` runs it in the pane's own; its head `: root`" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.createDirPath(std.testing.io, ".git");
+ try tmp.dir.createDirPath(std.testing.io, "a/b");
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a/b/x.txt", .data = "x\n" });
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ const root = try tmpPath(&s, &tmp);
+ _ = try s.lookFile(s.at("{s}/a/b/x.txt", .{root}));
+ try s.keys(":pwd\n");
+ const here = try paneNamed(&s, s.at("{s}/a/b", .{root}));
+ try std.testing.expect(std.mem.startsWith(u8, try s.read(s.at("/pane/{d}/body", .{here})), "% pwd\n"));
+ try s.keys(" :");
+ _ = try s.where(": root");
+ try s.keys("pwd\n");
+ const at_root = try paneNamed(&s, root);
+ try std.testing.expect(at_root != here);
+ try std.testing.expect(std.mem.startsWith(u8, try s.read(s.at("/pane/{d}/body", .{at_root})), "% pwd\n"));
+}
+
test "look-and-exec: a look opens spaced paths and locations without editing the source pane" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();