diff options
Diffstat (limited to 'test/behaviour/keys.zig')
| -rw-r--r-- | test/behaviour/keys.zig | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig new file mode 100644 index 00000000..41a2821a --- /dev/null +++ b/test/behaviour/keys.zig @@ -0,0 +1,227 @@ + + +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"); + } + } +} + +test "keys: a PDF shown as text turns pages for the moving keys, counted, and the editing keys do nothing" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const pages = (try s.pdfPage(n))[1]; + try std.testing.expect(pages > 3); + // j k, Ctrl-d Ctrl-u, Ctrl-f Ctrl-b: a page each, counted. + for ([_][]const u8{ "2j", "2k", "2\x04", "2\x15", "2\x06", "2\x02" }, 0..) |keys, i| { + try s.keys(keys); + try std.testing.expectEqual(@as(u32, if (i % 2 == 0) 3 else 1), (try s.pdfPage(n))[0]); + } + for ([_]struct { []const u8, u32 }{ .{ "ge", pages }, .{ "gg", 1 }, .{ "3gg", 3 }, .{ "2G", 2 } }) |step| { + try s.keys(step[0]); + try std.testing.expectEqual(step[1], (try s.pdfPage(n))[0]); + } + // Consumed, with their count: the page stays, no selection begins, and + // the next j is one page. + try s.keys("4dv|"); + try std.testing.expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal ")); + try s.keys("j"); + try std.testing.expectEqual(@as(u32, 3), (try s.pdfPage(n))[0]); +} + +test "keys: SPC j n and SPC j p in plain text select its places, never a comment's slashes, round the end, opening nothing" { + var s = try Session.initFile(std.testing.allocator, "// ordinary comment\n/// documentation comment\n(//) [///] punctuation\n/\n// /tmp\n//tmp\n"); + defer s.deinit(); + const n = try s.focused(); + const index = try s.read("/index"); + for ([_]struct { []const u8, []const u8, u32 }{ + .{ " jn", "/", 69 }, .{ " jn", "/tmp", 74 }, .{ " jn", "//tmp", 79 }, .{ " jn", "/", 69 }, + .{ " jp", "//tmp", 79 }, .{ " jp", "/tmp", 74 }, .{ " jp", "/", 69 }, + }) |step| { + try s.keys(step[0]); + try std.testing.expectEqual(n, try s.focused()); + try s.expect(s.at("/pane/{d}/sel", .{n}), step[1]); + try std.testing.expectEqual(step[2], (try s.range(s.at("/pane/{d}/dot", .{n})))[0]); + try s.expect("/index", index); + } +} + +/// The value the root ctl's read gives `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[word.len + 1 ..]; + return error.NoSuchSetting; +} + +test "keys: SPC j n and SPC j p walk Themes' rows, whole, never its section headings, and Tab sets the one selected" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const text = try s.focused(); + // An imported theme's, the list still begins with the natives. + try s.write("/ctl", "Theme dark\n"); + try s.write(s.at("/pane/{d}/exec", .{text}), "Themes\n"); + // Listed beside the text, which keeps the keyboard. + try std.testing.expectEqual(text, try s.focused()); + const list = text + 1; + try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+Themes ", .{list})) != null); + const body = try s.read(s.at("/pane/{d}/body", .{list})); + // A heading, then a row a theme; each section after the first a blank + // line and its heading. + var lines = std.mem.splitScalar(u8, body, '\n'); + try std.testing.expectEqualStrings("# Pardes themes", lines.next().?); + var names: std.ArrayList([]const u8) = .empty; + var first_break: ?usize = null; + while (lines.next()) |line| { + if (line.len == 0) { + const heading = lines.next() orelse break; // the newline ending the last row + try std.testing.expect(std.mem.startsWith(u8, heading, "# ")); + first_break = first_break orelse names.items.len; + continue; + } + try std.testing.expect(std.mem.startsWith(u8, line, "Theme ")); + try names.append(s.arena.allocator(), line); + } + const brk = first_break orelse return error.NoSections; + try std.testing.expect(std.mem.indexOf(u8, body[0..std.mem.indexOf(u8, body, "\n\n").?], "\nTheme acme\n") != null); + // Across the first section's end and back, the theme as it was. + for (names.items[0 .. brk + 1]) |row| { + try s.keys(" jn"); + try std.testing.expectEqual(list, try s.focused()); + try s.expect(s.at("/pane/{d}/sel", .{list}), row); + try std.testing.expectEqualStrings("dark", try setting(&s, "Theme")); + } + try s.keys(" jp"); + try s.expect(s.at("/pane/{d}/sel", .{list}), names.items[brk - 1]); + try s.keys("\t"); + try std.testing.expectEqualStrings(names.items[brk - 1]["Theme ".len..], try setting(&s, "Theme")); +} |
