diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-03 17:01:04 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-03 17:03:48 -0300 |
| commit | 63e62131aaedb91a8fa995b8e00ced808e053519 (patch) | |
| tree | cddcb8f17ea2139eb69893321beb0e78ac02e105 /test | |
| parent | d1273bbd99daf9b1855d82bf315db8ede20abde5 (diff) | |
| download | pardes-63e62131aaedb91a8fa995b8e00ced808e053519.tar.gz pardes-63e62131aaedb91a8fa995b8e00ced808e053519.zip | |
test: five of test/panes.zig's mouse tests through the 9P tree alone (test/behaviour/mouse.zig, guide.typ's The mouse): a double-click's word, bracketed, quoted and line selections (read from sel) and in a tag (yanked and pasted into the body), a B1 chord's cut and paste one undo step (one Undo gives back the text before the cut), a click past a line's or the file's end and on a tab's or a wide glyph's second cell (dot, then the Z insert types), a B1 sweep over several lines a stream in a body and a tag (a B1-B2 cut of it); Session.where, the screen cell text is first drawn at; each checked to fail against a break (double-click selecting nothing, chord_hold no longer folding the chord's undo steps, the click clamp a column short of the line's end, every grapheme one cell wide to a click, a sweep's start at the lesser of its two columns)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test')
| -rw-r--r-- | test/behaviour.zig | 1 | ||||
| -rw-r--r-- | test/behaviour/mouse.zig | 127 | ||||
| -rw-r--r-- | test/panes.zig | 186 | ||||
| -rw-r--r-- | test/session.zig | 6 |
4 files changed, 134 insertions, 186 deletions
diff --git a/test/behaviour.zig b/test/behaviour.zig index 32138f94..46fa8402 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -6,6 +6,7 @@ test { _ = @import("behaviour/columns_and_tags.zig"); _ = @import("behaviour/edit.zig"); _ = @import("behaviour/look_and_exec.zig"); + _ = @import("behaviour/mouse.zig"); _ = @import("behaviour/other_files.zig"); _ = @import("behaviour/panes.zig"); _ = @import("behaviour/root_ctl.zig"); diff --git a/test/behaviour/mouse.zig b/test/behaviour/mouse.zig new file mode 100644 index 00000000..27185409 --- /dev/null +++ b/test/behaviour/mouse.zig @@ -0,0 +1,127 @@ +//! 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, result: []const u8 }{ + .{ .text = "abc\nxy", .click_row = 0, .dot = 3, .result = "abcZ\nxy" }, + .{ .text = "abc\nxy", .click_row = 5, .dot = 6, .result = "abc\nxyZ" }, + .{ .text = "abc\n", .click_row = 5, .dot = 4, .result = "abc\nZ" }, + .{ .text = "", .click_row = 5, .dot = 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}))); + 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 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}))); + if (!already_insert) try s.keys("i"); + 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/panes.zig b/test/panes.zig index fc3f7804..6970cbb5 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -1119,159 +1119,6 @@ const ClickCursorTests = struct { try std.testing.expect(std.mem.count(u8, got, "\n") > 20); } - test "everything a B1 chord does while B1 is down is one undo step" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("one two three\n"); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = p.bodyTop(pane, rect); - const M = struct { - fn ev(pp: *Pardes, b: pardes.Mouse.Button, k: pardes.Mouse.Kind, cx: u16, cy: u16) void { - pp.update(.{ .mouse = .{ .button = b, .kind = k, .col = cx, .row = cy } }); - } - }; - // B1-B2 then B1-B3: cut, then paste back: a copy, one step. - const before = pane.file.?.history.undo_len; - M.ev(p, .left, .press, x + 4, y); - M.ev(p, .left, .drag, x + 6, y); - M.ev(p, .middle, .press, x + 6, y); - M.ev(p, .middle, .release, x + 6, y); - M.ev(p, .right, .press, x + 6, y); - M.ev(p, .right, .release, x + 6, y); - M.ev(p, .left, .release, x + 6, y); - try std.testing.expectEqualStrings("one two three\n", pane.file.?.content); - try std.testing.expectEqualStrings("two", p.registers.text(p.gpa, '"').?); - try std.testing.expect(pane.file.?.history.undo_len <= before + 1); - // A cut alone comes back with one Undo. - M.ev(p, .left, .press, x + 4, y); - M.ev(p, .left, .drag, x + 6, y); - M.ev(p, .middle, .press, x + 6, y); - M.ev(p, .middle, .release, x + 6, y); - M.ev(p, .left, .release, x + 6, y); - try std.testing.expectEqualStrings("one three\n", pane.file.?.content); - try std.testing.expect(p.executeBuiltinLine(0, "Undo")); - try std.testing.expectEqualStrings("one two three\n", pane.file.?.content); - } - - test "a double-click selects a word, a line, a bracketed or quoted text, as acme's" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("call foo_bar(a, b)\nsay \"hi there\"\n"); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = p.bodyTop(pane, rect); - const Want = struct { col: u16, row: u16, text: []const u8 }; - for ([_]Want{ - .{ .col = 6, .row = 0, .text = "foo_bar" }, - .{ .col = 13, .row = 0, .text = "a, b" }, - .{ .col = 0, .row = 0, .text = "call foo_bar(a, b)\n" }, - .{ .col = 5, .row = 1, .text = "hi there" }, - }) |w| { - click(p, .left, x + w.col, y + w.row); - click(p, .left, x + w.col, y + w.row); - try std.testing.expectEqualStrings(w.text, pardes.edit.currentSelText(p, pane).?); - // A third click is a plain click again: the caret, no selection. - click(p, .left, x + w.col, y + w.row); - try std.testing.expect(!pane.body.vsel.active); - } - // In the tag too. - const tag_text = try pardes.tagline.tagText(p, frame.allocator(), pane); - const del: u16 = @intCast(std.mem.indexOf(u8, tag_text, "Del").? + 1); - const tag_x = rect.x + pardes.TAG_TEXT_INSET + del; - click(p, .left, tag_x, p.tagTop(pane, rect)); - click(p, .left, tag_x, p.tagTop(pane, rect)); - try std.testing.expect(pane.tag.vsel.active); - const tag_now = try pardes.tagline.tagText(p, frame.allocator(), pane); - const a = modal.offsetAt(tag_now, .{ .row = @intCast(pane.tag.vsel.row), .col = @intCast(pane.tag.vsel.col) }); - const h = modal.offsetAt(tag_now, .{ .row = @intCast(pane.tag.cur_row), .col = @intCast(pane.tag.cur_col) }); - try std.testing.expectEqualStrings("Del", tag_now[a .. h + 1]); - } - - test "click cursor and insertion agree beyond line and file ends" { - for ([_]bool{ false, true }) |already_insert| { - for ([_]bool{ false, true }) |bottom_tag| { - for ([_]struct { text: []const u8, click_row: u16, row: i32, col: i32, result: []const u8 }{ - .{ .text = "abc\nxy", .click_row = 0, .row = 0, .col = 3, .result = "abcZ\nxy" }, - .{ .text = "abc\nxy", .click_row = 5, .row = 1, .col = 2, .result = "abc\nxyZ" }, - .{ .text = "abc\n", .click_row = 5, .row = 1, .col = 0, .result = "abc\nZ" }, - .{ .text = "", .click_row = 5, .row = 0, .col = 0, .result = "Z" }, - }) |case| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - p.settings.tag_bottom = bottom_tag; - const pane = try p.setTestFile(case.text); - p.presentation.enabled = false; - if (already_insert) p.update(.{ .key = .{ .cp = 'i' } }); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = bodyY(p, rect); - click(p, .left, x + 15, y + case.click_row); - try std.testing.expectEqual(case.row, pane.body.cur_row); - try std.testing.expectEqual(case.col, pane.body.cur_col); - const clicked = (try p.render(frame.allocator())).cursor orelse return error.MissingClickCursor; - try std.testing.expectEqual(x + @as(u16, @intCast(case.col)), clicked.x); - try std.testing.expectEqual(y + @as(u16, @intCast(case.row)), clicked.y); - if (!already_insert) p.update(.{ .key = .{ .cp = 'i' } }); - try std.testing.expectEqual(panes.Text.Mode.insert, pane.body.mode); - const insert = (try p.render(frame.allocator())).cursor orelse return error.MissingInsertCursor; - try std.testing.expectEqual(clicked.x, insert.x); - try std.testing.expectEqual(clicked.y, insert.y); - p.update(.{ .key = .{ .cp = 'Z', .text = "Z" } }); - try std.testing.expectEqualStrings(case.result, pane.file.?.content); - } - } - } - } - - test "click cursor snaps tab and wrapped wide glyph cells before insertion" { - for ([_]bool{ false, true }) |already_insert| { - for ([_]bool{ false, true }) |wrap| { - const source = if (wrap) "012345678901234567890123456789界tail\n" else "\t界tail\n"; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 24, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile(source); - p.settings.wrap = wrap; - p.presentation.enabled = false; - if (already_insert) p.update(.{ .key = .{ .cp = 'i' } }); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - const surface = try p.render(frame.allocator()); - var target: ?struct { x: u16, y: u16 } = null; - for (0..surface.rows) |row| for (0..surface.cols) |col| { - if (std.mem.eql(u8, surface.at(@intCast(col), @intCast(row)).grapheme(), "界")) - target = .{ .x = @intCast(col), .y = @intCast(row) }; - }; - const glyph = target orelse return error.MissingWideGlyph; - if (wrap) try std.testing.expect(glyph.y > p.rects[0].y + pardes.BOX_H); - click(p, .left, glyph.x + 1, glyph.y); - const byte = std.mem.indexOf(u8, source, "界").?; - try std.testing.expectEqual(@as(i32, @intCast(byte)), pane.body.cur_col); - const clicked = (try p.render(frame.allocator())).cursor orelse return error.MissingClickCursor; - try std.testing.expectEqual(glyph.x, clicked.x); - try std.testing.expectEqual(glyph.y, clicked.y); - if (!already_insert) p.update(.{ .key = .{ .cp = 'i' } }); - try std.testing.expectEqual(panes.Text.Mode.insert, pane.body.mode); - const insert = (try p.render(frame.allocator())).cursor orelse return error.MissingInsertCursor; - try std.testing.expectEqual(clicked.x, insert.x); - try std.testing.expectEqual(clicked.y, insert.y); - p.update(.{ .key = .{ .cp = 'Z', .text = "Z" } }); - const expected = try std.fmt.allocPrint(p.gpa, "{s}Z{s}", .{ source[0..byte], source[byte..] }); - defer p.gpa.free(expected); - try std.testing.expectEqualStrings(expected, pane.file.?.content); - } - } - } - test "click operands and hover follow the scrolled source selection" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 14 }); defer p.deinit(); @@ -1387,39 +1234,6 @@ const JumpSelectionTests = struct { try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); } - test "a B1 sweep over several lines is a stream, as acme's, in a body and a tag" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("alpha\nbravo\ncharlie\n"); - var frame = std.heap.ArenaAllocator.init(p.gpa); - defer frame.deinit(); - _ = try p.render(frame.allocator()); - const rect = p.rects[0]; - const x = rect.x + config.GUTTER + panes.File.gutterWidth(pane); - const y = p.bodyTop(pane, rect); - // From the press to the end of its line, the line between whole, the - // last from its start: no line numbers, no rectangle. - drag(p, x + 2, y, x + 1, y + 2); - try std.testing.expectEqualStrings("pha\nbravo\nch", pardes.exec.heldSelection(p, 0).?); - // Upward, the same text. - drag(p, x + 1, y + 2, x + 2, y); - try std.testing.expectEqualStrings("pha\nbravo\nch", pardes.exec.heldSelection(p, 0).?); - // A tag of several rows sweeps the same way. - if (pane.tag.own) |own| p.gpa.free(own); - pane.tag.own = try p.gpa.dupe(u8, "Save Del\nLook Edit"); - p.sync(); - _ = try p.render(frame.allocator()); - p.sync(); - try std.testing.expectEqual(@as(u16, 2), pane.tag_rows); - const sl: panes.Pane.Sel = .{ .state = .done, .r0 = 0, .c0 = 3, .r1 = 1, .c1 = 1 }; - const text = try pardes.edit.paneText(p, pane); - const swept = try pardes.edit.selectionText(p, pane, sl); - const first_line = text[0 .. std.mem.indexOfScalar(u8, text, '\n') orelse text.len]; - const second = std.mem.sliceTo(text[first_line.len + 1 ..], '\n'); - const want = try std.fmt.allocPrint(frame.allocator(), "{s}\n{s}", .{ first_line[3..], second[0..@min(2, second.len)] }); - try std.testing.expectEqualStrings(want, swept); - } - test "jump selection clears copied sweeps in both history directions" { for ([_]bool{ false, true }) |reverse| try exercise("alpha\nbravo\n", false, reverse, 1, 3, "lpha\nbrav"); diff --git a/test/session.zig b/test/session.zig index 8b3c5c41..b55987eb 100644 --- a/test/session.zig +++ b/test/session.zig @@ -182,6 +182,12 @@ pub const Session = struct { fn pardesOf(s: *Session) *Pardes { return @ptrCast(@alignCast(s.core)); } + + // panes: + /// Where `text` is first drawn on the screen: its column and row. + pub fn where(s: *Session, text: []const u8) !struct { usize, usize } { + return s.find(text); + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { |
