Skip to content

Commit 9b4162b

Browse files
committed
Create tests for the faulty code blocks finder
Create the unit tests that check whether the finder recognizes Python strings as codeblocks.
1 parent d00125c commit 9b4162b

3 files changed

Lines changed: 91 additions & 1 deletion

File tree

‎bot/exts/info/codeblock/_instructions.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def get_instructions(content: str) -> str | None:
148148
instructions = _get_no_ticks_message(content)
149149
else:
150150
log.trace("Searching results for a code block with invalid ticks.")
151-
block = next((block for block in blocks if block.tick != _parsing.BACKTICK), None)
151+
block = _parsing._get_block_with_invalid_ticks(blocks)
152152

153153
if block:
154154
log.trace("A code block exists but has invalid ticks.")

‎bot/exts/info/codeblock/_parsing.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,16 @@ def _fix_indentation(content: str) -> str:
245245
content = first_line + "".join(line[first_indent:] for line in lines[1:])
246246

247247
return content
248+
249+
250+
def _get_block_with_invalid_ticks(blocks: Sequence[CodeBlock]) -> CodeBlock | None:
251+
"""
252+
Find a block with invalid ticks and return it.
253+
254+
Return `None` if there are no blocks with invalid ticks.
255+
"""
256+
for block in blocks:
257+
if block.tick != BACKTICK:
258+
return block
259+
260+
return None

‎tests/bot/exts/info/codeblock/test_parsing.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from textwrap import dedent
23

34
from bot.exts.info.codeblock import _parsing as parsing
45

@@ -92,6 +93,36 @@ def test_should_not_recognize_normal_double_quotes_python_text(self):
9293
self.assertIsNotNone(faulty_code_blocks)
9394
self.assertEqual(len(faulty_code_blocks), 0)
9495

96+
def test_should_not_recognize_multiline_single_quote_python_text(self) -> None:
97+
message = dedent("""
98+
```py
99+
\'\'\'A script that iterates and prints the numbers\'\'\'
100+
numbs = [1, 2, 3]
101+
102+
for numb in numbs:
103+
print(numb)
104+
```
105+
""").strip()
106+
107+
faulty_code_blocks = parsing.find_faulty_code_blocks(message)
108+
109+
self.assertIsNone(faulty_code_blocks)
110+
111+
def test_should_not_recognize_multiline_double_quote_python_text(self) -> None:
112+
message = dedent('''
113+
```py
114+
"""A script that iterates and prints the numbers"""
115+
numbs = [1, 2, 3]
116+
117+
for numb in numbs:
118+
print(numb)
119+
```
120+
''').strip()
121+
122+
faulty_code_blocks = parsing.find_faulty_code_blocks(message)
123+
124+
self.assertIsNone(faulty_code_blocks)
125+
95126
def test_should_recognize_single_backtick_no_language(self):
96127
message = """`
97128
x = 4
@@ -151,3 +182,49 @@ def test_should_recognize_wrong_number_of_backticks(self):
151182
faulty_code_blocks = parsing.find_faulty_code_blocks(message)
152183
self.assertIsNotNone(faulty_code_blocks)
153184
self.assertEqual(len(faulty_code_blocks), 1)
185+
186+
187+
class FindCodeblockWithInvalidTicksTest(unittest.TestCase):
188+
def test_should_return_codeblock_with_single_quotes(self) -> None:
189+
first_block_content = dedent('''
190+
"""A script that iterates and prints the numbers"""
191+
numbs = [1, 2, 3]
192+
193+
for numb in numbs:
194+
print(numb)
195+
''').strip()
196+
first_block_language = "py"
197+
first_block_tick = parsing.BACKTICK
198+
first_block_ticks = first_block_tick * 3
199+
200+
second_block_content = dedent('''
201+
"""A script that iterates and prints the letters"""
202+
letters = ["a", "b", "c"]
203+
204+
for letter in letters:
205+
print(letter)
206+
''').strip()
207+
second_block_language = "py"
208+
second_block_tick = "'"
209+
second_block_ticks = second_block_tick * 3
210+
211+
first_block = parsing.CodeBlock(
212+
first_block_content,
213+
first_block_language,
214+
first_block_ticks,
215+
first_block_tick,
216+
True,
217+
)
218+
second_block = parsing.CodeBlock(
219+
second_block_content,
220+
second_block_language,
221+
second_block_ticks,
222+
second_block_tick,
223+
True,
224+
)
225+
blocks = (first_block, second_block)
226+
expected_block = second_block
227+
228+
block_with_invalid_ticks = parsing._get_block_with_invalid_ticks(blocks)
229+
230+
self.assertEqual(block_with_invalid_ticks, expected_block)

0 commit comments

Comments
 (0)