Refactored rule architecture_004 to use new parser.
Added token_case rule to support other case rules. Improved testing of architecture_004.
This commit is contained in:
parent
6c44ef62f7
commit
5efc4c5d1b
14
vsg/rule.py
14
vsg/rule.py
|
|
@ -56,6 +56,16 @@ class rule():
|
|||
dViolation['solution'] = self._get_solution(iLineNumber)
|
||||
lReturn.append(dViolation)
|
||||
|
||||
def _build_violation_dict_from_violation_object(self, violation):
|
||||
dViolation = {}
|
||||
dViolation['severity'] = {}
|
||||
dViolation['severity']['name'] = self.severity.name
|
||||
dViolation['severity']['type'] = self.severity.type
|
||||
dViolation['rule'] = self.get_unique_id()
|
||||
dViolation['lineNumber'] = str(violation.get_line_number())
|
||||
dViolation['solution'] = violation.get_solution()
|
||||
return dViolation
|
||||
|
||||
def get_unique_id(self):
|
||||
return self.name + '_' + self.identifier
|
||||
|
||||
|
|
@ -84,8 +94,8 @@ class rule():
|
|||
sViolation = str(dLineViolation['number'])
|
||||
self._build_violation_dict(lReturn, sViolation, iLineNumber)
|
||||
except AttributeError:
|
||||
sViolation = str(violation.get_line_number())
|
||||
self._build_violation_dict(lReturn, sViolation, iLineNumber)
|
||||
if violation.get_line_number() == iLineNumber:
|
||||
lReturn.append(self._build_violation_dict_from_violation_object(violation))
|
||||
|
||||
return lReturn
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ from .remove_spaces_before_token_rule import remove_spaces_before_token_rule
|
|||
from .move_item_next_to_another_item_rule import move_item_next_to_another_item_rule
|
||||
from .multiple_spaces_after_rule import multiple_spaces_after_rule
|
||||
from .case_item_rule import case_item_rule
|
||||
from .token_case import token_case
|
||||
from .search_for_and_replace_keyword_rule import search_for_and_replace_keyword_rule
|
||||
from .case_rule import case_rule
|
||||
from .prefix_rule import prefix_rule
|
||||
|
|
|
|||
|
|
@ -1,16 +1,12 @@
|
|||
|
||||
from vsg.rules import case_rule
|
||||
from vsg import utils
|
||||
from vsg.rules import token_case
|
||||
|
||||
from vsg.token import architecture_body as token
|
||||
|
||||
class rule_004(case_rule):
|
||||
class rule_004(token_case):
|
||||
'''
|
||||
Entity rule 004 checks the architecture keyword has proper case.
|
||||
'''
|
||||
|
||||
def __init__(self):
|
||||
case_rule.__init__(self, 'architecture', '004', 'isArchitectureKeyword')
|
||||
self.solution = 'Change "architecture" keyword to '
|
||||
|
||||
def _extract(self, oLine):
|
||||
return utils.extract_words(oLine, ['architecture'])
|
||||
token_case.__init__(self, 'architecture', '004', token.architecture_keyword)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
|
||||
|
||||
from vsg import rule_item
|
||||
from vsg import utils
|
||||
from vsg import parser
|
||||
|
||||
from vsg import violation
|
||||
|
||||
|
||||
class token_case(rule_item.Rule):
|
||||
'''
|
||||
Checks the case for words.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
|
||||
name : string
|
||||
The group the rule belongs to.
|
||||
|
||||
identifier : string
|
||||
unique identifier. Usually in the form of 00N.
|
||||
|
||||
trigger : parser object type
|
||||
object type to apply the case check against
|
||||
'''
|
||||
|
||||
def __init__(self, name, identifier, token):
|
||||
rule_item.Rule.__init__(self, name=name, identifier=identifier)
|
||||
self.solution = None
|
||||
self.phase = 6
|
||||
self.case = 'lower'
|
||||
self.configuration.append('case')
|
||||
self.oToken = token
|
||||
|
||||
def analyze(self, oFile):
|
||||
lTokens = oFile.get_tokens_matching([self.oToken])
|
||||
for oToken in lTokens:
|
||||
sObjectValue = oToken.get_tokens()[0].get_value()
|
||||
if self.case == 'lower':
|
||||
if not sObjectValue.islower():
|
||||
sSolution = 'Change "' + sObjectValue + '" to "' + sObjectValue.lower() + '"'
|
||||
self.add_violation(violation.New(oToken.get_line_number(), oToken, sSolution))
|
||||
if self.case == 'upper':
|
||||
if not sObjectValue.isupper():
|
||||
sSolution = 'Change "' + sObjectValue + '" to "' + sObjectValue.upper() + '"'
|
||||
self.add_violation(violation.New(oToken.get_line_number(), oToken, sSolution))
|
||||
|
||||
|
||||
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()
|
||||
if self.case == 'lower':
|
||||
lTokens[0].set_value(lTokens[0].get_value().lower())
|
||||
if self.case == 'upper':
|
||||
lTokens[0].set_value(lTokens[0].get_value().upper())
|
||||
oViolation.set_tokens(lTokens)
|
||||
oFile.update(self.violations)
|
||||
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
architecture RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
architecture RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
architecture RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
ARCHITECTURE RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
ARCHITECTURE RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
ARCHITECTURE RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
architecture RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
ARCHITECTURE RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
ArChItEcTuRe RTL of ENT is
|
||||
begin
|
||||
end;
|
||||
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
|
||||
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_004_test_input.vhd'))
|
||||
|
||||
lExpected_lower = []
|
||||
lExpected_lower.append('')
|
||||
utils.read_file(os.path.join(sTestDir, 'rule_004_test_input.fixed_lower.vhd'), lExpected_lower)
|
||||
|
||||
lExpected_upper = []
|
||||
lExpected_upper.append('')
|
||||
utils.read_file(os.path.join(sTestDir, 'rule_004_test_input.fixed_upper.vhd'), lExpected_upper)
|
||||
|
||||
class test_architecture_rule(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.oFile = vhdlFile.vhdlFile(lFile)
|
||||
|
||||
def test_rule_004_lower(self):
|
||||
oRule = architecture.rule_004()
|
||||
self.assertTrue(oRule)
|
||||
self.assertEqual(oRule.name, 'architecture')
|
||||
self.assertEqual(oRule.identifier, '004')
|
||||
|
||||
lExpected = [6, 10]
|
||||
|
||||
oRule.analyze(self.oFile)
|
||||
self.assertEqual(utils.extract_violation_lines_from_violation_object(oRule.violations), lExpected)
|
||||
|
||||
def test_rule_004_upper(self):
|
||||
oRule = architecture.rule_004()
|
||||
oRule.case = 'upper'
|
||||
self.assertTrue(oRule)
|
||||
self.assertEqual(oRule.name, 'architecture')
|
||||
self.assertEqual(oRule.identifier, '004')
|
||||
|
||||
lExpected = [2, 10]
|
||||
oRule.analyze(self.oFile)
|
||||
self.assertEqual(utils.extract_violation_lines_from_violation_object(oRule.violations), lExpected)
|
||||
|
||||
def test_fix_rule_004_lower(self):
|
||||
oRule = architecture.rule_004()
|
||||
|
||||
oRule.fix(self.oFile)
|
||||
|
||||
lActual = []
|
||||
for oLine in self.oFile.lines:
|
||||
lActual.append(oLine.line)
|
||||
|
||||
self.assertEqual(lExpected_lower, lActual)
|
||||
|
||||
oRule.analyze(self.oFile)
|
||||
self.assertEqual(oRule.violations, [])
|
||||
|
||||
def test_fix_rule_004_upper(self):
|
||||
oRule = architecture.rule_004()
|
||||
oRule.case = 'upper'
|
||||
|
||||
oRule.fix(self.oFile)
|
||||
|
||||
lActual = []
|
||||
for oLine in self.oFile.lines:
|
||||
lActual.append(oLine.line)
|
||||
|
||||
self.assertEqual(lExpected_upper, lActual)
|
||||
|
||||
oRule.analyze(self.oFile)
|
||||
self.assertEqual(oRule.violations, [])
|
||||
|
||||
|
|
@ -62,36 +62,6 @@ class testRuleArchitectureMethods(unittest.TestCase):
|
|||
oRule.analyze(oFile)
|
||||
self.assertEqual(oRule.violations, dExpected)
|
||||
|
||||
def test_rule_004_exists(self):
|
||||
oRule = architecture.rule_004()
|
||||
self.assertTrue(oRule)
|
||||
self.assertEqual(oRule.name, 'architecture')
|
||||
self.assertEqual(oRule.identifier, '004')
|
||||
|
||||
def test_rule_004_lowercase(self):
|
||||
oRule = architecture.rule_004()
|
||||
|
||||
dExpected = [{'lines':[{'number': 20}], 'words_to_fix': {'Architecture'}}]
|
||||
oRule.analyze(oFile)
|
||||
self.assertEqual(oRule.violations, dExpected)
|
||||
|
||||
def test_rule_004_uppercase(self):
|
||||
oRule = architecture.rule_004()
|
||||
oRule.case = 'upper'
|
||||
|
||||
dExpected = [{'lines':[{'number': 3}], 'words_to_fix': {'architecture'}},
|
||||
{'lines':[{'number': 9}], 'words_to_fix': {'architecture'}},
|
||||
{'lines':[{'number': 14}], 'words_to_fix': {'architecture'}},
|
||||
{'lines':[{'number': 20}], 'words_to_fix': {'Architecture'}},
|
||||
{'lines':[{'number': 26}], 'words_to_fix': {'architecture'}},
|
||||
{'lines':[{'number': 33}], 'words_to_fix': {'architecture'}},
|
||||
{'lines':[{'number': 37}], 'words_to_fix': {'architecture'}},
|
||||
{'lines':[{'number': 47}], 'words_to_fix': {'architecture'}},
|
||||
{'lines':[{'number': 59}], 'words_to_fix': {'architecture'}},
|
||||
{'lines':[{'number': 81}], 'words_to_fix': {'architecture'}}]
|
||||
oRule.analyze(oFile)
|
||||
self.assertEqual(oRule.violations, dExpected)
|
||||
|
||||
def test_rule_006_exists(self):
|
||||
oRule = architecture.rule_006()
|
||||
self.assertTrue(oRule)
|
||||
|
|
|
|||
|
|
@ -323,9 +323,20 @@ class vhdlFile():
|
|||
iLine +=1
|
||||
|
||||
return lReturn
|
||||
|
||||
|
||||
|
||||
def get_tokens_matching(self, lTokens):
|
||||
iLine = 1
|
||||
lReturn = []
|
||||
for iIndex in range(0, len(self.lAllObjects)):
|
||||
for oToken in lTokens:
|
||||
if isinstance(self.lAllObjects[iIndex], oToken):
|
||||
lReturn.append(Tokens(iIndex, iLine, [self.lAllObjects[iIndex]]))
|
||||
|
||||
if isinstance(self.lAllObjects[iIndex], parser.carriage_return):
|
||||
iLine +=1
|
||||
|
||||
return lReturn
|
||||
|
||||
|
||||
def _create_empty_return_dictionary():
|
||||
dReturn = {}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
class New():
|
||||
|
||||
def __init__(self, iLine, oTokens):
|
||||
def __init__(self, iLine, oTokens, sSolution=None):
|
||||
self.iLine = iLine
|
||||
self.oTokens = oTokens
|
||||
self.sSolution = sSolution
|
||||
|
||||
def get_tokens(self):
|
||||
return self.oTokens.get_tokens()
|
||||
|
|
@ -14,3 +15,9 @@ class New():
|
|||
|
||||
def set_tokens(self, lTokens):
|
||||
self.oTokens.set_tokens(lTokens)
|
||||
|
||||
def set_solution(self, sSolution):
|
||||
self.solution = sSolution
|
||||
|
||||
def get_solution(self):
|
||||
return self.sSolution
|
||||
|
|
|
|||
Loading…
Reference in New Issue