summaryrefslogtreecommitdiff
path: root/test/behaviour/look_and_exec.zig
blob: f9b49ae04c80405e4b656ff2bc4b656f61027754 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! docs/typ/reference.typ, look and exec <look-and-exec>.
const std = @import("std");
const Session = @import("session").Session;
const expectEqual = std.testing.expectEqual;

test "look-and-exec: a 9P look keeps its leading blanks, a blank-only look included, and exec still trims" {
    var s = try Session.initFile(std.testing.allocator, "a indented\nb\n    indented\n    \n");
    defer s.deinit();
    const n = try s.focused();
    const dot = s.at("/pane/{d}/dot", .{n});
    try s.write(dot, "0 0");
    // The indented line, not the `indented` after `a `.
    try s.write(s.at("/pane/{d}/look", .{n}), "    indented\n");
    try expectEqual(.{ @as(u32, 13), @as(u32, 25) }, try s.range(dot));
    // Blanks alone are text to find too.
    try s.write(dot, "26 26");
    try s.write(s.at("/pane/{d}/look", .{n}), "    \n");
    try expectEqual(.{ @as(u32, 26), @as(u32, 30) }, try s.range(dot));
    // exec's line is still trimmed: `  Msg hi` runs Msg.
    try s.write(s.at("/pane/{d}/exec", .{n}), "  Msg blanks trimmed\n");
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "blanks trimmed") != null);
}

test "look-and-exec: a bare :N or :N:M look addresses the pane itself, from its look, the root's and event write-back" {
    var s = try Session.initFile(std.testing.allocator, "alpha\nbeta\ngamma\n");
    defer s.deinit();
    const n = try s.focused();
    const dot = s.at("/pane/{d}/dot", .{n});
    try s.write(s.at("/pane/{d}/look", .{n}), ":2\n");
    try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot));
    try s.write("/look", ":3\n");
    try expectEqual(.{ @as(u32, 11), @as(u32, 17) }, try s.range(dot));
    try s.write(s.at("/pane/{d}/look", .{n}), ":3:2\n");
    try expectEqual(@as(u32, 12), (try s.range(dot))[0]);
    // Written back by an event reader, the same.
    try s.write(s.at("/pane/{d}/event", .{n}), "ML0 0 0 2 :1\n");
    try expectEqual(.{ @as(u32, 0), @as(u32, 6) }, try s.range(dot));
}

fn has(hay: []const u8, needle: []const u8) bool {
    return std.mem.indexOf(u8, hay, needle) != null;
}

fn lines(text: []const u8) usize {
    return std.mem.count(u8, text, "\n");
}

/// Whether /index lists the pane `serial`.
fn listed(s: *Session, serial: u32) !bool {
    var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
    while (it.next()) |line| {
        var words = std.mem.tokenizeScalar(u8, line, ' ');
        if (std.mem.eql(u8, words.next() orelse "", s.at("{d}", .{serial}))) return true;
    }
    return false;
}

test "look-and-exec: a look takes acme's :addr and file:addr, and a miss changes nothing" {
    var s = try Session.initFile(std.testing.allocator, "foo x foo y\nbar\n");
    defer s.deinit();
    const n = try s.focused();
    const look = s.at("/pane/{d}/look", .{n});
    const dot = s.at("/pane/{d}/dot", .{n});
    const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
    // :addr is an address in this pane, evaluated from its dot.
    try s.write(look, ":/y/\n");
    try expectEqual(.{ @as(u32, 10), @as(u32, 11) }, try s.range(dot));
    try s.write(look, ":#4\n");
    try expectEqual(.{ @as(u32, 4), @as(u32, 4) }, try s.range(dot));
    try s.write(look, ":/foo/\n"); // from #4: the second foo
    try expectEqual(.{ @as(u32, 6), @as(u32, 9) }, try s.range(dot));
    // file:addr names the pane's own file here.
    try s.write(look, s.at("{s}:/bar/\n", .{path}));
    try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot));
    // A miss: no pane made, the selection kept, and it says so.
    const panes = lines(try s.read("/index"));
    try s.write(look, "zzqq\n");
    try expectEqual(panes, lines(try s.read("/index")));
    try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot));
    try std.testing.expect(has(try s.read("/log"), "no match for \"zzqq\""));
    // A hit answers the pane the text is selected in, not a hits pane.
    try s.write(look, "foo\n");
    try s.expect(look, s.at("{d}\n", .{n}));
    // A pattern with a blank in it; 0/re/ searches from the top, whatever dot is.
    try s.write(look, ":/foo y/\n");
    try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot));
    try s.write(look, s.at("{s}:0/foo/\n", .{path}));
    try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot));
    // file:N selects the line; a line past the end moves nothing and reads back empty.
    try s.write(look, s.at("{s}:2\n", .{path}));
    try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot));
    try s.write(look, s.at("{s}:99\n", .{path}));
    try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot));
    try s.expect(look, "");
}

test "look-and-exec: the root look's file:/re/ goes on from the dot, and a miss leaves the dot as it was" {
    var s = try Session.initFile(std.testing.allocator, "foo x foo y\nfoo\n");
    defer s.deinit();
    const n = try s.focused();
    const dot = s.at("/pane/{d}/dot", .{n});
    const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
    for ([_]u32{ 0, 6, 12, 0 }) |at| {
        try s.write("/look", s.at("{s}:/foo/\n", .{path}));
        try expectEqual(.{ at, at + 3 }, try s.range(dot));
    }
    try s.write("/look", s.at("{s}:/zzqq/\n", .{path}));
    try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot));
}

test "look-and-exec: exec runs a builtin at the pane, and look and exec read back the pane it acted on" {
    var s = try Session.initFile(std.testing.allocator, "Msg fs-ran\n");
    defer s.deinit();
    const n = try s.focused();
    const own = s.at("{d}\n", .{n});
    try s.write(s.at("/pane/{d}/exec", .{n}), "Msg from exec\n");
    try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from exec\n", .{n})));
    try s.expect("/exec", own);
    try s.expect(s.at("/pane/{d}/look", .{n}), own);
    try s.write(s.at("/pane/{d}/exec", .{n}), "New\n");
    const made = try s.focused();
    try std.testing.expect(made != n);
    try s.expect("/exec", s.at("{d}\n", .{made}));
    try s.write(s.at("/pane/{d}/exec", .{made}), "Del\n");
    try std.testing.expect(try listed(&s, n) and !try listed(&s, made));
    try s.expect("/exec", s.at("{d}\n", .{made}));
}