summaryrefslogtreecommitdiff
path: root/test/behaviour/keys.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behaviour/keys.zig')
-rw-r--r--test/behaviour/keys.zig135
1 files changed, 135 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");
+ }
+ }
+}