Skip to content

Commit

Permalink
Fixed testcases after Symbol and Name changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paebbels committed Apr 15, 2023
1 parent a42ad96 commit d122187
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 66 deletions.
52 changes: 25 additions & 27 deletions pyVHDLModel/__init__.py
Expand Up @@ -761,7 +761,7 @@ def LinkLibraryReferences(self) -> None:
for libraryReference in designUnit._libraryReferences:
# A library clause can have multiple comma-separated references
for librarySymbol in libraryReference.Symbols:
libraryIdentifier = librarySymbol.NormalizedIdentifier
libraryIdentifier = librarySymbol.Name.NormalizedIdentifier
try:
library = self._libraries[libraryIdentifier]
except KeyError:
Expand Down Expand Up @@ -813,30 +813,29 @@ def LinkPackageReferences(self) -> None:

for packageReference in designUnit.PackageReferences:
# A use clause can have multiple comma-separated references
for symbol in packageReference.Symbols:
packageSymbol = symbol.Prefix
librarySymbol = packageSymbol.Prefix
for packageMemeberSymbol in packageReference.Symbols:
packageName = packageMemeberSymbol.Name.Prefix
libraryName = packageName.Prefix

libraryIdentifier = librarySymbol.NormalizedIdentifier
packageIdentifier = packageSymbol.NormalizedIdentifier
libraryIdentifier = libraryName.NormalizedIdentifier
packageIdentifier = packageName.NormalizedIdentifier

# In case work is used, resolve to the real library name.
if libraryIdentifier == "work":
library: Library = designUnit.Library
libraryIdentifier = library.NormalizedIdentifier
elif libraryIdentifier not in designUnit._referencedLibraries:
# TODO: This check doesn't trigger if it's the working library.
raise VHDLModelException(f"Use clause references library '{librarySymbol.Identifier}', which was not referenced by a library clause.")
raise VHDLModelException(f"Use clause references library '{libraryName.Identifier}', which was not referenced by a library clause.")

Check warning on line 829 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L829

Added line #L829 was not covered by tests
else:
library = self._libraries[libraryIdentifier]

try:
package = library._packages[packageIdentifier]
except KeyError:
raise VHDLModelException(f"Package '{packageSymbol.Identifier}' not found in {'working ' if librarySymbol.NormalizedIdentifier == 'work' else ''}library '{library.Identifier}'.")
raise VHDLModelException(f"Package '{packageName.Identifier}' not found in {'working ' if libraryName.NormalizedIdentifier == 'work' else ''}library '{library.Identifier}'.")

Check warning on line 836 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L836

Added line #L836 was not covered by tests

librarySymbol.Library = library
packageSymbol.Package = package
packageMemeberSymbol.Package = package

# TODO: warn duplicate package reference
designUnit._referencedPackages[libraryIdentifier][packageIdentifier] = package
Expand All @@ -845,11 +844,11 @@ def LinkPackageReferences(self) -> None:
dependency["kind"] = DependencyGraphEdgeKind.UseClause

# TODO: update the namespace with visible members
if isinstance(symbol, AllPackageMembersReferenceSymbol):
if isinstance(packageMemeberSymbol, AllPackageMembersReferenceSymbol):
for componentIdentifier, component in package._components.items():
designUnit._namespace._elements[componentIdentifier] = component

elif isinstance(symbol, PackageMembersReferenceSymbol):
elif isinstance(packageMemeberSymbol, PackageMembersReferenceSymbol):

Check warning on line 851 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L851

Added line #L851 was not covered by tests
raise NotImplementedError()
else:
raise VHDLModelException()
Expand All @@ -859,27 +858,26 @@ def LinkContextReferences(self) -> None:
for contextReference in designUnit._contextReferences:
# A context reference can have multiple comma-separated references
for contextSymbol in contextReference.Symbols:
librarySymbol = contextSymbol.Prefix
libraryName = contextSymbol.Name.Prefix

libraryIdentifier = librarySymbol.NormalizedIdentifier
contextIdentifier = contextSymbol.NormalizedIdentifier
libraryIdentifier = libraryName.NormalizedIdentifier
contextIdentifier = contextSymbol.Name.NormalizedIdentifier

# In case work is used, resolve to the real library name.
if libraryIdentifier == "work":
referencedLibrary = designUnit.Library
libraryIdentifier = referencedLibrary.NormalizedIdentifier
elif libraryIdentifier not in designUnit._referencedLibraries:
# TODO: This check doesn't trigger if it's the working library.
raise VHDLModelException(f"Context reference references library '{librarySymbol.Identifier}', which was not referenced by a library clause.")
raise VHDLModelException(f"Context reference references library '{libraryName.Identifier}', which was not referenced by a library clause.")

