From a05229f72d50854eacc63f0aebbd469c07358abf Mon Sep 17 00:00:00 2001 From: Jacob Jonsson Date: Sat, 14 Mar 2026 11:40:47 +0100 Subject: [PATCH] refactor(bot): add newTestMessage helper This commit reduces the repetitions in the tests when we create new messages. It hardcodes a bunch of fields that are currently not used, but might be eventually. --- src/bot.zig | 57 +++++++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 41 deletions(-) diff --git a/src/bot.zig b/src/bot.zig index aae3b94..1124d3c 100644 --- a/src/bot.zig +++ b/src/bot.zig @@ -185,15 +185,19 @@ pub const Bot = struct { } }; -test "deiniting an owned Message leaks no memory" { - const allocator = std.testing.allocator; - const testMessage = try Message.init_owned( +fn newTestMessage(allocator: std.mem.Allocator, content: []const u8) !*Message { + return try Message.init_owned( allocator, 12345, "jassob", "#test", - "All your codebase are belong to us.\n", + content, ); +} + +test "deiniting an owned Message leaks no memory" { + const allocator = std.testing.allocator; + const testMessage = try newTestMessage(allocator, "test"); testMessage.deinit(allocator); } @@ -207,13 +211,7 @@ test "hear and deinit has no leaks" { var bot = try Bot.init(allocator); defer bot.deinit(); - const testMessage = try Message.init_owned( - allocator, - 12345, - "jassob", - "#test", - "All your codebase are belong to us.\n", - ); + const testMessage = try newTestMessage(allocator, "test"); bot.hear(testMessage); try std.testing.expectEqual(0, bot.backlog.top); @@ -224,14 +222,8 @@ test "a few hears and deinit has no leaks" { var bot = try Bot.init(allocator); defer bot.deinit(); - for (0..2) |i| { - const testMessage = try Message.init_owned( - std.testing.allocator, - @intCast(i), - "jassob", - "#test", - "All your codebase are belong to us.\n", - ); + for (0..2) |_| { + const testMessage = try newTestMessage(std.testing.allocator, "test"); bot.hear(testMessage); } @@ -243,13 +235,7 @@ test "hear wraps" { defer bot.deinit(); for (0..1025) |_| { - const testMessage = try Message.init_owned( - std.testing.allocator, - 12345, - "jassob", - "#test", - "All your codebase are belong to us.\n", - ); + const testMessage = try newTestMessage(std.testing.allocator, "test"); bot.hear(testMessage); } @@ -274,17 +260,12 @@ test "execute substitution no previous message" { } test "execute substitution" { - var bot = try Bot.init(std.testing.allocator); + const allocator = std.testing.allocator; + var bot = try Bot.init(allocator); defer bot.deinit(); // hear original message with typo - const msg = try Message.init_owned( - std.testing.allocator, - 1234, - "jassob", - "#test", - "What", - ); + const msg = try newTestMessage(allocator, "What"); bot.hear(msg); // execute substitution @@ -307,13 +288,7 @@ test "execute substitution with no matching needle" { defer bot.deinit(); // hear original message - const msg = try Message.init_owned( - std.testing.allocator, - 1234, - "jassob", - "#test", - "original", - ); + const msg = try newTestMessage(std.testing.allocator, "original"); bot.hear(msg); // execute substitution