Skip to content

Commit 001bdca

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 001bdca

4 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_instructions.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,52 @@
44
from bot.exts.info.codeblock import _instructions as instructions, _parsing as parsing
55

66

7+
class FindCodeblockWithInvalidTicksTest(unittest.TestCase):
8+
def test_should_return_codeblock_with_single_quotes(self) -> None:
9+
first_block_content = dedent('''
10+
"""A script that iterates and prints the numbers"""
11+
numbs = [1, 2, 3]
12+
13+
for numb in numbs:
14+
print(numb)
15+
''').strip()
16+
first_block_language = "py"
17+
first_block_tick = parsing.BACKTICK
18+
first_block_ticks = first_block_tick * 3
19+
20+
second_block_content = dedent('''
21+
"""A script that iterates and prints the letters"""
22+
letters = ["a", "b", "c"]
23+
24+
for letter in letters:
25+
print(letter)
26+
''').strip()
27+
second_block_language = "py"
28+
second_block_tick = "'"
29+
second_block_ticks = second_block_tick * 3
30+
31+
first_block = parsing.CodeBlock(
32+
first_block_content,
33+
first_block_language,
34+
first_block_ticks,
35+
first_block_tick,
36+
True,
37+
)
38+
second_block = parsing.CodeBlock(
39+
second_block_content,
40+
second_block_language,
41+
second_block_ticks,
42+
second_block_tick,
43+
True,
44+
)
45+
blocks = (first_block, second_block)
46+
expected_block = second_block
47+
48+
block_with_invalid_ticks = parsing._get_block_with_invalid_ticks(blocks)
49+
50+
self.assertEqual(block_with_invalid_ticks, expected_block)
51+
52+
753
class ProvideBadTicksInstructionsTest(unittest.TestCase):
854
def __assert_is_instructions_for_message_bad_ticks_one(self, message: str) -> None:
955
code_blocks = parsing.find_faulty_code_blocks(message)

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

Lines changed: 31 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

0 commit comments

Comments
 (0)