Check warning on line 872 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L872

Added line #L872 was not covered by tests
else:
referencedLibrary = self._libraries[libraryIdentifier]

try:
referencedContext = referencedLibrary._contexts[contextIdentifier]
except KeyError:
raise VHDLModelException(f"Context '{contextSymbol.Identifier}' not found in {'working ' if librarySymbol.NormalizedIdentifier == 'work' else ''}library '{referencedLibrary.Identifier}'.")
raise VHDLModelException(f"Context '{contextSymbol.Identifier}' not found in {'working ' if libraryName.NormalizedIdentifier == 'work' else ''}library '{referencedLibrary.Identifier}'.")

Check warning on line 879 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L879

Added line #L879 was not covered by tests

librarySymbol.Library = referencedLibrary
contextSymbol.Package = referencedContext

# TODO: warn duplicate referencedContext reference
Expand Down Expand Up @@ -925,33 +923,33 @@ def LinkInstantiations(self) -> None:
for architecture in self.IterateDesignUnits(DesignUnitKind.Architecture):
for instance in architecture.IterateInstantiations():
if isinstance(instance, EntityInstantiation):
libraryIdentifier = instance.Entity.Prefix.Identifier
normalizedLibraryIdentifier = instance.Entity.Prefix.NormalizedIdentifier
libraryName = instance.Entity.Name.Prefix
libraryIdentifier = libraryName.Identifier
normalizedLibraryIdentifier = libraryName.NormalizedIdentifier
if normalizedLibraryIdentifier == "work":
libraryIdentifier = architecture.Library.Identifier
normalizedLibraryIdentifier = architecture.Library.NormalizedIdentifier
elif normalizedLibraryIdentifier not in architecture._referencedLibraries:
ex =Exception(f"Referenced library '{libraryIdentifier}' in direct entity instantiation '{instance.Label}: entity {instance.Entity.Prefix.Identifier}.{instance.Entity.Identifier}' not found in architecture '{architecture!r}'.")
ex = VHDLModelException(f"Referenced library '{libraryIdentifier}' in direct entity instantiation '{instance.Label}: entity {instance.Entity.Prefix.Identifier}.{instance.Entity.Identifier}' not found in architecture '{architecture!r}'.")

Check warning on line 933 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L933

Added line #L933 was not covered by tests
ex.add_note(f"Add a library reference to the architecture or entity using a library clause like: 'library {libraryIdentifier};'.")
raise ex

try:
library = self._libraries[normalizedLibraryIdentifier]
except KeyError:
ex = Exception(f"Referenced library '{libraryIdentifier}' in direct entity instantiation '{instance.Label}: entity {instance.Entity.Prefix.Identifier}.{instance.Entity.Identifier}' not found in design.")
ex = VHDLModelException(f"Referenced library '{libraryIdentifier}' in direct entity instantiation '{instance.Label}: entity {instance.Entity.Prefix.Identifier}.{instance.Entity.Identifier}' not found in design.")

Check warning on line 940 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L940

Added line #L940 was not covered by tests
ex.add_note(f"No design units were parsed into library '{libraryIdentifier}'. Thus it doesn't exist in design.")
raise ex

try:
entity = library._entities[instance.Entity.NormalizedIdentifier]
entity = library._entities[instance.Entity.Name.NormalizedIdentifier]
except KeyError:
ex = Exception(f"Referenced entity '{instance.Entity.Identifier}' in direct entity instantiation '{instance.Label}: entity {instance.Entity.Prefix.Identifier}.{instance.Entity.Identifier}' not found in {'working ' if instance.Entity.Prefix.NormalizedIdentifier == 'work' else ''}library '{libraryIdentifier}'.")
ex = VHDLModelException(f"Referenced entity '{instance.Entity.Identifier}' in direct entity instantiation '{instance.Label}: entity {instance.Entity.Prefix.Identifier}.{instance.Entity.Identifier}' not found in {'working ' if instance.Entity.Prefix.NormalizedIdentifier == 'work' else ''}library '{libraryIdentifier}'.")

Check warning on line 947 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L947

Added line #L947 was not covered by tests
libs = [library.Identifier for library in self._libraries.values() for entityIdentifier in library._entities.keys() if entityIdentifier == instance.Entity.NormalizedIdentifier]
if libs:
ex.add_note(f"Found entity '{instance.Entity.Identifier}' in other libraries: {', '.join(libs)}")
ex.add_note(f"Found entity '{instance.Entity!s}' in other libraries: {', '.join(libs)}")

Check warning on line 950 in pyVHDLModel/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyVHDLModel/__init__.py#L950

Added line #L950 was not covered by tests
raise ex

instance.Entity.Prefix.Library = library
instance.Entity.Entity = entity

