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
This commit is contained in:
Jeremiah Leary 2020-09-22 20:20:45 -05:00
parent d3640ddcfe
commit b6ea6f82db
7 changed files with 146 additions and 21 deletions

View File

@ -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

View File

@ -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.'

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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, [])

View File

@ -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)