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" {
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