|
| 1 | +import unittest |
| 2 | + |
| 3 | +from discord import Colour, Embed |
| 4 | +from discord.ext.commands import BadArgument |
| 5 | + |
| 6 | +from bot.exts.utils.utils import Utils, ZEN_OF_PYTHON |
| 7 | +from tests.helpers import MockBot, MockContext |
| 8 | + |
| 9 | + |
| 10 | +class ZenTests(unittest.IsolatedAsyncioTestCase): |
| 11 | + """ Tests for the `!zen` command. """ |
| 12 | + |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + self.bot = MockBot() |
| 16 | + self.cog = Utils(self.bot) |
| 17 | + self.ctx = MockContext() |
| 18 | + |
| 19 | + self.zen_list = ZEN_OF_PYTHON.splitlines() |
| 20 | + self.template_embed = Embed(colour=Colour.og_blurple(), title="The Zen of Python", description=ZEN_OF_PYTHON) |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + async def test_zen_without_arguments(self): |
| 25 | + """ Tests if the `!zen` command reacts properly to no arguments. """ |
| 26 | + self.template_embed.title += ", by Tim Peters" |
| 27 | + |
| 28 | + |
| 29 | + await self.cog.zen.callback(self.cog,self.ctx, search_value = None) |
| 30 | + self.ctx.send.assert_called_once_with(embed=self.template_embed) |
| 31 | + |
| 32 | + async def test_zen_with_valid_index(self): |
| 33 | + """ Tests if the `!zen` command reacts properly to a valid index as an argument. """ |
| 34 | + expected_results = { |
| 35 | + 0: ("The Zen of Python (line 0):", "Beautiful is better than ugly."), |
| 36 | + 10: ("The Zen of Python (line 10):", "Unless explicitly silenced."), |
| 37 | + 18: ("The Zen of Python (line 18):", "Namespaces are one honking great idea -- let's do more of those!"), |
| 38 | + -1: ("The Zen of Python (line 18):", "Namespaces are one honking great idea -- let's do more of those!"), |
| 39 | + -10: ("The Zen of Python (line 9):", "Errors should never pass silently."), |
| 40 | + -19: ("The Zen of Python (line 0):", "Beautiful is better than ugly.") |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + for index, (title, description) in expected_results.items(): |
| 45 | + self.template_embed.title = title |
| 46 | + self.template_embed.description = description |
| 47 | + ctx = MockContext() |
| 48 | + with self.subTest(index = index, expected_title=title, expected_description = description): |
| 49 | + await self.cog.zen.callback(self.cog, ctx, search_value = str(index)) |
| 50 | + ctx.send.assert_called_once_with(embed = self.template_embed) |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + async def test_zen_with_invalid_index(self): |
| 55 | + """ Tests if the `!zen` command reacts properly to an out-of-bounds index as an argument. """ |
| 56 | + # Negative index |
| 57 | + with self.subTest(index = -20), self.assertRaises(BadArgument): |
| 58 | + await self.cog.zen.callback(self.cog, self.ctx, search_value="-20") |
| 59 | + |
| 60 | + # Positive index |
| 61 | + with self.subTest(index = len(ZEN_OF_PYTHON)), self.assertRaises(BadArgument): |
| 62 | + await self.cog.zen.callback(self.cog, self.ctx, search_value=str(len(ZEN_OF_PYTHON))) |
| 63 | + |
| 64 | + async def test_zen_with_valid_slices(self): |
| 65 | + """ Tests if the `!zen` command reacts properly to valid slices for indexing as an argument. """ |
| 66 | + |
| 67 | + expected_results = { |
| 68 | + "0:19": ("The Zen of Python (lines 0-18):", "\n".join(self.zen_list[0:19])), |
| 69 | + "0:": ("The Zen of Python (lines 0-18):", "\n".join(self.zen_list[0:])), |
| 70 | + "-2:-1": ("The Zen of Python (lines 17-17):", self.zen_list[17]), |
| 71 | + "0:-1": ("The Zen of Python (lines 0-17):", "\n".join(self.zen_list[0:-1])), |
| 72 | + "10:13": ("The Zen of Python (lines 10-12):", "\n".join(self.zen_list[10:13])) |
| 73 | + } |
| 74 | + |
| 75 | + for input_slice, (title, description) in expected_results.items(): |
| 76 | + self.template_embed.title = title |
| 77 | + self.template_embed.description = description |
| 78 | + |
| 79 | + ctx = MockContext() |
| 80 | + with self.subTest(input_slice=input_slice, expected_title=title, expected_description=description): |
| 81 | + await self.cog.zen.callback(self.cog, ctx, search_value=input_slice) |
| 82 | + ctx.send.assert_called_once_with(embed = self.template_embed) |
| 83 | + |
| 84 | + async def test_zen_with_invalid_slices(self): |
| 85 | + """ Tests if the `!zen` command reacts properly to invalid slices for indexing as an argument. """ |
| 86 | + slices= ["19:", "10:9", "-1:-2", "0:20", "-100:", "0:-100"] |
| 87 | + |
| 88 | + for input_slice in slices: |
| 89 | + with self.subTest(input_slice = input_slice), self.assertRaises(BadArgument): |
| 90 | + await self.cog.zen.callback(self.cog, self.ctx, search_value=input_slice) |
0 commit comments