From ee7cacfe363dd8a1df2031723af0e343e78430f0 Mon Sep 17 00:00:00 2001 From: Ben-Miller0 Date: Sun, 28 Jul 2024 13:00:06 +0100 Subject: [PATCH 1/3] added check that the test files are valid zig under the current compiler --- grammar/check_tests.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 grammar/check_tests.sh diff --git a/grammar/check_tests.sh b/grammar/check_tests.sh new file mode 100755 index 0000000..819f160 --- /dev/null +++ b/grammar/check_tests.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +ZIG=${ZIG:-zig} + +if [ $# -eq 0 ]; then + FILES=(tests/*.zig) +else + FILES=("$@") +fi + +echo "running ${#FILES[@]} tests..." + +ANY_FAILURE= +for FILE in "${FILES[@]}"; do + $ZIG fmt --stdin < "$FILE" > /dev/null 2>&1 + zigret=$? + error=$(zig test "$FILE" 2>&1) + parserret=$? + + if [ "$zigret" -ne "0" ]; then + zigret=1 + fi + + if [ "$zigret" -ne "$parserret" ]; then + echo "FAIL: ${FILE}: zig: $zigret, grammar: $parserret" + if [ "$parserret" -eq 1 ]; then + echo "$error" + fi + ANY_FAILURE=1 + fi +done + +if [ -z "$ANY_FAILURE" ]; then + echo 'all tests passed' +else + exit 1 +fi From ea77fbb1cfaef86c684f30cd8a0be969df7b1606 Mon Sep 17 00:00:00 2001 From: Ben-Miller0 Date: Thu, 12 Sep 2024 09:56:16 +0100 Subject: [PATCH 2/3] switched to a build.zig for tests --- .gitignore | 2 ++ grammar/build.zig | 35 +++++++++++++++++++++++++++++++++++ grammar/check_tests.sh | 37 ------------------------------------- grammar/tests/while.zig | 19 +++++++------------ grammar/tests/widening.zig | 1 + 5 files changed, 45 insertions(+), 49 deletions(-) create mode 100644 grammar/build.zig delete mode 100755 grammar/check_tests.sh diff --git a/.gitignore b/.gitignore index 378eac2..f611581 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ build +zig-cache +zig-out diff --git a/grammar/build.zig b/grammar/build.zig new file mode 100644 index 0000000..0d829e6 --- /dev/null +++ b/grammar/build.zig @@ -0,0 +1,35 @@ +const std = @import("std"); + +const test_targets = [_]std.Target.Query{ + .{}, +}; + +pub fn build(b: *std.Build) !void { + const test_step = b.step("test", "Test the tests"); + + var files = std.ArrayList([]const u8).init(b.allocator); + defer files.deinit(); + + var dir = try std.fs.cwd().openDir("tests", .{ .iterate = true }); + var it = dir.iterate(); + while (try it.next()) |file| { + if (file.kind != .file) { + continue; + } + try files.append(b.dupe(file.name)); + } + + for (test_targets) |target| { + for (files.items) |file| { + const unit_test = b.addTest(.{ + .root_source_file = .{ .path = file }, + .target = b.resolveTargetQuery(target), + }); + + const run_unit_test = b.addRunArtifact(unit_test); + test_step.dependOn(&run_unit_test.step); + } + } + + b.default_step.dependOn(test_step); +} diff --git a/grammar/check_tests.sh b/grammar/check_tests.sh deleted file mode 100755 index 819f160..0000000 --- a/grammar/check_tests.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env bash - -ZIG=${ZIG:-zig} - -if [ $# -eq 0 ]; then - FILES=(tests/*.zig) -else - FILES=("$@") -fi - -echo "running ${#FILES[@]} tests..." - -ANY_FAILURE= -for FILE in "${FILES[@]}"; do - $ZIG fmt --stdin < "$FILE" > /dev/null 2>&1 - zigret=$? - error=$(zig test "$FILE" 2>&1) - parserret=$? - - if [ "$zigret" -ne "0" ]; then - zigret=1 - fi - - if [ "$zigret" -ne "$parserret" ]; then - echo "FAIL: ${FILE}: zig: $zigret, grammar: $parserret" - if [ "$parserret" -eq 1 ]; then - echo "$error" - fi - ANY_FAILURE=1 - fi -done - -if [ -z "$ANY_FAILURE" ]; then - echo 'all tests passed' -else - exit 1 -fi diff --git a/grammar/tests/while.zig b/grammar/tests/while.zig index c920739..111ba34 100644 --- a/grammar/tests/while.zig +++ b/grammar/tests/while.zig @@ -137,48 +137,42 @@ fn getNumberOrNull() ?i32 { test "while on optional with else result follow else prong" { const result = while (returnNull()) |value| { break value; - } else - @as(i32, 2); + } else @as(i32, 2); expect(result == 2); } test "while on optional with else result follow break prong" { const result = while (returnOptional(10)) |value| { break value; - } else - @as(i32, 2); + } else @as(i32, 2); expect(result == 10); } test "while on error union with else result follow else prong" { const result = while (returnError()) |value| { break value; - } else |err| - @as(i32, 2); + } else |err| @as(i32, 2); expect(result == 2); } test "while on error union with else result follow break prong" { const result = while (returnSuccess(10)) |value| { break value; - } else |err| - @as(i32, 2); + } else |err| @as(i32, 2); expect(result == 10); } test "while on bool with else result follow else prong" { const result = while (returnFalse()) { break @as(i32, 10); - } else - @as(i32, 2); + } else @as(i32, 2); expect(result == 2); } test "while on bool with else result follow break prong" { const result = while (returnTrue()) { break @as(i32, 10); - } else - @as(i32, 2); + } else @as(i32, 2); expect(result == 10); } @@ -287,3 +281,4 @@ test "while copies its payload" { S.doTheTest(); comptime S.doTheTest(); } + diff --git a/grammar/tests/widening.zig b/grammar/tests/widening.zig index 2f215cc..50eee7f 100644 --- a/grammar/tests/widening.zig +++ b/grammar/tests/widening.zig @@ -37,3 +37,4 @@ test "float widening f16 to f128" { var y: f128 = x; expect(x == y); } + From f4d054c2f56e638513edc43ebc77a822611b9ffa Mon Sep 17 00:00:00 2001 From: Ben-Miller0 Date: Thu, 12 Sep 2024 09:59:09 +0100 Subject: [PATCH 3/3] removed trailing newlines --- grammar/tests/while.zig | 1 - grammar/tests/widening.zig | 1 - 2 files changed, 2 deletions(-) diff --git a/grammar/tests/while.zig b/grammar/tests/while.zig index 111ba34..737b4ae 100644 --- a/grammar/tests/while.zig +++ b/grammar/tests/while.zig @@ -281,4 +281,3 @@ test "while copies its payload" { S.doTheTest(); comptime S.doTheTest(); } - diff --git a/grammar/tests/widening.zig b/grammar/tests/widening.zig index 50eee7f..2f215cc 100644 --- a/grammar/tests/widening.zig +++ b/grammar/tests/widening.zig @@ -37,4 +37,3 @@ test "float widening f16 to f128" { var y: f128 = x; expect(x == y); } -