diff options
Diffstat (limited to 'test/behaviour/keys.zig')
| -rw-r--r-- | test/behaviour/keys.zig | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig index 3880b116..476504ce 100644 --- a/test/behaviour/keys.zig +++ b/test/behaviour/keys.zig @@ -226,3 +226,155 @@ test "keys: SPC j n and SPC j p walk Themes' rows, whole, never its section head try s.keys("\t"); try std.testing.expectEqualStrings(names.items[brk - 1]["Theme ".len..], try setting(&s, "Theme")); } + +test "keys: Save's prompt on a scratch starts from the scratch's directory, one slash after it" { + for ([_][2][]const u8{ .{ "/dír space/f.txt", " Save /dír space/zq" }, .{ "/f.txt", " Save /zq" } }) |c| { + var s = try Session.initFile(std.testing.allocator, "one\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}\n", .{c[0]})); + // New from the file's pane: a scratch in its directory. + try s.write(s.at("/pane/{d}/exec", .{n}), "New\n"); + try std.testing.expect(try s.focused() != n); + try s.keys(":\tzq"); + _ = try s.where(c[1]); + try s.keys("\x1b"); + } +} + +/// A B1 sweep from cell (x0, y0) to (x1, y1). +fn sweep(s: *Session, x0: usize, y0: usize, x1: usize, y1: usize) !void { + try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 0\n", .{ x0, y0, x1, y1, x1, y1 })); +} + +/// Whether any cell /screen draws is in `style`. +fn drawsIn(s: *Session, style: []const u8) !bool { + for (try s.styles()) |cell| if (std.mem.eql(u8, cell, style)) return true; + return false; +} + +test "keys: Ctrl-o and Ctrl-i jump back and forth, leaving no sweep drawn, a wrapped Unicode and tab one included" { + const ctl = "\u{F017}"; + for ([_]struct { []const u8, []const u8, bool }{ + .{ "alpha\nbravo\n", "alpha", false }, + .{ "界\tabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n", "界", true }, + }) |c| for ([_]bool{ false, true }) |reverse| { + const prefix, const first, const wrap = c; + var buf: [4096]u8 = undefined; + @memcpy(buf[0..prefix.len], prefix); + for (0..80) |i| @memcpy(buf[prefix.len + i * 27 ..][0..27], "unrelated destination text\n"); + var s = try Session.initFile(std.testing.allocator, buf[0 .. prefix.len + 80 * 27]); + defer s.deinit(); + const n = try s.focused(); + s.detach(); + try s.write("/ctl", "size 34 10\n"); + try s.write("/ctl", if (wrap) "Wrap on\n" else "Wrap off\n"); + const dot = s.at("/pane/{d}/dot", .{n}); + const addr = s.at("/pane/{d}/addr", .{n}); + // Two places in the jump list: line 60, then line 1. + try s.keys("60G1G"); + const x, const y = try s.where(first); + const plain = (try s.styles())[0]; + if (reverse) try sweep(&s, x + 3, y + 1, x + 1, y) else try sweep(&s, x + 1, y, x + 3, y + 1); + const screen = try s.screen(); + const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]); + const swept = (try s.styles())[(y + 1) * cols + x]; + try std.testing.expect(!std.mem.eql(u8, swept, plain)); + try s.keys(ctl ++ "o"); + try s.write(addr, "60"); + const line60 = (try s.range(addr))[0]; + try std.testing.expectEqual(.{ line60, line60 }, try s.range(dot)); + try std.testing.expectError(error.NotOnScreen, s.where(first)); + try std.testing.expect(!try drawsIn(&s, swept)); + // A sweep made after Back is gone after Forward. + const x2, const y2 = try s.where("unrelated"); + try sweep(&s, x2 + 1, y2, x2 + 3, y2 + 1); + try std.testing.expect(try drawsIn(&s, swept)); + try s.keys(ctl ++ "i"); + // Back where the sweep left the caret, nothing selected. + const q0, const q1 = try s.range(dot); + try std.testing.expect(q0 == q1 and q1 < line60); + _ = try s.where(first); + try std.testing.expect(!try drawsIn(&s, swept)); + }; +} + +// Its rows whole and Tab running one are the Themes walk's test above. +test "keys: SPC j n from the pane a Look gave the keyboard walks back into the command output" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + for ([_][]const u8{ "a.txt", "b.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/look", s.at("{s}/a.txt\n", .{at})); + const a = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{a}), "Themes\n"); + try s.keys(" jn"); + const themes = try s.focused(); + try std.testing.expect(themes != a); + const sel = s.at("/pane/{d}/sel", .{themes}); + try s.expect(sel, "Theme orchard"); + try s.write(s.at("/pane/{d}/exec", .{a}), "Look b.txt\n"); + try std.testing.expect(try s.focused() != themes); + try s.keys(" jn"); + try std.testing.expectEqual(themes, try s.focused()); + try std.testing.expect(std.mem.startsWith(u8, try s.read(sel), "Theme ")); +} + +test "keys: SPC j n and SPC j p walk on from the place a Look's +Search listing chose, its keyboard left in the file" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "look-owner.txt", .data = "alpha target\nbeta target\ngamma target\n" }); + var dir: [4096]u8 = undefined; + const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)]; + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "LookWord list\n"); // the +Search listing is what is walked + try s.write("/look", s.at("{s}/look-owner.txt\n", .{at})); + const file = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{file}), "Look target\n"); + try std.testing.expectEqual(file, try s.focused()); + // The Look took the first; the walk goes on from it, and back. + try s.keys(" jn"); + const results = try s.focused(); + try std.testing.expect(results != file); + const sel = s.at("/pane/{d}/sel", .{results}); + try s.expect(sel, "look-owner.txt:2:6-11"); + try s.keys(" jp"); + try std.testing.expectEqual(results, try s.focused()); + try s.expect(sel, "look-owner.txt:1:7-12"); +} + +/// Screen row `row` as drawn. +fn screenRow(s: *Session, row: usize) ![]const u8 { + var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); + for (0..row) |_| _ = rows.next(); + return rows.next().?; +} + +test "keys: a PDF in normal mode keeps : (its tag and back), SPC (a leader, cancelled by a key it has not) and / (a search prompt, Esc closing it)" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const body = (try s.cursor()).?; + const tag = (try s.where("docs/design.pdf [1/"))[1]; + try s.keys(":"); + const on_tag = (try s.cursor()).?[1]; + try std.testing.expect(on_tag == tag or on_tag == tag + 1); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal ")); + try s.keys(":"); + try std.testing.expectEqual(body, (try s.cursor()).?); + // The leader shows its keys at the row's end until one it has not. + try s.keys(" "); + try std.testing.expect(std.mem.endsWith(u8, std.mem.trimEnd(u8, try screenRow(&s, body[1]), " "), " SPC")); + try s.keys("!"); + try std.testing.expect(!std.mem.endsWith(u8, std.mem.trimEnd(u8, try screenRow(&s, body[1]), " "), " SPC")); + try s.keys("/"); + try std.testing.expect(!std.meta.eql(body, (try s.cursor()).?)); + try std.testing.expectEqual(n, try s.focused()); + try s.keys("\x1b"); + try std.testing.expectEqual(body, (try s.cursor()).?); + try std.testing.expectEqual(n, try s.focused()); +} |
