From 72b686d750dbfee40223cea5f7613989b081dc0a Mon Sep 17 00:00:00 2001 From: Brett Broadhurst Date: Wed, 18 Mar 2026 04:33:18 -0600 Subject: [PATCH] chore: zig test harness setup for parsing --- src/parser_tests.zig | 110 ++++++++++++++++++ src/root.zig | 1 + testing/regression/syntax/empty-file.ink | 3 - .../syntax/expression-statement.ink | 16 --- testing/regression/syntax/hello-world.ink | 9 -- .../regression/syntax/simple-assignment.ink | 13 --- 6 files changed, 111 insertions(+), 41 deletions(-) create mode 100644 src/parser_tests.zig delete mode 100644 testing/regression/syntax/empty-file.ink delete mode 100644 testing/regression/syntax/expression-statement.ink delete mode 100644 testing/regression/syntax/hello-world.ink delete mode 100644 testing/regression/syntax/simple-assignment.ink diff --git a/src/parser_tests.zig b/src/parser_tests.zig new file mode 100644 index 0000000..01d75df --- /dev/null +++ b/src/parser_tests.zig @@ -0,0 +1,110 @@ +const std = @import("std"); +const Ast = @import("./Ast.zig"); + +test "parser: empty file" { + try testEqual( + \\ + , + \\File "" + \\ + , + ); +} + +test "parser: \"Hello World\"" { + try testEqual( + \\Hello, world! + , + \\File "" + \\`--BlockStmt + \\ `--ContentStmt + \\ `--Content + \\ `--StringLiteral `Hello, world!` + \\ + , + ); +} + +test "parser: expression statements" { + try testEqual( + \\~ (-1 + 2) * 3 - -4 + , + \\File "" + \\`--BlockStmt + \\ `--ExprStmt + \\ `--SubtractExpr + \\ |--MultiplyExpr + \\ | |--AddExpr + \\ | | |--NegateExpr + \\ | | | `--NumberLiteral `1` + \\ | | `--NumberLiteral `2` + \\ | `--NumberLiteral `3` + \\ `--NegateExpr + \\ `--NumberLiteral `4` + \\ + , + ); +} + +test "parser: temporary variables" { + try testEqual( + \\~ temp a = 1 + 2 + 3 + \\~ temp b = a + 1 + \\{b} + , + \\File "" + \\`--BlockStmt + \\ |--TempDecl + \\ | |--Identifier `a` + \\ | `--AddExpr + \\ | |--AddExpr + \\ | | |--NumberLiteral `1` + \\ | | `--NumberLiteral `2` + \\ | `--NumberLiteral `3` + \\ |--TempDecl + \\ | |--Identifier `b` + \\ | `--AddExpr + \\ | |--Identifier `a` + \\ | `--NumberLiteral `1` + \\ `--ContentStmt + \\ `--Content + \\ `--InlineLogicExpr + \\ `--Identifier `b` + \\ + , + ); +} + +test "parser: temporary assignment" { + try testEqual( + \\~ temp a = 123 + \\~ a = 321 + , + \\File "" + \\`--BlockStmt + \\ |--TempDecl + \\ | |--Identifier `a` + \\ | `--NumberLiteral `123` + \\ `--AssignStmt + \\ |--Identifier `a` + \\ `--NumberLiteral `321` + \\ + , + ); +} + +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, "", 0); + try ast.render(gpa, &allocating.writer, .{ + .use_color = false, + }); + return std.testing.expectEqualSlices(u8, expected_ast, allocating.written()); +} diff --git a/src/root.zig b/src/root.zig index eaf4636..5bd2440 100644 --- a/src/root.zig +++ b/src/root.zig @@ -6,5 +6,6 @@ test { _ = tokenizer; _ = Ast; _ = Story; + _ = @import("parser_tests.zig"); _ = @import("Story/runtime_tests.zig"); } diff --git a/testing/regression/syntax/empty-file.ink b/testing/regression/syntax/empty-file.ink deleted file mode 100644 index abb3c19..0000000 --- a/testing/regression/syntax/empty-file.ink +++ /dev/null @@ -1,3 +0,0 @@ -// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s - -// CHECK: File "" diff --git a/testing/regression/syntax/expression-statement.ink b/testing/regression/syntax/expression-statement.ink deleted file mode 100644 index 86c2e82..0000000 --- a/testing/regression/syntax/expression-statement.ink +++ /dev/null @@ -1,16 +0,0 @@ -// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s - -// CHECK: File "" -// CHECK-NEXT: `--BlockStmt -// CHECK-NEXT: `--ExprStmt -// CHECK-NEXT: `--SubtractExpr -// CHECK-NEXT: |--MultiplyExpr -// CHECK-NEXT: | |--AddExpr -// CHECK-NEXT: | | |--NegateExpr -// CHECK-NEXT: | | | `--NumberLiteral `1` -// CHECK-NEXT: | | `--NumberLiteral `2` -// CHECK-NEXT: | `--NumberLiteral `3` -// CHECK-NEXT: `--NegateExpr -// CHECK-NEXT: `--NumberLiteral `4` - -~ (-1 + 2) * 3 - -4 diff --git a/testing/regression/syntax/hello-world.ink b/testing/regression/syntax/hello-world.ink deleted file mode 100644 index 259d29d..0000000 --- a/testing/regression/syntax/hello-world.ink +++ /dev/null @@ -1,9 +0,0 @@ -// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s - -// CHECK: File "" -// CHECK-NEXT: `--BlockStmt -// CHECK-NEXT: `--ContentStmt -// CHECK-NEXT: `--Content -// CHECK-NEXT: `--StringLiteral `Hello, world!` - -Hello, world! diff --git a/testing/regression/syntax/simple-assignment.ink b/testing/regression/syntax/simple-assignment.ink deleted file mode 100644 index c7a35cf..0000000 --- a/testing/regression/syntax/simple-assignment.ink +++ /dev/null @@ -1,13 +0,0 @@ -// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s - -// CHECK: File "" -// CHECK-NEXT: `--BlockStmt -// CHECK-NEXT: |--TempDecl -// CHECK-NEXT: | |--Identifier `a` -// CHECK-NEXT: | `--NumberLiteral `123` -// CHECK-NEXT: `--AssignStmt -// CHECK-NEXT: |--Identifier `a` -// CHECK-NEXT: `--NumberLiteral `321` - -~ temp a = 123 -~ a = 321