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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
//! docs/typ/reference.typ, Panes <panes>.
const std = @import("std");
const Session = @import("session").Session;
test "panes: a collapsed pane's ctl says so" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const first = try s.focused();
_ = try s.newPane();
try s.write(s.at("/pane/{d}/ctl", .{first}), "Collapse\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " collapsed ") != null);
}
test "panes: a pane's ctl read ends with its mode, as its tag's box shows it" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const ctl = s.at("/pane/{d}/ctl", .{try s.focused()});
try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:normal "));
try s.keys("i");
try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:insert "));
try s.keys("\x1bv");
try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:select "));
}
test "panes: the ctl word name refuses a leading blank, as the name file does" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const ctl = s.at("/pane/{d}/ctl", .{try s.focused()});
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "name /tmp/lead.txt\n"));
try std.testing.expectEqualStrings("bad character in file name: a blank at its start", s.said);
try s.write(ctl, "name /tmp/ok.txt\n");
try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), "/tmp/ok.txt\n");
}
test "panes: Putall saves every pane with unsaved edits to a file, one each, and leaves the rest" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "clean.txt", .data = "clean\n" });
var dir_buf: [4096]u8 = undefined;
const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const first = try s.focused();
try s.write(s.at("/pane/{d}/name", .{first}), "/tmp/pardes-putall-a.txt\n");
try s.write(s.at("/pane/{d}/body", .{first}), "edited\n");
// A file opened from disk, unedited.
try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/clean.txt\n", .{dir}));
try std.testing.expectEqual(@as(usize, 0), s.saved);
try s.write("/ctl", "Putall\n");
try std.testing.expectEqual(@as(usize, 1), s.saved);
}
fn has(hay: []const u8, needle: []const u8) bool {
return std.mem.indexOf(u8, hay, needle) != null;
}
test "panes: focus reads the pane with the keyboard, and a serial written to it moves the keyboard there, and only that" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const first = try s.focused();
const second = try s.newPane();
try s.expect("/focus", s.at("{d}\n", .{second}));
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " notcurrent "));
// A column's tag has the keyboard: no pane does, and a write takes it back.
try s.write("/ctl", "Newcol\n");
try s.expect("/focus", "");
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent "));
try s.write("/focus", s.at("{d}\n", .{first}));
try s.expect("/focus", s.at("{d}\n", .{first}));
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " current "));
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent "));
try std.testing.expectEqual(@as(u16, 2), try s.refused("/focus", "99999\n"));
try std.testing.expectEqualStrings("no such pane", s.said);
for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try std.testing.expectEqual(@as(u16, 22), try s.refused("/focus", bad));
try s.expect("/focus", s.at("{d}\n", .{first}));
// Focus gives the keyboard only: a folded pane stays folded.
try s.write(s.at("/pane/{d}/ctl", .{second}), "Collapse\n");
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed "));
try s.write("/focus", s.at("{d}", .{second}));
try s.expect("/focus", s.at("{d}\n", .{second}));
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed "));
}
test "panes: the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" {
var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
defer s.deinit();
const n = try s.focused();
const ctl = s.at("/pane/{d}/ctl", .{n});
// A builtin is its own capitalised word: `GET` and `DEL` are none.
for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| {
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, s.at("{s}\n", .{bad})));
try std.testing.expectEqualStrings(s.at("unknown control message \"{s}\"", .{std.mem.sliceTo(bad, ' ')}), s.said);
}
_ = try s.refused(ctl, "Newcol\n");
try std.testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", s.said);
_ = try s.refused(ctl, "Verbose off\n");
try std.testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", s.said);
_ = try s.refused(ctl, "Collapse now\n");
try std.testing.expectEqualStrings("wrong #args in control message \"Collapse\"", s.said);
// Checked whole first: a bad line anywhere and nothing ran.
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nbogus\n"));
// A pane's builtin runs at that pane.
try s.write(ctl, "Msg from ctl\n");
try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from ctl\n", .{n})));
// One that means nothing bare is refused before anything runs.
_ = try s.refused(ctl, "Find\n");
try std.testing.expectEqualStrings("wrong #args in control message \"Find\"", s.said);
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nMsg\n"));
try std.testing.expect(!has(try s.read("/log"), "first"));
// Save with no name on a scratch would ask for one: refused, not asked.
const other = try s.newPane();
_ = try s.refused(s.at("/pane/{d}/ctl", .{other}), "Save\n");
try std.testing.expectEqualStrings("control message needs its argument \"Save\"", s.said);
// A line after the one that closed the pane has nowhere to run.
try std.testing.expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/ctl", .{other}), "Del\nMsg after\n"));
try std.testing.expectError(error.Refused, s.read(s.at("/pane/{d}/ctl", .{other})));
}
test "panes: a second lock is refused until the holder unlocks or closes, and binds nobody else" {
var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
defer s.deinit();
const n = try s.focused();
const ctl = s.at("/pane/{d}/ctl", .{n});
const a = try s.hold(ctl, 2);
const b = try s.hold(ctl, 1);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n"));
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n"));
// Another open's lock is refused at once, and so is its unlock; writes
// to the pane's other files are not.
try std.testing.expectEqual(@as(u16, 16), try s.writeFid(b, "lock\n"));
try std.testing.expectEqualStrings("file in use: another open of this ctl holds the lock", s.said);
try std.testing.expectEqual(@as(u16, 22), try s.writeFid(b, "unlock\n"));
try std.testing.expectEqualStrings("pane not locked by this open", s.said);
try s.write(s.at("/pane/{d}/addr", .{n}), "1");
try s.write(s.at("/pane/{d}/data", .{n}), "ONE\n");
try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n");
// Once the holder unlocks, the other's try takes it.
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "unlock\n"));
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(b, "lock\n"));
try std.testing.expectEqual(@as(u16, 16), try s.writeFid(a, "lock\nunlock\n"));
// Closing the holder gives it up.
s.clunk(b);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\nunlock\nlock\n"));
try std.testing.expectEqual(@as(u16, 22), try s.writeFid(a, "unlock\nunlock\n"));
// Still a's: another open is refused it.
const c = try s.hold(ctl, 1);
try std.testing.expectEqual(@as(u16, 16), try s.writeFid(c, "lock\n"));
s.clunk(c);
// The lock lives and dies with the pane.
const other = try s.newPane();
const d = try s.hold(s.at("/pane/{d}/ctl", .{other}), 1);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, "lock\n"));
try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n");
try std.testing.expectEqual(@as(u16, 2), try s.writeFid(d, "lock\n"));
s.clunk(d);
s.clunk(a);
// a's close gave its lock up too.
const e = try s.hold(ctl, 1);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(e, "lock\n"));
s.clunk(e);
}
|