summaryrefslogtreecommitdiff
path: root/test/behaviour/event.zig
blob: 65222175ce1fe8919133906c3c7f9bfb8839f2e5 (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
//! docs/typ/reference.typ, event <event>.
const std = @import("std");
const Session = @import("session").Session;

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

test "event: a pane's own look and exec go to the event reader holding it, which writes them back to act" {
    var s = try Session.initFile(std.testing.allocator, "Msg routed\n");
    defer s.deinit();
    const n = try s.focused();
    const ev = try s.hold(s.at("/pane/{d}/event", .{n}), 2);
    try s.write(s.at("/pane/{d}/exec", .{n}), "Msg routed\n");
    // Not done: the reader has it, as it would a middle click.
    try std.testing.expect(!has(try s.read("/log"), "routed"));
    const record = try s.readFid(ev);
    // Flag 1: its first word is a builtin; no 2, no expansion follows.
    try std.testing.expectEqualStrings("FX0 0 1 10 Msg routed\n", record);
    // Written back, it is done.
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ev, record));
    try std.testing.expect(has(try s.read("/log"), s.at("msg {d} routed\n", .{n})));
    try s.write(s.at("/pane/{d}/look", .{n}), "one.txt:3\n");
    try std.testing.expectEqualStrings("FL0 0 4 9 one.txt:3\n", try s.readFid(ev));
    // The root's is a click at the pane with the keyboard, this one.
    try s.write("/exec", "Msg rooted\n");
    try std.testing.expect(!has(try s.read("/log"), "rooted"));
    try std.testing.expectEqualStrings("FX0 0 1 10 Msg rooted\n", try s.readFid(ev));
    // A click in its tag is the reader's too, an `x` record, as acme's is;
    // written back, it is done.
    try s.write(s.at("/pane/{d}/tagexec", .{n}), "Msg tagged\n");
    try std.testing.expect(!has(try s.read("/log"), "tagged"));
    const tagged = try s.readFid(ev);
    try std.testing.expectEqualStrings("Fx0 0 1 10 Msg tagged\n", tagged);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ev, tagged));
    try std.testing.expect(has(try s.read("/log"), s.at("msg {d} tagged\n", .{n})));
    s.clunk(ev);
    // With no reader, the root's and the pane's act at once.
    try s.write(s.at("/pane/{d}/exec", .{n}), "Msg direct\n");
    try std.testing.expect(has(try s.read("/log"), s.at("msg {d} direct\n", .{n})));
}

test "event: a refused Del written back through event fails that write, EIO, with its err" {
    var s = try Session.initFile(std.testing.allocator, "text\n");
    defer s.deinit();
    const n = try s.focused();
    try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); // unsaved
    const ev = try s.hold(s.at("/pane/{d}/event", .{n}), 2);
    defer s.clunk(ev);
    try s.write(s.at("/pane/{d}/exec", .{n}), "Del\n");
    const record = try s.readFid(ev);
    try std.testing.expectEqual(@as(u16, 5), try s.writeFid(ev, record));
    try std.testing.expect(has(s.said, "Modified"));
    _ = try s.read(s.at("/pane/{d}/ctl", .{n})); // still there
}

test "event: a bad event record says what is wrong with it" {
    var s = try Session.initFile(std.testing.allocator, "one\n");
    defer s.deinit();
    const event = try s.hold(s.at("/pane/{d}/event", .{try s.focused()}), 2);
    defer s.clunk(event);
    for ([_][2][]const u8{
        .{ "MI0 1 0 1 x\n", "only a look or exec" },
        .{ "MX3 1 0 0 \n", "ends before it starts" },
        .{ "MX9 9 0 0 \n", "range past end" },
        .{ "garbage\n", "a record is" },
    }) |c| {
        try std.testing.expectEqual(@as(u16, 22), try s.writeFid(event, c[0]));
        try std.testing.expect(has(s.said, c[1]));
    }
}

test "event: a write to the body is an EI record to the reader holding event" {
    var s = try Session.initFile(std.testing.allocator, "replaced\n");
    defer s.deinit();
    const n = try s.focused();
    const event = try s.hold(s.at("/pane/{d}/event", .{n}), 0);
    defer s.clunk(event);
    try s.write(s.at("/pane/{d}/body", .{n}), "event\n");
    const record = try s.readFid(event);
    try std.testing.expect(std.mem.startsWith(u8, record, "EI") and has(record, "event\n"));
}