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