diff options
author | Tristan Gingold <tgingold@free.fr> | 2022-11-20 20:02:08 +0100 |
---|---|---|
committer | Tristan Gingold <tgingold@free.fr> | 2022-11-20 20:02:08 +0100 |
commit | 1ea6e91b7ef11e8d7fa4679bd9cb13e91db53684 (patch) | |
tree | 1dd3932d728dbcae31636d4ece25b36fe7637765 /testsuite/pyunit | |
parent | f722f900f4211bbddc0f432ce652e68313807ee0 (diff) | |
download | ghdl-1ea6e91b7ef11e8d7fa4679bd9cb13e91db53684.tar.gz ghdl-1ea6e91b7ef11e8d7fa4679bd9cb13e91db53684.tar.bz2 ghdl-1ea6e91b7ef11e8d7fa4679bd9cb13e91db53684.zip |
testsuite/pyunit: add Comments.py test
Diffstat (limited to 'testsuite/pyunit')
-rw-r--r-- | testsuite/pyunit/DesignComment.vhdl | 14 | ||||
-rw-r--r-- | testsuite/pyunit/libghdl/Comments.py | 76 | ||||
-rwxr-xr-x | testsuite/pyunit/libghdl/testsuite.sh | 3 |
3 files changed, 93 insertions, 0 deletions
diff --git a/testsuite/pyunit/DesignComment.vhdl b/testsuite/pyunit/DesignComment.vhdl new file mode 100644 index 000000000..5cd555d33 --- /dev/null +++ b/testsuite/pyunit/DesignComment.vhdl @@ -0,0 +1,14 @@ +-- No copyright for :accum: design. + +library ieee; +use ieee.std_logic_1164.all; + +entity accum is + port ( + -- :a: and :b: are the inputs of the adder. + a, b : in std_logic_vector (31 downto 0); + -- :res: is the result of the adder. + res : out std_logic_vector (31 downto 0) + ); +end accum; + diff --git a/testsuite/pyunit/libghdl/Comments.py b/testsuite/pyunit/libghdl/Comments.py new file mode 100644 index 000000000..ada8f326e --- /dev/null +++ b/testsuite/pyunit/libghdl/Comments.py @@ -0,0 +1,76 @@ +from pathlib import Path +from unittest import TestCase + +import pyGHDL.libghdl as libghdl +from pyGHDL.libghdl import name_table, files_map, errorout_console, flags +from pyGHDL.libghdl import file_comments +from pyGHDL.libghdl.vhdl import nodes, sem_lib + + +if __name__ == "__main__": + print("Use: 'python -m unitest <testcase module>'") + exit(1) + + +class Instantiate(TestCase): + _root = Path(__file__).resolve().parent.parent + _filename: Path = _root / "DesignComment.vhdl" + + @staticmethod + def getIdentifier(node) -> str: + """Return the Python string from node :obj:`node` identifier.""" + return name_table.Get_Name_Ptr(nodes.Get_Identifier(node)) + + def checkComments(self, node, name) -> None: + f = files_map.Location_To_File(nodes.Get_Location(node)) + idx = file_comments.Find_First_Comment(f, node) + while idx != file_comments.No_Comment_Index: + s = file_comments.Get_Comment(f, idx) + self.assertTrue(s.find(':'+name+':') >= 0, + "no :{}: in '{}'".format(name, s)) + idx = file_comments.Get_Next_Comment(f, idx) + + def test_Comments(self) -> None: + """Initialization: set options and then load libaries.""" + libghdl.initialize() + + # Print error messages on the console. + errorout_console.Install_Handler() + + # Set options. This must be done before analyze_init() + flags.Flag_Gather_Comments.value = True + libghdl.set_option("--std=08") + + # Finish initialization. This will load the standard package. + if libghdl.analyze_init_status() != 0: + self.fail("libghdl initialization error") + + # Load the file + file_id = name_table.Get_Identifier(str(self._filename)) + sfe = files_map.Read_Source_File(name_table.Null_Identifier, file_id) + if sfe == files_map.No_Source_File_Entry: + self.fail("Cannot read file '{!s}'".format(self._filename)) + + # Parse + file = sem_lib.Load_File(sfe) + + # Display all design units + designUnit = nodes.Get_First_Design_Unit(file) + while designUnit != nodes.Null_Iir: + libraryUnit = nodes.Get_Library_Unit(designUnit) + + if nodes.Get_Kind(libraryUnit) == nodes.Iir_Kind.Entity_Declaration: + entityName = self.getIdentifier(libraryUnit) + self.checkComments(designUnit, entityName) + port = nodes.Get_Port_Chain(libraryUnit) + while port != nodes.Null_Iir: + self.checkComments(port, self.getIdentifier(port)) + port = nodes.Get_Chain(port) + + elif nodes.Get_Kind(libraryUnit) == nodes.Iir_Kind.Architecture_Body: + architectureName = self.getIdentifier(libraryUnit) + + else: + self.fail("Unknown unit.") + + designUnit = nodes.Get_Chain(designUnit) diff --git a/testsuite/pyunit/libghdl/testsuite.sh b/testsuite/pyunit/libghdl/testsuite.sh new file mode 100755 index 000000000..714bf8bec --- /dev/null +++ b/testsuite/pyunit/libghdl/testsuite.sh @@ -0,0 +1,3 @@ +for f in Initialize.py Comments.py; do + PYTHONPATH=../../.. python3 -m unittest $f +done |