-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuite.zig
More file actions
506 lines (425 loc) · 15 KB
/
Copy pathsuite.zig
File metadata and controls
506 lines (425 loc) · 15 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
const std = @import("std");
pub const TestError = error{
TestFailed,
TestSkipped,
SetupFailed,
TeardownFailed,
};
/// Test function type
pub const TestFn = *const fn (allocator: std.mem.Allocator) anyerror!void;
/// Hook function type
pub const HookFn = *const fn (allocator: std.mem.Allocator) anyerror!void;
/// Test type
pub const TestType = enum {
sync,
async_test,
};
/// Test status
pub const TestStatus = enum {
pending,
running,
passed,
failed,
skipped,
};
/// Individual test case
pub const TestCase = struct {
name: []const u8,
test_fn: TestFn,
test_type: TestType = .sync,
timeout_ms: u64 = 5000,
status: TestStatus = .pending,
error_message: ?[]const u8 = null,
execution_time_ns: u64 = 0,
skip: bool = false,
only: bool = false,
file: []const u8 = "",
line: u32 = 0,
pub fn init(name: []const u8, test_fn: TestFn) TestCase {
return TestCase{
.name = name,
.test_fn = test_fn,
};
}
pub fn initWithTimeout(name: []const u8, test_fn: TestFn, timeout_ms: u64) TestCase {
return TestCase{
.name = name,
.test_fn = test_fn,
.timeout_ms = timeout_ms,
};
}
pub fn initAsync(name: []const u8, test_fn: TestFn, timeout_ms: u64) TestCase {
return TestCase{
.name = name,
.test_fn = test_fn,
.test_type = .async_test,
.timeout_ms = timeout_ms,
};
}
};
/// Test suite (describe block)
pub const TestSuite = struct {
name: []const u8,
tests: std.ArrayList(TestCase),
suites: std.ArrayList(*TestSuite),
before_each_hooks: std.ArrayList(HookFn),
after_each_hooks: std.ArrayList(HookFn),
before_all_hooks: std.ArrayList(HookFn),
after_all_hooks: std.ArrayList(HookFn),
allocator: std.mem.Allocator,
skip: bool = false,
only: bool = false,
parent: ?*TestSuite = null,
timeout_ms: u64 = 0,
const Self = @This();
pub fn init(allocator: std.mem.Allocator, name: []const u8) !*Self {
const suite = try allocator.create(Self);
suite.* = Self{
.name = name,
.tests = .empty,
.suites = .empty,
.before_each_hooks = .empty,
.after_each_hooks = .empty,
.before_all_hooks = .empty,
.after_all_hooks = .empty,
.allocator = allocator,
};
return suite;
}
pub fn deinit(self: *Self) void {
for (self.suites.items) |suite| {
suite.deinit();
}
self.tests.deinit(self.allocator);
self.suites.deinit(self.allocator);
self.before_each_hooks.deinit(self.allocator);
self.after_each_hooks.deinit(self.allocator);
self.before_all_hooks.deinit(self.allocator);
self.after_all_hooks.deinit(self.allocator);
self.allocator.destroy(self);
}
/// Add a test to this suite
pub fn addTest(self: *Self, test_case: TestCase) !void {
try self.tests.append(self.allocator, test_case);
}
/// Add a nested suite
pub fn addSuite(self: *Self, suite: *TestSuite) !void {
suite.parent = self;
try self.suites.append(self.allocator, suite);
}
/// Add a beforeEach hook
pub fn addBeforeEach(self: *Self, hook: HookFn) !void {
try self.before_each_hooks.append(self.allocator, hook);
}
/// Add an afterEach hook
pub fn addAfterEach(self: *Self, hook: HookFn) !void {
try self.after_each_hooks.append(self.allocator, hook);
}
/// Add a beforeAll hook
pub fn addBeforeAll(self: *Self, hook: HookFn) !void {
try self.before_all_hooks.append(self.allocator, hook);
}
/// Add an afterAll hook
pub fn addAfterAll(self: *Self, hook: HookFn) !void {
try self.after_all_hooks.append(self.allocator, hook);
}
/// Get all beforeEach hooks including parent hooks
pub fn getAllBeforeEachHooks(self: *Self, allocator: std.mem.Allocator) !std.ArrayList(HookFn) {
var hooks: std.ArrayList(HookFn) = .empty;
// Collect parent hooks first (they run before child hooks)
if (self.parent) |parent| {
var parent_hooks = try parent.getAllBeforeEachHooks(allocator);
defer parent_hooks.deinit(allocator);
try hooks.appendSlice(allocator, parent_hooks.items);
}
// Add this suite's hooks
try hooks.appendSlice(allocator, self.before_each_hooks.items);
return hooks;
}
/// Get all afterEach hooks including parent hooks
pub fn getAllAfterEachHooks(self: *Self, allocator: std.mem.Allocator) !std.ArrayList(HookFn) {
var hooks: std.ArrayList(HookFn) = .empty;
// Add this suite's hooks first
try hooks.appendSlice(allocator, self.after_each_hooks.items);
// Collect parent hooks last (they run after child hooks)
if (self.parent) |parent| {
var parent_hooks = try parent.getAllAfterEachHooks(allocator);
defer parent_hooks.deinit(allocator);
try hooks.appendSlice(allocator, parent_hooks.items);
}
return hooks;
}
/// Run all beforeAll hooks
pub fn runBeforeAllHooks(self: *Self, allocator: std.mem.Allocator) !void {
for (self.before_all_hooks.items) |hook| {
try hook(allocator);
}
}
/// Run all afterAll hooks
pub fn runAfterAllHooks(self: *Self, allocator: std.mem.Allocator) !void {
for (self.after_all_hooks.items) |hook| {
try hook(allocator);
}
}
/// Check if this suite should be skipped
pub fn shouldSkip(self: *Self) bool {
if (self.skip) return true;
if (self.parent) |parent| {
return parent.shouldSkip();
}
return false;
}
/// Check if only this suite (or a parent) is marked as only
pub fn hasOnly(self: *Self) bool {
if (self.only) return true;
if (self.parent) |parent| {
return parent.hasOnly();
}
return false;
}
/// Count total tests in this suite and nested suites
pub fn countTests(self: *Self) usize {
var count: usize = self.tests.items.len;
for (self.suites.items) |suite| {
count += suite.countTests();
}
return count;
}
};
/// Global test registry
pub const TestRegistry = struct {
root_suites: std.ArrayList(*TestSuite),
current_suite: ?*TestSuite = null,
allocator: std.mem.Allocator,
has_only: bool = false,
const Self = @This();
pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.root_suites = .empty,
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void {
for (self.root_suites.items) |suite| {
suite.deinit();
}
self.root_suites.deinit(self.allocator);
}
/// Register a new suite
pub fn registerSuite(self: *Self, suite: *TestSuite) !void {
if (self.current_suite) |current| {
try current.addSuite(suite);
} else {
try self.root_suites.append(self.allocator, suite);
}
if (suite.only) {
self.has_only = true;
}
}
/// Register a test in the current suite
pub fn registerTest(self: *Self, test_case: TestCase) !void {
if (self.current_suite) |current| {
try current.addTest(test_case);
} else {
// If no current suite, create a default one
const default_suite = try TestSuite.init(self.allocator, "default");
try default_suite.addTest(test_case);
try self.root_suites.append(self.allocator, default_suite);
}
if (test_case.only) {
self.has_only = true;
}
}
/// Count total tests across all suites
pub fn countAllTests(self: *Self) usize {
var count: usize = 0;
for (self.root_suites.items) |suite| {
count += suite.countTests();
}
return count;
}
};
/// Global test registry instance
var global_registry: ?TestRegistry = null;
/// Get the global test registry
pub fn getRegistry(allocator: std.mem.Allocator) *TestRegistry {
if (global_registry == null) {
global_registry = TestRegistry.init(allocator);
}
return &global_registry.?;
}
/// Clean up the global test registry
pub fn cleanupRegistry() void {
if (global_registry) |*registry| {
registry.deinit();
global_registry = null;
}
}
/// Helper to create a test suite (describe)
pub fn describe(allocator: std.mem.Allocator, name: []const u8, func: anytype) !void {
const registry = getRegistry(allocator);
const suite = try TestSuite.init(allocator, name);
const previous_suite = registry.current_suite;
registry.current_suite = suite;
// Execute the describe block
try func(allocator);
registry.current_suite = previous_suite;
try registry.registerSuite(suite);
}
/// Helper to create a test case (it/test)
pub fn it(allocator: std.mem.Allocator, name: []const u8, test_fn: TestFn) !void {
const registry = getRegistry(allocator);
const test_case = TestCase.init(name, test_fn);
try registry.registerTest(test_case);
}
/// Alias for it
pub const test_ = it;
/// Skip a describe block
pub fn describeSkip(allocator: std.mem.Allocator, name: []const u8, func: anytype) !void {
const registry = getRegistry(allocator);
const suite = try TestSuite.init(allocator, name);
suite.skip = true;
const previous_suite = registry.current_suite;
registry.current_suite = suite;
try func(allocator);
registry.current_suite = previous_suite;
try registry.registerSuite(suite);
}
/// Only run this describe block
pub fn describeOnly(allocator: std.mem.Allocator, name: []const u8, func: anytype) !void {
const registry = getRegistry(allocator);
const suite = try TestSuite.init(allocator, name);
suite.only = true;
const previous_suite = registry.current_suite;
registry.current_suite = suite;
try func(allocator);
registry.current_suite = previous_suite;
try registry.registerSuite(suite);
}
/// Skip a test
pub fn itSkip(allocator: std.mem.Allocator, name: []const u8, test_fn: TestFn) !void {
const registry = getRegistry(allocator);
var test_case = TestCase.init(name, test_fn);
test_case.skip = true;
try registry.registerTest(test_case);
}
/// Only run this test
pub fn itOnly(allocator: std.mem.Allocator, name: []const u8, test_fn: TestFn) !void {
const registry = getRegistry(allocator);
var test_case = TestCase.init(name, test_fn);
test_case.only = true;
try registry.registerTest(test_case);
}
/// Register a test with timeout
pub fn itTimeout(allocator: std.mem.Allocator, name: []const u8, test_fn: TestFn, timeout_ms: u64) !void {
const registry = getRegistry(allocator);
const test_case = TestCase.initWithTimeout(name, test_fn, timeout_ms);
try registry.registerTest(test_case);
}
/// Register an async test
pub fn itAsync(allocator: std.mem.Allocator, name: []const u8, test_fn: TestFn) !void {
return itAsyncTimeout(allocator, name, test_fn, 5000);
}
/// Register an async test with custom timeout
pub fn itAsyncTimeout(allocator: std.mem.Allocator, name: []const u8, test_fn: TestFn, timeout_ms: u64) !void {
const registry = getRegistry(allocator);
const test_case = TestCase.initAsync(name, test_fn, timeout_ms);
try registry.registerTest(test_case);
}
/// Skip an async test
pub fn itAsyncSkip(allocator: std.mem.Allocator, name: []const u8, test_fn: TestFn) !void {
const registry = getRegistry(allocator);
var test_case = TestCase.initAsync(name, test_fn, 5000);
test_case.skip = true;
try registry.registerTest(test_case);
}
/// Only run this async test
pub fn itAsyncOnly(allocator: std.mem.Allocator, name: []const u8, test_fn: TestFn) !void {
const registry = getRegistry(allocator);
var test_case = TestCase.initAsync(name, test_fn, 5000);
test_case.only = true;
try registry.registerTest(test_case);
}
/// Create a suite with timeout
pub fn describeTimeout(allocator: std.mem.Allocator, name: []const u8, timeout_ms: u64, func: anytype) !void {
const registry = getRegistry(allocator);
const new_suite = try TestSuite.init(allocator, name);
new_suite.timeout_ms = timeout_ms;
const previous_suite = registry.current_suite;
registry.current_suite = new_suite;
try func(allocator);
registry.current_suite = previous_suite;
try registry.registerSuite(new_suite);
}
/// Add beforeEach hook
pub fn beforeEach(allocator: std.mem.Allocator, hook: HookFn) !void {
const registry = getRegistry(allocator);
if (registry.current_suite) |suite| {
try suite.addBeforeEach(hook);
}
}
/// Add afterEach hook
pub fn afterEach(allocator: std.mem.Allocator, hook: HookFn) !void {
const registry = getRegistry(allocator);
if (registry.current_suite) |suite| {
try suite.addAfterEach(hook);
}
}
/// Add beforeAll hook
pub fn beforeAll(allocator: std.mem.Allocator, hook: HookFn) !void {
const registry = getRegistry(allocator);
if (registry.current_suite) |suite| {
try suite.addBeforeAll(hook);
}
}
/// Add afterAll hook
pub fn afterAll(allocator: std.mem.Allocator, hook: HookFn) !void {
const registry = getRegistry(allocator);
if (registry.current_suite) |suite| {
try suite.addAfterAll(hook);
}
}
// Tests
test "TestStatus enum values" {
const pending_status = TestStatus.pending;
const running_status = TestStatus.running;
const passed_status = TestStatus.passed;
const failed_status = TestStatus.failed;
const skipped_status = TestStatus.skipped;
try std.testing.expect(pending_status == .pending);
try std.testing.expect(running_status == .running);
try std.testing.expect(passed_status == .passed);
try std.testing.expect(failed_status == .failed);
try std.testing.expect(skipped_status == .skipped);
}
test "TestCase creation" {
const allocator = std.testing.allocator;
const name_duped = try allocator.dupe(u8, "test case");
defer allocator.free(name_duped);
const test_case = TestCase{
.name = name_duped,
.test_fn = undefined,
.status = .pending,
.skip = false,
.only = false,
.error_message = null,
};
try std.testing.expectEqualStrings("test case", test_case.name);
try std.testing.expect(test_case.status == .pending);
try std.testing.expect(!test_case.skip);
try std.testing.expect(!test_case.only);
}
test "TestSuite creation and cleanup" {
const allocator = std.testing.allocator;
var suite = try TestSuite.init(allocator, "Test Suite");
defer suite.deinit();
try std.testing.expectEqualStrings("Test Suite", suite.name);
try std.testing.expect(!suite.skip);
try std.testing.expect(!suite.only);
try std.testing.expectEqual(@as(usize, 0), suite.tests.items.len);
try std.testing.expectEqual(@as(usize, 0), suite.suites.items.len);
}
test "TestRegistry cleanup" {
// Verify cleanup doesn't crash
cleanupRegistry();
}