|
| 1 | +from os.path import abspath |
| 2 | +from queue import Queue |
| 3 | + |
| 4 | +from bears.general.RegexLintBear import RegexLintBear |
| 5 | +from coalib.testing.LocalBearTestHelper import ( |
| 6 | + LocalBearTestHelper, execute_bear) |
| 7 | +from coalib.results.Result import Result |
| 8 | +from coalib.settings.Section import Section |
| 9 | +from coalib.settings.Setting import Setting |
| 10 | + |
| 11 | + |
| 12 | +test_good_python_file = """ |
| 13 | +some_regex = r'[a-zA-Z]]' |
| 14 | +""" |
| 15 | + |
| 16 | +test_bad_python_file = """ |
| 17 | +some_regex = r'(else|elseif)' |
| 18 | +""" |
| 19 | + |
| 20 | +test_good_cpp_file = """ |
| 21 | +char some_regex[] = "[a-zA-Z]]"; |
| 22 | +""" |
| 23 | + |
| 24 | +test_bad_cpp_file = """ |
| 25 | +char some_regex[13] = "(else|elseif)"; |
| 26 | +""" |
| 27 | + |
| 28 | +test_re_error_file = """ |
| 29 | +some_regex = r'*ab' # This should be skipped |
| 30 | +some_other_regex = r'[a-z]' |
| 31 | +""" |
| 32 | + |
| 33 | +BAD_MESSAGE = ('E105:argv:root:0: Potential out of order alternation between ' |
| 34 | + '\'else\' and \'elseif\'\n' |
| 35 | + ' \'(else|elseif)\'\n' |
| 36 | + ' ^ here\n') |
| 37 | + |
| 38 | + |
| 39 | +class RegexLintBearTest(LocalBearTestHelper): |
| 40 | + |
| 41 | + def setUp(self): |
| 42 | + self.section = Section('') |
| 43 | + self.queue = Queue() |
| 44 | + self.uut = RegexLintBear(self.section, self.queue) |
| 45 | + |
| 46 | + def test_good_python_file(self): |
| 47 | + self.section.append(Setting('language', 'python 3')) |
| 48 | + with execute_bear(self.uut, abspath('good_python_file'), |
| 49 | + test_good_python_file.splitlines(True)) as results: |
| 50 | + self.assertEqual(results, []) |
| 51 | + |
| 52 | + def test_bad_python_file(self): |
| 53 | + self.section.append(Setting('language', 'python 3')) |
| 54 | + with execute_bear(self.uut, abspath('bad_python_file'), |
| 55 | + test_bad_python_file.splitlines()) as results: |
| 56 | + self.assertEqual(results[0], |
| 57 | + Result.from_values(origin=self.uut, |
| 58 | + message=BAD_MESSAGE, |
| 59 | + file='bad_python_file', |
| 60 | + line=2, column=15, |
| 61 | + end_line=2, end_column=29)) |
| 62 | + |
| 63 | + def test_good_cpp_file(self): |
| 64 | + self.section.append(Setting('language', 'cpp')) |
| 65 | + with execute_bear(self.uut, abspath('good_cpp_file'), |
| 66 | + test_good_cpp_file.splitlines()) as results: |
| 67 | + self.assertEqual(results, []) |
| 68 | + |
| 69 | + def test_bad_cpp_file(self): |
| 70 | + self.section.append(Setting('language', 'cpp')) |
| 71 | + with execute_bear(self.uut, abspath('bad_cpp_file'), |
| 72 | + test_bad_cpp_file.splitlines()) as results: |
| 73 | + self.assertEqual(results[0], |
| 74 | + Result.from_values(origin=self.uut, |
| 75 | + message=BAD_MESSAGE, |
| 76 | + file='bad_cpp_file', |
| 77 | + line=2, column=23, |
| 78 | + end_line=2, end_column=37)) |
| 79 | + |
| 80 | + def test_re_error_file(self): |
| 81 | + self.section.append(Setting('language', 'cpp')) |
| 82 | + with execute_bear(self.uut, abspath('re_error_file'), |
| 83 | + test_re_error_file.splitlines()) as results: |
| 84 | + self.assertEqual(results, []) |
0 commit comments