From d3fed0055ea5e19083f2e0221ecd36d2f201a6ca Mon Sep 17 00:00:00 2001 From: Jeremiah C Leary Date: Wed, 22 Nov 2017 21:23:36 -0600 Subject: [PATCH] Updates for issue #23 1) Added fixes for rules --- vsg/rules/case.py | 58 ++++++++++++++++--- vsg/tests/case/test_fix_rule_case.py | 84 +++++++++++++++++++++++++++- vsg/tests/case/test_rule_case.py | 2 +- 3 files changed, 132 insertions(+), 12 deletions(-) diff --git a/vsg/rules/case.py b/vsg/rules/case.py index dd9b4683..09226aff 100644 --- a/vsg/rules/case.py +++ b/vsg/rules/case.py @@ -4,7 +4,7 @@ import re class case_rule(rule.rule): - + def __init__(self): rule.rule.__init__(self) self.name = 'case' @@ -43,6 +43,10 @@ class rule_002(case_rule): if not re.match('^\s*case\s\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], 'case') + class rule_003(case_rule): '''Case rule 003 checks for a single space before the "is" keyword.''' @@ -58,6 +62,10 @@ class rule_003(case_rule): if oLine.isCaseIsKeyword: self._is_single_space_before('is', oLine, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._enforce_one_space_before_word(oFile.lines[iLineNumber], 'is') + class rule_004(case_rule): '''Case rule 004 checks for a single space after the "when" keyword.''' @@ -73,6 +81,10 @@ class rule_004(case_rule): if oLine.isCaseWhenKeyword: self._is_single_space_after('when', oLine, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'when') + class rule_005(case_rule): '''Case rule 005 checks for a single space before the "=>" keyword.''' @@ -88,6 +100,10 @@ class rule_005(case_rule): if oLine.isCaseWhenEnd: self._is_single_space_before('=>', oLine, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._enforce_one_space_before_word(oFile.lines[iLineNumber], '=>') + class rule_006(case_rule): '''Case rule 006 checks for a single space between the "end" and "case" keywords.''' @@ -104,6 +120,10 @@ class rule_006(case_rule): if not re.match('^\s*end\scase', 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], 'end') + class rule_007(case_rule): '''Case rule 007 ensures a blank line exists before the "case" keyword.''' @@ -119,6 +139,10 @@ class rule_007(case_rule): if oLine.isCaseKeyword: self._is_blank_line_before(oFile, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_above(oFile, iLineNumber) + class rule_008(case_rule): '''Case rule 008 ensures a blank line exists below the "case" keyword.''' @@ -134,6 +158,10 @@ class rule_008(case_rule): if oLine.isCaseIsKeyword: self._is_blank_line_after(oFile, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_below(oFile, iLineNumber) + class rule_009(case_rule): '''Case rule 009 ensures a blank line exists above the "end case" keywords.''' @@ -149,6 +177,10 @@ class rule_009(case_rule): if oLine.isEndCaseKeyword: self._is_blank_line_before(oFile, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_above(oFile, iLineNumber) + class rule_010(case_rule): '''Case rule 010 ensures a blank line exists below the "end case" keywords.''' @@ -164,6 +196,10 @@ class rule_010(case_rule): if oLine.isEndCaseKeyword: self._is_blank_line_after(oFile, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_below(oFile, iLineNumber) + class rule_011(case_rule): '''Case rule 011 ensures the alignment of multiline "when" statements.''' @@ -184,6 +220,10 @@ class rule_011(case_rule): if oLine.insideCaseWhen: self._check_multiline_alignment(iAlignmentColumn, oLine, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.dFix['violations']: + self._fix_multiline_alignment(oFile, iLineNumber) + class rule_012(case_rule): '''Case rule 012 ensures code does not exist after the => operator.''' @@ -200,10 +240,12 @@ class rule_012(case_rule): if re.match('^.*=>\s*\w', oLine.line): self.add_violation(iLineNumber) - -#TODO: -# multiline case alignment -# 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('=>') + len('=>') + 1 + oLine.update_line(oLine.line[:iIndex]) + oLine = oFile.lines[iLineNumber + 1] + oLine.update_line(oLine.line[iIndex:]) + oLine.isCaseWhenEnd = False diff --git a/vsg/tests/case/test_fix_rule_case.py b/vsg/tests/case/test_fix_rule_case.py index 5a904291..d1be47d8 100644 --- a/vsg/tests/case/test_fix_rule_case.py +++ b/vsg/tests/case/test_fix_rule_case.py @@ -8,10 +8,88 @@ from vsg import vhdlFile oFile = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','case','case_test_input.vhd')) oFileSequential = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','case','case_sequential_test_input.vhd')) -class testRuleConcurrentMethods(unittest.TestCase): +class testFixRuleCaseMethods(unittest.TestCase): - def test_rule_001(self): + def test_fix_rule_001(self): oRule = case.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 = case.rule_002() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_003(self): + oRule = case.rule_003() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_004(self): + oRule = case.rule_004() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_005(self): + oRule = case.rule_005() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_006(self): + oRule = case.rule_006() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_007(self): + oRule = case.rule_007() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_008(self): + oRule = case.rule_008() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_009(self): + oRule = case.rule_009() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_010(self): + oRule = case.rule_010() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_011(self): + oRule = case.rule_011() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_012(self): + oRule = case.rule_012() + dExpected = [] + oRule.fix(oFileSequential) + oRule.analyze(oFileSequential) + self.assertEqual(oRule.violations, dExpected) diff --git a/vsg/tests/case/test_rule_case.py b/vsg/tests/case/test_rule_case.py index 2bb39774..da676914 100644 --- a/vsg/tests/case/test_rule_case.py +++ b/vsg/tests/case/test_rule_case.py @@ -8,7 +8,7 @@ from vsg import vhdlFile oFile = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','case','case_test_input.vhd')) oFileSequential = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','case','case_sequential_test_input.vhd')) -class testRuleConcurrentMethods(unittest.TestCase): +class testRuleCaseMethods(unittest.TestCase): def test_rule_001(self): oRule = case.rule_001()