Updates for issue #33

1)  Added fixes for if rules
  2)  Updated tests to improve coverage
      NOTE:  had to add two pragmas to eliminate issue where "continues" were not covered.
This commit is contained in:
Jeremiah C Leary 2017-11-22 20:03:04 -06:00
parent aa0e7d760a
commit 47e464a7de
6 changed files with 248 additions and 30 deletions

View File

@ -223,6 +223,10 @@ class rule():
oFile.lines.pop(iLineNumber - 1)
iLineNumber -= 1
def _remove_blank_lines_below(self, oFile, iLineNumber):
while oFile.lines[iLineNumber + 1].isBlank:
oFile.lines.pop(iLineNumber + 1)
def _insert_blank_line_above(self, oFile, iLineNumber):
oFile.lines.insert(iLineNumber, line.blank_line())

View File

@ -4,13 +4,12 @@ import re
class if_rule(rule.rule):
def __init__(self):
rule.rule.__init__(self)
self.name = 'if'
class rule_001(if_rule):
'''If rule 001 checks for the proper indentation at the beginning of the line.'''
@ -47,6 +46,19 @@ class rule_002(if_rule):
if re.match('^\s*elsif\s+\w', oLine.lineLower):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
oLine = oFile.lines[iLineNumber]
if oLine.isIfKeyword:
oLine.update_line(oLine.line.replace('if', 'if ('))
if oLine.isElseIfKeyword:
oLine.update_line(oLine.line.replace('elsif', 'elsif ('))
iThenLineIndex = iLineNumber
while not oLine.isThenKeyword:
iThenLineIndex += 1
oLine = oFile.lines[iThenLineIndex]
oLine.update_line(oLine.line.replace('then', ') then'))
class rule_003(if_rule):
'''If rule 003 checks there is a single space between the if keyword and the (.'''
@ -54,7 +66,7 @@ class rule_003(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '003'
self.solution = 'Ensure only a single space exists between the "if" keyword and the (.'
self.solution = 'Ensure only a single space exists between the "if" keyword and the (.'
self.phase = 2
def analyze(self, oFile):
@ -64,6 +76,10 @@ class rule_003(if_rule):
if not re.match('^\s*if\s\(', oLine.lineLower):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'if')
class rule_004(if_rule):
'''If rule 004 checks there is a single space between the ) and "then" keyword.'''
@ -71,7 +87,7 @@ class rule_004(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '004'
self.solution = 'Ensure only a single space exists between the ) and "then" keyword.'
self.solution = 'Ensure only a single space exists between the ) and "then" keyword.'
self.phase = 2
def analyze(self, oFile):
@ -81,6 +97,10 @@ class rule_004(if_rule):
if not re.match('^\s*.*\)\sthen', oLine.lineLower):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._enforce_one_space_before_word(oFile.lines[iLineNumber], 'then')
class rule_005(if_rule):
'''If rule 005 checks there is a single space between the "elsif" keyword and the (.'''
@ -88,7 +108,7 @@ class rule_005(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '005'
self.solution = 'Ensure only a single space exists between the "elsif" keyword and the (.'
self.solution = 'Ensure only a single space exists between the "elsif" keyword and the (.'
self.phase = 2
def analyze(self, oFile):
@ -98,6 +118,10 @@ class rule_005(if_rule):
if not re.match('^\s*elsif\s\(', oLine.lineLower):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations:
self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'elsif')
class rule_006(if_rule):
'''If rule 006 checks for an empty line after the then keyword.'''
@ -105,7 +129,7 @@ class rule_006(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '006'
self.solution = 'Remove blank line(s) after the "then" keyword.'
self.solution = 'Remove blank line(s) after the "then" keyword.'
self.phase = 3
def analyze(self, oFile):
@ -113,6 +137,10 @@ class rule_006(if_rule):
if oLine.isThenKeyword and not oFile.lines[iLineNumber + 2].isCaseKeyword and not oLine.isEndIfKeyword:
self._is_no_blank_line_after(oFile, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
self._remove_blank_lines_below(oFile, iLineNumber)
class rule_007(if_rule):
'''If rule 007 checks for an empty line before the "elsif" keyword.'''
@ -120,7 +148,7 @@ class rule_007(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '007'
self.solution = 'Remove blank line(s) before the "elsif" keyword.'
self.solution = 'Remove blank line(s) before the "elsif" keyword.'
self.phase = 3
def analyze(self, oFile):
@ -128,6 +156,10 @@ class rule_007(if_rule):
if oLine.isElseIfKeyword:
self._is_no_blank_line_before(oFile, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
self._remove_blank_lines_above(oFile, iLineNumber)
class rule_008(if_rule):
'''If rule 008 checks for an empty line before the "end if" keyword.'''
@ -135,7 +167,7 @@ class rule_008(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '008'
self.solution = 'Remove blank line(s) before the "end if" keyword.'
self.solution = 'Remove blank line(s) before the "end if" keyword.'
self.phase = 3
def analyze(self, oFile):
@ -143,6 +175,10 @@ class rule_008(if_rule):
if oLine.isEndIfKeyword and not oFile.lines[iLineNumber - 2].isEndCaseKeyword and not oLine.isIfKeyword:
self._is_no_blank_line_before(oFile, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
self._remove_blank_lines_above(oFile, iLineNumber)
class rule_009(if_rule):
'''If rule 009 checks the alignment of multiline boolean expressions.'''
@ -150,7 +186,7 @@ class rule_009(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '009'
self.solution = 'Align with space after ( in first line of boolean expression.'
self.solution = 'Align with space after ( in first line of boolean expression.'
self.phase = 5
def analyze(self, oFile):
@ -164,18 +200,21 @@ class rule_009(if_rule):
if re.match('^\s+if\s\(', oLine.lineLower):
iAlignmentColumn = oLine.lineLower.find('(')
fCheckAlignment = True
continue
continue # pragma: no cover
if oLine.isElseIfKeyword:
if re.match('^\s+elsif\s\(', oLine.lineLower):
iAlignmentColumn = oLine.lineLower.find('(')
fCheckAlignment = True
continue
continue # pragma: no cover
if oLine.insideIf and fCheckAlignment:
if not re.match('^\s{' + str(iAlignmentColumn + 1) + '}\S', oLine.line):
self.add_violation(iLineNumber)
self._check_multiline_alignment(iAlignmentColumn + 1, oLine, iLineNumber)
if oLine.isThenKeyword:
fCheckAlignment = False
def _fix_violations(self, oFile):
for iLineNumber in self.dFix['violations']:
self._fix_multiline_alignment(oFile, iLineNumber)
class rule_010(if_rule):
'''If rule 010 checks for an empty line before the "else" keyword.'''
@ -183,7 +222,7 @@ class rule_010(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '010'
self.solution = 'Remove blank line(s) before the "else" keyword.'
self.solution = 'Remove blank line(s) before the "else" keyword.'
self.phase = 3
def analyze(self, oFile):
@ -191,6 +230,10 @@ class rule_010(if_rule):
if oLine.isElseKeyword and not oFile.lines[iLineNumber - 2].isEndCaseKeyword and not oLine.isIfKeyword:
self._is_no_blank_line_before(oFile, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
self._remove_blank_lines_above(oFile, iLineNumber)
class rule_011(if_rule):
'''If rule 011 checks for an empty line after the "else" keyword.'''
@ -198,7 +241,7 @@ class rule_011(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '011'
self.solution = 'Remove blank line(s) after the "else" keyword.'
self.solution = 'Remove blank line(s) after the "else" keyword.'
self.phase = 3
def analyze(self, oFile):
@ -206,6 +249,10 @@ class rule_011(if_rule):
if oLine.isElseKeyword and not oFile.lines[iLineNumber + 2].isCaseKeyword and not oLine.isEndIfKeyword:
self._is_no_blank_line_after(oFile, iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
self._remove_blank_lines_below(oFile, iLineNumber)
class rule_012(if_rule):
'''If rule 012 checks for code after the "then" keyword.'''
@ -213,7 +260,7 @@ class rule_012(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '012'
self.solution = 'Move code after "then" or "else" keyword to the next line.'
self.solution = 'Move code after "then" or "else" keyword to the next line.'
self.phase = 1
def analyze(self, oFile):
@ -224,7 +271,26 @@ class rule_012(if_rule):
elif oLine.isElseKeyword:
if re.match('^.*\selse\s+\w', oLine.lineLower):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
oFile.lines.insert(iLineNumber + 1,oFile.lines[iLineNumber])
oLine = oFile.lines[iLineNumber]
if oLine.isThenKeyword:
iIndex = oLine.line.find(' then') + len(' then') + 1
oLine.update_line(oLine.line[:iIndex])
oLine.isThenKeyword = False
oLine = oFile.lines[iLineNumber + 1]
oLine.update_line(oLine.line[iIndex:])
oLine.isIfKeyword = False
elif oLine.isElseKeyword:
iIndex = oLine.line.find(' else') + len(' else') + 1
oLine.update_line(oLine.line[:iIndex])
oLine.isElseKeyword = False
oLine = oFile.lines[iLineNumber + 1]
oLine.update_line(oLine.line[iIndex:])
oLine.isIfKeyword = False
class rule_013(if_rule):
'''If rule 013 checks the "else" keyword is on it's own line.'''
@ -232,7 +298,7 @@ class rule_013(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '013'
self.solution = 'Move "else" keyword to it\'s own line.'
self.solution = 'Move "else" keyword to it\'s own line.'
self.phase = 1
def analyze(self, oFile):
@ -241,6 +307,17 @@ class rule_013(if_rule):
if not re.match('^\s*else', oLine.lineLower):
self.add_violation(iLineNumber)
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
oFile.lines.insert(iLineNumber + 1,oFile.lines[iLineNumber])
oLine = oFile.lines[iLineNumber]
iIndex = oLine.line.find(' else')
oLine.update_line(oLine.line[:iIndex])
oLine.isElseKeyword = False
oLine = oFile.lines[iLineNumber + 1]
oLine.update_line(oLine.line[iIndex:])
oLine.isIfKeyword = False
class rule_014(if_rule):
'''If rule 014 checks the "end if" keyword is on it's own line.'''
@ -248,7 +325,7 @@ class rule_014(if_rule):
def __init__(self):
if_rule.__init__(self)
self.identifier = '014'
self.solution = 'Move "end if" keyword to it\'s own line.'
self.solution = 'Move "end if" keyword to it\'s own line.'
self.phase = 1
def analyze(self, oFile):
@ -257,5 +334,16 @@ class rule_014(if_rule):
if not re.match('^\s*end\s+if', oLine.lineLower):
self.add_violation(iLineNumber)
#TODO:
# check if, then, elsif, end if keywords are lower case
def _fix_violations(self, oFile):
for iLineNumber in self.violations[::-1]:
oFile.lines.insert(iLineNumber + 1,oFile.lines[iLineNumber])
oLine = oFile.lines[iLineNumber]
iIndex = oLine.line.find(' end')
oLine.update_line(oLine.line[:iIndex])
oLine.isEndIfKeyword = False
oLine = oFile.lines[iLineNumber + 1]
oLine.update_line(oLine.line[iIndex:])
oLine.isIfKeyword = False
oLine.isElseKeyword = False
oLine.isElseIfKeyword = False
oLine.isThenKeyword = False

View File

@ -109,6 +109,13 @@ begin
(b = 3) then b <= '1'; else
c <= '1'; end if;
if (a = 2) then
a <= '0';
elsif (a = 3 or
b = 4) then
a <= '1';
end if;
end process PROC_NAME;
end architecture ARCH;

View File

@ -9,11 +9,130 @@ from vsg import vhdlFile
oFile = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','if_statement','if_test_input.vhd'))
oFileCase = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','if_statement','if_case_test_input.vhd'))
class testRuleIfMethods(unittest.TestCase):
class testFixRuleIfMethods(unittest.TestCase):
def test_fix_rule_001(self):
oRule = if_statement.rule_001()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, [])
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_002(self):
oRule = if_statement.rule_002()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_003(self):
oRule = if_statement.rule_003()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_004(self):
oRule = if_statement.rule_004()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_005(self):
oRule = if_statement.rule_005()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_006(self):
oRule = if_statement.rule_006()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_006_case(self):
oRule = if_statement.rule_006()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFileCase)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_007(self):
oRule = if_statement.rule_007()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_008(self):
oRule = if_statement.rule_008()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_008_case(self):
oRule = if_statement.rule_008()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFileCase)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_009(self):
oRule = if_statement.rule_009()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_010(self):
oRule = if_statement.rule_010()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_010_case(self):
oRule = if_statement.rule_010()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFileCase)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_011(self):
oRule = if_statement.rule_011()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_011_case(self):
oRule = if_statement.rule_011()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFileCase)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_012(self):
oRule = if_statement.rule_012()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_013(self):
oRule = if_statement.rule_013()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)
def test_fix_rule_014(self):
oRule = if_statement.rule_014()
dExpected = []
oRule.fix(oFile)
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)

View File

@ -106,7 +106,7 @@ class testRuleIfMethods(unittest.TestCase):
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'if')
self.assertEqual(oRule.identifier, '009')
dExpected = [20,21,67,68]
dExpected = [20,21,67,68,115]
oRule.analyze(oFile)
self.assertEqual(oRule.violations, dExpected)

View File

@ -9,7 +9,7 @@ class testVhdlFileIfAssignments(unittest.TestCase):
def test_insideIf_assignment(self):
lExpected = [8,13,14,19,20,21,24,30,31,32,33,41,42,43,46,52,53,54,57,66,67,68,73,80,91,98,99,100,105,107,108,109]
lExpected = [8,13,14,19,20,21,24,30,31,32,33,41,42,43,46,52,53,54,57,66,67,68,73,80,91,98,99,100,105,107,108,109,112,114,115]
# Generic actual list
lActual = []
for iIndex, oLine in enumerate(oFile.lines):
@ -19,7 +19,7 @@ class testVhdlFileIfAssignments(unittest.TestCase):
self.assertEqual(lActual, lExpected)
def test_isElseIfKeyword_assignment(self):
lExpected = [24,73,99,100,108]
lExpected = [24,73,99,100,108,114]
# Generic actual list
lActual = []
for iIndex, oLine in enumerate(oFile.lines):
@ -29,7 +29,7 @@ class testVhdlFileIfAssignments(unittest.TestCase):
self.assertEqual(lActual, lExpected)
def test_isEndIfKeyword_assignment(self):
lExpected = [11,17,27,36,39,49,50,60,62,78,89,96,103,105,110]
lExpected = [11,17,27,36,39,49,50,60,62,78,89,96,103,105,110,117]
# Generic actual list
lActual = []
for iIndex, oLine in enumerate(oFile.lines):
@ -39,7 +39,7 @@ class testVhdlFileIfAssignments(unittest.TestCase):
self.assertEqual(lActual, lExpected)
def test_isIfKeyword_assignment(self):
lExpected = [8,13,19,30,33,41,46,52,57,66,80,91,98,105,107]
lExpected = [8,13,19,30,33,41,46,52,57,66,80,91,98,105,107,112]
# Generic actual list
lActual = []
for iIndex, oLine in enumerate(oFile.lines):
@ -49,7 +49,7 @@ class testVhdlFileIfAssignments(unittest.TestCase):
self.assertEqual(lActual, lExpected)
def test_isThenKeyword_assignment(self):
lExpected = [8,14,21,24,32,33,43,46,54,57,68,73,80,91,98,99,100,105,107,109]
lExpected = [8,14,21,24,32,33,43,46,54,57,68,73,80,91,98,99,100,105,107,109,112,115]
# Generic actual list
lActual = []
for iIndex, oLine in enumerate(oFile.lines):