From 065ec579c68e2825e2eaa3cf01501d6de3c4bc4d Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Sun, 1 Nov 2020 11:16:47 -0600 Subject: [PATCH] Refactoring rule process_020 for new parser. --- .../multiline_alignment_between_tokens.py | 6 +- vsg/rules/process/rule_020.py | 34 +++-------- .../process/rule_020_test_input.fixed.vhd | 59 +++++++++++++++++++ vsg/tests/process/rule_020_test_input.vhd | 59 +++++++++++++++++++ vsg/tests/process/test_rule_020.py | 46 +++++++++++++++ vsg/tests/process/test_rule_process.py | 9 --- vsg/vhdlFile/vhdlFile.py | 8 ++- 7 files changed, 183 insertions(+), 38 deletions(-) create mode 100644 vsg/tests/process/rule_020_test_input.fixed.vhd create mode 100644 vsg/tests/process/rule_020_test_input.vhd create mode 100644 vsg/tests/process/test_rule_020.py diff --git a/vsg/rules/multiline_alignment_between_tokens.py b/vsg/rules/multiline_alignment_between_tokens.py index 0e06bf76..67ebdc1a 100644 --- a/vsg/rules/multiline_alignment_between_tokens.py +++ b/vsg/rules/multiline_alignment_between_tokens.py @@ -25,21 +25,23 @@ class multiline_alignment_between_tokens(rule_item.Rule): object type to apply the case check against ''' - def __init__(self, name, identifier, lTokenPairs): + def __init__(self, name, identifier, lTokenPairs, bExcludeLastToken=False): rule_item.Rule.__init__(self, name=name, identifier=identifier) self.solution = 'Align with open parenthesis on previous line.' self.phase = 4 self.lTokenPairs = lTokenPairs + self.bExcludeLastToken = bExcludeLastToken def analyze(self, oFile): lToi = [] for lTokenPair in self.lTokenPairs: - aToi = oFile.get_tokens_bounded_by(lTokenPair[0], lTokenPair[1]) + aToi = oFile.get_tokens_bounded_by(lTokenPair[0], lTokenPair[1], bExcludeLastToken=self.bExcludeLastToken) lToi = utils.combine_two_token_class_lists(lToi, aToi) for oToi in lToi: iLine, lTokens = utils.get_toi_parameters(oToi) + iStartColumn = calculate_start_column(oFile, oToi) lColumn = [] lColumn.append(iStartColumn) diff --git a/vsg/rules/process/rule_020.py b/vsg/rules/process/rule_020.py index f501d0f4..32552989 100644 --- a/vsg/rules/process/rule_020.py +++ b/vsg/rules/process/rule_020.py @@ -1,34 +1,16 @@ -from vsg import rule -from vsg import check -from vsg import fix +from vsg.rules import multiline_alignment_between_tokens -import re +from vsg import token + +lTokenPairs = [] +lTokenPairs.append([token.process_statement.open_parenthesis, token.process_statement.close_parenthesis]) -class rule_020(rule.rule): +class rule_020(multiline_alignment_between_tokens): ''' - Process rule 020 checks the indentation on multiline sensitivity lists. + Checks the alignment of multiline sensitivity lists. ''' def __init__(self): - rule.rule.__init__(self, 'process', '020') - self.solution = 'Fix indentation of sensitivity list.' - self.phase = 5 - - def analyze(self, oFile): - for iLineNumber, oLine in enumerate(oFile.lines): - if not self._is_vsg_off(oLine): - if oLine.isSensitivityListBegin and oLine.isSensitivityListEnd: - continue - if oLine.insideSensitivityList: - if oLine.isSensitivityListBegin: - iAlignmentColumn = oLine.line.find('(') - elif oLine.isSensitivityListEnd and re.match('^\s*\)', oLine.line): - continue - else: - check.multiline_alignment(self, iAlignmentColumn + 1, oLine, iLineNumber) - - def _fix_violations(self, oFile): - for iLineNumber in self.dFix['violations']: - fix.multiline_alignment(self, oFile, iLineNumber) + multiline_alignment_between_tokens.__init__(self, 'process', '020', lTokenPairs, True) diff --git a/vsg/tests/process/rule_020_test_input.fixed.vhd b/vsg/tests/process/rule_020_test_input.fixed.vhd new file mode 100644 index 00000000..51057f4d --- /dev/null +++ b/vsg/tests/process/rule_020_test_input.fixed.vhd @@ -0,0 +1,59 @@ + +architecture ARCH of ENTITY is + +begin + + process (a, b, c, d) + begin + end process; + + process (a, + b, + c, + d) + begin + end process; + + process (a, + b, + c, + d + ) + begin + end process; + + -- Violations + + process (a, + b, + c, + d) + begin + end process; + + process (a, + b, + c, + d) + begin + end process; + + process (a, + b, + c, + d + ) + begin + end process; + + process (a, + b, + c, + d + ) + begin + end process; + + +end architecture ARCH; + diff --git a/vsg/tests/process/rule_020_test_input.vhd b/vsg/tests/process/rule_020_test_input.vhd new file mode 100644 index 00000000..cb394348 --- /dev/null +++ b/vsg/tests/process/rule_020_test_input.vhd @@ -0,0 +1,59 @@ + +architecture ARCH of ENTITY is + +begin + + process (a, b, c, d) + begin + end process; + + process (a, + b, + c, + d) + begin + end process; + + process (a, + b, + c, + d + ) + begin + end process; + + -- Violations + + process (a, + b, + c, + d) + begin + end process; + + process (a, + b, + c, + d) + begin + end process; + + process (a, + b, + c, + d + ) + begin + end process; + + process (a, + b, + c, + d + ) + begin + end process; + + +end architecture ARCH; + diff --git a/vsg/tests/process/test_rule_020.py b/vsg/tests/process/test_rule_020.py new file mode 100644 index 00000000..ee35b7a7 --- /dev/null +++ b/vsg/tests/process/test_rule_020.py @@ -0,0 +1,46 @@ + +import os +import unittest + +from vsg.rules import process +from vsg import vhdlFile +from vsg.tests import utils + +sTestDir = os.path.dirname(__file__) + +lFile = utils.read_vhdlfile(os.path.join(sTestDir,'rule_020_test_input.vhd')) + +lExpected = [] +lExpected.append('') +utils.read_file(os.path.join(sTestDir, 'rule_020_test_input.fixed.vhd'), lExpected) + + +class test_process_rule(unittest.TestCase): + + def setUp(self): + self.oFile = vhdlFile.vhdlFile(lFile) + + def test_rule_020(self): + oRule = process.rule_020() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'process') + self.assertEqual(oRule.identifier, '020') + + lExpected = [28, 30, 35, 36] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) + + def test_fix_rule_020(self): + oRule = process.rule_020() + + oRule.fix(self.oFile) + + lActual = [] + for oLine in self.oFile.lines: + lActual.append(oLine.line) + + self.assertEqual(lExpected, lActual) + + oRule.analyze(self.oFile) + self.assertEqual(oRule.violations, []) diff --git a/vsg/tests/process/test_rule_process.py b/vsg/tests/process/test_rule_process.py index 490781af..4c3a5644 100644 --- a/vsg/tests/process/test_rule_process.py +++ b/vsg/tests/process/test_rule_process.py @@ -16,15 +16,6 @@ oFileSensitivity = vhdlFile.vhdlFile(lFileSensitivity) class testRuleProcessMethods(unittest.TestCase): - def test_rule_020(self): - oRule = process.rule_020() - self.assertTrue(oRule) - self.assertEqual(oRule.name, 'process') - self.assertEqual(oRule.identifier, '020') - dExpected = utils.add_violation_list([19,25,26]) - oRule.analyze(oFile) - self.assertEqual(oRule.violations, dExpected) - def test_rule_028(self): oRule = process.rule_028() self.assertTrue(oRule) diff --git a/vsg/vhdlFile/vhdlFile.py b/vsg/vhdlFile/vhdlFile.py index 6cd41a45..a5f53d43 100644 --- a/vsg/vhdlFile/vhdlFile.py +++ b/vsg/vhdlFile/vhdlFile.py @@ -519,7 +519,7 @@ class vhdlFile(): return lReturn - def get_tokens_bounded_by(self, oLeft, oRight, include_trailing_whitespace=False): + def get_tokens_bounded_by(self, oLeft, oRight, include_trailing_whitespace=False, bExcludeLastToken=False): iLine = 1 lTemp = [] lReturn = [] @@ -542,6 +542,12 @@ class vhdlFile(): bStore = False if isinstance(self.lAllObjects[iIndex], oRight) and bStore: if not include_trailing_whitespace: + if bExcludeLastToken: + lTemp.pop() + if isinstance(lTemp[-1], parser.whitespace): + lTemp.pop() + if isinstance(lTemp[-1], parser.carriage_return): + lTemp.pop() lReturn.append(Tokens(iStart, iStartLine, lTemp)) lTemp = [] bStore = False