|
| 1 | +import unittest |
| 2 | + |
| 3 | +from bot.exts.info.codeblock import _parsing as parsing |
| 4 | + |
| 5 | + |
| 6 | +class FindFaultyCodeblocksTest(unittest.TestCase): |
| 7 | + def test_should_recognize_missing_language(self): |
| 8 | + message = """``` |
| 9 | + x = 4 |
| 10 | + y = 2 |
| 11 | + print("abc") |
| 12 | + ```""" |
| 13 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 14 | + self.assertIsNotNone(faulty_code_blocks) |
| 15 | + self.assertEqual(len(faulty_code_blocks), 1) |
| 16 | + |
| 17 | + def test_should_recognize_contained_codeblock(self): |
| 18 | + message = """' |
| 19 | + wouldn't it be easier to do: |
| 20 | + ```py |
| 21 | + say_hi = lambda: |
| 22 | + print('hello') |
| 23 | + print('world') |
| 24 | + say_hi() |
| 25 | +
|
| 26 | + ' |
| 27 | + ``` |
| 28 | +
|
| 29 | + '""" |
| 30 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 31 | + self.assertIsNone(faulty_code_blocks) |
| 32 | + |
| 33 | + def test_should_recognize_contained_codeblock_even_if_that_breaks_formatting(self): |
| 34 | + message = """``` |
| 35 | + ```py |
| 36 | + x = 4 |
| 37 | + y = 3 |
| 38 | + z = 2 |
| 39 | + print("abc") |
| 40 | + ``` |
| 41 | + ``` |
| 42 | + """ |
| 43 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 44 | + self.assertIsNone(faulty_code_blocks) |
| 45 | + |
| 46 | + def test_should_not_recognize_normal_single_quotes(self): |
| 47 | + """normal single quotes refers to single quotes that appear normally in text, |
| 48 | + like for example in "I'll", "We're", etc.""" |
| 49 | + message = """I'm writing line 1 |
| 50 | + and we're writing line 2 |
| 51 | + we'll also be checking another of those |
| 52 | + and some odd 'variations |
| 53 | + isn't it beautiful?""" |
| 54 | + |
| 55 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 56 | + self.assertIsNotNone(faulty_code_blocks) |
| 57 | + self.assertEqual(len(faulty_code_blocks), 0) |
| 58 | + |
| 59 | + def test_should_not_recognize_quoting_single_quotes(self): |
| 60 | + message = """ 'I am doing a long quote. |
| 61 | + Sure, I could just use the > character |
| 62 | + for correct quoting |
| 63 | + but whatever... |
| 64 | + End of quote' """ |
| 65 | + |
| 66 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 67 | + self.assertIsNotNone(faulty_code_blocks) |
| 68 | + self.assertEqual(len(faulty_code_blocks), 0) |
| 69 | + |
| 70 | + |
| 71 | + def test_should_not_recognize_normal_double_quotes(self): |
| 72 | + """normal double quotes refer to double quotes that appear normally in text to quote something""" |
| 73 | + message = """ "I am doing a long quote. |
| 74 | + Sure, I could just use the > character |
| 75 | + for correct quoting |
| 76 | + but whatever... |
| 77 | + End of quote" """ |
| 78 | + |
| 79 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 80 | + self.assertIsNotNone(faulty_code_blocks) |
| 81 | + self.assertEqual(len(faulty_code_blocks), 0) |
| 82 | + |
| 83 | + def test_should_not_recognize_normal_double_quotes_python_text(self): |
| 84 | + message = """ "python is a great language |
| 85 | + great |
| 86 | + great |
| 87 | + great language |
| 88 | + enough lines? |
| 89 | + yes" """ |
| 90 | + |
| 91 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 92 | + self.assertIsNotNone(faulty_code_blocks) |
| 93 | + self.assertEqual(len(faulty_code_blocks), 0) |
| 94 | + |
| 95 | + def test_should_recognize_single_backtick_no_language(self): |
| 96 | + message = """` |
| 97 | + x = 4 |
| 98 | + y = 3 |
| 99 | + z = 2 |
| 100 | + print("abc") |
| 101 | + `""" |
| 102 | + |
| 103 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 104 | + self.assertIsNotNone(faulty_code_blocks) |
| 105 | + self.assertEqual(len(faulty_code_blocks), 1) |
| 106 | + |
| 107 | + def test_should_recognize_single_backtick_with_language(self): |
| 108 | + message = """`py |
| 109 | + x = 4 |
| 110 | + y = 3 |
| 111 | + z = 2 |
| 112 | + print("abc") |
| 113 | + `""" |
| 114 | + |
| 115 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 116 | + self.assertIsNotNone(faulty_code_blocks) |
| 117 | + self.assertEqual(len(faulty_code_blocks), 1) |
| 118 | + |
| 119 | + def test_should_recognize_single_single_quote_with_py_language(self): |
| 120 | + message = """'py |
| 121 | + x = 4 |
| 122 | + y = 3 |
| 123 | + z = 2 |
| 124 | + print("abc") |
| 125 | + '""" |
| 126 | + |
| 127 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 128 | + self.assertIsNotNone(faulty_code_blocks) |
| 129 | + self.assertEqual(len(faulty_code_blocks), 1) |
| 130 | + |
| 131 | + def test_should_recognize_single_single_quote_with_python_language(self): |
| 132 | + message = """'python |
| 133 | + x = 4 |
| 134 | + y = 3 |
| 135 | + z = 2 |
| 136 | + print("abc") |
| 137 | + '""" |
| 138 | + |
| 139 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 140 | + self.assertIsNotNone(faulty_code_blocks) |
| 141 | + self.assertEqual(len(faulty_code_blocks), 1) |
| 142 | + |
| 143 | + def test_should_recognize_wrong_number_of_backticks(self): |
| 144 | + message = """``py |
| 145 | + x = 4 |
| 146 | + y = 3 |
| 147 | + z = 2 |
| 148 | + print("abc") |
| 149 | + ``""" |
| 150 | + |
| 151 | + faulty_code_blocks = parsing.find_faulty_code_blocks(message) |
| 152 | + self.assertIsNotNone(faulty_code_blocks) |
| 153 | + self.assertEqual(len(faulty_code_blocks), 1) |
0 commit comments