dependency = architecture._dependencyVertex.EdgeToVertex(entity._dependencyVertex, edgeValue=instance)
Expand Down Expand Up @@ -1350,7 +1348,7 @@ def _AddDesignUnit(self, item: DesignUnit) -> None:
if isinstance(item, Entity):
self._entities[identifier] = item
elif isinstance(item, Architecture):
entityIdentifier = item.Entity.NormalizedIdentifier
entityIdentifier = item.Entity.Name.NormalizedIdentifier
try:
architectures = self._architectures[entityIdentifier]
if identifier in architectures:
Expand Down
75 changes: 36 additions & 39 deletions tests/unit/Instantiate.py
Expand Up @@ -57,11 +57,11 @@ class Symbols(TestCase):
def test_LibraryReferenceSymbol(self):
symbol = LibraryReferenceSymbol(SimpleName("Lib"))

self.assertEqual("Lib", symbol.Identifier)
self.assertEqual("lib", symbol.NormalizedIdentifier)
self.assertIs(symbol, symbol.Root)
self.assertIsNone(symbol.Prefix)
self.assertFalse(symbol.HasPrefix)
self.assertEqual("Lib", symbol.Name.Identifier)
self.assertEqual("lib", symbol.Name.NormalizedIdentifier)
self.assertIs(symbol.Name, symbol.Name.Root)
self.assertIsNone(symbol.Name.Prefix)
self.assertFalse(symbol.Name.HasPrefix)
self.assertFalse(symbol.IsResolved)

library = Library("liB")
Expand All @@ -73,44 +73,41 @@ def test_LibraryReferenceSymbol(self):
def test_PackageReferenceSymbol(self):
symbol = PackageReferenceSymbol(SelectedName("pack", SimpleName("Lib")))

self.assertEqual("pack", symbol.NormalizedIdentifier)
self.assertEqual("lib", symbol.Prefix.NormalizedIdentifier)
self.assertTrue(symbol.HasPrefix)
self.assertEqual("pack", symbol.Name.NormalizedIdentifier)
self.assertEqual("lib", symbol.Name.Prefix.NormalizedIdentifier)
self.assertTrue(symbol.Name.HasPrefix)

library = Library("liB")
# library = Library("liB")
package = Package("Pack")
symbol.Package = package
symbol.Prefix.Library = library

self.assertTrue(symbol.IsResolved)
self.assertTrue(symbol.Prefix.IsResolved)
self.assertIs(library, symbol.Prefix.Library)
self.assertIs(package, symbol.Package)

def test_PackageMembersReferenceSymbol(self):
symbol = PackageMembersReferenceSymbol(SelectedName("obj", SelectedName("pack", SimpleName("Lib"))))

self.assertEqual("obj", symbol.NormalizedIdentifier)
self.assertEqual("pack", symbol.Prefix.NormalizedIdentifier)
self.assertEqual("lib", symbol.Prefix.Prefix.NormalizedIdentifier)
self.assertEqual("obj", symbol.Name.NormalizedIdentifier)
self.assertEqual("pack", symbol.Name.Prefix.NormalizedIdentifier)
self.assertEqual("lib", symbol.Name.Prefix.Prefix.NormalizedIdentifier)

def test_AllPackageMembersReferenceSymbol(self):
symbol = AllPackageMembersReferenceSymbol(AllName(SelectedName("pack", SimpleName("Lib"))))

self.assertEqual("all", symbol.NormalizedIdentifier)
self.assertEqual("pack", symbol.Prefix.NormalizedIdentifier)
self.assertEqual("lib", symbol.Prefix.Prefix.NormalizedIdentifier)
self.assertEqual("all", symbol.Name.NormalizedIdentifier)
self.assertEqual("pack", symbol.Name.Prefix.NormalizedIdentifier)
self.assertEqual("lib", symbol.Name.Prefix.Prefix.NormalizedIdentifier)

def test_ContextReferenceSymbol(self):
symbol = ContextReferenceSymbol(SelectedName("ctx", SimpleName("Lib")))

self.assertEqual("ctx", symbol.NormalizedIdentifier)
self.assertEqual("lib", symbol.Prefix.NormalizedIdentifier)
self.assertEqual("ctx", symbol.Name.NormalizedIdentifier)
self.assertEqual("lib", symbol.Name.Prefix.NormalizedIdentifier)

def test_EntitySymbol(self):
symbol = EntitySymbol(SimpleName("ent"))

self.assertEqual("ent", symbol.NormalizedIdentifier)
self.assertEqual("ent", symbol.Name.NormalizedIdentifier)

