Skip to content

Commit 6bb14d4

Browse files
committed
Make parse date error tests independent of when tests run
Some error messages depends on what day/month it is, and I prefer being less exact in testing the message rather than mocking the date so that the message is always the same.
1 parent 7b15270 commit 6bb14d4

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/date.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const std = @import("std");
22
const builtin = @import("builtin");
33
const epoch = std.time.epoch;
44
const windows = std.os.windows;
5+
const expectEqual = std.testing.expectEqual;
56

67
const Allocator = std.mem.Allocator;
78

src/test.zig

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const std = @import("std");
22
const date = @import("date.zig");
33
const main = @import("main.zig");
44
const color = @import("color.zig");
5+
56
const testing = std.testing;
7+
const expect = testing.expect;
68
const expectEqual = testing.expectEqual;
79
const expectEqualSlices = testing.expectEqualSlices;
810

@@ -72,7 +74,7 @@ test "basic" {
7274
try expectEqual(@as(u32, 0), meta._padding);
7375
try expectEqual(chain_db.chains.items.len, 0);
7476
}
75-
try expectErrorMessage(db, "No chain found with index '1'", "link 1 20240101");
77+
try expectErrorMessage(db, "No chain found with index '1'", "link 1 20240101", .exact);
7678
}
7779

7880
test "linking same day twice" {
@@ -84,7 +86,7 @@ test "linking same day twice" {
8486

8587
const links = &.{ Link{ .chain_id = 0, .timestamp = 1704063600 }};
8688
try expectLinks(&db, links);
87-
try expectErrorMessage(db, "Link already exists on date 2024-01-01, skipping", "link 1 20240101");
89+
try expectErrorMessage(db, "Link already exists on date 2024-01-01, skipping", "link 1 20240101", .exact);
8890
try expectLinks(&db, links);
8991
}
9092

@@ -214,26 +216,27 @@ test "parse date error" {
214216

215217
try run(db, "add foo daily");
216218

217-
try expectErrorMessage(db, "Invalid link date '2023011', does not match any format", "link 1 2023011");
218-
try expectErrorMessage(db, "Invalid link date '-123', does not match any format", "link 1 -123");
219-
try expectErrorMessage(db, "Invalid link date 'asdf', does not match any format", "link 1 asdf");
220-
try expectErrorMessage(db, "Invalid link date '100', does not match any format", "link 1 100");
221-
try expectErrorMessage(db, "Invalid link date '99th', out of range for December which has 31 days", "link 1 99th");
222-
try expectErrorMessage(db, "Invalid link date '0th', does not match any format", "link 1 0th");
223-
try expectErrorMessage(db, "Invalid link date '20100101', year before 2022 not supported", "link 1 20100101");
219+
try expectErrorMessage(db, "Invalid link date '2023011', does not match any format", "link 1 2023011", .exact);
220+
try expectErrorMessage(db, "Invalid link date '-123', does not match any format", "link 1 -123", .exact);
221+
try expectErrorMessage(db, "Invalid link date 'asdf', does not match any format", "link 1 asdf", .exact);
222+
try expectErrorMessage(db, "Invalid link date '100', does not match any format", "link 1 100", .exact);
223+
try expectErrorMessage(db, "Invalid link date '0th', does not match any format", "link 1 0th", .exact);
224+
try expectErrorMessage(db, "Invalid link date '20100101', year before 2022 not supported", "link 1 20100101", .exact);
225+
226+
try expectErrorMessage(db, "Invalid link date '99th'", "link 1 99th", .prefix);
224227
}
225228

226229
test "error messages" {
227230
// now = 2024-01-26T00:00:00Z
228231
var db = try TestDb.init(.{ .override_now = 1706227200 });
229232
defer db.deinit();
230233

231-
try expectErrorMessage(db, "Expected 'kind' argument", "add foo");
232-
try expectErrorMessage(db, "No chain found with index '1'", "link 1");
233-
try expectErrorMessage(db, "No chain found with index '1'", "unlink 1");
234+
try expectErrorMessage(db, "Expected 'kind' argument", "add foo", .exact);
235+
try expectErrorMessage(db, "No chain found with index '1'", "link 1", .exact);
236+
try expectErrorMessage(db, "No chain found with index '1'", "unlink 1", .exact);
234237

235238
try run(db, "add foo daily");
236-
try expectErrorMessage(db, "No link found on 2024-01-26", "unlink 1");
239+
try expectErrorMessage(db, "No link found on 2024-01-26", "unlink 1", .exact);
237240
}
238241

239242
const TestDb = struct {
@@ -300,12 +303,15 @@ fn expectLinks(db: *TestDb, expected: []const Link) !void {
300303
);
301304
}
302305

303-
fn expectErrorMessage(db: TestDb, message: []const u8, input: []const u8) !void {
306+
fn expectErrorMessage(db: TestDb, message: []const u8, input: []const u8, mode: enum { exact, prefix }) !void {
304307
const result = try runCapture(db, input);
305308
defer allocator.free(result.stdout);
306309
defer allocator.free(result.stderr);
307310
var it = std.mem.split(u8, result.stdout, "\n");
308-
try expectEqualSlices(u8, message, it.next().?);
311+
switch (mode) {
312+
.exact => try expectEqualSlices(u8, message, it.next().?),
313+
.prefix => try expect(std.mem.startsWith(u8, it.next().?, message)),
314+
}
309315
}
310316

311317
fn run(db: TestDb, input: []const u8) !void {

0 commit comments

Comments
 (0)