diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/behaviour.zig | 1 | ||||
| -rw-r--r-- | test/behaviour/follow.zig | 204 | ||||
| -rw-r--r-- | test/snapshots/builtins.golden | 8 | ||||
| -rw-r--r-- | test/snapshots/follow.golden | 61 | ||||
| -rw-r--r-- | test/snapshots/follow.snap | 20 | ||||
| -rw-r--r-- | test/snapshots/leader.golden | 7 | ||||
| -rw-r--r-- | test/snapshots/ttyhelp.golden | 12 |
7 files changed, 300 insertions, 13 deletions
diff --git a/test/behaviour.zig b/test/behaviour.zig index de0cb83c..5b31b6d9 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -12,6 +12,7 @@ test { _ = @import("behaviour/edit.zig"); _ = @import("behaviour/event.zig"); _ = @import("behaviour/flags.zig"); + _ = @import("behaviour/follow.zig"); _ = @import("behaviour/from_acme.zig"); _ = @import("behaviour/keys.zig"); _ = @import("behaviour/language_servers.zig"); diff --git a/test/behaviour/follow.zig b/test/behaviour/follow.zig new file mode 100644 index 00000000..5f2acb02 --- /dev/null +++ b/test/behaviour/follow.zig @@ -0,0 +1,204 @@ +//! docs/typ/reference.typ, Follow <follow>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// A session on a 300-line file, `L001` to `L300`, in a window wide enough +/// for three columns' tags on one row each. +fn init() !Session { + var text: [300 * 5]u8 = undefined; + for (0..300) |i| _ = try std.fmt.bufPrint(text[i * 5 ..][0..5], "L{d:0>3}\n", .{i + 1}); + var s = try Session.initFile(std.testing.allocator, &text); + s.detach(); + try s.write("/ctl", "size 200 30\n"); + return s; +} + +const Page = struct { x: usize, first: usize, last: usize, rows: usize }; + +/// Each pane's lines as /screen draws them, left to right: the labels +/// drawn at one column of cells are one pane's. +fn pages(s: *Session) ![]Page { + var out: std.ArrayList(Page) = .empty; + var rows = std.mem.splitScalar(u8, try s.screen(), '\n'); + while (rows.next()) |row| { + var i: usize = 0; + while (std.mem.indexOfScalarPos(u8, row, i, 'L')) |at| : (i = at + 1) { + if (at + 4 > row.len) break; + const n = std.fmt.parseInt(usize, row[at + 1 .. at + 4], 10) catch continue; + const x = try std.unicode.utf8CountCodepoints(row[0..at]); + for (out.items) |*p| { + if (p.x != x) continue; + p.first = @min(p.first, n); + p.last = @max(p.last, n); + p.rows += 1; + break; + } else try out.append(s.arena.allocator(), .{ .x = x, .first = n, .last = n, .rows = 1 }); + } + } + std.mem.sort(Page, out.items, {}, struct { + fn lt(_: void, a: Page, b: Page) bool { + return a.x < b.x; + } + }.lt); + return out.items; +} + +/// The pages go on one from another: each starts the line after the one +/// before it ends, every line drawn once. +fn expectConsecutive(ps: []const Page) !void { + for (ps) |p| try std.testing.expectEqual(p.last - p.first + 1, p.rows); + for (ps[1..], ps[0 .. ps.len - 1]) |p, before| try std.testing.expectEqual(before.last + 1, p.first); +} + +/// The pane the last Follow made: the newest serial in the index. +fn newest(s: *Session) !u32 { + var max: u32 = 0; + var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + while (lines.next()) |l| max = @max(max, try std.fmt.parseInt(u32, std.mem.sliceTo(l, ' '), 10)); + return max; +} + +/// The first line of the cursor's selection, 0-based (a label is 5 bytes). +fn line(s: *Session, pane: u32) !usize { + return (try s.range(s.at("/pane/{d}/dot", .{pane})))[0] / 5; +} + +test "follow: Follow puts the next page right of the chain, in a new column or at the end of the one there, Unfollow on its tag alone" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const second = try newest(&s); + // No column to the right: a new one; the keyboard stays on the head. + try std.testing.expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ try s.column(0), head, try s.column(1), second }), try s.columns()); + try std.testing.expectEqual(head, try s.focused()); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{second})), " Unfollow")); + try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{head})), "Unfollow")); + try expectConsecutive(try pages(&s)); + // A column right of the last page: the next goes at its end, from + // any page of the chain. + try s.write(s.at("/pane/{d}/exec", .{second}), "Newcol\n"); + const other = try s.newPane(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const third = try newest(&s); + try std.testing.expect(has(try s.columns(), s.at(":{d},{d}", .{ other, third }))); + // A twin out of the chain is refused one of its own. + try s.write(s.at("/pane/{d}/exec", .{head}), "Zerox\n"); + try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{try newest(&s)}), "Follow\n") != 0); + try std.testing.expect(has(s.said, "Follow: this text has a chain")); +} + +test "follow: two and three pages show consecutive lines; the wheel in any page moves them all" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + var ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 2), ps.len); + try std.testing.expectEqual(@as(usize, 1), ps[0].first); + try expectConsecutive(ps); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 3), ps.len); + try expectConsecutive(ps); + // Three notches down in the second page: the first follows. + for (0..3) |_| try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[1].x + 2, ps[1].x + 2 })); + const scrolled = try pages(&s); + try std.testing.expectEqual(@as(usize, 4), scrolled[0].first); + try expectConsecutive(scrolled); + // And up again in the last. + for (0..3) |_| try s.write("/mousein", s.at("m {d} 10 8\nm {d} 10 0\n", .{ ps[2].x + 2, ps[2].x + 2 })); + try std.testing.expectEqual(@as(usize, 1), (try pages(&s))[0].first); + try expectConsecutive(try pages(&s)); +} + +test "follow: the cursor past a page's last row takes the keyboard to the next page, and back above its first" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const second = try newest(&s); + const ps = try pages(&s); + const rows = ps[0].rows; + try s.keys(s.at("{d}j", .{rows - 1})); + try std.testing.expectEqual(head, try s.focused()); + try std.testing.expectEqual(rows - 1, try line(&s, head)); + try s.keys("j"); + try std.testing.expectEqual(second, try s.focused()); + try std.testing.expectEqual(rows, try line(&s, second)); + // Nothing scrolled: the pages are where they were. + try std.testing.expectEqualSlices(Page, ps, try pages(&s)); + try s.keys("k"); + try std.testing.expectEqual(head, try s.focused()); + try std.testing.expectEqual(rows - 1, try line(&s, head)); + // Past the chain's end the chain scrolls as one pane. + try s.keys(s.at("{d}j", .{2 * rows + 2})); + try std.testing.expectEqual(second, try s.focused()); + const moved = try pages(&s); + try std.testing.expect(moved[0].first > 1); + try expectConsecutive(moved); + // A dot written to the other page scrolls it there, the chain with it. + try s.write(s.at("/pane/{d}/dot", .{head}), "745 745"); + const shown = try pages(&s); + try std.testing.expect(shown[0].first <= 150 and 150 <= shown[0].last); + try expectConsecutive(shown); +} + +test "follow: Unfollow leaves a twin with its own scroll and closes the chain up; Del of the head makes the next one head" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const second = try newest(&s); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + const third = try newest(&s); + const was = try pages(&s); + try s.write(s.at("/pane/{d}/exec", .{second}), "Unfollow\n"); + try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{second})), "Unfollow")); + var ps = try pages(&s); + // The twin keeps its lines; the third follows the head now. + try std.testing.expectEqual(was[1], ps[1]); + try std.testing.expectEqual(was[0].last + 1, ps[2].first); + // A wheel in the twin moves it alone, as Zerox's does. + try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[1].x + 2, ps[1].x + 2 })); + const after = try pages(&s); + try std.testing.expectEqual(ps[0], after[0]); + try std.testing.expectEqual(ps[2], after[2]); + try std.testing.expectEqual(ps[1].first + 1, after[1].first); + try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{second}), "Unfollow\n") != 0); + // Del of the head: the third is a chain of one, which is no chain. + try s.write(s.at("/pane/{d}/ctl", .{second}), "Del\n"); + try s.write(s.at("/pane/{d}/exec", .{third}), "Follow\n"); + const fourth = try newest(&s); + try s.write(s.at("/pane/{d}/ctl", .{head}), "Del\n"); + try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{third})), "Unfollow")); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{fourth})), "Unfollow")); + ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 2), ps.len); + try expectConsecutive(ps); + try s.write(s.at("/pane/{d}/ctl", .{third}), "Del\n"); + try std.testing.expect(!has(try s.read(s.at("/pane/{d}/tag", .{fourth})), "Unfollow")); +} + +test "follow: a Dump and Restore keep the chain, still one" { + var s = try init(); + defer s.deinit(); + const head = try s.focused(); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + try s.write(s.at("/pane/{d}/exec", .{head}), "Follow\n"); + try s.write("/ctl", "Dump\n"); + const label = try s.arena.allocator().dupe(u8, std.mem.sliceTo(try s.read("/dumps"), ' ')); + try s.write(s.at("/pane/{d}/exec", .{head}), "Unfollow\n"); + try s.write("/ctl", s.at("Restore {s}\n", .{label})); + var ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 3), ps.len); + try expectConsecutive(ps); + for (0..2) |_| try s.write("/mousein", s.at("m {d} 10 16\nm {d} 10 0\n", .{ ps[2].x + 2, ps[2].x + 2 })); + ps = try pages(&s); + try std.testing.expectEqual(@as(usize, 3), ps[0].first); + try expectConsecutive(ps); +} diff --git a/test/snapshots/builtins.golden b/test/snapshots/builtins.golden index fd60bc74..7230f9b8 100644 --- a/test/snapshots/builtins.golden +++ b/test/snapshots/builtins.golden @@ -14,16 +14,16 @@ |10: 1 pardes builtins under SPC c |11: 2 |12: 3 SPC c d Delcol -|13: 4 SPC c j Joincol topbar -|14: 5 SPC c n Newcol topbar -|15: 6 +|13: 4 SPC c f Follow +|14: 5 SPC c j Joincol topbar +|15: 6 SPC c n Newcol topbar +|16: 7 == snap docs-group grid=120x67 cursor=7,10 |10: 1 pardes builtins under SPC h |12: 3 SPC h b Build |13: 4 SPC h c Changelog topbar |14: 5 SPC h m Messages |15: 6 SPC h t Tutor topbar -|16: 7 == snap tutor-from-index grid=120x67 cursor=7,17 |10: 1 pardes builtins under SPC h Tutor |16: /tmp/pardes-snap/builtins/cwd/+Tutor Save Term Collapse Del diff --git a/test/snapshots/follow.golden b/test/snapshots/follow.golden new file mode 100644 index 00000000..99ef20b9 --- /dev/null +++ b/test/snapshots/follow.golden @@ -0,0 +1,61 @@ +== snap three-pages grid=150x30 cursor=7,3 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Term Find Grep Joincol Delcol New Term Find Grep Joincol Delcol New Term Find Grep Joincol Delcol +| /tmp/pardes-snap/follow/cwd/big.txt Save Term Collapse Del /tmp/pardes-snap/follow/cwd/big.txt /tmp/pardes-snap/follow/cwd/big.txt +| 1 line 1 Follow Save Term Collapse Del Unfollow Save Term Collapse Del Unfollow +| 2 line 2 28 line 28 54 line 54 +| 3 line 3 29 line 29 55 line 55 +| 4 line 4 30 line 30 56 line 56 +| 5 line 5 31 line 31 57 line 57 +| 6 line 6 32 line 32 58 line 58 +| 7 line 7 33 line 33 59 line 59 +| 8 line 8 34 line 34 60 line 60 +| 9 line 9 35 line 35 61 line 61 +| 10 line 10 36 line 36 62 line 62 +| 11 line 11 37 line 37 63 line 63 +| 12 line 12 38 line 38 64 line 64 +| 13 line 13 39 line 39 65 line 65 +| 14 line 14 40 line 40 66 line 66 +| 15 line 15 41 line 41 67 line 67 +| 16 line 16 42 line 42 68 line 68 +| 17 line 17 43 line 43 69 line 69 +| 18 line 18 44 line 44 70 line 70 +| 19 line 19 45 line 45 71 line 71 +| 20 line 20 46 line 46 72 line 72 +| 21 line 21 47 line 47 73 line 73 +| 22 line 22 48 line 48 74 line 74 +| 23 line 23 49 line 49 75 line 75 +| 24 line 24 50 line 50 76 line 76 +| 25 line 25 51 line 51 77 line 77 +| 26 line 26 52 line 52 78 line 78 +| 27 line 27 53 line 53 79 line 79 +== snap hop grid=150x30 cursor=82,4 +|3: 1 line 1 Save Term Collapse Del Unfollow Save Term Collapse Del Unfollow +== snap wheel grid=150x30 cursor=82,4 +|3: 2 line 2 Save Term Collapse Del Unfollow Save Term Collapse Del Unfollow +|4: 3 line 3 29 line 29 55 line 55 +|5: 4 line 4 30 line 30 56 line 56 +|6: 5 line 5 31 line 31 57 line 57 +|7: 6 line 6 32 line 32 58 line 58 +|8: 7 line 7 33 line 33 59 line 59 +|9: 8 line 8 34 line 34 60 line 60 +|10: 9 line 9 35 line 35 61 line 61 +|11: 10 line 10 36 line 36 62 line 62 +|12: 11 line 11 37 line 37 63 line 63 +|13: 12 line 12 38 line 38 64 line 64 +|14: 13 line 13 39 line 39 65 line 65 +|15: 14 line 14 40 line 40 66 line 66 +|16: 15 line 15 41 line 41 67 line 67 +|17: 16 line 16 42 line 42 68 line 68 +|18: 17 line 17 43 line 43 69 line 69 +|19: 18 line 18 44 line 44 70 line 70 +|20: 19 line 19 45 line 45 71 line 71 +|21: 20 line 20 46 line 46 72 line 72 +|22: 21 line 21 47 line 47 73 line 73 +|23: 22 line 22 48 line 48 74 line 74 +|24: 23 line 23 49 line 49 75 line 75 +|25: 24 line 24 50 line 50 76 line 76 +|26: 25 line 25 51 line 51 77 line 77 +|27: 26 line 26 52 line 52 78 line 78 +|28: 27 line 27 53 line 53 79 line 79 +|29: 28 line 28 54 line 54 80 line 80 diff --git a/test/snapshots/follow.snap b/test/snapshots/follow.snap new file mode 100644 index 00000000..0e5d23a2 --- /dev/null +++ b/test/snapshots/follow.snap @@ -0,0 +1,20 @@ +# Follow (SPC c f) twice on a 300-line file: three pages in three columns, +# each going on the line after the one before ends; Unfollow on the two +# followers' tags only. Then j past the first page's last row takes the +# keyboard to the second page without scrolling, and a wheel notch in the +# last page moves all three. +lines big.txt 300 +start 30 150 big.txt +wait 8000 big.txt +stable 500 15000 +key space c f +stable 500 8000 +key space c f +stable 500 8000 +snap three-pages +text 27j +stable 500 8000 +snap hop +wheel down 120 12 +stable 500 8000 +snap wheel diff --git a/test/snapshots/leader.golden b/test/snapshots/leader.golden index 932e8a95..1895a833 100644 --- a/test/snapshots/leader.golden +++ b/test/snapshots/leader.golden @@ -90,9 +90,10 @@ |6: 1 pardes builtins under SPC c |7: 2 |8: 3 SPC c d Delcol -|9: 4 SPC c j Joincol topbar -|10: 5 SPC c n Newcol topbar -|11: 6 +|9: 4 SPC c f Follow +|10: 5 SPC c j Joincol topbar +|11: 6 SPC c n Newcol topbar +|12: 7 |34: |35: |36: diff --git a/test/snapshots/ttyhelp.golden b/test/snapshots/ttyhelp.golden index d0b90887..c3b4d57e 100644 --- a/test/snapshots/ttyhelp.golden +++ b/test/snapshots/ttyhelp.golden @@ -34,11 +34,11 @@ |12: 7 SPC R ClipReplace |13: 8 SPC Y ClipYankMain |14: 9 SPC c d Delcol -|15: 10 SPC c j Joincol topbar -|16: 11 SPC c n Newcol topbar -|17: 12 SPC d Del -|18: 13 SPC f c Config topbar -|19: 14 SPC f f Find topbar -|20: 15 SPC f g Grep topbar +|15: 10 SPC c f Follow +|16: 11 SPC c j Joincol topbar +|17: 12 SPC c n Newcol topbar +|18: 13 SPC d Del +|19: 14 SPC f c Config topbar +|20: 15 SPC f f Find topbar == snap leaderworks grid=100x31 cursor=7,6 |3: $ |
