From f707bfece3d0b629e5738cc5e4fc1b47f51623d2 Mon Sep 17 00:00:00 2001 From: Petr Pucil Date: Wed, 12 Nov 2025 22:32:04 +0100 Subject: [PATCH] Zig Build System > Testing: fix code examples This follows up on https://github.com/ziglang/www.ziglang.org/pull/545. Although it did fix one compile error, fixing it actually revealed other issues that are addressed by this pull request: 1. Both versions of `main.zig` were using the old `std.ArrayList` API from when it was managed, but as of Zig 0.15.1, it is no longer managed: https://ziglang.org/download/0.15.1/release-notes.html#ArrayList-make-unmanaged-the-default 2. The file `unit-testing-skip-foreign/build.zig` was missing `additional_option=test`, so the command `zig build --summary all` was run instead of the intended command `zig build test --summary all`. --- .../build-system/unit-testing-skip-foreign/build.zig | 3 +-- .../build-system/unit-testing-skip-foreign/main.zig | 12 ++++++++---- zig-code/build-system/unit-testing/main.zig | 9 ++++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/zig-code/build-system/unit-testing-skip-foreign/build.zig b/zig-code/build-system/unit-testing-skip-foreign/build.zig index 0ac8180a1..06eca5674 100644 --- a/zig-code/build-system/unit-testing-skip-foreign/build.zig +++ b/zig-code/build-system/unit-testing-skip-foreign/build.zig @@ -29,8 +29,7 @@ pub fn build(b: *std.Build) void { } } -// zig-doctest: build-system --collapseable -- test --summary all - // build=succeed +// additional_option=test // additional_option=--summary // additional_option=all diff --git a/zig-code/build-system/unit-testing-skip-foreign/main.zig b/zig-code/build-system/unit-testing-skip-foreign/main.zig index 42f187fd0..fd32fd0d0 100644 --- a/zig-code/build-system/unit-testing-skip-foreign/main.zig +++ b/zig-code/build-system/unit-testing-skip-foreign/main.zig @@ -1,9 +1,13 @@ -// zig-doctest: syntax --name main const std = @import("std"); test "simple test" { - var list = std.ArrayList(i32).init(std.testing.allocator); - defer list.deinit(); - try list.append(42); + const allocator = std.testing.allocator; + + var list: std.ArrayList(i32) = .empty; + defer list.deinit(allocator); + + try list.append(allocator, 42); try std.testing.expectEqual(@as(i32, 42), list.pop()); } + +// syntax diff --git a/zig-code/build-system/unit-testing/main.zig b/zig-code/build-system/unit-testing/main.zig index c49b4c3c6..fd32fd0d0 100644 --- a/zig-code/build-system/unit-testing/main.zig +++ b/zig-code/build-system/unit-testing/main.zig @@ -1,9 +1,12 @@ const std = @import("std"); test "simple test" { - var list = std.ArrayList(i32).init(std.testing.allocator); - defer list.deinit(); - try list.append(42); + const allocator = std.testing.allocator; + + var list: std.ArrayList(i32) = .empty; + defer list.deinit(allocator); + + try list.append(allocator, 42); try std.testing.expectEqual(@as(i32, 42), list.pop()); }