-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_test.zig
More file actions
403 lines (318 loc) · 15.4 KB
/
Copy pathtime_test.zig
File metadata and controls
403 lines (318 loc) · 15.4 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
const std = @import("std");
const ztf = @import("zig_test_framework");
const compat = ztf.compat;
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Test 1: Basic Time Mocking
try ztf.describe(allocator, "Basic Time Mocking", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should set and get mocked time", testSetSystemTime);
try ztf.it(alloc, "should reset to real time", testResetTime);
try ztf.it(alloc, "should use fake timers", testUseFakeTimers);
}
fn testSetSystemTime(alloc: std.mem.Allocator) !void {
// Set to Jan 1, 2020
const test_time: i64 = 1577836800000; // 2020-01-01T00:00:00.000Z
ztf.setSystemTime(alloc, test_time);
const current = ztf.time.now(alloc);
try ztf.expect(alloc, current).toBe(test_time);
// Clean up
ztf.setSystemTime(alloc, null);
}
fn testResetTime(alloc: std.mem.Allocator) !void {
// Set mocked time
const test_time: i64 = 1577836800000;
ztf.setSystemTime(alloc, test_time);
try ztf.expect(alloc, ztf.time.now(alloc)).toBe(test_time);
// Reset to real time
ztf.setSystemTime(alloc, null);
const real_time = ztf.time.now(alloc);
// Real time should be much greater than 2020
try ztf.expect(alloc, real_time).toBeGreaterThan(test_time);
}
fn testUseFakeTimers(alloc: std.mem.Allocator) !void {
ztf.useFakeTimers(alloc);
ztf.setSystemTime(alloc, 1577836800000);
const before = ztf.time.now(alloc);
compat.sleep(100 * std.time.ns_per_ms); // Sleep 100ms
const after = ztf.time.now(alloc);
// Time shouldn't advance when using fake timers with a set time
try ztf.expect(alloc, after).toBe(before);
// Clean up
ztf.useRealTimers(alloc);
}
}.testSuite);
// Test 2: Jest-Compatible API
try ztf.describe(allocator, "Jest-Compatible API", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should work with jest.setSystemTime", testJestSetSystemTime);
try ztf.it(alloc, "should work with jest.useFakeTimers", testJestFakeTimers);
try ztf.it(alloc, "should get time with jest.now()", testJestNow);
}
fn testJestSetSystemTime(alloc: std.mem.Allocator) !void {
const test_time: i64 = 1577836800000; // 2020-01-01
ztf.jest.setSystemTime(alloc, test_time);
try ztf.expect(alloc, ztf.jest.now(alloc)).toBe(test_time);
// Clean up
ztf.jest.useRealTimers(alloc);
}
fn testJestFakeTimers(alloc: std.mem.Allocator) !void {
ztf.jest.useFakeTimers(alloc);
ztf.jest.setSystemTime(alloc, 1577836800000);
try ztf.expect(alloc, ztf.jest.now(alloc)).toBe(@as(i64, 1577836800000));
ztf.jest.useRealTimers(alloc);
}
fn testJestNow(alloc: std.mem.Allocator) !void {
const test_time: i64 = 1609459200000; // 2021-01-01
ztf.jest.setSystemTime(alloc, test_time);
const now1 = ztf.jest.now(alloc);
const now2 = ztf.time.now(alloc);
try ztf.expect(alloc, now1).toBe(test_time);
try ztf.expect(alloc, now2).toBe(test_time);
try ztf.expect(alloc, now1).toBe(now2);
// Clean up
ztf.jest.useRealTimers(alloc);
}
}.testSuite);
// Test 3: Advancing Time
try ztf.describe(allocator, "Advancing Time", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should advance time by milliseconds", testAdvanceTime);
try ztf.it(alloc, "should advance multiple times", testMultipleAdvances);
}
fn testAdvanceTime(alloc: std.mem.Allocator) !void {
const start_time: i64 = 1577836800000;
ztf.setSystemTime(alloc, start_time);
const before = ztf.time.now(alloc);
ztf.advanceTimersByTime(alloc, 5000); // Advance 5 seconds
const after = ztf.time.now(alloc);
try ztf.expect(alloc, after).toBe(before + 5000);
// Clean up
ztf.setSystemTime(alloc, null);
}
fn testMultipleAdvances(alloc: std.mem.Allocator) !void {
const start_time: i64 = 1577836800000;
ztf.jest.setSystemTime(alloc, start_time);
ztf.jest.advanceTimersByTime(alloc, 1000);
try ztf.expect(alloc, ztf.jest.now(alloc)).toBe(start_time + 1000);
ztf.jest.advanceTimersByTime(alloc, 2000);
try ztf.expect(alloc, ztf.jest.now(alloc)).toBe(start_time + 3000);
ztf.jest.advanceTimersByTime(alloc, 7000);
try ztf.expect(alloc, ztf.jest.now(alloc)).toBe(start_time + 10000);
// Clean up
ztf.jest.useRealTimers(alloc);
}
}.testSuite);
// Test 4: DateHelper Functionality
try ztf.describe(allocator, "DateHelper", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should parse ISO 8601 strings", testISOParsing);
try ztf.it(alloc, "should extract year from timestamp", testYearExtraction);
try ztf.it(alloc, "should respect mocked time", testDateHelperWithMock);
}
fn testISOParsing(alloc: std.mem.Allocator) !void {
// Parse a date string
const timestamp = try ztf.DateHelper.fromISO("2020-01-01T00:00:00.000Z");
// Verify we get a reasonable 2020 timestamp
// 2020-01-01 should be around 1577836800000
// Allow for calculation variance
const year = ztf.DateHelper.getYear(timestamp);
try ztf.expect(alloc, year).toBe(@as(u16, 2020));
}
fn testYearExtraction(alloc: std.mem.Allocator) !void {
const timestamp_2020: i64 = 1577836800000; // 2020-01-01
const year_2020 = ztf.DateHelper.getYear(timestamp_2020);
try ztf.expect(alloc, year_2020).toBe(@as(u16, 2020));
const timestamp_2021: i64 = 1609459200000; // 2021-01-01
const year_2021 = ztf.DateHelper.getYear(timestamp_2021);
try ztf.expect(alloc, year_2021).toBe(@as(u16, 2021));
const timestamp_2025: i64 = 1735689600000; // 2025-01-01
const year_2025 = ztf.DateHelper.getYear(timestamp_2025);
try ztf.expect(alloc, year_2025).toBe(@as(u16, 2025));
}
fn testDateHelperWithMock(alloc: std.mem.Allocator) !void {
const helper = ztf.createDateHelper(alloc);
// Set mocked time
ztf.setSystemTime(alloc, 1577836800000);
// DateHelper should respect mocked time
try ztf.expect(alloc, helper.now()).toBe(@as(i64, 1577836800000));
// Clean up
ztf.setSystemTime(alloc, null);
}
}.testSuite);
// Test 5: Multiple Date Scenarios
try ztf.describe(allocator, "Multiple Date Scenarios", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should handle different years", testDifferentYears);
try ztf.it(alloc, "should handle year transitions", testYearTransitions);
}
fn testDifferentYears(alloc: std.mem.Allocator) !void {
// Test 2020
ztf.setSystemTime(alloc, 1577836800000);
const year_2020 = ztf.DateHelper.getYear(ztf.time.now(alloc));
try ztf.expect(alloc, year_2020).toBe(@as(u16, 2020));
// Test 2023
ztf.setSystemTime(alloc, 1672531200000);
const year_2023 = ztf.DateHelper.getYear(ztf.time.now(alloc));
try ztf.expect(alloc, year_2023).toBe(@as(u16, 2023));
// Test 2025
ztf.setSystemTime(alloc, 1735689600000);
const year_2025 = ztf.DateHelper.getYear(ztf.time.now(alloc));
try ztf.expect(alloc, year_2025).toBe(@as(u16, 2025));
// Clean up
ztf.setSystemTime(alloc, null);
}
fn testYearTransitions(alloc: std.mem.Allocator) !void {
// Dec 31, 2019, 23:59:59
const end_2019: i64 = 1577836799000;
ztf.setSystemTime(alloc, end_2019);
try ztf.expect(alloc, ztf.DateHelper.getYear(ztf.time.now(alloc))).toBe(@as(u16, 2019));
// Advance 1 second to Jan 1, 2020
ztf.advanceTimersByTime(alloc, 1000);
try ztf.expect(alloc, ztf.DateHelper.getYear(ztf.time.now(alloc))).toBe(@as(u16, 2020));
// Clean up
ztf.setSystemTime(alloc, null);
}
}.testSuite);
// Test 6: Real vs Fake Time
try ztf.describe(allocator, "Real vs Fake Time", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should distinguish between real and fake time", testRealVsFake);
try ztf.it(alloc, "should transition between modes", testModeTransitions);
}
fn testRealVsFake(alloc: std.mem.Allocator) !void {
// Switch to fake time
ztf.useFakeTimers(alloc);
ztf.setSystemTime(alloc, 1577836800000);
try ztf.expect(alloc, ztf.time.now(alloc)).toBe(@as(i64, 1577836800000));
// Back to real time
ztf.useRealTimers(alloc);
const real_time2 = ztf.time.now(alloc);
try ztf.expect(alloc, real_time2).toBeGreaterThan(@as(i64, 1577836800000));
}
fn testModeTransitions(alloc: std.mem.Allocator) !void {
// Fake time
ztf.useFakeTimers(alloc);
ztf.setSystemTime(alloc, 1000000000000);
const t2 = ztf.time.now(alloc);
try ztf.expect(alloc, t2).toBe(@as(i64, 1000000000000));
// Real time
ztf.useRealTimers(alloc);
const t3 = ztf.time.now(alloc);
try ztf.expect(alloc, t3).toBeGreaterThan(@as(i64, 1000000000000));
}
}.testSuite);
// Test 7: ISO Date Parsing Edge Cases
try ztf.describe(allocator, "ISO Date Parsing", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should parse dates with time", testFullISO);
try ztf.it(alloc, "should parse dates with milliseconds", testISOWithMillis);
}
fn testFullISO(alloc: std.mem.Allocator) !void {
const ts1 = try ztf.DateHelper.fromISO("2020-01-01T00:00:00.000Z");
const ts2 = try ztf.DateHelper.fromISO("2020-06-15T12:30:45.123Z");
// Verify year extraction works
const year1 = ztf.DateHelper.getYear(ts1);
const year2 = ztf.DateHelper.getYear(ts2);
try ztf.expect(alloc, year1).toBe(@as(u16, 2020));
try ztf.expect(alloc, year2).toBe(@as(u16, 2020));
// ts2 should be later than ts1 (both are 2020, but ts2 is mid-year)
try ztf.expect(alloc, ts2).toBeGreaterThan(ts1);
}
fn testISOWithMillis(alloc: std.mem.Allocator) !void {
const ts1 = try ztf.DateHelper.fromISO("2021-03-15T10:20:30.000Z");
const ts2 = try ztf.DateHelper.fromISO("2021-03-15T10:20:30.999Z");
// Should differ by ~999 milliseconds
const diff = ts2 - ts1;
try ztf.expect(alloc, diff).toBe(@as(i64, 999));
}
}.testSuite);
// Test 8: Time Freezing
try ztf.describe(allocator, "Time Freezing", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should freeze time when using fake timers", testTimeFreezing);
}
fn testTimeFreezing(alloc: std.mem.Allocator) !void {
const frozen_time: i64 = 1577836800000;
ztf.setSystemTime(alloc, frozen_time);
const t1 = ztf.time.now(alloc);
compat.sleep(50 * std.time.ns_per_ms); // Sleep 50ms
const t2 = ztf.time.now(alloc);
compat.sleep(50 * std.time.ns_per_ms); // Sleep another 50ms
const t3 = ztf.time.now(alloc);
// Time should remain frozen
try ztf.expect(alloc, t1).toBe(frozen_time);
try ztf.expect(alloc, t2).toBe(frozen_time);
try ztf.expect(alloc, t3).toBe(frozen_time);
try ztf.expect(alloc, t1).toBe(t2);
try ztf.expect(alloc, t2).toBe(t3);
// Clean up
ztf.setSystemTime(alloc, null);
}
}.testSuite);
// Test 9: Time Mocking in beforeAll/afterAll
try ztf.describe(allocator, "Time Mocking in Hooks", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.beforeAll(alloc, beforeAllHook);
try ztf.afterAll(alloc, afterAllHook);
try ztf.it(alloc, "should have mocked time from beforeAll", testWithMockedTime);
}
fn beforeAllHook(alloc: std.mem.Allocator) !void {
ztf.setSystemTime(alloc, 1577836800000); // 2020-01-01
}
fn afterAllHook(alloc: std.mem.Allocator) !void {
ztf.setSystemTime(alloc, null); // Reset
}
fn testWithMockedTime(alloc: std.mem.Allocator) !void {
const year = ztf.DateHelper.getYear(ztf.time.now(alloc));
try ztf.expect(alloc, year).toBe(@as(u16, 2020));
}
}.testSuite);
// Test 10: Complex Time Scenarios
try ztf.describe(allocator, "Complex Time Scenarios", struct {
fn testSuite(alloc: std.mem.Allocator) !void {
try ztf.it(alloc, "should handle multiple time changes", testMultipleChanges);
try ztf.it(alloc, "should handle large time jumps", testLargeJumps);
}
fn testMultipleChanges(alloc: std.mem.Allocator) !void {
// Set to 2020
ztf.setSystemTime(alloc, 1577836800000);
try ztf.expect(alloc, ztf.DateHelper.getYear(ztf.time.now(alloc))).toBe(@as(u16, 2020));
// Jump to 2021
ztf.setSystemTime(alloc, 1609459200000);
try ztf.expect(alloc, ztf.DateHelper.getYear(ztf.time.now(alloc))).toBe(@as(u16, 2021));
// Jump to 2023
ztf.setSystemTime(alloc, 1672531200000);
try ztf.expect(alloc, ztf.DateHelper.getYear(ztf.time.now(alloc))).toBe(@as(u16, 2023));
// Reset
ztf.setSystemTime(alloc, null);
}
fn testLargeJumps(alloc: std.mem.Allocator) !void {
// Start at 2020
ztf.setSystemTime(alloc, 1577836800000);
// Jump forward 10 years worth of milliseconds
const ten_years_ms: i64 = 10 * 365 * 24 * 60 * 60 * 1000;
ztf.advanceTimersByTime(alloc, ten_years_ms);
const new_time = ztf.time.now(alloc);
try ztf.expect(alloc, new_time).toBe(1577836800000 + ten_years_ms);
// Clean up
ztf.setSystemTime(alloc, null);
}
}.testSuite);
// Run all tests
const registry = ztf.getRegistry(allocator);
const success = try ztf.runTestsWithOptions(allocator, registry, .{
.reporter_type = .spec,
.use_colors = false,
});
// Clean up
ztf.cleanupRegistry();
ztf.cleanupTimeMock();
if (!success) {
std.debug.print("\nSome time tests failed!\n", .{});
std.process.exit(1);
}
std.debug.print("\n✓ All time manipulation tests passed!\n", .{});
}