authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2022-11-02 22:41:20+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2022-11-02 22:41:20+11:00
logae0f12885bd0f6f436269a51634f56c3fe7fddc8
tree86c1b236947e6951fdc614d302014e143d01ab04
parent33a401dd603ec76805ebb5c933fe41f1dfeabd5b

test: add test_runner_path test


4 files changed, 73 insertions(+), 0 deletions(-)

test/standalone.zig+1
......@@ -15,6 +15,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
1515 cases.add("test/standalone/main_return_error/error_u8_non_zero.zig");
1616 cases.add("test/standalone/noreturn_call/inline.zig");
1717 cases.add("test/standalone/noreturn_call/as_arg.zig");
18 cases.addBuildFile("test/standalone/test_runner_path/build.zig", .{ .requires_stage2 = true });
1819 cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{});
1920 cases.addBuildFile("test/standalone/shared_library/build.zig", .{});
2021 cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{});
test/standalone/test_runner_path/build.zig created+11
......@@ -0,0 +1,11 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const test_exe = b.addTestExe("test", "test.zig");
5 test_exe.test_runner = "test_runner.zig";
6
7 const test_run = test_exe.run();
8
9 const test_step = b.step("test", "Test the program");
10 test_step.dependOn(&test_run.step);
11}
test/standalone/test_runner_path/test.zig created+9
......@@ -0,0 +1,9 @@
1test "test runner path pass" {}
2
3test "test runner path fail" {
4 return error.Fail;
5}
6
7test "test runner path skip" {
8 return error.SkipZigTest;
9}
test/standalone/test_runner_path/test_runner.zig created+52
......@@ -0,0 +1,52 @@
1const std = @import("std");
2const io = std.io;
3const builtin = @import("builtin");
4
5pub const io_mode: io.Mode = builtin.test_io_mode;
6
7pub fn main() void {
8 const test_fn_list = builtin.test_functions;
9 var ok_count: usize = 0;
10 var skip_count: usize = 0;
11 var fail_count: usize = 0;
12
13 var async_frame_buffer: []align(std.Target.stack_align) u8 = undefined;
14 // TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly
15 // ignores the alignment of the slice.
16 async_frame_buffer = &[_]u8{};
17
18 for (test_fn_list) |test_fn| {
19 const result = if (test_fn.async_frame_size) |size| switch (io_mode) {
20 .evented => blk: {
21 if (async_frame_buffer.len < size) {
22 std.heap.page_allocator.free(async_frame_buffer);
23 async_frame_buffer = std.heap.page_allocator.alignedAlloc(u8, std.Target.stack_align, size) catch @panic("out of memory");
24 }
25 const casted_fn = @ptrCast(fn () callconv(.Async) anyerror!void, test_fn.func);
26 break :blk await @asyncCall(async_frame_buffer, {}, casted_fn, .{});
27 },
28 .blocking => {
29 skip_count += 1;
30 continue;
31 },
32 } else test_fn.func();
33 if (result) |_| {
34 ok_count += 1;
35 } else |err| switch (err) {
36 error.SkipZigTest => {
37 skip_count += 1;
38 },
39 else => {
40 fail_count += 1;
41 },
42 }
43 }
44 if (ok_count == test_fn_list.len) {
45 std.debug.print("All {d} tests passed.\n", .{ok_count});
46 } else {
47 std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count });
48 }
49 if (ok_count != 1 or skip_count != 1 or fail_count != 1) {
50 std.process.exit(1);
51 }
52}