From ba5ae10b8ed49b21a88198a143bb79db5b151325 Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Sun, 26 Jul 2020 14:48:02 -0500 Subject: [PATCH] Updates for issue #393: Adding two new library rules: 1) Added rules library_010 and library_011 2) Added tests 3) Added documentation --- docs/library_rules.rst | 35 ++++++++++++++ vsg/rules/library/__init__.py | 2 + vsg/rules/library/rule_010.py | 15 ++++++ vsg/rules/library/rule_011.py | 15 ++++++ ...ontext_classification_test_input.fixed.vhd | 9 ++-- ...erence_classification_test_input.fixed.vhd | 3 +- .../library/rule_010_test_input.fixed.vhd | 13 +++++ vsg/tests/library/rule_010_test_input.vhd | 10 ++++ .../library/rule_011_test_input.fixed.vhd | 17 +++++++ vsg/tests/library/rule_011_test_input.vhd | 11 +++++ vsg/tests/library/test_rule_010.py | 47 +++++++++++++++++++ vsg/tests/library/test_rule_011.py | 47 +++++++++++++++++++ vsg/tests/utils.py | 5 ++ 13 files changed, 225 insertions(+), 4 deletions(-) create mode 100644 vsg/rules/library/rule_010.py create mode 100644 vsg/rules/library/rule_011.py create mode 100644 vsg/tests/library/rule_010_test_input.fixed.vhd create mode 100644 vsg/tests/library/rule_010_test_input.vhd create mode 100644 vsg/tests/library/rule_011_test_input.fixed.vhd create mode 100644 vsg/tests/library/rule_011_test_input.vhd create mode 100644 vsg/tests/library/test_rule_010.py create mode 100644 vsg/tests/library/test_rule_011.py diff --git a/docs/library_rules.rst b/docs/library_rules.rst index 657c7b98..20c812b4 100644 --- a/docs/library_rules.rst +++ b/docs/library_rules.rst @@ -191,3 +191,38 @@ This rule checks alignment of comments above library use statements. -- Use standard logic library use ieee.std_logic_1164.all; +library_010 +########### + +This rule checks the **library** keyword is on it's own line. + +**Violation** + +.. code-block:: vhdl + + context c1 is library ieee; use ieee.std_logic_1164.all; end context c1; + +**Fix** + +.. code-block:: vhdl + + context c1 is + library ieee; use ieee.std_logic_1164.all; end context c1; + +library_011 +########### + +This rule checks the **use** keyword is on it's own line. + +**Violation** + +.. code-block:: vhdl + + context c1 is library ieee; use ieee.std_logic_1164.all; end context c1; + +**Fix** + +.. code-block:: vhdl + + context c1 is library ieee; + use ieee.std_logic_1164.all; end context c1; diff --git a/vsg/rules/library/__init__.py b/vsg/rules/library/__init__.py index 38027e85..395595ca 100644 --- a/vsg/rules/library/__init__.py +++ b/vsg/rules/library/__init__.py @@ -8,3 +8,5 @@ from .rule_006 import rule_006 from .rule_007 import rule_007 from .rule_008 import rule_008 from .rule_009 import rule_009 +from .rule_010 import rule_010 +from .rule_011 import rule_011 diff --git a/vsg/rules/library/rule_010.py b/vsg/rules/library/rule_010.py new file mode 100644 index 00000000..7b69ee63 --- /dev/null +++ b/vsg/rules/library/rule_010.py @@ -0,0 +1,15 @@ + +from vsg import parser +from vsg.rules import move_item_and_items_to_the_right_to_next_line_rule + + +class rule_010(move_item_and_items_to_the_right_to_next_line_rule): + ''' + Moves library keyword and code to the right to it's own line. + + ''' + + def __init__(self): + move_item_and_items_to_the_right_to_next_line_rule.__init__(self, 'library', '010', parser.library_keyword) + self.regionBegin = parser.library_keyword + self.regionEnd = parser.library_semicolon diff --git a/vsg/rules/library/rule_011.py b/vsg/rules/library/rule_011.py new file mode 100644 index 00000000..cfa7bc97 --- /dev/null +++ b/vsg/rules/library/rule_011.py @@ -0,0 +1,15 @@ + +from vsg import parser +from vsg.rules import move_item_and_items_to_the_right_to_next_line_rule + + +class rule_011(move_item_and_items_to_the_right_to_next_line_rule): + ''' + Moves use keyword and code to the right to it's own line. + + ''' + + def __init__(self): + move_item_and_items_to_the_right_to_next_line_rule.__init__(self, 'library', '011', parser.use_keyword) + self.regionBegin = parser.use_keyword + self.regionEnd = parser.use_semicolon diff --git a/vsg/tests/context/context_classification_test_input.fixed.vhd b/vsg/tests/context/context_classification_test_input.fixed.vhd index 181f5a8a..964265ad 100644 --- a/vsg/tests/context/context_classification_test_input.fixed.vhd +++ b/vsg/tests/context/context_classification_test_input.fixed.vhd @@ -33,19 +33,22 @@ end context c1; -- Try single line context c1 is - library ieee; use ieee.std_logic_1164; + library ieee; + use ieee.std_logic_1164; end context c1; context c1 is - library ieee; use ieee.std_logic_1164; + library ieee; + use ieee.std_logic_1164; end context c1; context c1 is - library ieee; use ieee.std_logic_1164; + library ieee; + use ieee.std_logic_1164; end context c1; diff --git a/vsg/tests/context_reference/context_reference_classification_test_input.fixed.vhd b/vsg/tests/context_reference/context_reference_classification_test_input.fixed.vhd index f5d90b2b..09030f95 100644 --- a/vsg/tests/context_reference/context_reference_classification_test_input.fixed.vhd +++ b/vsg/tests/context_reference/context_reference_classification_test_input.fixed.vhd @@ -37,7 +37,8 @@ end context c1; -- Try single line context c1 is - library ieee; use ieee.std_logic_1164; + library ieee; + use ieee.std_logic_1164; context cr1,cr2; end context c1; diff --git a/vsg/tests/library/rule_010_test_input.fixed.vhd b/vsg/tests/library/rule_010_test_input.fixed.vhd new file mode 100644 index 00000000..16e8cb57 --- /dev/null +++ b/vsg/tests/library/rule_010_test_input.fixed.vhd @@ -0,0 +1,13 @@ + +-- This should pass +library ieee; + use ieee.std_logic_1164.all; + +--- this should fail +library ieee; use ieee.std_logic_1164; +library analog; use analog.all; +library digital; use digital.gates.and; + +library ieee; use ieee.std_logic_1164; +library +analog; use analog.all; diff --git a/vsg/tests/library/rule_010_test_input.vhd b/vsg/tests/library/rule_010_test_input.vhd new file mode 100644 index 00000000..0dec4512 --- /dev/null +++ b/vsg/tests/library/rule_010_test_input.vhd @@ -0,0 +1,10 @@ + +-- This should pass +library ieee; + use ieee.std_logic_1164.all; + +--- this should fail +library ieee; use ieee.std_logic_1164; library analog; use analog.all; library digital; use digital.gates.and; + +library ieee; use ieee.std_logic_1164; library +analog; use analog.all; diff --git a/vsg/tests/library/rule_011_test_input.fixed.vhd b/vsg/tests/library/rule_011_test_input.fixed.vhd new file mode 100644 index 00000000..ff2c5e59 --- /dev/null +++ b/vsg/tests/library/rule_011_test_input.fixed.vhd @@ -0,0 +1,17 @@ + +-- This should pass +library ieee; + use ieee.std_logic_1164.all; + +--- this should fail +library ieee; +use ieee.std_logic_1164; library analog; +use analog.all; library digital; +use digital.gates.and; + +library ieee; +use ieee.std_logic_1164; library analog; +use +analog.all; +use +analog.all; diff --git a/vsg/tests/library/rule_011_test_input.vhd b/vsg/tests/library/rule_011_test_input.vhd new file mode 100644 index 00000000..5a5eaf40 --- /dev/null +++ b/vsg/tests/library/rule_011_test_input.vhd @@ -0,0 +1,11 @@ + +-- This should pass +library ieee; + use ieee.std_logic_1164.all; + +--- this should fail +library ieee; use ieee.std_logic_1164; library analog; use analog.all; library digital; use digital.gates.and; + +library ieee; use ieee.std_logic_1164; library analog; use +analog.all; use +analog.all; diff --git a/vsg/tests/library/test_rule_010.py b/vsg/tests/library/test_rule_010.py new file mode 100644 index 00000000..ee2b2e1d --- /dev/null +++ b/vsg/tests/library/test_rule_010.py @@ -0,0 +1,47 @@ + +import os +import unittest + +from vsg.rules import library +from vsg import vhdlFile +from vsg.tests import utils + +sTestDir = os.path.dirname(__file__) + +lFile = utils.read_vhdlfile(os.path.join(sTestDir,'rule_010_test_input.vhd')) +lExpected = [] +lExpected.append('') +utils.read_file(os.path.join(sTestDir, 'rule_010_test_input.fixed.vhd'), lExpected, False) + + +class test_library_rule(unittest.TestCase): + + def setUp(self): + self.oFile = vhdlFile.vhdlFile(lFile) + + def test_rule_010(self): + oRule = library.rule_010() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'library') + self.assertEqual(oRule.identifier, '010') + + lExpected = [7, 7, 9] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines(oRule.violations)) + + def test_fix_rule_010(self): + self.maxDiff = None + oRule = library.rule_010() + + 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/library/test_rule_011.py b/vsg/tests/library/test_rule_011.py new file mode 100644 index 00000000..48e11643 --- /dev/null +++ b/vsg/tests/library/test_rule_011.py @@ -0,0 +1,47 @@ + +import os +import unittest + +from vsg.rules import library +from vsg import vhdlFile +from vsg.tests import utils + +sTestDir = os.path.dirname(__file__) + +lFile = utils.read_vhdlfile(os.path.join(sTestDir,'rule_011_test_input.vhd')) +lExpected = [] +lExpected.append('') +utils.read_file(os.path.join(sTestDir, 'rule_011_test_input.fixed.vhd'), lExpected, False) + + +class test_library_rule(unittest.TestCase): + + def setUp(self): + self.oFile = vhdlFile.vhdlFile(lFile) + + def test_rule_011(self): + oRule = library.rule_011() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'library') + self.assertEqual(oRule.identifier, '011') + + lExpected = [7, 7, 7, 9, 9, 10] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines(oRule.violations)) + + def test_fix_rule_011(self): + self.maxDiff = None + oRule = library.rule_011() + + 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/utils.py b/vsg/tests/utils.py index 97736456..86e32dfc 100644 --- a/vsg/tests/utils.py +++ b/vsg/tests/utils.py @@ -68,3 +68,8 @@ def read_configuration(sFileName): with open(sFileName) as yaml_file: return yaml.full_load(yaml_file) +def extract_violation_lines(lViolations): + lReturn = [] + for dViolation in lViolations: + lReturn.append(dViolation['lines'][0]['number']) + return lReturn