From 5efc4c5d1b50b970cd3b5b501db1cd97e9930b2d Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Sun, 20 Sep 2020 12:28:09 -0500 Subject: [PATCH] Refactored rule architecture_004 to use new parser. Added token_case rule to support other case rules. Improved testing of architecture_004. --- vsg/rule.py | 14 +++- vsg/rules/__init__.py | 1 + vsg/rules/architecture/rule_004.py | 12 +-- vsg/rules/token_case.py | 68 +++++++++++++++++ .../rule_004_test_input.fixed_lower.vhd | 13 ++++ .../rule_004_test_input.fixed_upper.vhd | 13 ++++ .../architecture/rule_004_test_input.vhd | 13 ++++ vsg/tests/architecture/test_rule_004.py | 76 +++++++++++++++++++ .../architecture/test_rule_architecture.py | 30 -------- vsg/vhdlFile/vhdlFile.py | 15 +++- vsg/violation.py | 9 ++- 11 files changed, 221 insertions(+), 43 deletions(-) create mode 100644 vsg/rules/token_case.py create mode 100644 vsg/tests/architecture/rule_004_test_input.fixed_lower.vhd create mode 100644 vsg/tests/architecture/rule_004_test_input.fixed_upper.vhd create mode 100644 vsg/tests/architecture/rule_004_test_input.vhd create mode 100644 vsg/tests/architecture/test_rule_004.py diff --git a/vsg/rule.py b/vsg/rule.py index 4e5228a6..4775e44f 100644 --- a/vsg/rule.py +++ b/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 diff --git a/vsg/rules/__init__.py b/vsg/rules/__init__.py index c047b42c..63a2653f 100644 --- a/vsg/rules/__init__.py +++ b/vsg/rules/__init__.py @@ -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 diff --git a/vsg/rules/architecture/rule_004.py b/vsg/rules/architecture/rule_004.py index cff92f44..9f62c3df 100644 --- a/vsg/rules/architecture/rule_004.py +++ b/vsg/rules/architecture/rule_004.py @@ -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) diff --git a/vsg/rules/token_case.py b/vsg/rules/token_case.py new file mode 100644 index 00000000..5d26c8fd --- /dev/null +++ b/vsg/rules/token_case.py @@ -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) + diff --git a/vsg/tests/architecture/rule_004_test_input.fixed_lower.vhd b/vsg/tests/architecture/rule_004_test_input.fixed_lower.vhd new file mode 100644 index 00000000..f6a6e2f0 --- /dev/null +++ b/vsg/tests/architecture/rule_004_test_input.fixed_lower.vhd @@ -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; + diff --git a/vsg/tests/architecture/rule_004_test_input.fixed_upper.vhd b/vsg/tests/architecture/rule_004_test_input.fixed_upper.vhd new file mode 100644 index 00000000..0e59e421 --- /dev/null +++ b/vsg/tests/architecture/rule_004_test_input.fixed_upper.vhd @@ -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; + diff --git a/vsg/tests/architecture/rule_004_test_input.vhd b/vsg/tests/architecture/rule_004_test_input.vhd new file mode 100644 index 00000000..2180ab9c --- /dev/null +++ b/vsg/tests/architecture/rule_004_test_input.vhd @@ -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; + diff --git a/vsg/tests/architecture/test_rule_004.py b/vsg/tests/architecture/test_rule_004.py new file mode 100644 index 00000000..7c36d66f --- /dev/null +++ b/vsg/tests/architecture/test_rule_004.py @@ -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, []) + diff --git a/vsg/tests/architecture/test_rule_architecture.py b/vsg/tests/architecture/test_rule_architecture.py index d62bf627..e16ef13e 100644 --- a/vsg/tests/architecture/test_rule_architecture.py +++ b/vsg/tests/architecture/test_rule_architecture.py @@ -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) diff --git a/vsg/vhdlFile/vhdlFile.py b/vsg/vhdlFile/vhdlFile.py index 52bdd244..fc796abc 100644 --- a/vsg/vhdlFile/vhdlFile.py +++ b/vsg/vhdlFile/vhdlFile.py @@ -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 = {} diff --git a/vsg/violation.py b/vsg/violation.py index 1e69ebaf..c0ed0a2a 100644 --- a/vsg/violation.py +++ b/vsg/violation.py @@ -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