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);
|
bot.hear(msg);
|
||||||
|
|
||||||
// execute substitution
|
// execute substitution
|
||||||
const cmd = UserCommand{
|
const cmd = UserCommand.init_substitute("jassob", "What", "what", false);
|
||||||
.substitute = .{ .author = "jassob", .needle = "What", .replacement = "what" },
|
|
||||||
};
|
|
||||||
const response = try bot.execute(&cmd, null, "#test");
|
const response = try bot.execute(&cmd, null, "#test");
|
||||||
|
|
||||||
// expect response matching the correct message
|
// expect response matching the correct message
|
||||||
|
|
@ -292,9 +290,7 @@ test "execute substitution with no matching needle" {
|
||||||
bot.hear(msg);
|
bot.hear(msg);
|
||||||
|
|
||||||
// execute substitution
|
// execute substitution
|
||||||
const cmd = UserCommand{
|
const cmd = UserCommand.init_substitute("jassob", "something else", "weird", false);
|
||||||
.substitute = .{ .author = "jassob", .needle = "something else", .replacement = "weird" },
|
|
||||||
};
|
|
||||||
try std.testing.expectError(Error.NoMessage, bot.execute(
|
try std.testing.expectError(Error.NoMessage, bot.execute(
|
||||||
&cmd,
|
&cmd,
|
||||||
null,
|
null,
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,15 @@ pub const UserCommand = union(enum) {
|
||||||
/// !help
|
/// !help
|
||||||
help: void,
|
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 {
|
pub fn parse(nick: []const u8, text: []const u8) ?UserCommand {
|
||||||
const original = Parser.init(text);
|
const original = Parser.init(text);
|
||||||
if (original.consume_str("!help")) |_| {
|
if (original.consume_str("!help")) |_| {
|
||||||
|
|
@ -38,14 +47,7 @@ pub const UserCommand = union(enum) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
parser, const correction = result.?;
|
parser, const correction = result.?;
|
||||||
return .{
|
return .init_substitute(nick, typo, correction, false);
|
||||||
.substitute = .{
|
|
||||||
.author = nick,
|
|
||||||
.needle = typo,
|
|
||||||
.replacement = correction,
|
|
||||||
.all = false,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue