summaryrefslogtreecommitdiff
path: root/test/behaviour
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour')
-rw-r--r--test/behaviour/keys.zig135
-rw-r--r--test/behaviour/look_and_exec.zig46
-rw-r--r--test/behaviour/tags.zig189
3 files changed, 370 insertions, 0 deletions
diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig
new file mode 100644
index 00000000..81087297
--- /dev/null
+++ b/test/behaviour/keys.zig
@@ -0,0 +1,135 @@
+//! docs/typ/guide.typ, Keys <keys>.
+const std = @import("std");
+const Session = @import("session").Session;
+
+const alt = "\u{F015}";
+const window = "\x17"; // Ctrl-w
+
+test "keys: a prompt's entry and cancellation leave the tag, the body and its cursor as they were" {
+ var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const tag = s.at("/pane/{d}/tag", .{n});
+ const dot = s.at("/pane/{d}/dot", .{n});
+ try s.replace(tag, " keep");
+ for ([_]struct { keys: []const u8, shown: []const u8 }{
+ .{ .keys = "s", .shown = " /zq" },
+ .{ .keys = "|", .shown = " |zq" },
+ .{ .keys = alt ++ "|", .shown = " |-zq" },
+ .{ .keys = "!", .shown = " !zq" },
+ .{ .keys = alt ++ "!", .shown = " !+zq" },
+ }) |case| {
+ try s.write(dot, "6 6");
+ try s.keys(case.keys);
+ // The answer is typed into the prompt's own line, never the tag.
+ try s.keys("zq");
+ _ = try s.where(case.shown);
+ try std.testing.expectEqualStrings("/test.txt keep", try s.read(tag));
+ try s.keys("\x1b");
+ try std.testing.expectError(error.NotOnScreen, s.where(case.shown));
+ try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal "));
+ try std.testing.expectEqualStrings("/test.txt keep", try s.read(tag));
+ try std.testing.expectEqual(.{ 6, 6 }, try s.range(dot));
+ try s.expect(s.at("/pane/{d}/body", .{n}), "one\ntwo\n");
+ }
+ // Save on a scratch asks for a path, starting from its directory.
+ const scratch = try s.newPane();
+ const scratch_tag = try s.read(s.at("/pane/{d}/tag", .{scratch}));
+ try s.keys(":\tzq");
+ _ = try s.where("/zq");
+ try s.expect(s.at("/pane/{d}/tag", .{scratch}), scratch_tag);
+ try s.keys("\x1b");
+ try std.testing.expectError(error.NotOnScreen, s.where("/zq"));
+ try s.expect(s.at("/pane/{d}/tag", .{scratch}), scratch_tag);
+ try std.testing.expectEqual(@as(usize, 0), s.saved);
+}
+
+test "keys: a prompt opens whatever the tag already holds" {
+ var s = try Session.initFile(std.testing.allocator, "one\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const tag = s.at("/pane/{d}/tag", .{n});
+ try s.replace(tag, &@as([4096]u8, @splat('x')));
+ const full = try s.read(tag);
+ for ([_][]const u8{ "s", "|" }) |key| {
+ try s.keys(key);
+ try s.keys("zq");
+ _ = try s.where("zq");
+ try std.testing.expectEqualStrings(full, try s.read(tag));
+ try s.keys("\x1b");
+ }
+}
+
+test "keys: generated output refuses every pipe prompt before seeding the tag" {
+ var s = try Session.initFile(std.testing.allocator, "one\n");
+ defer s.deinit();
+ try s.write("/ctl", "Help\n");
+ const n = try s.focused();
+ const tag = try s.read(s.at("/pane/{d}/tag", .{n}));
+ const body = try s.read(s.at("/pane/{d}/body", .{n}));
+ for ([_][]const u8{ "|", alt ++ "|", "!", alt ++ "!" }) |key| {
+ try s.keys(key);
+ try std.testing.expectError(error.NotOnScreen, s.where(" |"));
+ try std.testing.expectError(error.NotOnScreen, s.where(" !"));
+ try s.expect(s.at("/pane/{d}/tag", .{n}), tag);
+ try s.expect(s.at("/pane/{d}/body", .{n}), body);
+ }
+}
+
+test "keys: a cancelled prompt restores the selection its regex previews changed" {
+ var s = try Session.initFile(std.testing.allocator, "one two one");
+ defer s.deinit();
+ const n = try s.focused();
+ const dot = s.at("/pane/{d}/dot", .{n});
+ for ([_][]const u8{ "sone", "S " }) |typed| {
+ try s.write(dot, "0 11");
+ try s.keys(typed);
+ try s.keys("\x1b");
+ try std.testing.expectEqual(.{ 0, 11 }, try s.range(dot));
+ try s.expect(s.at("/pane/{d}/sel", .{n}), "one two one");
+ try s.expect(s.at("/pane/{d}/body", .{n}), "one two one");
+ }
+}
+
+test "keys: a counted paste of nothing leaves no count for the next motion" {
+ var s = try Session.initFile(std.testing.allocator, "abcdefgh\n");
+ defer s.deinit();
+ const n = try s.focused();
+ try s.keys("3pl");
+ try std.testing.expectEqual(.{ 1, 1 }, try s.range(s.at("/pane/{d}/dot", .{n})));
+ try s.expect(s.at("/pane/{d}/body", .{n}), "abcdefgh\n");
+}
+
+test "keys: a count or a prefix a click or a change of pane cancels leaves nothing for the next motion" {
+ const Gesture = enum { body, tag, focus, last };
+ for ([_][]const u8{ "3", "3g", "mr(" }) |typed| {
+ for ([_]Gesture{ .body, .tag, .focus, .last }) |gesture| {
+ errdefer std.debug.print("\nkeys={s} gesture={s}\n", .{ typed, @tagName(gesture) });
+ var s = try Session.initFile(std.testing.allocator, "abcdefghij\n");
+ defer s.deinit();
+ const n = try s.focused();
+ try s.write("/ctl", "Newcol\n");
+ const other = try s.newPane();
+ try s.write("/focus", s.at("{d}", .{n}));
+ try s.keys(typed);
+ switch (gesture) {
+ .body => try s.click("abcdefghij", 3, 1),
+ .tag => {
+ try s.click("/test.txt Save", "/test.txt ".len + 3, 1);
+ try s.keys("\x1b:");
+ },
+ .focus, .last => {
+ try s.keys(window ++ "l\n");
+ try std.testing.expectEqual(other, try s.focused());
+ try s.keys(if (gesture == .last) "\x1b" else window ++ "h\n");
+ try std.testing.expectEqual(n, try s.focused());
+ },
+ }
+ const dot = s.at("/pane/{d}/dot", .{n});
+ const at = (try s.range(dot))[0];
+ try s.keys("l");
+ try std.testing.expectEqual(at + 1, (try s.range(dot))[0]);
+ try s.expect(s.at("/pane/{d}/body", .{n}), "abcdefghij\n");
+ }
+ }
+}
diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig
index 2ff4345b..ec26b74f 100644
--- a/test/behaviour/look_and_exec.zig
+++ b/test/behaviour/look_and_exec.zig
@@ -36,3 +36,49 @@ test "look-and-exec: a bare :N or :N:M look addresses the pane itself, from its
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));
}
+
+test "look-and-exec: a look replaces the selection it was made from, for an address, a range and text found" {
+ var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\nneedle\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const dot = s.at("/pane/{d}/dot", .{n});
+ const look = s.at("/pane/{d}/look", .{n});
+ try s.write(dot, "0 5");
+ try s.write(look, "/test.txt:3:2\n");
+ try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dot));
+ try s.write(dot, "0 5");
+ try s.write(look, "/test.txt:2:2-4\n");
+ try s.expect(s.at("/pane/{d}/sel", .{n}), "rav");
+ try s.write(s.at("/pane/{d}/name", .{n}), "/tmp/look-selection-source.txt");
+ try s.write(dot, "0 5");
+ // A count typed before it is gone after it.
+ try s.keys("5");
+ try s.write(look, "needle\n");
+ try s.expect(s.at("/pane/{d}/sel", .{n}), "needle");
+ try s.keys("l");
+ try expectEqual(.{ @as(u32, 26), @as(u32, 26) }, try s.range(dot));
+ try expectEqual(n, try s.focused());
+}
+
+test "look-and-exec: a look into another pane leaves the source's dot and replaces the destination's selection" {
+ var s = try Session.initFile(std.testing.allocator, "source\nlines\n");
+ defer s.deinit();
+ const source = try s.focused();
+ const dest = try s.newPane();
+ try s.write(s.at("/pane/{d}/name", .{dest}), "/look-destination.txt");
+ try s.write(s.at("/pane/{d}/body", .{dest}), "alpha\nbravo\ncharlie\n");
+ const dest_dot = s.at("/pane/{d}/dot", .{dest});
+ for ([_]bool{ false, true }) |range| {
+ try s.write("/focus", s.at("{d}", .{source}));
+ try s.write(s.at("/pane/{d}/dot", .{source}), "0 6");
+ try s.write(dest_dot, "0 5");
+ try s.write(s.at("/pane/{d}/look", .{source}), if (range) "/look-destination.txt:2:2-4\n" else "/look-destination.txt:3:2\n");
+ try expectEqual(dest, try s.focused());
+ // The source keeps its dot, as acme's look leaves it.
+ try s.expect(s.at("/pane/{d}/sel", .{source}), "source");
+ if (range)
+ try s.expect(s.at("/pane/{d}/sel", .{dest}), "rav")
+ else
+ try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dest_dot));
+ }
+}
diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig
index e9b33cb0..9a8430d7 100644
--- a/test/behaviour/tags.zig
+++ b/test/behaviour/tags.zig
@@ -191,3 +191,192 @@ test "tags: a tag's normal mode is the body's: motions stay in the tag and inser
try std.testing.expectEqual(first, try s.focused());
}
}
+
+test "tags: a tag with a newline in it is as tall as its lines, and each line is typed into and pointed at" {
+ for ([_]bool{ false, true }) |bottom| {
+ var s = try Session.initFile(std.testing.allocator, "body\n");
+ defer s.deinit();
+ const n = try s.focused();
+ if (bottom) try s.write("/ctl", "Tagbottom on\n");
+ // Insert mode in a tag is a body's: Enter is a new line.
+ try s.keys(":A\nSecond");
+ try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del\nSecond", try tag(&s, n));
+ const x, const y = try s.where("/test.txt");
+ try std.testing.expectEqual(.{ x, y + 1 }, try s.where("Second"));
+ // The caret is on the second line, after what was typed.
+ try std.testing.expectEqual(.{ x + 6, y + 1 }, try s.cursor());
+ // The body starts under (or over) the whole tag.
+ const by = (try s.where("1 body"))[1];
+ try std.testing.expect(if (bottom) by < y else by == y + 2);
+ // A click on the second line puts the tag's cursor on it.
+ try s.keys("\x1b:");
+ try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2, y + 1, x + 2, y + 1 }));
+ try std.testing.expectEqual(.{ x + 2, y + 1 }, try s.cursor());
+ try s.keys("X");
+ try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del\nSeXcond", try tag(&s, n));
+ try s.expect(s.at("/pane/{d}/body", .{n}), "body\n");
+ }
+}
+
+test "tags: a tag with more lines than rows scrolls to keep its caret in view" {
+ var s = try Session.initFile(std.testing.allocator, "body\n");
+ defer s.deinit();
+ const n = try s.focused();
+ var many: std.ArrayList(u8) = .empty;
+ defer many.deinit(std.testing.allocator);
+ for (1..21) |i| try many.print(std.testing.allocator, "\nL{d}", .{i});
+ try s.write(s.at("/pane/{d}/tag", .{n}), many.items);
+ const x, const y = try s.where("/test.txt");
+ // To the last line: the tag scrolls so it is the bottom row, caret on it.
+ try s.keys(":ge");
+ const lx, const ly = try s.where("L20");
+ try std.testing.expectEqual(.{ lx, ly }, try s.cursor());
+ // What the pointer sees is what is shown: its first row is line 13.
+ try std.testing.expectEqual(.{ x, y }, try s.where("L13"));
+ try std.testing.expectError(error.NotOnScreen, s.where("L12"));
+ try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 0\n", .{ x + 1, y + 1, x + 1, y + 1 }));
+ try s.keys("X");
+ _ = try s.where("LX14");
+ // Back to the first line brings the prefix back into view.
+ try s.keys("\x1bgg");
+ try std.testing.expectEqual(.{ x, y }, try s.where("/test.txt"));
+ try std.testing.expectEqual(y, (try s.cursor())[1]);
+ // A column tag is one row, as acme's: its lines are joined.
+ var layout = std.mem.tokenizeScalar(u8, try s.read("/layout"), ' ');
+ const col_tag = s.at("/col/{s}/tag", .{layout.next().?});
+ try s.replace(col_tag, "New\nC1\nC2");
+ try std.testing.expect(std.mem.indexOfScalar(u8, try s.read(col_tag), '\n') == null);
+ try std.testing.expectEqual(y, (try s.where("/test.txt"))[1]);
+}
+
+test "tags: a long pane tag wraps onto rows, and a click on a wrapped row maps to the whole tag's address" {
+ var s = try Session.initFile(std.testing.allocator, "body\n");
+ defer s.deinit();
+ s.detach();
+ try s.write("/ctl", "size 50 12\n");
+ const n = try s.focused();
+ try s.write(s.at("/pane/{d}/name", .{n}), "/an/especially/deep/project/path/with/a/long/component/filename.zig");
+ try s.write(s.at("/pane/{d}/tag", .{n}), " SyntaxBold");
+ const before = try tag(&s, n);
+ try s.keys(":A");
+ try std.testing.expect((try s.cursor())[0] < 50);
+ // SyntaxBold is on a row of its own, below the path's.
+ try std.testing.expect((try s.where("SyntaxBold"))[1] > (try s.where("/an/"))[1]);
+ // Its last character, which a wrapped row's offset not mapped back misses.
+ try s.click("SyntaxBold", "SyntaxBol".len, 2);
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "SyntaxBold on\n") != null);
+ try std.testing.expectEqualStrings(before, try tag(&s, n));
+}
+
+test "tags: a word the tag's wrapping breaks across rows is still one word to a click" {
+ var s = try Session.initFile(std.testing.allocator, "body\n");
+ defer s.deinit();
+ s.detach();
+ try s.write("/ctl", "size 50 12\n");
+ const n = try s.focused();
+ // Lengthen the path until the wrap splits the word after "Synt" (a
+ // rename resets the tag's text, so it is written again each time).
+ var pad: usize = 1;
+ while (pad < 80) : (pad += 1) {
+ try s.write(s.at("/pane/{d}/name", .{n}), s.at("/{s}/f.zig", .{(&@as([80]u8, @splat('a')))[0..pad]}));
+ try s.write(s.at("/pane/{d}/tag", .{n}), " SyntaxBold");
+ var rows = std.mem.splitScalar(u8, try s.screen(), '\n');
+ const split = while (rows.next()) |row| {
+ if (std.mem.endsWith(u8, std.mem.trimEnd(u8, row, " "), "Synt")) break true;
+ } else false;
+ if (split) break;
+ } else return error.NoSplit;
+ try s.click("Synt", 1, 2);
+ try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "SyntaxBold on\n") != null);
+}
+
+/// A click on the tag's path, at its last character: a name drafted
+/// there, its caret taken to its end.
+fn clickPathEnd(s: *Session, path: []const u8) !void {
+ try s.click(path, path.len - 1, 1);
+ try s.keys("\u{F018}");
+}
+
+test "tags: a name drafted in the tag commits with Enter, cancels with two Escs, and changes neither body nor tag text" {
+ var s = try Session.initFile(std.testing.allocator, "original body\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const name = s.at("/pane/{d}/name", .{n});
+ try s.write(s.at("/pane/{d}/tag", .{n}), " SyntaxBold");
+ for ([_]bool{ false, true }) |commit| {
+ const path = std.mem.trimEnd(u8, try s.read(name), "\n");
+ try clickPathEnd(&s, path);
+ try s.keys("\x15/new name.zig");
+ try s.expect(name, s.at("{s}\n", .{path}));
+ _ = try s.where("/new name.zig Save Tty Collapse Del SyntaxBold");
+ try s.expect(s.at("/pane/{d}/body", .{n}), "original body\n");
+ // Esc keeps the draft pending; a second drops it.
+ try s.keys(if (commit) "\n" else "\x1b\x1b");
+ try s.expect(name, if (commit) "/new name.zig\n" else "/test.txt\n");
+ try std.testing.expectEqualStrings(s.at("{s} Save Tty Collapse Del SyntaxBold", .{if (commit) "/new name.zig" else "/test.txt"}), try tag(&s, n));
+ // The keyboard is the body's again.
+ try std.testing.expect((try s.cursor())[1] != (try s.where("Save Tty"))[1]);
+ }
+ try std.testing.expectEqual(@as(usize, 0), s.saved);
+ // A name alone is no edit: the pane stays clean.
+ try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
+}
+
+test "tags: a drafted name's edits are by grapheme, and an empty one is not committed" {
+ var s = try Session.initFile(std.testing.allocator, "untouched\n");
+ defer s.deinit();
+ const n = try s.focused();
+ try clickPathEnd(&s, "/test.txt");
+ try s.keys("\x15/café界\x08");
+ _ = try s.where("/café Save");
+ try s.keys("\x08");
+ _ = try s.where("/caf Save");
+ try s.keys("\x15\n");
+ try std.testing.expectError(error.NotOnScreen, s.where("/test.txt"));
+ try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n");
+ try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n");
+ // Esc keeps the draft pending; a second drops it.
+ try s.keys("\x1b");
+ try std.testing.expectError(error.NotOnScreen, s.where("/test.txt"));
+ try s.keys("\x1b");
+ _ = try s.where("/test.txt");
+}
+
+test "tags: leaving a pane or entering a header drops an uncommitted name" {
+ for ([_]bool{ false, true }) |header| {
+ var s = try Session.initFile(std.testing.allocator, "untouched\n");
+ defer s.deinit();
+ const n = try s.focused();
+ try clickPathEnd(&s, "/test.txt");
+ try s.keys("\x15/draft");
+ _ = try s.where("/draft");
+ if (header) try s.click("Newcol", 1, 1) else _ = try s.newPane();
+ try std.testing.expectError(error.NotOnScreen, s.where("/draft"));
+ try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n");
+ }
+}
+
+test "tags: middle clicking Save commits the drafted name before saving" {
+ var s = try Session.initFile(std.testing.allocator, "untouched\n");
+ defer s.deinit();
+ const n = try s.focused();
+ try clickPathEnd(&s, "/test.txt");
+ try s.keys("\x15/renamed.txt");
+ try s.click("Save Tty", 0, 2);
+ try s.expect(s.at("/pane/{d}/name", .{n}), "/renamed.txt\n");
+ try std.testing.expectEqualStrings("/renamed.txt Save Tty Collapse Del", try tag(&s, n));
+ try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n");
+ try std.testing.expectEqual(@as(usize, 1), s.saved);
+}
+
+test "tags: an oversized drafted name is refused at commit and the draft stays to correct" {
+ var s = try Session.initFile(std.testing.allocator, "untouched\n");
+ defer s.deinit();
+ const n = try s.focused();
+ try clickPathEnd(&s, "/test.txt");
+ const excess = s.at("\x15/{s}\n", .{&@as([4999]u8, @splat('x'))});
+ try s.keys(excess);
+ try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n");
+ try s.expect(s.at("/pane/{d}/body", .{n}), "untouched\n");
+ _ = try s.where("/xxxx");
+}