summaryrefslogtreecommitdiff
path: root/test/behaviour/edit.zig
blob: bfb1753313fb29017aa1e7287c7a84e76f459870 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! docs/typ/reference.typ, Edit <edit>.
const std = @import("std");
const Session = @import("session").Session;

test "edit: a write of an Edit block carries it whole, to a pane's ctl, the root's ctl and exec" {
    var s = try Session.initFile(std.testing.allocator, "foo x\nfoo y\n");
    defer s.deinit();
    const n = try s.focused();
    const body = s.at("/pane/{d}/body", .{n});
    try s.write(s.at("/pane/{d}/ctl", .{n}), "Edit ,x/foo/{\ni/[/\na/]/\n}\n");
    try s.expect(body, "[foo] x\n[foo] y\n");
    try s.write("/ctl", "Edit ,x/\\[/{\nc/</\n}\n");
    try s.expect(body, "<foo] x\n<foo] y\n");
    try s.write(s.at("/pane/{d}/exec", .{n}), "Edit $a\nend\n.\n");
    try s.expect(body, "<foo] x\n<foo] y\nend\n");
    // A group still open when its open closes fails there, an err in the
    // log, and nothing after it runs as a line.
    try s.write(s.at("/pane/{d}/ctl", .{n}), "Edit ,x/foo/{\nd\n");
    try s.expect(body, "<foo] x\n<foo] y\nend\n");
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "unmatched") != null);
}

test "edit: a bad command inside an Edit block written whole is named, not the } after it" {
    var s = try Session.initFile(std.testing.allocator, "abc\n");
    defer s.deinit();
    const n = try s.focused();
    try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{n}), "Edit {\n,s/a/b/\nzq\n}\n") != 0);
    try std.testing.expect(std.mem.indexOf(u8, s.said, "\"}\"") == null);
    try std.testing.expect(std.mem.indexOf(u8, s.said, "z") != null);
    try s.expect(s.at("/pane/{d}/body", .{n}), "abc\n");
}

test "edit: Edit through a pane's ctl renames every foo in one undo step, and a failed one changes nothing" {
    var s = try Session.initFile(std.testing.allocator, "foo x foo y foo\n");
    defer s.deinit();
    const n = try s.focused();
    const ctl = s.at("/pane/{d}/ctl", .{n});
    const body = s.at("/pane/{d}/body", .{n});
    try s.write(ctl, "Edit ,x/foo/c/foobar/\n");
    try s.expect(body, "foobar x foobar y foobar\n");
    try std.testing.expectEqual(@as(u16, 5), try s.refused(ctl, "Edit ,s/nothing/x/\n"));
    try std.testing.expect(std.mem.startsWith(u8, s.said, "Edit: no substitution"));
    try s.expect(body, "foobar x foobar y foobar\n");
    // The whole rename is one undo step.
    try s.write(ctl, "Undo");
    try s.expect(body, "foo x foo y foo\n");
}