summaryrefslogtreecommitdiff
path: root/test/behaviour/settings.zig
blob: 5ba227bc79d718c715e7b2499e3bb6c0d455e244 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175


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;
}

/// The root ctl's line for `word`.
fn setting(s: *Session, word: []const u8) ![]const u8 {
    var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n');
    while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line;
    return error.NoSuchSetting;
}

test "settings: Pager is pardes by default, off taken, anything else refused with its choices" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try std.testing.expectEqualStrings("Pager pardes", try setting(&s, "Pager"));
    try s.write("/ctl", "Pager off\n");
    try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager"));
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pager less\n"));
    try std.testing.expectEqualStrings("bad value in control message; takes pardes, off \"less\"", s.said);
    try std.testing.expectEqualStrings("Pager off", try setting(&s, "Pager"));
}

test "settings: Shell refuses a path that is no executable, and bare it goes back to the default" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const default = try setting(&s, "Shell");
    try std.testing.expect(try s.refused("/ctl", "Shell /nonexistent/zzsh\n") != 0);
    try std.testing.expect(has(s.said, "Shell: shell \"/nonexistent/zzsh\" not found"));
    try std.testing.expect(has(try s.read("/log"), "shell \"/nonexistent/zzsh\" not found"));
    // Its words say a directory, which a mount reads as EISDIR too.
    try std.testing.expectEqual(@as(u16, 21), try s.refused("/ctl", "Shell /etc\n"));
    try std.testing.expect(has(try s.read("/log"), "Shell: not a shell: /etc is a directory"));
    try std.testing.expectEqualStrings(default, try setting(&s, "Shell"));
    try s.write("/ctl", "Shell /bin/sh\n");
    try std.testing.expectEqualStrings("Shell /bin/sh", try setting(&s, "Shell"));
    try s.write("/ctl", "Shell\n");
    try std.testing.expect(has(try s.read("/log"), "Shell: the default again, "));
    try std.testing.expectEqualStrings(default, try setting(&s, "Shell"));
}

test "settings: Pet is the window's: a terminal's view refuses it, from a pane and the root; a window takes only its pets" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    // Which build, from its options, not from the answer: a terminal's view
    // taking Pet must fail here, not pass as a window.
    if (comptime @import("pardes_config").platform == .gui) {
        try s.write("/ctl", "Pet cat\n");
        // A window's: dog is no pet of its, and off is.
        try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet cat\n") != null);
        _ = try s.refused("/ctl", "Pet dog\n");
        try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet cat\n") != null);
        try s.write("/ctl", "Pet frog\n");
        try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet frog\n") != null);
        try s.write("/ctl", "Pet off\n");
        try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPet off\n") != null);
        return;
    }
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/ctl", "Pet cat\n"));
    try std.testing.expect(std.mem.startsWith(u8, s.said, "Pet is GUI-only, invalid here"));
    try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Pet cat\n"));
    try std.testing.expectEqualStrings("Pet is GUI-only, invalid here", s.said);
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "Pet ") == null);
}

fn expectHas(text: []const u8, part: []const u8) !void {
    if (std.mem.indexOf(u8, text, part) == null) {
        std.debug.print("not in it: {s}\n", .{part});
        return error.TestExpectedText;
    }
}

test "settings: DumpConfig reports the settings in its one +DumpConfig pane, a second refreshing it" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const text = try s.focused();
    const exec = s.at("/pane/{d}/exec", .{text});
    try s.write(exec, "DumpConfig\n");
    const out = try s.focused();
    try std.testing.expect(out != text);
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+DumpConfig ", .{out})) != null);
    const report = try s.read(s.at("/pane/{d}/body", .{out}));
    for ([_][]const u8{ "Theme orchard\n", "Last shell spawned: (none)\n", "Shell change pending: on\n", "\nShell ", "LocationsConfig ", "no per-user config path" }) |line| try expectHas(report, line);
    try s.write("/ctl", "Theme acme\n");
    try s.write("/focus", s.at("{d}", .{text}));
    try s.write(exec, "DumpConfig\n");
    try std.testing.expectEqual(out, try s.focused());
    try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/index"), "/+DumpConfig "));
    try expectHas(try s.read(s.at("/pane/{d}/body", .{out})), "Theme acme\n");
}

