-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_test.zig
More file actions
134 lines (112 loc) · 5.04 KB
/
basic_test.zig
File metadata and controls
134 lines (112 loc) · 5.04 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
const std = @import("std");
const ztf = @import("zig_test_framework");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Example 1: Basic describe/it structure
try ztf.describe(allocator, "Math operations", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should add two numbers correctly", testAddition);
try ztf.it(alloc, "should subtract two numbers correctly", testSubtraction);
try ztf.it(alloc, "should multiply two numbers correctly", testMultiplication);
try ztf.it(alloc, "should divide two numbers correctly", testDivision);
}
fn testAddition(alloc: std.mem.Allocator) !void {
const result: i32 = 2 + 2;
try ztf.expect(alloc, result).toBe(@as(i32, 4));
}
fn testSubtraction(alloc: std.mem.Allocator) !void {
const result: i32 = 10 - 5;
try ztf.expect(alloc, result).toBe(@as(i32, 5));
}
fn testMultiplication(alloc: std.mem.Allocator) !void {
const result: i32 = 3 * 4;
try ztf.expect(alloc, result).toBe(@as(i32, 12));
}
fn testDivision(alloc: std.mem.Allocator) !void {
const result: i32 = 20 / 4;
try ztf.expect(alloc, result).toBe(@as(i32, 5));
}
}.testSuite);
// Example 2: String assertions
try ztf.describe(allocator, "String operations", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should compare strings", testStringEquality);
try ztf.it(alloc, "should check if string contains substring", testStringContains);
try ztf.it(alloc, "should check string length", testStringLength);
}
fn testStringEquality(alloc: std.mem.Allocator) !void {
const message = "Hello, World!";
try ztf.expect(alloc, message).toBe("Hello, World!");
}
fn testStringContains(alloc: std.mem.Allocator) !void {
const message = "The quick brown fox";
try ztf.expect(alloc, message).toContain("quick");
try ztf.expect(alloc, message).toStartWith("The");
try ztf.expect(alloc, message).toEndWith("fox");
}
fn testStringLength(alloc: std.mem.Allocator) !void {
const message = "Hello";
try ztf.expect(alloc, message).toHaveLength(5);
}
}.testSuite);
// Example 3: Comparison assertions
try ztf.describe(allocator, "Comparisons", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should compare numbers", testComparisons);
}
fn testComparisons(alloc: std.mem.Allocator) !void {
try ztf.expect(alloc, @as(i32, 10)).toBeGreaterThan(@as(i32, 5));
try ztf.expect(alloc, @as(i32, 10)).toBeGreaterThanOrEqual(@as(i32, 10));
try ztf.expect(alloc, @as(i32, 5)).toBeLessThan(@as(i32, 10));
try ztf.expect(alloc, @as(i32, 5)).toBeLessThanOrEqual(@as(i32, 5));
}
}.testSuite);
// Example 4: Boolean assertions
try ztf.describe(allocator, "Boolean tests", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should handle booleans", testBooleans);
}
fn testBooleans(alloc: std.mem.Allocator) !void {
try ztf.expect(alloc, true).toBeTruthy();
try ztf.expect(alloc, false).toBeFalsy();
try ztf.expect(alloc, true).not().toBeFalsy();
}
}.testSuite);
// Example 5: Optional (nullable) values
try ztf.describe(allocator, "Optional values", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should handle null values", testNull);
try ztf.it(alloc, "should handle defined values", testDefined);
}
fn testNull(alloc: std.mem.Allocator) !void {
const value: ?i32 = null;
try ztf.expect(alloc, value).toBeNull();
}
fn testDefined(alloc: std.mem.Allocator) !void {
const value: ?i32 = 42;
try ztf.expect(alloc, value).toBeDefined();
try ztf.expect(alloc, value).not().toBeNull();
}
}.testSuite);
// Example 6: Negation with .not()
try ztf.describe(allocator, "Negation tests", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should support .not() modifier", testNegation);
}
fn testNegation(alloc: std.mem.Allocator) !void {
try ztf.expect(alloc, @as(i32, 5)).not().toBe(@as(i32, 10));
try ztf.expect(alloc, "hello").not().toBe("world");
try ztf.expect(alloc, true).not().toBe(false);
}
}.testSuite);
// Run all tests
const registry = ztf.getRegistry(allocator);
const success = try ztf.runTests(allocator, registry);
// Clean up the registry
ztf.cleanupRegistry();
if (!success) {
std.process.exit(1);
}
}