From b60b5916f8804eae75615b4ba70cc72b3a0d766c Mon Sep 17 00:00:00 2001 From: Klemek Date: Thu, 8 Apr 2021 23:50:33 +0200 Subject: [PATCH] more tests --- miniscord/_bot.py | 9 +- tests/unit/miniscord/test_bot.py | 177 +++++++++++++++++++++++++++++-- 2 files changed, 172 insertions(+), 14 deletions(-) diff --git a/miniscord/_bot.py b/miniscord/_bot.py index abcb50e..a867d26 100644 --- a/miniscord/_bot.py +++ b/miniscord/_bot.py @@ -193,7 +193,7 @@ class Bot(object): message.content = re.sub(r"<@!?[^>]+>", "", message.content) elif is_mention: message.content = re.sub( - f"^<@!?{self.client.user.id}>", "", message.content + f"<@!?{self.client.user.id}>", "", message.content, count=1 ) command_args = parse_arguments(message.content) @@ -212,12 +212,13 @@ class Bot(object): command_found = False + if self.lower_command_names: + command_args[0] = command_args[0].lower() + for command in self.__commands: if re.match( command.regex, - command_args[0].lower() - if self.lower_command_names - else command_args[0], + command_args[0], ): if self.log_calls: debug(message, str(command_args)) diff --git a/tests/unit/miniscord/test_bot.py b/tests/unit/miniscord/test_bot.py index 3a4478f..e9f0781 100644 --- a/tests/unit/miniscord/test_bot.py +++ b/tests/unit/miniscord/test_bot.py @@ -122,6 +122,17 @@ class TestHelp(AsyncTestCase): "desc2", reference=message, mention_author=False ) + @patch_discord + def test_not_found(self): + bot = Bot("app_name", "version") + message = AsyncMock() + self._await(bot.help(None, message, "help", "notfound")) + message.channel.send.assert_awaited_once_with( + f"Command `notfound` not found", + reference=message, + mention_author=False, + ) + class TestRegisterEvent(TestCase): @skip @@ -313,25 +324,171 @@ class TestOnMessage(AsyncTestCase): f" #test_channel in server 'test_guild'" ) - @skip + @patch_discord def test_mention_self(self): - self.fail("not implemented") + bot = Bot("app_name", "version") + bot.enforce_write_permission = False + bot.client.user.id = "12345" + simple_callback = AsyncMock() + bot.register_command("test", simple_callback, "short", "long") + watcher_callback = AsyncMock() + bot.register_watcher(watcher_callback) + fallback_callback = AsyncMock() + bot.register_fallback(fallback_callback) + message = AsyncMock() + message.content = "<@12345> test arg0 arg1" + message.author = bot.client.user + self._await(bot.on_message(message)) + simple_callback.assert_not_awaited() + watcher_callback.assert_not_awaited() + fallback_callback.assert_not_awaited() - @skip + @patch_discord def test_mention_direct(self): - self.fail("not implemented") + bot = Bot("app_name", "version") + bot.enforce_write_permission = False + bot.client.user.id = "12345" + simple_callback = AsyncMock() + bot.register_command("test", simple_callback, "short", "long") + watcher_callback = AsyncMock() + bot.register_watcher(watcher_callback) + fallback_callback = AsyncMock() + bot.register_fallback(fallback_callback) + message = AsyncMock() + message.content = "<@12345> test arg0 arg1" + message.channel.type == discord.ChannelType.private + self._await(bot.on_message(message)) + simple_callback.assert_awaited_once_with( + bot.client, message, "test", "arg0", "arg1" + ) + watcher_callback.assert_awaited_once_with(bot.client, message) + fallback_callback.assert_not_awaited() - @skip + @patch_discord def test_any_mention(self): - self.fail("not implemented") + bot = Bot("app_name", "version") + bot.enforce_write_permission = False + bot.any_mention = True + bot.client.user.id = "12345" + simple_callback = AsyncMock() + bot.register_command("test", simple_callback, "short", "long") + watcher_callback = AsyncMock() + bot.register_watcher(watcher_callback) + fallback_callback = AsyncMock() + bot.register_fallback(fallback_callback) + message = AsyncMock() + message.content = "test <@12345> arg0 arg1" + message.channel.type == discord.ChannelType.private + message.mentions = [bot.client.user] + self._await(bot.on_message(message)) + simple_callback.assert_awaited_once_with( + bot.client, message, "test", "arg0", "arg1" + ) + watcher_callback.assert_awaited_once_with(bot.client, message) + fallback_callback.assert_not_awaited() - @skip + @patch_discord + def test_any_mention_off(self): + bot = Bot("app_name", "version") + bot.enforce_write_permission = False + bot.any_mention = False + bot.client.user.id = "12345" + simple_callback = AsyncMock() + bot.register_command("test", simple_callback, "short", "long") + watcher_callback = AsyncMock() + bot.register_watcher(watcher_callback) + fallback_callback = AsyncMock() + bot.register_fallback(fallback_callback) + message = AsyncMock() + message.content = "test <@12345> arg0 arg1" + message.channel.type == discord.ChannelType.private + message.mentions = [bot.client.user] + self._await(bot.on_message(message)) + simple_callback.assert_not_awaited() + watcher_callback.assert_awaited_once_with(bot.client, message) + fallback_callback.assert_not_awaited() + + @patch_discord def test_remove_mentions(self): - self.fail("not implemented") + bot = Bot("app_name", "version") + bot.enforce_write_permission = False + bot.remove_mentions = True + bot.client.user.id = "12345" + simple_callback = AsyncMock() + bot.register_command("test", simple_callback, "short", "long") + watcher_callback = AsyncMock() + bot.register_watcher(watcher_callback) + fallback_callback = AsyncMock() + bot.register_fallback(fallback_callback) + message = AsyncMock() + message.content = "<@12345> test <@12345> arg1" + self._await(bot.on_message(message)) + simple_callback.assert_awaited_once_with(bot.client, message, "test", "arg1") + watcher_callback.assert_awaited_once_with(bot.client, message) + fallback_callback.assert_not_awaited() - @skip + @patch_discord + def test_remove_mentions_off(self): + bot = Bot("app_name", "version") + bot.enforce_write_permission = False + bot.remove_mentions = False + bot.client.user.id = "12345" + simple_callback = AsyncMock() + bot.register_command("test", simple_callback, "short", "long") + watcher_callback = AsyncMock() + bot.register_watcher(watcher_callback) + fallback_callback = AsyncMock() + bot.register_fallback(fallback_callback) + message = AsyncMock() + message.content = "<@12345> test <@12345> arg1" + self._await(bot.on_message(message)) + simple_callback.assert_awaited_once_with( + bot.client, message, "test", "<@12345>", "arg1" + ) + watcher_callback.assert_awaited_once_with(bot.client, message) + fallback_callback.assert_not_awaited() + + @patch_discord def test_lower_command_names(self): - self.fail("not implemented") + bot = Bot("app_name", "version") + bot.enforce_write_permission = False + bot.lower_command_names = True + bot.client.user.id = "12345" + simple_callback = AsyncMock() + bot.register_command("test", simple_callback, "short", "long") + watcher_callback = AsyncMock() + bot.register_watcher(watcher_callback) + fallback_callback = AsyncMock() + bot.register_fallback(fallback_callback) + message = AsyncMock() + message.content = "<@12345> Test arg0 arg1" + self._await(bot.on_message(message)) + simple_callback.assert_awaited_once_with( + bot.client, message, "test", "arg0", "arg1" + ) + watcher_callback.assert_awaited_once_with(bot.client, message) + fallback_callback.assert_not_awaited() + + @patch_discord + def test_lower_command_names_off(self): + bot = Bot("app_name", "version") + bot.enforce_write_permission = False + bot.lower_command_names = False + bot.client.user.id = "12345" + simple_callback = AsyncMock() + bot.register_command("test", simple_callback, "short", "long") + watcher_callback = AsyncMock() + bot.register_watcher(watcher_callback) + fallback_callback = AsyncMock() + bot.register_fallback(fallback_callback) + message = AsyncMock() + message.content = "<@12345> Test arg0 arg1" + self._await(bot.on_message(message)) + simple_callback.assert_not_awaited() + watcher_callback.assert_awaited_once_with(bot.client, message) + fallback_callback.assert_awaited_once_with( + bot.client, message, "Test", "arg0", "arg1" + ) @skip def test_fire_registered_event(self):