diff options
Diffstat (limited to 'test/behaviour')
| -rw-r--r-- | test/behaviour/columns_and_tags.zig | 168 | ||||
| -rw-r--r-- | test/behaviour/keys.zig | 227 | ||||
| -rw-r--r-- | test/behaviour/limits.zig | 18 | ||||
| -rw-r--r-- | test/behaviour/look_and_exec.zig | 160 | ||||
| -rw-r--r-- | test/behaviour/modes.zig | 104 | ||||
| -rw-r--r-- | test/behaviour/mouse.zig | 133 | ||||
| -rw-r--r-- | test/behaviour/other_files.zig | 10 | ||||
| -rw-r--r-- | test/behaviour/panes.zig | 197 | ||||
| -rw-r--r-- | test/behaviour/placement.zig | 61 | ||||
| -rw-r--r-- | test/behaviour/settings.zig | 62 | ||||
| -rw-r--r-- | test/behaviour/tags.zig | 399 | ||||
| -rw-r--r-- | test/behaviour/terminals.zig | 46 |
12 files changed, 1584 insertions, 1 deletions
diff --git a/test/behaviour/columns_and_tags.zig b/test/behaviour/columns_and_tags.zig index fa3baf48..d485ee6e 100644 --- a/test/behaviour/columns_and_tags.zig +++ b/test/behaviour/columns_and_tags.zig @@ -1,6 +1,7 @@ //! docs/typ/reference.typ, Columns and tags <columns-and-tags>. const std = @import("std"); const Session = @import("session").Session; +const expectEqualStrings = std.testing.expectEqualStrings; /// /layout's line for the last column. fn lastColumn(s: *Session) ![]const u8 { @@ -32,6 +33,173 @@ test "columns-and-tags: a look after Newcol fills the empty column the keyboard try std.testing.expect(std.mem.endsWith(u8, col, s.at(" {d}", .{try s.focused()}))); } +test "columns-and-tags: Joincol folds a column into its right neighbour, keeping its panes, and with none it is inert" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const left = try s.focused(); + const left_col = try s.column(0); + try s.write("/ctl", "Newcol\n"); + const right_col = try s.column(1); + try s.write(s.at("/col/{d}/ctl", .{right_col}), "New\n"); + const right = try s.focused(); + try s.write(s.at("/col/{d}/ctl", .{left_col}), "Joincol\n"); + try expectEqualStrings(s.at("{d}:{d},{d}", .{ right_col, right, left }), try s.columns()); + _ = try s.refused(s.at("/col/{d}/ctl", .{right_col}), "Joincol\n"); + try expectEqualStrings("Joincol: no column to the right", s.said); + try expectEqualStrings(s.at("{d}:{d},{d}", .{ right_col, right, left }), try s.columns()); +} + +test "columns-and-tags: Newcol makes an empty column with the keyboard on its tag, and the next pane made fills it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const left, const col = .{ try s.column(0), try s.column(1) }; + try expectEqualStrings(s.at("{d}:{d} {d}:", .{ left, first, col }), try s.columns()); + try s.expect("/focus", ""); + try s.write(s.at("/pane/{d}/exec", .{first}), "Tty\n"); + const shell = try s.focused(); + try expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ left, first, col, shell }), try s.columns()); + // The next Tty splits as it always has: nothing is empty any more. + try s.write(s.at("/pane/{d}/exec", .{shell}), "Tty\n"); + try expectEqualStrings(s.at("{d}:{d} {d}:{d},{d}", .{ left, first, col, shell, try s.focused() }), try s.columns()); +} + +test "columns-and-tags: a column's exec acts on that column, and a pane's Delcol on its own" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + try s.write("/ctl", "Newcol\n"); + const left, const middle = .{ try s.column(0), try s.column(1) }; + // New from the middle column's tag lands in it, not below the pane + // with the keyboard. + try s.write(s.at("/col/{d}/exec", .{middle}), "New\n"); + const made = try s.focused(); + try expectEqualStrings(s.at("{d}:{d} {d}:{d} {d}:", .{ left, first, middle, made, try s.column(2) }), try s.columns()); + // Delcol from the empty right column's tag closes that one. + try s.write(s.at("/col/{d}/exec", .{try s.column(2)}), "Delcol\n"); + try expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ left, first, middle, made }), try s.columns()); + // A pane's own Delcol closes its column whatever tag has the keyboard. + try s.write("/ctl", "Newcol\n"); + try s.expect("/focus", ""); + const right = try s.column(2); + try s.write(s.at("/pane/{d}/exec", .{made}), "Delcol\n"); + try expectEqualStrings(s.at("{d}:{d} {d}:", .{ left, first, right }), try s.columns()); +} + +test "columns-and-tags: closing a column's last pane leaves it empty, its tag with the keyboard; Delcol takes it away" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const left, const col = .{ try s.column(0), try s.column(1) }; + try s.write(s.at("/col/{d}/ctl", .{col}), "New\n"); + try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n"); + // The column outlives the pane, as acme's does. + try expectEqualStrings(s.at("{d}:{d} {d}:", .{ left, first, col }), try s.columns()); + try s.expect("/focus", ""); + // Down from it goes nowhere: nothing is below. + try s.write(s.at("/pane/{d}/exec", .{first}), "Down\n"); + try s.expect("/focus", ""); + // The tag with the keyboard is the emptied column's: what is typed lands there. + try s.keys("iMine\x1b"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/col/{d}/tag", .{col})), "Mine") != null); + try s.write(s.at("/col/{d}/ctl", .{col}), "Delcol\n"); + try expectEqualStrings(s.at("{d}:{d}", .{ left, first }), try s.columns()); + try std.testing.expectEqual(first, try s.focused()); +} + +test "columns-and-tags: a column's tag follows its column through Newcol and Joincol" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write(s.at("/col/{d}/tag", .{try s.column(0)}), " Left"); + try s.write(s.at("/pane/{d}/exec", .{try s.focused()}), "Newcol\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/col/{d}/tag", .{try s.column(0)})), " Left")); + try s.write(s.at("/col/{d}/tag", .{try s.column(1)}), " Right"); + try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Joincol\n"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/col/{d}/tag", .{try s.column(0)})), " Right")); +} + +test "columns-and-tags: Delcol on the last column leaves the window, drawn and typed into, and Newcol starts it again" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Delcol\n"); + try expectEqualStrings("", try s.columns()); + try s.expect("/index", ""); + // Drawn, and keys go to the workspace tag. + try s.keys("x\n"); + try std.testing.expect(std.mem.startsWith(u8, try s.screen(), " Newcol ")); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "New\n"); + try expectEqualStrings(s.at("{d}:{d}", .{ try s.column(0), try s.focused() }), try s.columns()); +} + +test "columns-and-tags: New is a column tag's word, in no pane's tag nor the workspace's" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + var t = try Session.initTerm(std.testing.allocator); + defer t.deinit(); + // The file pane's, read before 9P's new takes the keyboard; a scratch + // made by New, and 9P's new (a +New file pane). + const file = try s.read(s.at("/pane/{d}/tag", .{try s.focused()})); + const made = try s.newPane(); + try s.write(s.at("/col/{d}/exec", .{try s.column(0)}), "New\n"); + const scratch = try s.focused(); + try std.testing.expect(made != scratch); + for ([_][]const u8{ + try s.read("/tag"), + file, + try s.read(s.at("/pane/{d}/tag", .{made})), + try s.read(s.at("/pane/{d}/tag", .{scratch})), + try t.read(t.at("/pane/{d}/tag", .{try t.focused()})), + }) |tag| { + var words = std.mem.tokenizeScalar(u8, tag, ' '); + while (words.next()) |word| try std.testing.expect(!std.mem.eql(u8, word, "New")); + } + try expectEqualStrings("New Tty Find Grep Joincol Delcol", try s.read(s.at("/col/{d}/tag", .{try s.column(0)}))); +} + +test "columns-and-tags: a click past a column tag's text types after it, a newline typed there a space, and a long tag scrolls to its caret" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const tag = s.at("/col/{d}/tag", .{try s.column(0)}); + // Its row, then the pane's tag: the column's stays one row. + const rows = " New Tty Find Grep Joincol Delcol"; + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), rows ++ " ") != null); + try s.click("Delcol", 20, 1); + try s.keys("\nSnarf"); + try expectEqualStrings("New Tty Find Grep Joincol Delcol Snarf", try s.read(tag)); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), rows ++ " Snarf ") != null); + // The pane's tag on the row right under it: the column's is one row. + try std.testing.expectEqual((try s.where(rows))[1] + 1, (try s.where("/test.txt Save"))[1]); + // Wider than the screen: the tag scrolls to keep its caret drawn. + try s.keys(" 界界界界界界界界界界界界界界界界界界界界界界界界界界界界界界"); + const col, const row = (try s.cursor()).?; + try std.testing.expectEqual(@as(usize, 1), row); + try std.testing.expect(col < 80); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), rows) == null); + try s.keys("\u{F00D}"); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), rows) != null); +} + +test "columns-and-tags: Enter in a tag in normal mode runs the word under its cursor, and Tab does too, letting go of the keyboard first" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const first = try s.focused(); + try s.click("Newcol", 1, 1); + try s.keys("\x1b\n"); + try expectEqualStrings(s.at("{d}:{d} {d}:", .{ try s.column(0), first, try s.column(1) }), try s.columns()); + // The workspace tag lets go of the keyboard: the new, empty column's + // tag has it. + try s.expect("/focus", ""); + try s.keys("iMine\x1b"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/col/{d}/tag", .{try s.column(1)})), "Mine") != null); + try s.click("Tutor", 1, 1); + try s.keys("\x1b\t"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/name", .{try s.focused()})), "/+Tutor\n")); +} + test "columns-and-tags: a tag write refuses NUL and the other control characters, the workspace's, a column's and a pane's" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); 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")); +} diff --git a/test/behaviour/limits.zig b/test/behaviour/limits.zig new file mode 100644 index 00000000..08c864eb --- /dev/null +++ b/test/behaviour/limits.zig @@ -0,0 +1,18 @@ +//! docs/typ/reference.typ, Limits <limits>. +const std = @import("std"); +const Session = @import("session").Session; + +test "limits: a builtin that needs a pane says so when every slot is taken" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + // Room on the screen for every pane, so the slots run out first. + s.detach(); + try s.write("/ctl", "size 100 400\n"); + var made: usize = 1; + while (s.newPane()) |_| made += 1 else |_| {} + try std.testing.expectEqual(@as(usize, 64), made); + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Help\n"); + try std.testing.expectEqualStrings("help: no space for a pane: 64 max", s.said); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "help: no space for a pane") != null); +} diff --git a/test/behaviour/look_and_exec.zig b/test/behaviour/look_and_exec.zig index 72052f7d..98cab342 100644 --- a/test/behaviour/look_and_exec.zig +++ b/test/behaviour/look_and_exec.zig @@ -163,3 +163,163 @@ test "look-and-exec: a line starting with # runs as nothing at an exec or a ctl, try s.expect("/index", index); try s.expect("/log", log); } + +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}); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const body = s.at("/pane/{d}/body", .{n}); + try s.write(dot, "0 5"); + // A second cursor on the line below, and select mode. + try s.keys("Cv"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:select ")); + try s.write(look, "/test.txt:3:2\n"); + try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dot)); + // Both are gone with it: normal mode, and what is typed goes in once. + try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:normal ")); + try s.keys("iZ\x1b"); + try s.expect(body, "alpha\nbravo\ncZharlie\nneedle\n"); + try s.write(ctl, "Undo\n"); + try s.expect(body, "alpha\nbravo\ncharlie\nneedle\n"); + 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)); + } +} + +test "look-and-exec: a look that finds nothing says so" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.newPane(); + try s.write(s.at("/pane/{d}/look", .{n}), "/no/such/zzqq\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("err {d} look: no match for \"/no/such/zzqq\"\n", .{n})) != null); +} + +/// The log's `err` records, counted. +fn errs(s: *Session) !usize { + return std.mem.count(u8, try s.read("/log"), "\nerr "); +} + +/// The serial of the pane /index names with `suffix`. +fn paneNamed(s: *Session, suffix: []const u8) !u32 { + var rows = std.mem.splitScalar(u8, try s.read("/index"), '\n'); + while (rows.next()) |line| { + // serial kind dirty name column + const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue]; + if (std.mem.endsWith(u8, name, suffix)) return std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10); + } + return error.NoSuchPane; +} + +test "look-and-exec: `:N` on a PDF turns to page N, from the pane and from the root's look, and `@pN:N` logs no err" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const look = s.at("/pane/{d}/look", .{n}); + const pages = (try s.pdfPage(n))[1]; + try std.testing.expect(pages >= 3); + try s.write(look, ":2\n"); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try s.write(look, s.at("@p{d}:1\n", .{n})); + try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); + try s.write("/look", s.at("@p{d}:2\n", .{n})); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try expectEqual(@as(usize, 0), try errs(&s)); + // `:N:H` is hit H of its search on page N, as a search row's + // `file.pdf:N:H` is: with no search there is no hit 3, a miss that turns + // no page. + try s.write(look, ":1\n"); + _ = try s.refused(look, ":2:3\n"); + try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no search hit 3 on page 2") != null); + try s.write(look, ":2\n"); + _ = try s.refused(look, ":0\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 0") != null); + // A byte offset or a pattern is no PDF address: refused with words. + _ = try s.refused(look, ":#3\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "its address is a page, :N") != null); + const before = try errs(&s); + _ = try s.refused(look, s.at(":{d}\n", .{pages + 1})); + try expectEqual(before + 1, try errs(&s)); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); +} + +test "look-and-exec: a look at a PDF's page past its last is a miss, never the last page, and opens nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + try std.Io.Dir.cwd().copyFile("docs/design.pdf", tmp.dir, "other.pdf", std.testing.io, .{}); + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const look = s.at("/pane/{d}/look", .{n}); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + // Open: it stays on its page. + try s.write(look, ":2\n"); + _ = try s.refused(look, s.at("{s}:999\n", .{name})); + try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 999") != null); + // Not open: nothing opens. + const index = try s.read("/index"); + _ = try s.refused(look, s.at("{s}/other.pdf:999\n", .{dir})); + try s.expect("/index", index); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "other.pdf has no page 999") != null); +} + +test "look-and-exec: a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + try s.write(s.at("/pane/{d}/exec", .{n}), "PdfSections\n"); + const out = try paneNamed(&s, "/+PdfSections"); + const body = try s.read(s.at("/pane/{d}/body", .{out})); + const row = body[0..std.mem.indexOfScalar(u8, body, '\n').?]; + const place = row[0..std.mem.indexOfScalar(u8, row, ' ').?]; // design.pdf:P:S + var fields = std.mem.splitScalar(u8, place, ':'); + _ = fields.next(); + const page = try std.fmt.parseInt(u32, fields.next().?, 10); + const look = s.at("/pane/{d}/look", .{out}); + // As listed: found, no miss. + try s.write(look, s.at("{s}\n", .{place})); + try expectEqual(@as(usize, 0), try errs(&s)); + // The section on another page than the row says, and one not there. + _ = try s.refused(look, s.at("design.pdf:{d}:1\n", .{page + 1})); + try expectEqual(@as(usize, 1), try errs(&s)); + _ = try s.refused(look, "design.pdf:1:999\n"); + try expectEqual(@as(usize, 2), try errs(&s)); +} diff --git a/test/behaviour/modes.zig b/test/behaviour/modes.zig new file mode 100644 index 00000000..dde3ffb6 --- /dev/null +++ b/test/behaviour/modes.zig @@ -0,0 +1,104 @@ +//! docs/typ/guide.typ, Modes <modes>. +const std = @import("std"); +const Session = @import("session").Session; +const expectEqual = std.testing.expectEqual; + +fn mode(s: *Session, pane: u32) ![]const u8 { + const ctl = try s.read(s.at("/pane/{d}/ctl", .{pane})); + const at = std.mem.lastIndexOf(u8, ctl, " mode:").? + " mode:".len; + return std.mem.trimEnd(u8, ctl[at..], " "); +} + +test "modes: Esc in normal mode alternates between two panes of the same kind" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.txt", .data = "b\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, "a\n"); + defer s.deinit(); + const a = try s.focused(); + try s.write("/look", s.at("{s}/b.txt\n", .{at})); + const b = try s.focused(); + try std.testing.expect(a != b); + for (0..6) |_| { + const was = try s.focused(); + try s.keys("\x1b"); + try expectEqual(if (was == a) b else a, try s.focused()); + } +} + +test "modes: Esc in a column or the workspace tag gives the keyboard back to a pane, as Shift-Esc does" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + _ = try s.newPane(); + for ([_][]const u8{ "New Tty", "Newcol" }) |tag| { + const was = try s.focused(); + // A click types into the tag; Esc is normal mode there, the next + // one is out of it. + try s.click(tag, 0, 1); + try s.keys("\x1b"); + try s.expect("/focus", ""); + try s.keys("\x1b"); + try std.testing.expect(was != try s.focused()); + } +} + +test "modes: Shift-Esc backs out to the previous pane in every mode, never into raw" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const doc = try s.focused(); + try s.keys("\u{F015}n"); // a shell under the doc + const shell = try s.focused(); + if (std.mem.eql(u8, try mode(&s, shell), "tty")) try s.keys("\x02"); + try std.testing.expectEqualStrings("normal", try mode(&s, shell)); + // A terminal in normal mode: out, still normal, never raw. + try s.keys("\u{F016}\x1b"); + try expectEqual(doc, try s.focused()); + try std.testing.expectEqualStrings("normal", try mode(&s, shell)); + // A text pane in insert mode: out of insert, and out of the pane. + try s.keys("i"); + try std.testing.expectEqualStrings("insert", try mode(&s, doc)); + try s.keys("\u{F016}\x1b"); + try std.testing.expectEqualStrings("normal", try mode(&s, doc)); + try std.testing.expect(doc != try s.focused()); +} + +/// The first line number drawn under the tag that starts with `tag`. +fn topLine(s: *Session, tag: []const u8) !usize { + const row = (try s.where(tag))[1]; + var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); + for (0..row + 1) |_| _ = rows.next(); + var words = std.mem.tokenizeScalar(u8, rows.next().?, ' '); + return std.fmt.parseInt(usize, words.next().?, 10); +} + +test "modes: Esc back into a file leaves its view where it was, and a look centres its line" { + var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); + defer s.deinit(); + const doc = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{doc}); + try s.keys("\u{F015}n"); // a shell under the doc + const shell = try s.focused(); + try s.keys("\x1b"); + try expectEqual(doc, try s.focused()); + // Ride the cursor down until the view has scrolled. + try s.keys(&@as([20]u8, @splat('j'))); + const top = try topLine(&s, "/test.txt"); + const at = try s.range(dot); + try std.testing.expect(top > 1); + try s.keys("\x1b"); + try expectEqual(shell, try s.focused()); + try s.keys("\x1b"); + try expectEqual(doc, try s.focused()); + // Nothing moved: the old landing recentred the line under the cursor. + try expectEqual(top, try topLine(&s, "/test.txt")); + try expectEqual(at, try s.range(dot)); + // The other arm still centres: a look's `:NN`. + const line = 60; + try s.write(s.at("/pane/{d}/look", .{doc}), s.at(":{d}\n", .{line})); + const rows = (try s.where("Tty Save Mode"))[1] - (try s.where("/test.txt"))[1] - 1; + // Drawn mid-pane, a row either way. + const drawn_at = line - try topLine(&s, "/test.txt"); + try std.testing.expect(drawn_at + 1 >= rows / 2 and drawn_at <= rows / 2 + 1); +} diff --git a/test/behaviour/mouse.zig b/test/behaviour/mouse.zig new file mode 100644 index 00000000..0abe15dc --- /dev/null +++ b/test/behaviour/mouse.zig @@ -0,0 +1,133 @@ +//! docs/typ/guide.typ, The mouse <mouse>. +const std = @import("std"); +const Session = @import("session").Session; + +/// The mouse as `/mousein` lines say it, `m col row buttons` each. +fn mouse(s: *Session, comptime lines: []const u8, args: anytype) !void { + try s.write("/mousein", s.at(lines, args)); +} + +test "mouse: a double-click selects a word, a line, a bracketed or quoted text, as acme's" { + var s = try Session.initFile(std.testing.allocator, "call foo_bar(a, b)\nsay \"hi there\"\n"); + defer s.deinit(); + const n = try s.focused(); + const sel = s.at("/pane/{d}/sel", .{n}); + for ([_]struct { at: []const u8, into: usize, want: []const u8 }{ + .{ .at = "foo_bar", .into = 2, .want = "foo_bar" }, + .{ .at = "(a, b)", .into = 1, .want = "a, b" }, + .{ .at = "call", .into = 0, .want = "call foo_bar(a, b)\n" }, + .{ .at = "\"hi", .into = 1, .want = "hi there" }, + }) |w| { + try s.click(w.at, w.into, 1); + try s.click(w.at, w.into, 1); + try s.expect(sel, w.want); + // A third click is a plain click again: the caret, no selection. + try s.click(w.at, w.into, 1); + try s.expect(sel, ""); + } + // In the tag too: what it selected is what a yank takes. + try s.click("Collapse Del", "Collapse D".len, 1); + try s.click("Collapse Del", "Collapse D".len, 1); + try s.keys("\x1by:ggP"); + try s.expect(s.at("/pane/{d}/body", .{n}), "Delcall foo_bar(a, b)\nsay \"hi there\"\n"); +} + +test "mouse: everything a B1 chord does while B1 is down is one undo step" { + var s = try Session.initFile(std.testing.allocator, "one two three\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const x, const y = try s.where("one two"); + // B1-B2 then B1-B3: cut, then paste back: a copy, one step. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 5\nm {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y }); + try s.expect(body, "one two three\n"); + // Undone in one step, it is the text before the cut, not after it. + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "one two three\n"); + // A cut alone comes back with one Undo. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y }); + try s.expect(body, "one three\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + try s.expect(body, "one two three\n"); +} + +test "mouse: a click past a line's or the file's end puts the caret where insertion types" { + for ([_]bool{ false, true }) |already_insert| { + for ([_]bool{ false, true }) |bottom_tag| { + for ([_]struct { text: []const u8, click_row: usize, dot: u32, col: usize, row: usize, result: []const u8 }{ + .{ .text = "abc\nxy", .click_row = 0, .dot = 3, .col = 3, .row = 0, .result = "abcZ\nxy" }, + .{ .text = "abc\nxy", .click_row = 5, .dot = 6, .col = 2, .row = 1, .result = "abc\nxyZ" }, + .{ .text = "abc\n", .click_row = 5, .dot = 4, .col = 0, .row = 1, .result = "abc\nZ" }, + .{ .text = "", .click_row = 5, .dot = 0, .col = 0, .row = 0, .result = "Z" }, + }) |case| { + var s = try Session.initFile(std.testing.allocator, case.text); + defer s.deinit(); + const n = try s.focused(); + if (bottom_tag) try s.write("/ctl", "Tagbottom on\n"); + if (already_insert) try s.keys("i"); + // The first line's number, then a blank, then its text. + const x, const y = try s.where("1 "); + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2 + 15, y + case.click_row, x + 2 + 15, y + case.click_row }); + try std.testing.expectEqual(.{ case.dot, case.dot }, try s.range(s.at("/pane/{d}/dot", .{n}))); + // The caret is drawn where it is, and insertion draws it there too. + try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?); + if (!already_insert) try s.keys("i"); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:insert ")); + try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?); + try s.keys("Z"); + try s.expect(s.at("/pane/{d}/body", .{n}), case.result); + } + } + } +} + +test "mouse: a click on a tab's or a wrapped wide glyph's cells puts the caret on it" { + for ([_]bool{ false, true }) |already_insert| { + for ([_]bool{ false, true }) |wrap| { + const source = if (wrap) "012345678901234567890123456789界tail\n" else "\t界tail\n"; + var s = try Session.initFile(std.testing.allocator, source); + defer s.deinit(); + s.detach(); + try s.write("/ctl", "size 24 12\n"); + const n = try s.focused(); + if (!wrap) try s.write("/ctl", "Wrap off\n"); + if (already_insert) try s.keys("i"); + const x, const y = try s.where("界"); + if (wrap) try std.testing.expect(y > (try s.where("1 "))[1]); + // The glyph's second cell. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 1, y, x + 1, y }); + const at: u32 = @intCast(try std.unicode.utf8CountCodepoints(source[0..std.mem.indexOf(u8, source, "界").?])); + try std.testing.expectEqual(.{ at, at }, try s.range(s.at("/pane/{d}/dot", .{n}))); + // Drawn on the glyph's first cell, in insert mode too. + try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?); + if (!already_insert) try s.keys("i"); + try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?); + try s.keys("Z"); + const byte = std.mem.indexOf(u8, source, "界").?; + try s.expect(s.at("/pane/{d}/body", .{n}), s.at("{s}Z{s}", .{ source[0..byte], source[byte..] })); + } + } +} + +test "mouse: a B1 sweep over several lines is a stream, as acme's, in a body and a tag" { + var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\n"); + defer s.deinit(); + const n = try s.focused(); + const body = s.at("/pane/{d}/body", .{n}); + const x, const y = try s.where("alpha"); + // From the press to the end of its line, the line between whole, the + // last from its start: no line numbers, no rectangle. B2 cuts it. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ x + 2, y, x + 1, y + 2, x + 1, y + 2, x + 1, y + 2 }); + try s.expect(body, "alarlie\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n"); + // Upward, the same text. + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ x + 1, y + 2, x + 2, y, x + 2, y, x + 2, y }); + try s.expect(body, "alarlie\n"); + // A tag of several rows sweeps the same way. + const tag = s.at("/pane/{d}/tag", .{n}); + try s.write(tag, "\nLook Edit"); + const tx, const ty = try s.where("Collapse"); + const lx, const ly = try s.where("Look Edit"); + try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ tx + 3, ty, lx + 1, ly, lx + 1, ly, lx + 1, ly }); + try s.expect(tag, "/test.txt Save Tty Colok Edit"); +} diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig index 8625df3b..ef1fab7e 100644 --- a/test/behaviour/other_files.zig +++ b/test/behaviour/other_files.zig @@ -70,3 +70,13 @@ test "other-files: pager answers its +Pager on the open that asked, whatever ano try std.testing.expectEqual(@as(u16, 2), try s.refused("/pager", "/tmp/pardes-surely-missing-dir\n")); try std.testing.expectEqual(@as(u16, 20), try s.refused("/pager", "/etc/passwd\n")); } + +test "other-files: /recent keeps a PDF as it keeps a file: open, then closed" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/recent"), s.at("open {s}\n", .{name})) != null); + try s.write(s.at("/pane/{d}/ctl", .{n}), "Del\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/recent"), s.at("closed {s}\n", .{name})) != null); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index 4ea41693..a46f8432 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -166,3 +166,200 @@ test "panes: a second lock is refused until the holder unlocks or closes, and bi try std.testing.expectEqual(@as(u16, 0), try s.writeFid(e, "lock\n")); s.clunk(e); } + +test "panes: an unsaved file's tag is its name alone, its dirty file says it is unsaved" { + var s = try Session.initFile(std.testing.allocator, "before\n"); + defer s.deinit(); + const n = try s.focused(); + const tag = s.at("/pane/{d}/tag", .{n}); + const clean = try s.read(tag); + try s.write(s.at("/pane/{d}/body", .{n}), "after\n"); + try std.testing.expectEqualStrings(clean, try s.read(tag)); + try std.testing.expect(std.mem.indexOf(u8, clean, " *") == null); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n"); +} + +test "panes: a pane closed while its question stands ends the question in the log" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Placement pardes\n"); + const top = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); + const closing = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{closing}), "New\n"); + // A middle pane: Del from a key asks which side. + try s.write("/focus", s.at("{d}\n", .{closing})); + try s.keys(" d"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("ask {d} del k j\n", .{closing})) != null); + // A bare answer says what this question takes. + const ctl = s.at("/pane/{d}/ctl", .{closing}); + _ = try s.refused(ctl, "answer\n"); + try std.testing.expectEqualStrings("answer takes k j or -", s.said); + try s.write(ctl, "Del\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("answer {d} -\ndel {d}", .{ closing, closing })) != null); +} + +test "panes: focus on a closed pane goes to the pane that takes its place, not the one focused before" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const left = try s.focused(); + try s.write("/ctl", "Newcol\n"); + try s.write(s.at("/col/{d}/ctl", .{try s.column(1)}), "New\n"); + const right = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{left}), "New\n"); + const low = try s.focused(); + // `right` was focused just before `low`, but `left` grows into `low`'s + // rows, so `left` gets the focus. + try s.write(s.at("/pane/{d}/ctl", .{low}), "Del\n"); + try std.testing.expectEqual(left, try s.focused()); + // Closing a pane that has no focus leaves the focus where it is. + try s.write(s.at("/pane/{d}/exec", .{left}), "New\n"); + const other = try s.focused(); + try s.write("/focus", s.at("{d}\n", .{right})); + try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n"); + try std.testing.expectEqual(right, try s.focused()); + // Delcol: the column that takes the width holds, level with the closed + // pane, the pane that takes its place; `right` was focused just before. + try s.write(s.at("/pane/{d}/exec", .{right}), "New\n"); + const right_low = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{left}), "New\n"); + const left_low = try s.focused(); + try s.write("/focus", s.at("{d}\n", .{right})); + try s.write("/focus", s.at("{d}\n", .{left_low})); + try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Delcol\n"); + try std.testing.expectEqual(right_low, try s.focused()); +} + +test "panes: Collapse keeps the text and folds the pane to its tag, and again restores it" { + var s = try Session.initFile(std.testing.allocator, "keep this text\n"); + defer s.deinit(); + var t = try Session.initTerm(std.testing.allocator); + defer t.deinit(); + const n = try s.focused(); + for ([_][]const u8{ + try s.read(s.at("/pane/{d}/tag", .{n})), + try s.read(s.at("/pane/{d}/tag", .{try s.newPane()})), + try t.read(t.at("/pane/{d}/tag", .{try t.focused()})), + }) |tag| try std.testing.expect(std.mem.endsWith(u8, tag, " Collapse Del")); + try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n"); + const ctl = s.at("/pane/{d}/ctl", .{n}); + try s.write(ctl, "Collapse\n"); + // Folded to its tag: no text drawn, no cursor in it, the text kept. + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") != null); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "keep this text") == null); + try std.testing.expectEqual(null, try s.cursor()); + try s.expect(s.at("/pane/{d}/body", .{n}), "keep this text\n"); + try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n"); + // Its tag still takes the keyboard, the cursor on the tag's row. + try s.keys(":"); + try std.testing.expectEqual((try s.where("/test.txt"))[1], (try s.cursor()).?[1]); + try s.write(ctl, "Collapse\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") == null); + try std.testing.expectEqual((try s.where("/test.txt"))[1] + 1, (try s.where("1 keep this text"))[1]); +} + +test "panes: a pane its whole text fits in never scrolls to keep a margin" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\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, "one\ntwo\nthree\n"); + defer s.deinit(); + const n = try s.focused(); + // A second pane under it, as acme's placement puts one: right under the + // three lines, which leaves the file exactly three body rows. + try s.write(s.at("/pane/{d}/look", .{n}), s.at("{s}/f.txt\n", .{at})); + try s.write("/focus", s.at("{d}\n", .{n})); + try s.keys("jj"); + const top = (try s.where("/test.txt"))[1]; + try std.testing.expectEqual(top + 4, (try s.where(at[0..@min(at.len, 20)]))[1]); + // On the last line, and line 1 still the first drawn. + try std.testing.expectEqual(.{ @as(u32, 8), @as(u32, 8) }, try s.range(s.at("/pane/{d}/dot", .{n}))); + try std.testing.expectEqual(top + 1, (try s.where("1 one"))[1]); +} + +/// `name` in `dir`: a one-page PDF with no outline and no text. +fn plainPdf(dir: std.Io.Dir, name: []const u8) !void { + const objects = [_][]const u8{ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Count 1 /Kids [3 0 R] >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>", + }; + var buf: [1024]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + var at: [objects.len]usize = undefined; + try w.writeAll("%PDF-1.4\n"); + for (objects, 0..) |object, i| { + at[i] = w.end; + try w.print("{d} 0 obj\n{s}\nendobj\n", .{ i + 1, object }); + } + const xref = w.end; + try w.print("xref\n0 {d}\n0000000000 65535 f \n", .{objects.len + 1}); + for (at) |off| try w.print("{d:0>10} 00000 n \n", .{off}); + try w.print("trailer\n<< /Size {d} /Root 1 0 R >>\nstartxref\n{d}\n%%EOF\n", .{ objects.len + 1, xref }); + try dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() }); +} + +test "panes: a PDF's body is the text of the page shown, each line of it a line, never a heading run into the text under it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.lookFile("docs/design.pdf"); + const body = s.at("/pane/{d}/body", .{n}); + try std.testing.expect(std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\nArchitecture and implementation of the rewrite\n")); + try s.write(s.at("/pane/{d}/look", .{n}), ":2\n"); + try std.testing.expect(!std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\n")); +} + +test "panes: PdfTint steps a PDF's tint, read in its ctl and never in its tag, and only a PDF pane takes it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const text = try s.focused(); + const n = try s.lookFile("docs/design.pdf"); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const tag = try s.read(s.at("/pane/{d}/tag", .{n})); + try std.testing.expect(std.mem.endsWith(u8, tag, " PdfSections PdfTint PdfFit Collapse Del")); + for ([_][]const u8{ "full", "disabled", "filtered" }) |want| { + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), "fit:width ") != null); + try s.write(ctl, "PdfTint\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null); + try s.expect(s.at("/pane/{d}/tag", .{n}), tag); + // Another pane's PdfTint is refused and leaves the PDF's alone. + try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/ctl", .{text}), "PdfTint\n")); + try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null); + } +} + +test "panes: PdfSections on a PDF with no outline says so and opens nothing" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try plainPdf(tmp.dir, "plain.pdf"); + 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(); + const n = try s.lookFile(s.at("{s}/plain.pdf", .{dir})); + try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[1]); + const index = try s.read("/index"); + for (0..2) |_| { + _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "PdfSections\n"); + try s.expect("/index", index); + } + try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "PdfSections: this PDF has no outline") != null); +} + +test "panes: a Mini that cannot read its file leaves the panes, the last Mini's snapshot and the keyboard as they were" { + var s = try Session.initFile(std.testing.allocator, "abc\n"); + defer s.deinit(); + const text = try s.focused(); + const exec = s.at("/pane/{d}/exec", .{text}); + try s.write(exec, s.at("Mini /virtual/pane/{d}/body\n", .{text})); + const mini = try s.focused(); + try std.testing.expect(mini != text); + const body = try s.read(s.at("/pane/{d}/body", .{mini})); + const index = try s.read("/index"); + try std.testing.expectEqual(@as(u16, 2), try s.refused(exec, "Mini /virtual/does-not-exist\n")); + try std.testing.expectEqual(mini, try s.focused()); + try s.expect("/index", index); + try s.expect(s.at("/pane/{d}/body", .{mini}), body); +} diff --git a/test/behaviour/placement.zig b/test/behaviour/placement.zig new file mode 100644 index 00000000..079b5a5e --- /dev/null +++ b/test/behaviour/placement.zig @@ -0,0 +1,61 @@ +//! docs/typ/reference.typ, Placement <placement>. +const std = @import("std"); +const Session = @import("session").Session; + +test "placement: an empty active column takes the new pane whole, a look from another column's text too" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\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(); + const first = try s.focused(); + // Its tag has the keyboard: it is the active column, and stays so + // when Esc gives the keyboard back. + try s.write("/ctl", "Newcol\n"); + try s.keys("\x1b"); + try std.testing.expectEqual(first, try s.focused()); + // A look from the first pane's text still lands in the active column, + // as acme's makenewwindow puts it in activecol first (util.c:456). + try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/f.txt\n", .{at})); + const made = try s.focused(); + try std.testing.expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ try s.column(0), first, try s.column(1), made }), try s.columns()); +} + +/// The screen row the tag starting `name` is drawn on. +fn tagRow(s: *Session, name: []const u8) !usize { + return (try s.where(s.at("{s} Save", .{name})))[1]; +} + +test "placement: New and 9P's new take the bottom half of the column's last pane" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{first}), "New\n"); + const last = try s.focused(); + try s.write(s.at("/pane/{d}/name", .{last}), "/last\n"); + const was = try tagRow(&s, "/last"); + const made = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{made}), "/made\n"); + try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d}", .{ try s.column(0), first, last, made }), try s.columns()); + // The last pane keeps its top and gives up its bottom half. + try std.testing.expectEqual(was, try tagRow(&s, "/last")); + const rows = std.mem.count(u8, try s.screen(), "\n") + 1; + try std.testing.expect(try tagRow(&s, "/made") >= was + (rows - was) / 2); +} + +test "placement: Placement pardes puts a new pane right under the asking one, and bare it steps back to acme" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Placement pardes\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement pardes\n") != null); + const top = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); + const below = try s.focused(); + // Directly under the asking pane, as New always put it. + try s.write(s.at("/pane/{d}/exec", .{top}), "New\n"); + try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d}", .{ try s.column(0), top, try s.focused(), below }), try s.columns()); + try s.write("/ctl", "Placement\n"); + try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement acme\n") != null); +} diff --git a/test/behaviour/settings.zig b/test/behaviour/settings.zig index 5970e6b5..ff24704a 100644 --- a/test/behaviour/settings.zig +++ b/test/behaviour/settings.zig @@ -1,4 +1,5 @@ -//! docs/typ/reference.typ, Settings <settings>. + + const std = @import("std"); const Session = @import("session").Session; @@ -41,3 +42,62 @@ test "settings: Shell refuses a path that is no executable, and bare it goes bac 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"); +} diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig new file mode 100644 index 00000000..6c1c6fcd --- /dev/null +++ b/test/behaviour/tags.zig @@ -0,0 +1,399 @@ + + +const std = @import("std"); +const Session = @import("session").Session; + +const home = "\u{F00D}"; +const right = "\u{F012}"; +const window = "\x17"; // Ctrl-w + +/// The pane's tag file. +fn tag(s: *Session, n: u32) ![]const u8 { + return s.read(s.at("/pane/{d}/tag", .{n})); +} + +/// The column of the screen's cursor in the tag drawn starting with +/// `path`, which must be on its row. +fn tagCol(s: *Session, path: []const u8) !usize { + const x, const y = try s.where(path); + const cx, const cy = (try s.cursor()).?; + try std.testing.expectEqual(y, cy); + return cx - x; +} + +test "tags: s and S in a tag answer for the tag's own text; / still searches the body" { + var s = try Session.initFile(std.testing.allocator, "Some body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.replace(s.at("/pane/{d}/tag", .{n}), " Save Del Snarf"); + // The tag's line, its S's selected, then changed: both are the tag's. + try s.keys(":xsS\ncZ\x1b"); + try std.testing.expectEqualStrings("/test.txt Zave Del Znarf", try tag(&s, n)); + try s.expect(s.at("/pane/{d}/body", .{n}), "Some body\n"); + // `/` from the tag is the body's search, as acme's Look from a tag is. + // Its places list the body's match. + try s.keys(",/body\n"); + _ = try s.where("test.txt:1:6-9"); +} + +test "tags: ms and md edit a tag's own text, and the text the edit made is read back whole" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.keys(":miwms("); + try std.testing.expectEqualStrings("/test.txt (Save) Tty Collapse Del", try tag(&s, n)); + try s.keys("md("); + try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del", try tag(&s, n)); +} + +test "tags: the keyboard reaches a tag's path, which it can select and yank but not edit" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + const before = try tag(&s, n); + // `0` is the line's start, as in acme, where the name is ordinary text. + try s.keys(":0"); + try std.testing.expectEqual(@as(usize, 0), try tagCol(&s, "/test.txt")); + // An edit that reaches into it is refused, the cursor with it. + try s.keys("Eyd"); + try std.testing.expectEqualStrings(before, try tag(&s, n)); + try std.testing.expectEqual(@as(usize, "/test.tx".len), try tagCol(&s, "/test.txt")); + // Typed into, the path drafts a name there, as a click on it does. + try s.keys(";0ix"); + _ = try s.where("x/test.txt"); + // Esc keeps the draft pending; a second drops it. + try s.keys("\x1b\x1b"); + try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n"); + // After the path, the text is the tag's own to edit. + try s.keys(":A!\x1b"); + try std.testing.expectEqualStrings("/test.txt Save Tty Collapse Del!", try tag(&s, n)); + // What it yanked was the path. + try s.keys(":P"); + try s.expect(s.at("/pane/{d}/body", .{n}), "/test.txtbody\n"); +} + +test "tags: a tag remembers its cursor, starts on Save, and a click moves it" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const second = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{second}), "/second.txt"); + const focus = s.at("{d}", .{first}); + const other = s.at("{d}", .{second}); + try s.write("/focus", focus); + // Nothing to remember yet: `:` lands on Save. + try s.keys(":"); + try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); + // Home is the line's start, the path's: the keyboard reaches it. + try s.keys(home ++ right ++ right ++ right); + try std.testing.expectEqual(@as(usize, 3), try tagCol(&s, "/test.txt")); + // `:` in the tag's normal mode is the way back to the body. + try s.keys(":"); + try std.testing.expect((try s.cursor()).?[1] != (try s.where("/test.txt"))[1]); + try s.write("/focus", other); + try s.keys(":" ++ home ++ right ++ right ++ right ++ right ++ right ++ ":"); + try s.write("/focus", focus); + try s.keys(":"); + try std.testing.expectEqual(@as(usize, 3), try tagCol(&s, "/test.txt")); + try s.keys(":"); + try s.write("/focus", other); + try s.keys(":"); + try std.testing.expectEqual(@as(usize, 5), try tagCol(&s, "/second.txt")); + try s.keys(":"); + // A click in the tag puts its cursor there, the pane's keyboard with it. + try s.click("Save Tty", 6, 1); + try std.testing.expectEqual(first, try s.focused()); + try std.testing.expectEqual(@as(usize, "/test.txt Save T".len), try tagCol(&s, "/test.txt")); + // A prompt opened and left from the body leaves it there. + try s.keys("\x1b:/\x1b:"); + try std.testing.expectEqual(@as(usize, "/test.txt Save T".len), try tagCol(&s, "/test.txt")); + try s.keys(":"); + try s.expect(s.at("/pane/{d}/body", .{first}), "body\n"); +} + +test "tags: executing a word from the tag runs it and gives the keyboard back to the body" { + for ([_][]const u8{ "\t", "\n" }) |chord| { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.keys("ix\x1b"); + // `:` lands on Save; the execute key runs it, the look key looks + // it up. Either way the tag lets go of the keyboard first. + try s.keys(":"); + try s.keys(chord); + try std.testing.expectEqual(@as(usize, if (chord[0] == '\t') 1 else 0), s.saved); + try s.keys("iQ\x1b"); + try s.expect(s.at("/pane/{d}/body", .{n}), "xQbody\n"); + // ...and the tag still knows where it was. + try s.keys(":"); + try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt")); + } +} + +test "tags: a renamed path moves the tag's cursor with the text after it and a shorter tag pulls it back" { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + const n = try s.focused(); + try s.keys("::"); // on Save, and back + try s.write(s.at("/pane/{d}/name", .{n}), "/界.zig"); + try s.keys(":"); + try std.testing.expectEqual(@as(usize, "/界.zig ".len - 1), try tagCol(&s, "/界")); + try s.keys(":"); + try s.replace(s.at("/pane/{d}/tag", .{n}), "é"); + try s.keys(":"); + // `/界.zig é`: the cursor is pulled back onto the last character. + try std.testing.expectEqual(@as(usize, "/界.zig ".len - 1), try tagCol(&s, "/界")); + try s.expect(s.at("/pane/{d}/body", .{n}), "body\n"); +} + +test "tags: window keys move between panes from a tag and each tag keeps its cursor" { + for ([_]bool{ false, true }) |bottom| { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + if (bottom) try s.write("/ctl", "Tagbottom on\n"); + const first = try s.focused(); + const second = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{second}), "/second.txt"); + try s.write("/focus", s.at("{d}", .{first})); + try s.keys(":" ++ home ++ "ll" ++ window ++ "j\n"); + try std.testing.expectEqual(second, try s.focused()); + try s.keys(":" ++ home ++ "llll" ++ window ++ "k\n"); + try std.testing.expectEqual(first, try s.focused()); + try s.keys(":"); + try std.testing.expectEqual(@as(usize, 2), try tagCol(&s, "/test.txt")); + try s.keys(window ++ "j\n:"); + try std.testing.expectEqual(@as(usize, 4), try tagCol(&s, "/second.txt")); + } +} + +test "tags: a tag's normal mode is the body's: motions stay in the tag and insert types into it" { + for ([_]bool{ false, true }) |bottom| { + var s = try Session.initFile(std.testing.allocator, "body\n"); + defer s.deinit(); + if (bottom) try s.write("/ctl", "Tagbottom on\n"); + const first = try s.focused(); + try s.write("/ctl", "Newcol\n"); + const right_pane = try s.newPane(); + try s.write("/focus", s.at("{d}", .{first})); + try s.keys(":" ++ home ++ "l"); + try std.testing.expectEqual(@as(usize, 1), try tagCol(&s, "/test.txt")); + try s.keys("hkj"); + try std.testing.expectEqual(@as(usize, 0), try tagCol(&s, "/test.txt")); + try std.testing.expectEqual(first, try s.focused()); + try s.keys(window ++ "l\n"); + try std.testing.expectEqual(right_pane, try s.focused()); + try s.keys(window ++ "h\n"); + try std.testing.expectEqual(first, try s.focused()); + try s.keys(":i\u{F018}hjklHJKL:"); + try std.testing.expectEqualStrings("/test.txt Save Tty Collapse DelhjklHJKL:", try tag(&s, first)); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " mode:normal ")); + try s.expect(s.at("/pane/{d}/body", .{first}), "body\n"); + 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"); +} + +test "tags: a name typed into the tag's path survives Esc, pending, and the next Enter renames; a second Esc drops it" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const n = try s.focused(); + const name = s.at("/pane/{d}/name", .{n}); + // A click on the path types into it; a stray key while it waits + // changes nothing. + try s.click("/test.txt", 1, 1); + try s.keys("z\x1bq\n"); + try s.expect(name, "/ztest.txt\n"); + try s.click("/ztest.txt", 1, 1); + try s.keys("z\x1bq\x1b"); + try s.expect(name, "/ztest.txt\n"); + try std.testing.expect(std.mem.startsWith(u8, try s.read(s.at("/pane/{d}/tag", .{n})), "/ztest.txt ")); +} diff --git a/test/behaviour/terminals.zig b/test/behaviour/terminals.zig new file mode 100644 index 00000000..f6bbdfa0 --- /dev/null +++ b/test/behaviour/terminals.zig @@ -0,0 +1,46 @@ +//! docs/typ/guide.typ, Terminals <terminals>. +const std = @import("std"); +const Session = @import("session").Session; + +fn mode(s: *Session, pane: u32) ![]const u8 { + const ctl = try s.read(s.at("/pane/{d}/ctl", .{pane})); + const at = std.mem.lastIndexOf(u8, ctl, " mode:").? + " mode:".len; + return std.mem.trimEnd(u8, ctl[at..], " "); +} + +test "terminals: Togglettymode and Ctrl-B share the mode's transitions, from the body or its tag, and a text pane refuses it" { + var s = try Session.initTerm(std.testing.allocator); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/tag", .{n})), " Mode ") != null); + try s.write(s.at("/pane/{d}/tag", .{n}), " Togglettymode"); + try std.testing.expectEqualStrings("tty", try mode(&s, n)); + try s.write(ctl, "Togglettymode\n"); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + try s.keys("\x02"); + try std.testing.expectEqualStrings("tty", try mode(&s, n)); + // The tag has its own mode: the body keeps tty while the tag is typed + // into, and either way of toggling gives the body the keyboard back. + const tag_row = (try s.where("Togglettymode"))[1]; + try s.click("Togglettymode", 0, 1); + try std.testing.expectEqualStrings("tty", try mode(&s, n)); + try std.testing.expectEqual(tag_row, (try s.cursor()).?[1]); + try s.write(ctl, "Togglettymode\n"); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + try std.testing.expect((try s.cursor()).?[1] > tag_row); + try s.click("Togglettymode", 0, 1); + try s.keys("\x02"); + try std.testing.expectEqualStrings("tty", try mode(&s, n)); + try std.testing.expect((try s.cursor()).?[1] > tag_row); + // A middle click on the word in the tag. + try s.click("Togglettymode", 0, 2); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + // A text pane refuses it, normal, its text as it was. + const file = try s.newPane(); + try s.write(s.at("/pane/{d}/body", .{file}), "unchanged\n"); + _ = try s.refused(s.at("/pane/{d}/ctl", .{file}), "Togglettymode\n"); + try std.testing.expectEqualStrings("Togglettymode: only a terminal pane takes it", s.said); + try std.testing.expectEqualStrings("normal", try mode(&s, file)); + try s.expect(s.at("/pane/{d}/body", .{file}), "unchanged\n"); +} |