# def test_ArchitectureSymbol(self):
# symbol = ArchitectureSymbol("rtl")
Expand All @@ -120,23 +117,23 @@ def test_EntitySymbol(self):
def test_PackageSymbol(self):
symbol = PackageSymbol(SimpleName("pack"))

self.assertEqual("pack", symbol.NormalizedIdentifier)
self.assertEqual("pack", symbol.Name.NormalizedIdentifier)

def test_EntityInstantiationSymbol(self):
symbol = EntityInstantiationSymbol(SelectedName("ent", SimpleName("Lib")))

self.assertEqual("ent", symbol.NormalizedIdentifier)
self.assertEqual("lib", symbol.Prefix.NormalizedIdentifier)
self.assertEqual("ent", symbol.Name.NormalizedIdentifier)
self.assertEqual("lib", symbol.Name.Prefix.NormalizedIdentifier)

def test_ComponentInstantiationSymbol(self):
symbol = ComponentInstantiationSymbol(SimpleName("comp"))

self.assertEqual("comp", symbol.NormalizedIdentifier)
self.assertEqual("comp", symbol.Name.NormalizedIdentifier)

def test_ConfigurationInstantiationSymbol(self):
symbol = ConfigurationInstantiationSymbol(SimpleName("cfg"))

self.assertEqual("cfg", symbol.NormalizedIdentifier)
self.assertEqual("cfg", symbol.Name.NormalizedIdentifier)


class SimpleInstance(TestCase):
Expand Down Expand Up @@ -188,8 +185,8 @@ def test_Entity(self):
self.assertEqual(0, len(entity.Statements))

def test_Architecture(self):
entity = EntitySymbol("entity_1")
architecture = Architecture("arch_1", entity)
entitySymbol = EntitySymbol(SimpleName("entity_1"))
architecture = Architecture("arch_1", entitySymbol)

self.assertIsNotNone(architecture)
self.assertEqual("arch_1", architecture.Identifier)
Expand All @@ -204,7 +201,7 @@ def test_Package(self):
self.assertEqual(0, len(package.DeclaredItems))

def test_PackageBody(self):
packageSymbol = PackageSymbol("pack_1")
packageSymbol = PackageSymbol(SimpleName("pack_1"))
packageBody = PackageBody(packageSymbol)

self.assertIsNotNone(packageBody)
Expand Down Expand Up @@ -275,8 +272,8 @@ def test_Architecture(self):
path = Path("tests.vhdl")
document = Document(path)

entity = EntitySymbol("entity_1")
architecture = Architecture("arch_1", entity)
entitySymbol = EntitySymbol(SimpleName("entity_1"))
architecture = Architecture("arch_1", entitySymbol)
document._AddArchitecture(architecture)

self.assertEqual(1, len(document.Architectures))
Expand All @@ -296,7 +293,7 @@ def test_PackageBody(self):
path = Path("tests.vhdl")
document = Document(path)

packageSymbol = PackageSymbol("pack_1")
packageSymbol = PackageSymbol(SimpleName("pack_1"))
packageBody = PackageBody(packageSymbol)
document._AddPackageBody(packageBody)

Expand Down Expand Up @@ -327,17 +324,17 @@ def test_DesignUnits(self):
path = Path("tests.vhdl")
document = Document(path, documentation="Testing 'Document' class.")

entity = Entity("entity_1")
document._AddDesignUnit(entity)
entitySymbol = Entity("entity_1")
document._AddDesignUnit(entitySymbol)

entity = EntitySymbol("entity_1")
architecture = Architecture("arch_1", entity)
entitySymbol = EntitySymbol(SimpleName("entity_1"))
architecture = Architecture("arch_1", entitySymbol)
document._AddDesignUnit(architecture)

package = Package("pack_1")
document._AddDesignUnit(package)

packageSymbol = PackageSymbol("pack_1")
packageSymbol = PackageSymbol(SimpleName("pack_1"))
packageBody = PackageBody(packageSymbol)
document._AddDesignUnit(packageBody)

Expand Down Expand Up @@ -389,9 +386,9 @@ def test_AddDocument(self):
document = Document(path, documentation="Testing 'Library' class.")

document._AddDesignUnit(Entity("entity_1"))
document._AddDesignUnit(Architecture("arch_1", EntitySymbol("entity_1")))
document._AddDesignUnit(Architecture("arch_1", EntitySymbol(SimpleName("entity_1"))))
document._AddDesignUnit(Package("pack_1"))
document._AddDesignUnit(PackageBody(PackageSymbol("pack_1")))
document._AddDesignUnit(PackageBody(PackageSymbol(SimpleName("pack_1"))))
document._AddDesignUnit(Context("ctx_1"))
document._AddDesignUnit(Configuration("cfg_1"))

Expand Down

0 comments on commit d122187

Please sign in to comment.