summaryrefslogtreecommitdiff
path: root/test/consumer/one.zig
blob: 4a1138420583983cdd580ceb9d704fc53b48c1a3 (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
//! `pardes-syntax` built with one language named (`.languages`, build.zig
//! here: the build's loop over pardes's manifest makes one of these a
//! language): that language and what it needs (the manifest's `needs`) are
//! compiled in, and no other.
const std = @import("std");
const ps = @import("pardes-syntax");
const language = @import("language").name;

test "one language named: it and what it needs are in the build, no other grammar is linked" {
    const gpa = std.testing.allocator;
    ps.start(gpa);
    defer ps.stop();
    const g = ps.grammars;
    const named = comptime named: {
        @setEvalBranchQuota(10_000);
        break :named g.all[g.byName(language).?];
    };
    // This test program's own bytes (a Debug build keeps its symbols): a
    // name is spelled whole only in its symbol table.
    const self = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "/proc/self/exe", gpa, .unlimited);
    defer gpa.free(self);
    var name: [64]u8 = undefined;
    inline for (g.all) |other| {
        const in = comptime std.mem.eql(u8, other.name, named.name) or for (named.needs) |n| {
            if (std.mem.eql(u8, n, other.name)) break true;
        } else false;
        // the parser itself, by its symbol in this program's symbol table:
        // linked, or not in it
        const symbol = try std.fmt.bufPrint(&name, "\x00tree_sitter_{s}\x00", .{other.name});
        try std.testing.expectEqual(in, std.mem.indexOf(u8, self, symbol) != null);
        if (other.exts.len > 0) try std.testing.expectEqual(in, ps.supportsPath(ps.pathFor(other.name).?));
    }

    // what two of them colour: rust alone, and markdown with the inline
    // grammar it brings (its bold is markdown_inline's)
    for ([_]struct { []const u8, []const u8, []const u8, ps.Syn }{
        .{ "rust", "fn main() { let x = 1; }\n", "fn", .keyword },
        .{ "markdown", "# Title\n\nsome **bold** text\n", "bold", .comment },
    }) |case| if (std.mem.eql(u8, case[0], named.name)) {
        const styles = try ps.highlightFileRange(gpa, ps.pathFor(language).?, case[1], 0, case[1].len);
        defer gpa.free(styles);
        const at = std.mem.indexOf(u8, case[1], case[2]).?;
        for (styles[at..][0..case[2].len]) |s| try std.testing.expectEqual(case[3], @as(ps.Syn, @fromBackingInt(s)));
    };
}