summaryrefslogtreecommitdiff
path: root/test/behaviour/callstack.zig
blob: 4244803136370b0d3bc256fe25acb5a5d88e6e24 (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
//! docs/typ/guide.typ, Call hierarchy <callstack>, asked of the scripted
//! language server (test/lspmock.zig): `add` in lib.rs is called twice by
//! `main` in main.rs, and `main` by itself.
const std = @import("std");
const Session = @import("session").Session;

/// main.rs and lib.rs in a directory of their own, main.rs shown with its
/// cursor on the first call of `add`; its pane's serial.
fn openMain(s: *Session, tmp: *std.testing.TmpDir) !u32 {
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "main.rs", .data = "mod lib;\n\nfn main() {\n    let x = add(1, 2);\n    let y = add(3, 4);\n    let _ = x + y;\n}\n" });
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "lib.rs", .data = "// mock fixture\npub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n" });
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    return s.lookFile(s.at("{s}/main.rs:4:13", .{dir}));
}

test "callstack: Callstack opens +Callstack, the outermost callers first, each with its signature and its call sites, a recursion marked" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    const main = try openMain(&s, &tmp);
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Callstack\n");
    const tree = try s.focused();
    try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/name", .{tree})), "/+Callstack\n"));
    try s.expect(s.at("/pane/{d}/body", .{tree}),
        \\╰──┬ main.rs:3:4-7
        \\   │ fn main() {
        \\   ├─ main.rs:4:13-15
        \\   │  let x = add(1, 2);
        \\   ├─ main.rs:5:13-15
        \\   │  let y = add(3, 4);
        \\   ╰──┬ main.rs:3:4-7
        \\      │ fn main() {  ↻ recursion
        \\      ├─ main.rs:4:13-15
        \\      │  let x = add(1, 2);
        \\      ╰─ main.rs:5:13-15
        \\         let y = add(3, 4);
        \\
    );
}

test "callstack: CallstackDepth bounds the walk, its last row inviting a Callstack from a function above" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    const main = try openMain(&s, &tmp);
    try s.write("/ctl", "CallstackDepth 1\n");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Callstack\n");
    try s.expect(s.at("/pane/{d}/body", .{try s.focused()}),
        \\╰──┬ main.rs:3:4-7
        \\   │ fn main() {
        \\   ├─ main.rs:4:13-15
        \\   │  let x = add(1, 2);
        \\   ╰─ main.rs:5:13-15
        \\      let y = add(3, 4);
        \\… continue from a function above with Callstack
        \\
    );
}