aboutsummaryrefslogtreecommitdiff
path: root/src/z/parser/js/root.zig
blob: 6b2d5c7ddce0617e5184f24f950a9d5d82c56f52 (plain)
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
const std = @import("std");

pub const lexical_grammar = @import("lexical_grammar.zig");
pub const ast = @import("ast/core.zig");

test "simple literal" {
    comptime {
        _ = try ast.Program.parse("42;");
    }
}

test "variable declaration" {
    comptime {
        _ = try ast.Program.parse("var x = 1;");
    }
}

test "function declaration" {
    comptime {
        _ = try ast.Program.parse("function foo() { return 1; }");
    }
}

test "if statement" {
    comptime {
        _ = try ast.Program.parse("if (x > 0) { x = 1; } else { x = 2; }");
    }
}

test "binary expression" {
    comptime {
        _ = try ast.Program.parse("a + b * c;");
    }
}

test "arrow function" {
    comptime {
        _ = try ast.Program.parse("const f = (x) => x + 1;");
    }
}

test "class declaration" {
    comptime {
        _ = try ast.Program.parse("class Foo { constructor() { } }");
    }
}

test "for loop" {
    comptime {
        _ = try ast.Program.parse("for (var i = 0; i < 10; i++) { break; }");
    }
}

test "try catch" {
    comptime {
        _ = try ast.Program.parse("try { x; } catch (e) { y; }");
    }
}

test "multiple statements" {
    comptime {
        _ = try ast.Program.parse("var a = 1; var b = 2; var c = a + b;");
    }
}

test "comprehensive fixture" {
    comptime {
        _ = try ast.Program.parse(@embedFile("test_fixture.js"));
    }
}

test "format fixture" {
    comptime {
        var buf: [65536]u8 = undefined;
        const ast1 = try ast.Program.parse(@embedFile("test_fixture.js"));
        var f = ast.Formatter{ .buf = buf[0..], .pos = 0, .indent_level = 0, .options = .{} };
        ast1.fmt(&f);
        const formatted = buf[0..f.pos];
        try std.testing.expect(formatted.len > 0);
    }
}

test "minify" {
    comptime {
        var buf: [65536]u8 = undefined;
        const ast1 = try ast.Program.parse("function foo() { var x = 1; return x; }");
        var f = ast.Formatter{ .buf = buf[0..], .pos = 0, .indent_level = 0, .options = .{ .minify = true } };
        ast1.fmt(&f);
        const minified = buf[0..f.pos];
        try std.testing.expect(std.mem.indexOf(u8, minified, "\n") == null);
        try std.testing.expect(minified.len < 50);
    }
}

test {
    _ = std.testing.refAllDecls(@This());
}