summaryrefslogtreecommitdiff
path: root/test/behaviour/find_grep.zig
blob: 49bf64aa22e96ad82d74d30ee19bfe1efdb63784 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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"));
}