forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aspectsYEAHBear.py: Add aspectYEAHBear
Add aspectYEAHBear to make sure `aspect` is always written with a lower case `a` and `aspectsYEAH` is always written with upper case `YEAH`. Closes coala#1573
- Loading branch information
1 parent
e0121a5
commit 3dd33b1
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 = {'[email protected]'} | ||
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) |
This file contains 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,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) |