refactor(commands): add a substitute constructor
This commit recuces some repetitiveness from creating substitution literals in tests.
This commit is contained in:
parent
a05229f72d
commit
b0f0daa19d
2 changed files with 12 additions and 14 deletions
|
|
@ -269,9 +269,7 @@ test "execute substitution" {
|
|||
bot.hear(msg);
|
||||
|
||||
// execute substitution
|
||||
const cmd = UserCommand{
|
||||
.substitute = .{ .author = "jassob", .needle = "What", .replacement = "what" },
|
||||
};
|
||||
const cmd = UserCommand.init_substitute("jassob", "What", "what", false);
|
||||
const response = try bot.execute(&cmd, null, "#test");
|
||||
|
||||
// expect response matching the correct message
|
||||
|
|
@ -292,9 +290,7 @@ test "execute substitution with no matching needle" {
|
|||
bot.hear(msg);
|
||||
|
||||
// execute substitution
|
||||
const cmd = UserCommand{
|
||||
.substitute = .{ .author = "jassob", .needle = "something else", .replacement = "weird" },
|
||||
};
|
||||
const cmd = UserCommand.init_substitute("jassob", "something else", "weird", false);
|
||||
try std.testing.expectError(Error.NoMessage, bot.execute(
|
||||
&cmd,
|
||||
null,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,15 @@ pub const UserCommand = union(enum) {
|
|||
/// !help
|
||||
help: void,
|
||||
|
||||
pub fn init_substitute(author: []const u8, needle: []const u8, replacement: []const u8, all: bool) UserCommand {
|
||||
return .{ .substitute = .{
|
||||
.author = author,
|
||||
.needle = needle,
|
||||
.replacement = replacement,
|
||||
.all = all,
|
||||
} };
|
||||
}
|
||||
|
||||
pub fn parse(nick: []const u8, text: []const u8) ?UserCommand {
|
||||
const original = Parser.init(text);
|
||||
if (original.consume_str("!help")) |_| {
|
||||
|
|
@ -38,14 +47,7 @@ pub const UserCommand = union(enum) {
|
|||
return null;
|
||||
}
|
||||
parser, const correction = result.?;
|
||||
return .{
|
||||
.substitute = .{
|
||||
.author = nick,
|
||||
.needle = typo,
|
||||
.replacement = correction,
|
||||
.all = false,
|
||||
},
|
||||
};
|
||||
return .init_substitute(nick, typo, correction, false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue