summaryrefslogtreecommitdiff
path: root/test/behaviour/rules.zig
blob: 80f94406144c97d5eb0a12db2c6790b3ae838eed (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! docs/typ/reference.typ, Rules for every file <rules>.
const std = @import("std");
const Session = @import("session").Session;

test "rules: a control character in a write to any ctl refuses the whole write, and a quoted line shows a tab as a blank" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const n = try s.focused();
    // The first column's serial, /layout's first word.
    const col = std.mem.sliceTo(try s.read("/layout"), ' ');
    for ([_][]const u8{ "/ctl", s.at("/pane/{d}/ctl", .{n}), s.at("/col/{s}/ctl", .{col}) }) |ctl| {
        try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Wrap off\nbo\x01gus\n"));
        try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said);
    }
    try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{n}), "bogus\tword\n"));
    // The offending word alone is quoted: a tab ends it.
    try std.testing.expect(std.mem.indexOf(u8, s.said, "\"bogus\"") != null);
    try std.testing.expect(std.mem.indexOfScalar(u8, s.said, '\t') == null);
}

test "rules: a write refused for a control character takes its line with it: the start a write before held, and the rest the next write brings" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const ctl = try s.hold("/ctl", 1);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ctl, "bogus"));
    try std.testing.expectEqual(@as(u16, 22), try s.writeFid(ctl, "wo\x01rd"));
    // The refused line's rest goes through its newline unread; what follows
    // is a line of its own, which runs at the close.
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ctl, "tail\nnext"));
    s.clunk(ctl);
    const log = try s.read("/log");
    try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, log, "err "));
    try std.testing.expect(std.mem.indexOf(u8, log, "bogus") == null);
    try std.testing.expect(std.mem.indexOf(u8, log, "tail") == null);
    try std.testing.expect(std.mem.indexOf(u8, log, "\"next\"") != null);
}

test "rules: body reads at any offset and writes append; a stat says its length, and its version moves with each edit" {
    var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
    defer s.deinit();
    const body = s.at("/pane/{d}/body", .{try s.focused()});
    try std.testing.expectEqualStrings("two\n", try s.readAt(body, 4, 100));
    try std.testing.expectEqualStrings("wo", try s.readAt(body, 5, 2));
    try std.testing.expectEqualStrings("", try s.readAt(body, 999, 2));
    try s.write(body, "three\n");
    try s.expect(body, "one\ntwo\nthree\n");
    // A rune cut at the write's end is not taken.
    try s.write(body, "a\xC3");
    try s.expect(body, "one\ntwo\nthree\na");
    const st = try s.stat(body);
    try std.testing.expectEqual(@as(u64, "one\ntwo\nthree\na".len), st.size);
    try std.testing.expectEqual(@as(u16, 0o666), st.mode);
    try s.write(body, "more\n");
    try std.testing.expect((try s.stat(body)).version != st.version);
    try std.testing.expectEqual(@as(u32, 0), (try s.stat(s.at("/pane/{d}/tag", .{try s.focused()}))).version);
}

test "rules: every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const n = try s.focused();
    for ([_][2][]const u8{
        .{ "/exec", "echo a\x7fb\n" },
        .{ "/look", "a\x7fb\n" },
    }) |c| {
        try std.testing.expectEqual(@as(u16, 22), try s.refused(c[0], c[1]));
        try std.testing.expect(std.mem.indexOf(u8, s.said, "control character") != null);
    }
    try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/name", .{n}), "\n"));
    try std.testing.expect(std.mem.indexOf(u8, s.said, "an empty name") != null);
    const log = try s.hold("/log", 2);
    try std.testing.expectEqual(@as(u16, 22), try s.writeFid(log, "bogus\n"));
    try std.testing.expect(std.mem.indexOf(u8, s.said, "takes `follow`") != null);
    s.clunk(log);
    const text = try s.read("/log");
    try std.testing.expect(std.mem.indexOf(u8, text, "Invalid argument") == null);
    try std.testing.expect(std.mem.indexOf(u8, text, "an empty name") != null);
}