Skip to content

Commit

Permalink
aspectsYEAHBear.py: Add aspectYEAHBear
Browse files Browse the repository at this point in the history
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
pratyushprakash committed Apr 11, 2017
1 parent e0121a5 commit 9ab88a1
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
66 changes: 66 additions & 0 deletions bears/general/aspectsYEAHBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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`` 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)
44 changes: 44 additions & 0 deletions tests/general/aspectsYEAHBearTest.py
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)

0 comments on commit 9ab88a1

Please sign in to comment.