diff --git a/bears/general/aspectsYEAHBear.py b/bears/general/aspectsYEAHBear.py new file mode 100644 index 0000000000..95566b223e --- /dev/null +++ b/bears/general/aspectsYEAHBear.py @@ -0,0 +1,67 @@ +import re + +from coalib.bears.LocalBear import LocalBear +from coalib.results.Diff import Diff +from coalib.results.Result import Result +from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY +from coalib.bearlib.aspects.Spelling import aspectsYEAH +from coalib.bearlib.languages import Unknown + + +class aspectsYEAHBear(LocalBear, aspects={ + 'detect': [aspectsYEAH], + 'fix': [aspectsYEAH], }): + LANGUAGES = {'All'} + AUTHORS = {'The coala developers'} + AUTHORS_EMAILS = {'coala-devel@googlegroups.com'} + LICENSE = 'AGPL-3.0' + CAN_DETECT = {'Spelling'} + + def run(self, filename, file, aspects=[ + aspectsYEAH(Unknown), + ]): + """ + Check for the correct spelling of ``aspect`` and ``aspectsYEAH`` + in the file. + """ + + for aspect in aspects: + if type(aspect) is aspectsYEAH: + corrected_aspects = [] + corrected_aspectsYEAH = [] + for line in file: + wrong_spelling1 = r'A([sS][pP][eE][cC][tT])' + wrong_spelling2 = r'(?!aspectsYEAH)[aA][sS]' + '[pP][eE][cC][tT][sS][yY][eE][aA][hH]' + if not re.match(wrong_spelling2, line): + corrected_aspects += [re.sub(wrong_spelling1, + 'aspects', + line)] + corrected_aspectsYEAH += [re.sub(wrong_spelling2, + 'aspectsYEAH', + line)] + + aspects_diffs = Diff.from_string_arrays(file, + corrected_aspects + ).split_diff() + aspectsYEAH_diffs = \ + Diff.from_string_arrays(file, + corrected_aspectsYEAH + ).split_diff() + for diff in aspects_diffs: + yield Result(self, + '``aspect`` is always written with' + ' a lower case ``a``', + aspect=aspect, + affected_code=(diff.range(filename),), + diffs={filename: diff}, + severity=RESULT_SEVERITY.MAJOR) + for diff in aspectsYEAH_diffs: + yield Result(self, + '``aspectsYEAH`` is always written with' + ' a lower case ``a`` and upper case ' + '``YEAH``', + aspect=aspect, + affected_code=(diff.range(filename),), + diffs={filename: diff}, + severity=RESULT_SEVERITY.MAJOR) diff --git a/tests/general/aspectsYEAHBearTest.py b/tests/general/aspectsYEAHBearTest.py new file mode 100644 index 0000000000..7f3be318a0 --- /dev/null +++ b/tests/general/aspectsYEAHBearTest.py @@ -0,0 +1,44 @@ +import unittest +from queue import Queue + +from coalib.settings.Section import Section +from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY +from coalib.bearlib.aspects.Spelling import aspectsYEAH +from bears.general.aspectsYEAHBear import aspectsYEAHBear + + +class aspectsYEAHBearTest(unittest.TestCase): + + def setUp(self): + self.section = Section('name') + self.queue = Queue() + self.uut = aspectsYEAHBear(self.section, self.queue) + + def test_severity(self): + bad_file = """ + AsPectYEAH + """ + results = list(self.uut.run('bad_file', bad_file.split('\n'))) + self.assertEqual(results[0].severity, + RESULT_SEVERITY.MAJOR) + + def test_message(self): + bad_file = """ + Aspects + Aspectyeah""" + results = list(self.uut.run('bad_file', bad_file.split('\n'))) + print(str(results)) + self.assertEqual(results[0].message, + '``aspect`` is always written' + ' with a lower case ``a``') + self.assertEqual(results[1].message, + '``aspectsYEAH`` is always written with a ' + 'lower case ``a`` and upper case ``YEAH``') + + def test_aspect(self): + bad_file = """ + Aspects + aspectsYEAH + """ + results = list(self.uut.run('bad_file', bad_file.split('\n'))) + self.assertEqual(type(results[0].aspect), aspectsYEAH)