-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.zig
More file actions
103 lines (81 loc) · 2.79 KB
/
test_runner.zig
File metadata and controls
103 lines (81 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const std = @import("std");
const builtin = @import("builtin");
// https://www.openmymind.net/Using-A-Custom-Test-Runner-In-Zig/
// set log level for tests
pub const std_options: std.Options = .{
.log_level = .warn,
};
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
pub fn main() !void {
var arena_alloc = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_alloc.deinit();
const arena = arena_alloc.allocator();
const args = try std.process.argsAlloc(arena);
var hm: std.StringHashMapUnmanaged([]const u8) = .empty;
for (args, 0..) |arg, i| {
if (i == 0) continue;
try hm.put(arena, arg, arg);
}
var n_tests: u32 = 0;
var passed_tests: u32 = 0;
var n_leaks: u32 = 0;
const passed = "\u{001b}[32mpassed\u{001b}[0m";
const failed = "\u{001b}[31mfailed\u{001b}[0m";
var did_fail = false; // some test failed
for (builtin.test_functions, 0..) |t, i| {
if (i == 0) continue;
const name = extractName(t);
const file = extractFile(t);
// filter tests
if (args.len > 1) {
const root = extractRoot(file);
if (hm.get(root) == null) continue;
}
std.testing.allocator_instance = .{};
const result = t.func();
const leaked = std.testing.allocator_instance.detectLeaks();
if (leaked) n_leaks += 1;
try stdout.print("[{d:>3}/{d:<3}] ", .{ n_tests + 1, builtin.test_functions.len - 1 });
if (result) {
passed_tests += 1;
try stdout.print("{s:<20} | {s:<20} | {s} | leaked: {}\n", .{
file,
name,
passed,
leaked,
});
} else |err| {
did_fail = true;
try stdout.print("{s:<20} | {s:<20} | {s} | leaked: {} | error: {}\n", .{
file,
name,
failed,
leaked,
err,
});
}
n_tests += 1;
}
try stdout.print("passing: {}/{}, leaks: {}/{}\n", .{
passed_tests,
n_tests,
n_leaks,
n_tests,
});
if (did_fail) return error.TestingFailure;
try stdout.flush();
}
fn extractFile(t: std.builtin.TestFn) []const u8 {
const marker = std.mem.lastIndexOf(u8, t.name, ".test.") orelse return t.name;
return t.name[0..marker];
}
fn extractName(t: std.builtin.TestFn) []const u8 {
const marker = std.mem.lastIndexOf(u8, t.name, ".test.") orelse return t.name;
return t.name[marker + 6 ..];
}
fn extractRoot(file_name: []const u8) []const u8 {
const marker = std.mem.indexOfScalar(u8, file_name, '.') orelse return file_name;
return file_name[0..marker];
}