Compare commits

..

2 Commits

Author SHA1 Message Date
eb3dd98adb added testcases for messages.py 2023-08-18 16:35:34 +02:00
5862071b3b added new module 'message.py' 2023-08-18 16:35:34 +02:00
2 changed files with 7 additions and 10 deletions

View File

@ -22,16 +22,13 @@ def source_code(text: str, include_delims: bool = False) -> list[str]:
code_lines: list[str] = [] code_lines: list[str] = []
in_code_block = False in_code_block = False
print(text)
for line in text.split('\n'): for line in text.split('\n'):
if line.strip().startswith("```"): if line.strip().startswith('```'):
if include_delims: if include_delims:
code_lines.append(line) code_lines.append(line)
if in_code_block: if in_code_block:
code_sections.append("\n".join(code_lines)) code_sections.append('\n'.join(code_lines) + '\n')
code_lines.clear() code_lines.clear()
print(f" --> New code section:\n{code_sections}")
in_code_block = not in_code_block in_code_block = not in_code_block
elif in_code_block: elif in_code_block:
code_lines.append(line) code_lines.append(line)

View File

@ -363,8 +363,8 @@ class SourceCodeTestCase(CmmTestCase):
``` ```
""" """
expected_result = [ expected_result = [
" ```python\n print(\"Hello, World!\")\n ```", " ```python\n print(\"Hello, World!\")\n ```\n",
" ```python\n x = 10\n y = 20\n print(x + y)\n```" " ```python\n x = 10\n y = 20\n print(x + y)\n ```\n"
] ]
result = source_code(text, include_delims=True) result = source_code(text, include_delims=True)
self.assertEqual(result, expected_result) self.assertEqual(result, expected_result)
@ -383,15 +383,15 @@ class SourceCodeTestCase(CmmTestCase):
``` ```
""" """
expected_result = [ expected_result = [
" print(\"Hello, World!\")\n", " print(\"Hello, World!\")\n",
" x = 10\n y = 20\n print(x + y\n)" " x = 10\n y = 20\n print(x + y)\n"
] ]
result = source_code(text, include_delims=False) result = source_code(text, include_delims=False)
self.assertEqual(result, expected_result) self.assertEqual(result, expected_result)
def test_source_code_with_single_code_block(self) -> None: def test_source_code_with_single_code_block(self) -> None:
text = "```python\nprint(\"Hello, World!\")\n```" text = "```python\nprint(\"Hello, World!\")\n```"
expected_result = ["```python\nprint(\"Hello, World!\")\n```"] expected_result = ["```python\nprint(\"Hello, World!\")\n```\n"]
result = source_code(text, include_delims=True) result = source_code(text, include_delims=True)
self.assertEqual(result, expected_result) self.assertEqual(result, expected_result)