test "settings: Config without a configuration directory says so and opens nothing" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const index = try s.read("/index");
    try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/exec", .{try s.focused()}), "Config\n"));
    try s.expect("/index", index);
    try expectHas(try s.read("/log"), "Config: no startup file here: no configuration directory");
}

const syntax = @import("pardes_config").syntax_highlighting;

/// The style /screen draws the cell `into` cells into the first place
/// `text` is drawn.
fn styleOf(s: *Session, text: []const u8, into: usize) ![]const u8 {
    const x, const y = try s.where(text);
    const screen = try s.screen();
    const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]);
    return (try s.styles())[y * cols + x + into];
}

/// How many ` fn ` and ` if ` keywords the screen draws, each of whose two
/// cells must be drawn in `want`.
fn keywordsIn(s: *Session, want: []const u8) !usize {
    const screen = try s.screen();
    const styles = try s.styles();
    const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]);
    var seen: usize = 0;
    var rows = std.mem.splitScalar(u8, screen, '\n');
    var y: usize = 0;
    while (rows.next()) |row| : (y += 1) for ([_][]const u8{ " fn ", " if " }) |word| {
        var from: usize = 0;
        while (std.mem.indexOfPos(u8, row, from, word)) |i| : (from = i + 1) {
            const x = try std.unicode.utf8CountCodepoints(row[0..i]) + 1;
            for (0..2) |k| try std.testing.expectEqualStrings(want, styles[y * cols + x + k]);
            seen += 1;
        }
    };
    return seen;
}

test "settings: Colors off draws a source plain, and on again colours its keywords, the layout as it was, an edit made while off included" {
    if (!syntax) return error.SkipZigTest;
    for ([_]bool{ false, true }) |edit_while_off| {
        var s = try Session.initFile(std.testing.allocator, "pub fn first() void { if (true) {} }\n");
        defer s.deinit();
        const n = try s.focused();
        try s.write(s.at("/pane/{d}/name", .{n}), "/colors.zig\n");
        var ident: []const u8 = "first";
        if (edit_while_off) {
            try std.testing.expect(!std.mem.eql(u8, try styleOf(&s, " fn ", 1), try styleOf(&s, "first", 0)));
            try s.write("/ctl", "Colors off\n");
            try s.replace(s.at("/pane/{d}/body", .{n}), "pub fn edited() void { if (false) {} }\n");
            ident = "edited";
        } else try s.write("/ctl", "Colors off\n");
        try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, try styleOf(&s, ident, 0)));
        const layout = try s.read("/layout");
        try s.write("/ctl", "Colors on\n");
        try s.expect("/layout", layout);
        const kw = try styleOf(&s, " fn ", 1);
        try std.testing.expect(!std.mem.eql(u8, kw, try styleOf(&s, ident, 0)));
        try std.testing.expect(!has(kw, "\"bold\":true"));
        try std.testing.expectEqual(@as(usize, 2), try keywordsIn(&s, kw));
    }
}

test "settings: SyntaxBold keywords stay coloured and bold in every row a pane grows into" {
    if (!syntax) return error.SkipZigTest;
    var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][33]u8, @splat("pub fn f() void { if (true) {} }\n".*))));
    defer s.deinit();
    try s.write(s.at("/pane/{d}/name", .{try s.focused()}), "/resize.zig\n");
    try s.write("/ctl", "SyntaxBold on\n");
    s.detach();
    try s.write("/ctl", "size 80 8\n");
    const kw = try styleOf(&s, " fn ", 1);
    try std.testing.expect(has(kw, "\"bold\":true"));
    for ([_]usize{ 50, 12, 70 }) |rows| {
        try s.write("/ctl", s.at("size 80 {d}\n", .{rows}));
        try std.testing.expect(try keywordsIn(&s, kw) >= 2 * (rows - 4));
    }
}