diff options
Diffstat (limited to 'test/behaviour')
| -rw-r--r-- | test/behaviour/tags.zig | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig new file mode 100644 index 00000000..e9b33cb0 --- /dev/null +++ b/test/behaviour/tags.zig @@ -0,0 +1,193 @@ +//! docs/typ/guide.typ, Tags <tags>. +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()); + } +} |
