Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bot/exts/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ async def zen(
# Try to handle first exact word due difflib.SequenceMatched may use some other similar word instead
# exact word.
for i, line in enumerate(zen_lines):
for word in line.split():
if word.lower() == search_value.lower():
# Only compare the word itself, so "ugly" matches "ugly."
for word in re.findall(r"[\w']+", line.lower()):
if word == search_value.lower():
embed.title += f" (line {i}):"
embed.description = line
await ctx.send(embed=embed)
Expand Down
13 changes: 13 additions & 0 deletions tests/bot/exts/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ async def test_zen_with_valid_slices(self):
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_exact_word_ignores_punctuation(self):
""" Tests if the `!zen` command matches an exact word that has punctuation next to it. """
expected_results = {"ugly": 0, "silenced": 10, "obvious": 12, "RIGHT": 15}

for word, line in expected_results.items():
self.template_embed.title = f"The Zen of Python (line {line}):"
self.template_embed.description = self.zen_list[line]

ctx = MockContext()
with self.subTest(word=word, line=line):
await self.cog.zen.callback(self.cog, ctx, search_value=word)
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:18", "10:9", "-1:-2", "0:-100", "::0", "1:2:-1", "-5:-4:-1"]
Expand Down
Loading