-
-
Notifications
You must be signed in to change notification settings - Fork 747
Fix Codeblock False Positives #3517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SamuelRoettgermann
wants to merge
9
commits into
python-discord:main
Choose a base branch
from
SamuelRoettgermann:fix-codeblock-false-positives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−12
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2d52c17
enhanced codeblock regex and made stricter requirements for valid cod…
SamuelRoettgermann e23f230
added empty newline for readability
SamuelRoettgermann 72a988b
minor code cleanup
SamuelRoettgermann 9b9bdb1
added unit tests for the codeblock detection
SamuelRoettgermann f272a86
made faulty codeblock detection more restrictive and improved bad tic…
SamuelRoettgermann 3770cec
Merge remote-tracking branch 'upstream/main' into fix-codeblock-false…
SamuelRoettgermann eb4a021
improved string formatting in the tests
SamuelRoettgermann d92d178
Update bot/exts/info/codeblock/_parsing.py
SamuelRoettgermann 2ade4a3
added is_python field for code blocks; added comment explaining regex…
SamuelRoettgermann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import unittest | ||
|
|
||
| from bot.exts.info.codeblock import _parsing as parsing | ||
|
|
||
|
|
||
| class FindFaultyCodeblocksTest(unittest.TestCase): | ||
|
SamuelRoettgermann marked this conversation as resolved.
|
||
| def test_should_recognize_missing_language(self): | ||
| message = """``` | ||
| x = 4 | ||
| y = 2 | ||
| print("abc") | ||
| ```""" | ||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 1) | ||
|
|
||
| def test_should_recognize_contained_codeblock(self): | ||
| message = """' | ||
| wouldn't it be easier to do: | ||
| ```py | ||
| say_hi = lambda: | ||
| print('hello') | ||
| print('world') | ||
| say_hi() | ||
|
|
||
| ' | ||
| ``` | ||
|
|
||
| '""" | ||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNone(faulty_code_blocks) | ||
|
|
||
| def test_should_recognize_contained_codeblock_even_if_that_breaks_formatting(self): | ||
| message = """``` | ||
| ```py | ||
| x = 4 | ||
| y = 3 | ||
| z = 2 | ||
| print("abc") | ||
| ``` | ||
| ``` | ||
| """ | ||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNone(faulty_code_blocks) | ||
|
|
||
| def test_should_not_recognize_normal_single_quotes(self): | ||
| """normal single quotes refers to single quotes that appear normally in text, | ||
| like for example in "I'll", "We're", etc.""" | ||
| message = """I'm writing line 1 | ||
| and we're writing line 2 | ||
| we'll also be checking another of those | ||
| and some odd 'variations | ||
| isn't it beautiful?""" | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 0) | ||
|
|
||
| def test_should_not_recognize_quoting_single_quotes(self): | ||
| message = """ 'I am doing a long quote. | ||
| Sure, I could just use the > character | ||
| for correct quoting | ||
| but whatever... | ||
| End of quote' """ | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 0) | ||
|
|
||
|
|
||
| def test_should_not_recognize_normal_double_quotes(self): | ||
| """normal double quotes refer to double quotes that appear normally in text to quote something""" | ||
| message = """ "I am doing a long quote. | ||
| Sure, I could just use the > character | ||
| for correct quoting | ||
| but whatever... | ||
| End of quote" """ | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 0) | ||
|
|
||
| def test_should_not_recognize_normal_double_quotes_python_text(self): | ||
| message = """ "python is a great language | ||
| great | ||
| great | ||
| great language | ||
| enough lines? | ||
| yes" """ | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 0) | ||
|
|
||
| def test_should_recognize_single_backtick_no_language(self): | ||
| message = """` | ||
| x = 4 | ||
| y = 3 | ||
| z = 2 | ||
| print("abc") | ||
| `""" | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 1) | ||
|
|
||
| def test_should_recognize_single_backtick_with_language(self): | ||
| message = """`py | ||
| x = 4 | ||
| y = 3 | ||
| z = 2 | ||
| print("abc") | ||
| `""" | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 1) | ||
|
|
||
| def test_should_recognize_single_single_quote_with_py_language(self): | ||
| message = """'py | ||
| x = 4 | ||
| y = 3 | ||
| z = 2 | ||
| print("abc") | ||
| '""" | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 1) | ||
|
|
||
| def test_should_recognize_single_single_quote_with_python_language(self): | ||
| message = """'python | ||
| x = 4 | ||
| y = 3 | ||
| z = 2 | ||
| print("abc") | ||
| '""" | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 1) | ||
|
|
||
| def test_should_recognize_wrong_number_of_backticks(self): | ||
| message = """``py | ||
| x = 4 | ||
| y = 3 | ||
| z = 2 | ||
| print("abc") | ||
| ``""" | ||
|
|
||
| faulty_code_blocks = parsing.find_faulty_code_blocks(message) | ||
| self.assertIsNotNone(faulty_code_blocks) | ||
| self.assertEqual(len(faulty_code_blocks), 1) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.