diff options
author | 2025-03-24 20:35:29 -0300 | |
---|---|---|
committer | 2025-03-24 20:35:29 -0300 | |
commit | 756419124c0f2c1c808650ad9d62b12cfbaef0a0 (patch) | |
tree | 5b7c9010631138d74c03b4ee1d80cf9fc3c978ce /tests | |
parent | Update end index display when slicing in the zen command (diff) |
Added tests for the zen command
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bot/exts/utils/test_utils.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/tests/bot/exts/utils/test_utils.py b/tests/bot/exts/utils/test_utils.py new file mode 100644 index 000000000..5392e3512 --- /dev/null +++ b/tests/bot/exts/utils/test_utils.py @@ -0,0 +1,90 @@ +import unittest + +from discord import Colour, Embed +from discord.ext.commands import BadArgument + +from bot.exts.utils.utils import Utils, ZEN_OF_PYTHON +from tests.helpers import MockBot, MockContext + + +class ZenTests(unittest.IsolatedAsyncioTestCase): + """ Tests for the `!zen` command. """ + + + def setUp(self): + self.bot = MockBot() + self.cog = Utils(self.bot) + self.ctx = MockContext() + + self.zen_list = ZEN_OF_PYTHON.splitlines() + self.template_embed = Embed(colour=Colour.og_blurple(), title="The Zen of Python", description=ZEN_OF_PYTHON) + + + + async def test_zen_without_arguments(self): + """ Tests if the `!zen` command reacts properly to no arguments. """ + self.template_embed.title += ", by Tim Peters" + + + await self.cog.zen.callback(self.cog,self.ctx, search_value = None) + self.ctx.send.assert_called_once_with(embed=self.template_embed) + + async def test_zen_with_valid_index(self): + """ Tests if the `!zen` command reacts properly to a valid index as an argument. """ + expected_results = { + 0: ("The Zen of Python (line 0):", "Beautiful is better than ugly."), + 10: ("The Zen of Python (line 10):", "Unless explicitly silenced."), + 18: ("The Zen of Python (line 18):", "Namespaces are one honking great idea -- let's do more of those!"), + -1: ("The Zen of Python (line 18):", "Namespaces are one honking great idea -- let's do more of those!"), + -10: ("The Zen of Python (line 9):", "Errors should never pass silently."), + -19: ("The Zen of Python (line 0):", "Beautiful is better than ugly.") + + } + + for index, (title, description) in expected_results.items(): + self.template_embed.title = title + self.template_embed.description = description + ctx = MockContext() + with self.subTest(index = index, expected_title=title, expected_description = description): + await self.cog.zen.callback(self.cog, ctx, search_value = str(index)) + ctx.send.assert_called_once_with(embed = self.template_embed) + + + + async def test_zen_with_invalid_index(self): + """ Tests if the `!zen` command reacts properly to an out-of-bounds index as an argument. """ + # Negative index + with self.subTest(index = -20), self.assertRaises(BadArgument): + await self.cog.zen.callback(self.cog, self.ctx, search_value="-20") + + # Positive index + with self.subTest(index = len(ZEN_OF_PYTHON)), self.assertRaises(BadArgument): + await self.cog.zen.callback(self.cog, self.ctx, search_value=str(len(ZEN_OF_PYTHON))) + + async def test_zen_with_valid_slices(self): + """ Tests if the `!zen` command reacts properly to valid slices for indexing as an argument. """ + + expected_results = { + "0:19": ("The Zen of Python (lines 0-18):", "\n".join(self.zen_list[0:19])), + "0:": ("The Zen of Python (lines 0-18):", "\n".join(self.zen_list[0:])), + "-2:-1": ("The Zen of Python (lines 17-17):", self.zen_list[17]), + "0:-1": ("The Zen of Python (lines 0-17):", "\n".join(self.zen_list[0:-1])), + "10:13": ("The Zen of Python (lines 10-12):", "\n".join(self.zen_list[10:13])) + } + + for input_slice, (title, description) in expected_results.items(): + self.template_embed.title = title + self.template_embed.description = description + + ctx = MockContext() + with self.subTest(input_slice=input_slice, expected_title=title, expected_description=description): + await self.cog.zen.callback(self.cog, ctx, search_value=input_slice) + ctx.send.assert_called_once_with(embed = self.template_embed) + + async def test_zen_with_invalid_slices(self): + """ Tests if the `!zen` command reacts properly to invalid slices for indexing as an argument. """ + slices= ["19:", "10:9", "-1:-2", "0:20", "-100:", "0:-100"] + + for input_slice in slices: + with self.subTest(input_slice = input_slice), self.assertRaises(BadArgument): + await self.cog.zen.callback(self.cog, self.ctx, search_value=input_slice) |