diff options
Diffstat (limited to '')
| -rw-r--r-- | bot/exts/utils/snekbox.py | 8 | ||||
| -rw-r--r-- | tests/bot/exts/utils/test_snekbox.py | 7 | 
2 files changed, 14 insertions, 1 deletions
| diff --git a/bot/exts/utils/snekbox.py b/bot/exts/utils/snekbox.py index 77830209e..295c84901 100644 --- a/bot/exts/utils/snekbox.py +++ b/bot/exts/utils/snekbox.py @@ -74,7 +74,13 @@ class Snekbox(Cog):      @staticmethod      def prepare_input(code: str) -> str: -        """Extract code from the Markdown, format it, and insert it into the code template.""" +        """ +        Extract code from the Markdown, format it, and insert it into the code template. + +        If there is Markdown, ignores surrounding text. +        If there are several Markdown parts in the message, concatenates only the code blocks. +        If there is inline code but no code blocks, takes the first instance of inline code. +        """          if match := list(FORMATTED_CODE_REGEX.finditer(code)):              blocks = [block for block in match if block.group("block")] diff --git a/tests/bot/exts/utils/test_snekbox.py b/tests/bot/exts/utils/test_snekbox.py index 6601fad2c..9a42d0610 100644 --- a/tests/bot/exts/utils/test_snekbox.py +++ b/tests/bot/exts/utils/test_snekbox.py @@ -52,6 +52,13 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase):              ('`print("Hello world!")`', 'print("Hello world!")', 'one line code block'),              ('```\nprint("Hello world!")```', 'print("Hello world!")', 'multiline code block'),              ('```py\nprint("Hello world!")```', 'print("Hello world!")', 'multiline python code block'), +            ('text```print("Hello world!")```text', 'print("Hello world!")', 'code block surrounded by text'), +            ('```print("Hello world!")```\ntext\n```py\nprint("Hello world!")```', +             'print("Hello world!")\nprint("Hello world!")', 'two code blocks with text in-between'), +            ('`print("Hello world!")`\ntext\n```print("How\'s it going?")```', +             'print("How\'s it going?")', 'code block preceded by inline code'), +            ('`print("Hello world!")`\ntext\n`print("Hello world!")`', +             'print("Hello world!")', 'one inline code block of two')          )          for case, expected, testname in cases:              with self.subTest(msg=f'Extract code from {testname}.'): | 
