chore: zig test harness setup for parsing
This commit is contained in:
parent
d4546d58be
commit
72b686d750
6 changed files with 111 additions and 41 deletions
110
src/parser_tests.zig
Normal file
110
src/parser_tests.zig
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
const std = @import("std");
|
||||
const Ast = @import("./Ast.zig");
|
||||
|
||||
test "parser: empty file" {
|
||||
try testEqual(
|
||||
\\
|
||||
,
|
||||
\\File "<STDIN>"
|
||||
\\
|
||||
,
|
||||
);
|
||||
}
|
||||
|
||||
test "parser: \"Hello World\"" {
|
||||
try testEqual(
|
||||
\\Hello, world!
|
||||
,
|
||||
\\File "<STDIN>"
|
||||
\\`--BlockStmt <line:1, line:1>
|
||||
\\ `--ContentStmt <line:1, col:1:14>
|
||||
\\ `--Content <col:1, col:14>
|
||||
\\ `--StringLiteral `Hello, world!` <col:1, col:14>
|
||||
\\
|
||||
,
|
||||
);
|
||||
}
|
||||
|
||||
test "parser: expression statements" {
|
||||
try testEqual(
|
||||
\\~ (-1 + 2) * 3 - -4
|
||||
,
|
||||
\\File "<STDIN>"
|
||||
\\`--BlockStmt <line:1, line:1>
|
||||
\\ `--ExprStmt <line:1, col:3:20>
|
||||
\\ `--SubtractExpr <col:4, col:20>
|
||||
\\ |--MultiplyExpr <col:4, col:15>
|
||||
\\ | |--AddExpr <col:4, col:10>
|
||||
\\ | | |--NegateExpr <col:4, col:6>
|
||||
\\ | | | `--NumberLiteral `1` <col:5, col:6>
|
||||
\\ | | `--NumberLiteral `2` <col:9, col:10>
|
||||
\\ | `--NumberLiteral `3` <col:14, col:15>
|
||||
\\ `--NegateExpr <col:18, col:20>
|
||||
\\ `--NumberLiteral `4` <col:19, col:20>
|
||||
\\
|
||||
,
|
||||
);
|
||||
}
|
||||
|
||||
test "parser: temporary variables" {
|
||||
try testEqual(
|
||||
\\~ temp a = 1 + 2 + 3
|
||||
\\~ temp b = a + 1
|
||||
\\{b}
|
||||
,
|
||||
\\File "<STDIN>"
|
||||
\\`--BlockStmt <line:1, line:3>
|
||||
\\ |--TempDecl <line:1, col:3:21>
|
||||
\\ | |--Identifier `a` <col:8, col:9>
|
||||
\\ | `--AddExpr <col:12, col:21>
|
||||
\\ | |--AddExpr <col:12, col:17>
|
||||
\\ | | |--NumberLiteral `1` <col:12, col:13>
|
||||
\\ | | `--NumberLiteral `2` <col:16, col:17>
|
||||
\\ | `--NumberLiteral `3` <col:20, col:21>
|
||||
\\ |--TempDecl <line:2, col:3:17>
|
||||
\\ | |--Identifier `b` <col:8, col:9>
|
||||
\\ | `--AddExpr <col:12, col:17>
|
||||
\\ | |--Identifier `a` <col:12, col:13>
|
||||
\\ | `--NumberLiteral `1` <col:16, col:17>
|
||||
\\ `--ContentStmt <line:3, col:1:4>
|
||||
\\ `--Content <col:1, col:4>
|
||||
\\ `--InlineLogicExpr <col:1, col:4>
|
||||
\\ `--Identifier `b` <col:2, col:3>
|
||||
\\
|
||||
,
|
||||
);
|
||||
}
|
||||
|
||||
test "parser: temporary assignment" {
|
||||
try testEqual(
|
||||
\\~ temp a = 123
|
||||
\\~ a = 321
|
||||
,
|
||||
\\File "<STDIN>"
|
||||
\\`--BlockStmt <line:1, line:2>
|
||||
\\ |--TempDecl <line:1, col:3:15>
|
||||
\\ | |--Identifier `a` <col:8, col:9>
|
||||
\\ | `--NumberLiteral `123` <col:12, col:15>
|
||||
\\ `--AssignStmt <line:2, col:3:10>
|
||||
\\ |--Identifier `a` <col:3, col:4>
|
||||
\\ `--NumberLiteral `321` <col:7, col:10>
|
||||
\\
|
||||
,
|
||||
);
|
||||
}
|
||||
|
||||
fn testEqual(source_bytes: [:0]const u8, expected_ast: []const u8) !void {
|
||||
const gpa = std.testing.allocator;
|
||||
var arena_allocator = std.heap.ArenaAllocator.init(gpa);
|
||||
defer arena_allocator.deinit();
|
||||
const arena = arena_allocator.allocator();
|
||||
|
||||
var allocating = std.io.Writer.Allocating.init(gpa);
|
||||
defer allocating.deinit();
|
||||
|
||||
const ast = try Ast.parse(gpa, arena, source_bytes, "<STDIN>", 0);
|
||||
try ast.render(gpa, &allocating.writer, .{
|
||||
.use_color = false,
|
||||
});
|
||||
return std.testing.expectEqualSlices(u8, expected_ast, allocating.written());
|
||||
}
|
||||
|
|
@ -6,5 +6,6 @@ test {
|
|||
_ = tokenizer;
|
||||
_ = Ast;
|
||||
_ = Story;
|
||||
_ = @import("parser_tests.zig");
|
||||
_ = @import("Story/runtime_tests.zig");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s
|
||||
|
||||
// CHECK: File "<STDIN>"
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s
|
||||
|
||||
// CHECK: File "<STDIN>"
|
||||
// CHECK-NEXT: `--BlockStmt <line:16, line:16>
|
||||
// CHECK-NEXT: `--ExprStmt <line:16, col:3:20>
|
||||
// CHECK-NEXT: `--SubtractExpr <col:4, col:20>
|
||||
// CHECK-NEXT: |--MultiplyExpr <col:4, col:15>
|
||||
// CHECK-NEXT: | |--AddExpr <col:4, col:10>
|
||||
// CHECK-NEXT: | | |--NegateExpr <col:4, col:6>
|
||||
// CHECK-NEXT: | | | `--NumberLiteral `1` <col:5, col:6>
|
||||
// CHECK-NEXT: | | `--NumberLiteral `2` <col:9, col:10>
|
||||
// CHECK-NEXT: | `--NumberLiteral `3` <col:14, col:15>
|
||||
// CHECK-NEXT: `--NegateExpr <col:18, col:20>
|
||||
// CHECK-NEXT: `--NumberLiteral `4` <col:19, col:20>
|
||||
|
||||
~ (-1 + 2) * 3 - -4
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s
|
||||
|
||||
// CHECK: File "<STDIN>"
|
||||
// CHECK-NEXT: `--BlockStmt <line:9, line:9>
|
||||
// CHECK-NEXT: `--ContentStmt <line:9, col:1:14>
|
||||
// CHECK-NEXT: `--Content <col:1, col:14>
|
||||
// CHECK-NEXT: `--StringLiteral `Hello, world!` <col:1, col:14>
|
||||
|
||||
Hello, world!
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s
|
||||
|
||||
// CHECK: File "<STDIN>"
|
||||
// CHECK-NEXT: `--BlockStmt <line:12, line:13>
|
||||
// CHECK-NEXT: |--TempDecl <line:12, col:3:15>
|
||||
// CHECK-NEXT: | |--Identifier `a` <col:8, col:9>
|
||||
// CHECK-NEXT: | `--NumberLiteral `123` <col:12, col:15>
|
||||
// CHECK-NEXT: `--AssignStmt <line:13, col:3:10>
|
||||
// CHECK-NEXT: |--Identifier `a` <col:3, col:4>
|
||||
// CHECK-NEXT: `--NumberLiteral `321` <col:7, col:10>
|
||||
|
||||
~ temp a = 123
|
||||
~ a = 321
|
||||
Loading…
Add table
Add a link
Reference in a new issue