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.
This commit is contained in:
Jacob Jonsson 2026-03-14 11:40:47 +01:00
parent 6796a62a5f
commit a05229f72d
Signed by: Jassob
GPG key ID: 7E30B9B047F7202E

View file

@ -185,15 +185,19 @@ pub const Bot = struct {
} }
}; };
test "deiniting an owned Message leaks no memory" { fn newTestMessage(allocator: std.mem.Allocator, content: []const u8) !*Message {
const allocator = std.testing.allocator; return try Message.init_owned(
const testMessage = try Message.init_owned(
allocator, allocator,
12345, 12345,
"jassob", "jassob",
"#test", "#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); testMessage.deinit(allocator);
} }
@ -207,13 +211,7 @@ test "hear and deinit has no leaks" {
var bot = try Bot.init(allocator); var bot = try Bot.init(allocator);
defer bot.deinit(); defer bot.deinit();
const testMessage = try Message.init_owned( const testMessage = try newTestMessage(allocator, "test");
allocator,
12345,
"jassob",
"#test",
"All your codebase are belong to us.\n",
);
bot.hear(testMessage); bot.hear(testMessage);
try std.testing.expectEqual(0, bot.backlog.top); 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); var bot = try Bot.init(allocator);
defer bot.deinit(); defer bot.deinit();
for (0..2) |i| { for (0..2) |_| {
const testMessage = try Message.init_owned( const testMessage = try newTestMessage(std.testing.allocator, "test");
std.testing.allocator,
@intCast(i),
"jassob",
"#test",
"All your codebase are belong to us.\n",
);
bot.hear(testMessage); bot.hear(testMessage);
} }
@ -243,13 +235,7 @@ test "hear wraps" {
defer bot.deinit(); defer bot.deinit();
for (0..1025) |_| { for (0..1025) |_| {
const testMessage = try Message.init_owned( const testMessage = try newTestMessage(std.testing.allocator, "test");
std.testing.allocator,
12345,
"jassob",
"#test",
"All your codebase are belong to us.\n",
);
bot.hear(testMessage); bot.hear(testMessage);
} }
@ -274,17 +260,12 @@ test "execute substitution no previous message" {
} }
test "execute substitution" { 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(); defer bot.deinit();
// hear original message with typo // hear original message with typo
const msg = try Message.init_owned( const msg = try newTestMessage(allocator, "What");
std.testing.allocator,
1234,
"jassob",
"#test",
"What",
);
bot.hear(msg); bot.hear(msg);
// execute substitution // execute substitution
@ -307,13 +288,7 @@ test "execute substitution with no matching needle" {
defer bot.deinit(); defer bot.deinit();
// hear original message // hear original message
const msg = try Message.init_owned( const msg = try newTestMessage(std.testing.allocator, "original");
std.testing.allocator,
1234,
"jassob",
"#test",
"original",
);
bot.hear(msg); bot.hear(msg);
// execute substitution // execute substitution