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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
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;
}
/// The root ctl's line for `word`.
fn setting(s: *Session, word: []const u8) ![]const u8 {
var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n');
while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line;
return error.NoSuchSetting;
}
test "settings: Pager is pardes by default, off taken, anything else refused with its choices" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
try std.testing.expectEqualStrings("Pager pardes", try setting(&s, "Pager"));
try s.write("/ctl", "Pager off\n");
try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager"));
try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pager less\n"));
try std.testing.expectEqualStrings("bad value in control message; takes pardes, off \"less\"", s.said);
try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager"));
}
test "settings: Shell refuses a path that is no executable, and bare it goes back to the default" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const default = try setting(&s, "Shell");
try std.testing.expect(try s.refused("/ctl", "Shell /nonexistent/zzsh\n") != 0);
try std.testing.expect(has(s.said, "Shell: shell \"/nonexistent/zzsh\" not found"));
try std.testing.expect(has(try s.read("/log"), "shell \"/nonexistent/zzsh\" not found"));
// Its words say a directory, which a mount reads as EISDIR too.
try std.testing.expectEqual(@as(u16, 21), try s.refused("/ctl", "Shell /etc\n"));
try std.testing.expect(has(try s.read("/log"), "Shell: not a shell: /etc is a directory"));
try std.testing.expectEqualStrings(default, try setting(&s, "Shell"));
try s.write("/ctl", "Shell /bin/sh\n");
try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell"));
try s.write("/ctl", "Shell\n");
try std.testing.expect(has(try s.read("/log"), "Shell: the default again, "));
try std.testing.expectEqualStrings(default, try setting(&s, "Shell"));
}
test "settings: Pet is the window's: a terminal's view refuses it, from a pane and the root; a window takes only its pets" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
// Which build, from its options, not from the answer: a terminal's view
// taking Pet must fail here, not pass as a window.
if (comptime @import("pardes_config").platform == .gui) {
try s.write("/ctl", "Pet cat\n");
// A window's: dog is no pet of its, and off is.
try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet cat\n") != null);
_ = try s.refused("/ctl", "Pet dog\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet cat\n") != null);
try s.write("/ctl", "Pet frog\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet frog\n") != null);
try s.write("/ctl", "Pet off\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet off\n") != null);
return;
}
try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pet cat\n"));
try std.testing.expect(std.mem.startsWith(u8, s.said, "Pet is GUI-only, invalid here"));
try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Pet cat\n"));
try std.testing.expectEqualStrings("Pet is GUI-only, invalid here", s.said);
try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "Pet ") == null);
}
fn expectHas(text: []const u8, part: []const u8) !void {
if (std.mem.indexOf(u8, text, part) == null) {
std.debug.print("not in it: {s}\n", .{part});
return error.TestExpectedText;
}
}
test "settings: DumpConfig reports the settings in its one +DumpConfig pane, a second refreshing it" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const text = try s.focused();
const exec = s.at("/pane/{d}/exec", .{text});
try s.write(exec, "DumpConfig\n");
const out = try s.focused();
try std.testing.expect(out != text);
try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+DumpConfig ", .{out})) != null);
const report = try s.read(s.at("/pane/{d}/body", .{out}));
for ([_][]const u8{ "Theme orchard\n", "Last shell spawned: (none)\n", "Shell change pending: on\n", "\nShell ", "LocationsConfig ", "no per-user config path" }) |line| try expectHas(report, line);
try s.write("/ctl", "Theme acme\n");
try s.write("/focus", s.at("{d}", .{text}));
try s.write(exec, "DumpConfig\n");
try std.testing.expectEqual(out, try s.focused());
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/index"), "/+DumpConfig "));
try expectHas(try s.read(s.at("/pane/{d}/body", .{out})), "Theme acme\n");
}
test "settings: Config without a configuration directory says so and opens nothing" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const index = try s.read("/index");
try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Config\n"));
try s.expect("/index", index);
try expectHas(try s.read("/log"), "Config: no startup file here: no configuration directory");
}
test "settings: a bad setting value is quoted, so two different bad values log as two records, never one counted twice" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
_ = try s.refused("/ctl", "Verbose maybe\n");
_ = try s.refused("/ctl", "Verbose nope\n");
const log = try s.read("/log");
try std.testing.expect(has(log, "takes on, off \"maybe\"\n"));
try std.testing.expect(has(log, "takes on, off \"nope\"\n"));
try std.testing.expect(!has(log, "(x2)"));
}
test "settings: a setting this frontend cannot show says GUI-only, never run as a command, and DumpDir reads back" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
if (comptime @import("pardes_config").platform != .gui) {
_ = try s.refused("/ctl", "Lift on\n");
try std.testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", s.said);
_ = try s.refused("/ctl", "GripWidth 120\n");
try std.testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", s.said);
// A builtin the GUI alone has, written to a ctl or an exec, never
// runs as a shell command.
_ = try s.refused("/ctl", "Fonts\n");
try std.testing.expectEqualStrings("Fonts is GUI-only, invalid here \"Fonts\"", s.said);
try std.testing.expectEqual(@as(u16, 22), try s.refused("/exec", "Fonts\n"));
try std.testing.expectEqualStrings("Fonts is GUI-only, invalid here", s.said);
}
_ = try s.refused("/ctl", "Liftt on\n");
try std.testing.expectEqualStrings("unknown control message \"Liftt\"", s.said);
try std.testing.expect(has(try s.read("/ctl"), "\nDumpDir /"));
}
test "settings: Back and Forward written to exec keep to the focused file, and JumpScope all, written to ctl, takes every place in order" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))) });
var buf: [4096]u8 = undefined;
const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
// Each a look, as a user's jumps are: b1 a1 b40 a40 b80 a80.
for ([_][]const u8{ "b.txt:1", "a.txt:1", "b.txt:40", "a.txt:40", "b.txt:80", "a.txt:80" }) |place| try s.write("/look", s.at("{s}/{s}\n", .{ dir, place }));
const Step = struct { []const u8, []const u8, u32 };
const walk = struct {
fn run(q: *Session, steps: []const Step) !void {
for (steps) |st| {
try q.write("/exec", q.at("{s}\n", .{st[0]}));
const n = try q.focused();
try std.testing.expect(std.mem.endsWith(u8, try q.read(q.at("/pane/{d}/name", .{n})), q.at("{s}\n", .{st[1]})));
// Each line is `row\n`: the line the dot is on.
try std.testing.expectEqual(st[2] - 1, (try q.range(q.at("/pane/{d}/dot", .{n})))[0] / 4);
}
}
}.run;
// a's places first, b only once a has none older; Forward mirrors it
// from b and ends at a80, where it began.
try walk(&s, &.{
.{ "Back", "/a.txt", 40 },
.{ "Back", "/a.txt", 1 },
.{ "Back", "/b.txt", 1 },
.{ "Forward", "/b.txt", 40 },
.{ "Forward", "/b.txt", 80 },
.{ "Forward", "/a.txt", 80 },
});
try s.write("/ctl", "JumpScope all\n");
try std.testing.expect(has(try s.read("/ctl"), "JumpScope all\n"));
try walk(&s, &.{
.{ "Back", "/b.txt", 80 },
.{ "Back", "/a.txt", 40 },
.{ "Forward", "/b.txt", 80 },
});
}
const syntax = @import("pardes_config").syntax_highlighting;
/// The style /screen draws the cell `into` cells into the first place
/// `text` is drawn.
fn styleOf(s: *Session, text: []const u8, into: usize) ![]const u8 {
const x, const y = try s.where(text);
const screen = try s.screen();
const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]);
return (try s.styles())[y * cols + x + into];
}
/// How many ` fn ` and ` if ` keywords the screen draws, each of whose two
/// cells must be drawn in `want`.
fn keywordsIn(s: *Session, want: []const u8) !usize {
const screen = try s.screen();
const styles = try s.styles();
const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]);
var seen: usize = 0;
var rows = std.mem.splitScalar(u8, screen, '\n');
var y: usize = 0;
while (rows.next()) |row| : (y += 1) for ([_][]const u8{ " fn ", " if " }) |word| {
var from: usize = 0;
while (std.mem.indexOfPos(u8, row, from, word)) |i| : (from = i + 1) {
const x = try std.unicode.utf8CountCodepoints(row[0..i]) + 1;
for (0..2) |k| try std.testing.expectEqualStrings(want, styles[y * cols + x + k]);
seen += 1;
}
};
return seen;
}
test "settings: Colors off draws a source plain, and on again colours its keywords, the layout as it was, an edit made while off included" {
if (!syntax) return error.SkipZigTest;
for ([_]bool{ false, true }) |edit_while_off| {
var s = try Session.initFile(std.testing.allocator, "pub fn first() void { if (true) {} }\n");
defer s.deinit();
const n = try s.focused();
try s.write(s.at("/pane/{d}/name", .{n}), "/colors.zig\n");
var ident: []const u8 = "first";
if (edit_while_off) {
try std.testing.expect(!std.mem.eql(u8, try styleOf(&s, " fn ", 1), try styleOf(&s, "first", 0)));
try s.write("/ctl", "Colors off\n");
try s.replace(s.at("/pane/{d}/body", .{n}), "pub fn edited() void { if (false) {} }\n");
ident = "edited";
} else try s.write("/ctl", "Colors off\n");
try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, try styleOf(&s, ident, 0)));
const layout = try s.read("/layout");
try s.write("/ctl", "Colors on\n");
try s.expect("/layout", layout);
const kw = try styleOf(&s, " fn ", 1);
try std.testing.expect(!std.mem.eql(u8, kw, try styleOf(&s, ident, 0)));
try std.testing.expect(!has(kw, "\"bold\":true"));
try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, kw));
}
}
test "settings: SyntaxBold keywords stay coloured and bold in every row a pane grows into" {
if (!syntax) return error.SkipZigTest;
var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*))));
defer s.deinit();
try s.write(s.at("/pane/{d}/name", .{try s.focused()}), "/resize.zig\n");
try s.write("/ctl", "SyntaxBold on\n");
s.detach();
try s.write("/ctl", "size 80 8\n");
const kw = try styleOf(&s, " fn ", 1);
try std.testing.expect(has(kw, "\"bold\":true"));
for ([_]usize{ 50, 12, 70 }) |rows| {
try s.write("/ctl", s.at("size 80 {d}\n", .{rows}));
try std.testing.expect(try keywordsIn(&s, kw) >= 2 * (rows - 4));
}
}
test "settings: every on/off setting the root ctl reads flips when its word is written bare, and Theme and Shell take a value" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
var lines = std.mem.tokenizeScalar(u8, try s.read("/ctl"), '\n');
var flipped: usize = 0;
while (lines.next()) |line| {
const space = std.mem.indexOfScalar(u8, line, ' ') orelse continue;
const word, const was = .{ line[0..space], line[space + 1 ..] };
const on = std.mem.eql(u8, was, "on");
if (!on and !std.mem.eql(u8, was, "off")) continue;
try s.write("/ctl", s.at("{s}\n", .{word}));
try std.testing.expectEqualStrings(s.at("{s} {s}", .{ word, if (on) "off" else "on" }), try setting(&s, word));
flipped += 1;
}
try std.testing.expect(flipped > 20);
for ([_][]const u8{ "Colors off", "Wrap off", "Tagbottom on", "Debug on" }) |want|
try std.testing.expectEqualStrings(want, try setting(&s, want[0..std.mem.indexOfScalar(u8, want, ' ').?]));
try s.write("/ctl", "Theme acme\n");
try s.write("/ctl", "Shell /bin/sh\n");
try std.testing.expectEqualStrings("Theme acme", try setting(&s, "Theme"));
try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell"));
}
/// A cell's style (one of `Session.styles`) as its bold flag and ink.
fn look(s: *Session, style: []const u8) !struct { bool, []const u8 } {
const a = s.arena.allocator();
const json = try std.json.parseFromSliceLeaky(std.json.Value, a, style, .{});
return .{ json.object.get("bold").?.bool, try std.json.Stringify.valueAlloc(a, json.object.get("fg").?, .{}) };
}
test "settings: a keyword is coloured apart from the text beside it, and not bold (SyntaxBold is off by default), at a long file's end" {
var s = try Session.initFile(std.testing.allocator, "initial\n");
defer s.deinit();
const n = try s.focused();
try s.write(s.at("/pane/{d}/name", .{n}), "keywords.zig\n");
var source: std.ArrayList(u8) = .empty;
for (0..9999) |_| try source.appendSlice(s.arena.allocator(), "// padding\n");
try source.appendSlice(s.arena.allocator(), "fn check() void {\n if (true) return;\n}\n");
try s.replace(s.at("/pane/{d}/body", .{n}), source.items);
const screen = try s.screen();
const styles = try s.styles();
const cols = std.mem.indexOfScalar(u8, screen, '\n').?;
try std.testing.expectEqual(styles.len, cols * (std.mem.count(u8, screen, "\n") + 1));
for ([_][]const u8{ "fn", "if" }) |word| {
const col, const row = try s.where(s.at("{s} ", .{word}));
const at = row * cols + col;
_, const ink = try look(&s, styles[at]);
for (styles[at..][0..word.len]) |cell| {
const bold, const each = try look(&s, cell);
try std.testing.expect(!bold);
try std.testing.expectEqualStrings(ink, each);
}
_, const plain = try look(&s, styles[at + word.len]);
try std.testing.expect(!std.mem.eql(u8, ink, plain));
}
}
|