From b6ea6f82dbd88d545d2e6f7f8f4ea0303c8ef607 Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Tue, 22 Sep 2020 20:20:45 -0500 Subject: [PATCH] Refactored rule architecture_012 for new parser. 1) Refactored rule 2) Added base rule single_space_between_tokens 3) Updated tests 4) Removed old tests --- vsg/rules/__init__.py | 1 + vsg/rules/architecture/rule_012.py | 12 ++-- vsg/rules/single_space_between_tokens.py | 57 +++++++++++++++++++ .../rule_012_test_input.fixed.vhd | 18 ++++++ .../architecture/rule_012_test_input.vhd | 18 ++++++ vsg/tests/architecture/test_rule_012.py | 46 +++++++++++++++ .../architecture/test_rule_architecture.py | 15 ----- 7 files changed, 146 insertions(+), 21 deletions(-) create mode 100644 vsg/rules/single_space_between_tokens.py create mode 100644 vsg/tests/architecture/rule_012_test_input.fixed.vhd create mode 100644 vsg/tests/architecture/rule_012_test_input.vhd create mode 100644 vsg/tests/architecture/test_rule_012.py diff --git a/vsg/rules/__init__.py b/vsg/rules/__init__.py index 36799b32..e8c2291d 100644 --- a/vsg/rules/__init__.py +++ b/vsg/rules/__init__.py @@ -36,6 +36,7 @@ from .indent_item_rule import indent_item_rule from .move_item_next_to_one_of_several_items_rule import move_item_next_to_one_of_several_items_rule from .blank_line_above_line_starting_with_token import blank_line_above_line_starting_with_token from .insert_token_right_of_token_if_it_does_not_exist import insert_token_right_of_token_if_it_does_not_exist +from .single_space_between_tokens import single_space_between_tokens from vsg.rules import after from vsg.rules import architecture diff --git a/vsg/rules/architecture/rule_012.py b/vsg/rules/architecture/rule_012.py index b1978d6b..65bc20ff 100644 --- a/vsg/rules/architecture/rule_012.py +++ b/vsg/rules/architecture/rule_012.py @@ -1,12 +1,12 @@ -from vsg.rules import single_space_after_rule +from vsg.rules import single_space_between_tokens +from vsg.token import architecture_body as token -class rule_012(single_space_after_rule): +class rule_012(single_space_between_tokens): ''' - Architecture rule 012 checks for a single space between the "end" and "architecture" keywords. + Architecture rule 012 checks for a single space between the *end* and *architecture* keywords. ''' - def __init__(self): - single_space_after_rule.__init__(self, 'architecture', '012', 'isEndArchitecture', 'end') - self.solution = 'Single space between "end" and "architecture" keywords.' + single_space_between_tokens.__init__(self, 'architecture', '012', token.end_keyword, token.end_architecture_keyword) + self.solution = 'Reduce spaces between end and architecture keywords to a single space.' diff --git a/vsg/rules/single_space_between_tokens.py b/vsg/rules/single_space_between_tokens.py new file mode 100644 index 00000000..625a4a12 --- /dev/null +++ b/vsg/rules/single_space_between_tokens.py @@ -0,0 +1,57 @@ + + +from vsg import parser +from vsg import rule_item +from vsg import violation + + +class single_space_between_tokens(rule_item.Rule): + ''' + Checks for a single space between two tokens. + + Parameters + ---------- + + name : string + The group the rule belongs to. + + identifier : string + unique identifier. Usually in the form of 00N. + + left_token : token object + The left token to check for a space between the right token + + right_token : token object + The right token to check for a space between the left token + ''' + + def __init__(self, name, identifier, left_token, right_token): + rule_item.Rule.__init__(self, name=name, identifier=identifier) + self.solution = None + self.phase = 2 + self.left_token = left_token + self.right_token = right_token + + def analyze(self, oFile): + lToi = oFile.get_sequence_of_tokens_matching([self.left_token, parser.whitespace, self.right_token]) + for oToi in lToi: + lTokens = oToi.get_tokens() + if len(lTokens[1].get_value()) != 1: + self.add_violation(violation.New(oToi.get_line_number(), oToi, self.solution)) + + def fix(self, oFile): + ''' + Applies fixes for any rule violations. + ''' + if self.fixable: + self.analyze(oFile) + self._print_debug_message('Fixing rule: ' + self.name + '_' + self.identifier) + self._fix_violation(oFile) + self.violations = [] + + def _fix_violation(self, oFile): + for oViolation in self.violations: + lTokens = oViolation.get_tokens() + lTokens[1].set_value(' ') + oViolation.set_tokens(lTokens) + oFile.update(self.violations) diff --git a/vsg/tests/architecture/rule_012_test_input.fixed.vhd b/vsg/tests/architecture/rule_012_test_input.fixed.vhd new file mode 100644 index 00000000..48e49c70 --- /dev/null +++ b/vsg/tests/architecture/rule_012_test_input.fixed.vhd @@ -0,0 +1,18 @@ + +architecture RTL of ENT is +begin +end architecture RTL; + +architecture RTL of ENT is +begin +end RTL; + +architecture RTL of ENT is +begin +end architecture RTL; + +architecture RTL of ENT is +begin +end +architecture RTL; + diff --git a/vsg/tests/architecture/rule_012_test_input.vhd b/vsg/tests/architecture/rule_012_test_input.vhd new file mode 100644 index 00000000..c4a88fac --- /dev/null +++ b/vsg/tests/architecture/rule_012_test_input.vhd @@ -0,0 +1,18 @@ + +architecture RTL of ENT is +begin +end architecture RTL; + +architecture RTL of ENT is +begin +end RTL; + +architecture RTL of ENT is +begin +end architecture RTL; + +architecture RTL of ENT is +begin +end +architecture RTL; + diff --git a/vsg/tests/architecture/test_rule_012.py b/vsg/tests/architecture/test_rule_012.py new file mode 100644 index 00000000..c0a58d3c --- /dev/null +++ b/vsg/tests/architecture/test_rule_012.py @@ -0,0 +1,46 @@ + +import os +import unittest + +from vsg.rules import architecture +from vsg import vhdlFile +from vsg.tests import utils + +sTestDir = os.path.dirname(__file__) + +lFile = utils.read_vhdlfile(os.path.join(sTestDir,'rule_012_test_input.vhd')) + +lExpected = [] +lExpected.append('') +utils.read_file(os.path.join(sTestDir, 'rule_012_test_input.fixed.vhd'), lExpected) + + +class test_architecture_rule(unittest.TestCase): + + def setUp(self): + self.oFile = vhdlFile.vhdlFile(lFile) + + def test_rule_012(self): + oRule = architecture.rule_012() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'architecture') + self.assertEqual(oRule.identifier, '012') + + lExpected = [12] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) + + def test_fix_rule_012(self): + oRule = architecture.rule_012() + + 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/architecture/test_rule_architecture.py b/vsg/tests/architecture/test_rule_architecture.py index 6f84747d..e14b9064 100644 --- a/vsg/tests/architecture/test_rule_architecture.py +++ b/vsg/tests/architecture/test_rule_architecture.py @@ -12,12 +12,6 @@ oFile = vhdlFile.vhdlFile(lFile) lFileComment = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'..','architecture','architecture_comment_test_input.vhd')) oFileComment = vhdlFile.vhdlFile(lFileComment) -lFileIs = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'architecture_is_test_input.vhd')) -oFileIs = vhdlFile.vhdlFile(lFileIs) - -lFileEnd = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'architecture_end_test_input.vhd')) -oFileEnd = vhdlFile.vhdlFile(lFileEnd) - lFileName = utils.read_vhdlfile(os.path.join(os.path.dirname(__file__),'architecture_name_test_input.vhd')) oFileName = vhdlFile.vhdlFile(lFileName) @@ -42,15 +36,6 @@ class testRuleArchitectureMethods(unittest.TestCase): self.assertEqual(oRule.name, 'architecture') self.assertEqual(oRule.identifier, '003') - def test_rule_012(self): - oRule = architecture.rule_012() - self.assertTrue(oRule) - self.assertEqual(oRule.name, 'architecture') - self.assertEqual(oRule.identifier, '012') - dExpected = utils.add_violation_list([7,18]) - oRule.analyze(oFile) - self.assertEqual(oRule.violations, dExpected) - def test_rule_015(self): oRule = architecture.rule_015() self.assertTrue(oRule)