[ILibraryView] fix assignment during dfs()
This commit is contained in:
parent
fbe138d443
commit
09a95a6608
2 changed files with 49 additions and 0 deletions
|
|
@ -538,6 +538,7 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
|
||||||
raise LibraryError('visit_* functions returned a new `Pattern` object'
|
raise LibraryError('visit_* functions returned a new `Pattern` object'
|
||||||
' but no top-level name was provided in `hierarchy`')
|
' but no top-level name was provided in `hierarchy`')
|
||||||
|
|
||||||
|
del cast('ILibrary', self)[name]
|
||||||
cast('ILibrary', self)[name] = pattern
|
cast('ILibrary', self)[name] = pattern
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
|
||||||
|
|
@ -221,6 +221,54 @@ def test_library_rename() -> None:
|
||||||
assert "old" not in lib["parent"].refs
|
assert "old" not in lib["parent"].refs
|
||||||
|
|
||||||
|
|
||||||
|
def test_library_dfs_can_replace_existing_patterns() -> None:
|
||||||
|
lib = Library()
|
||||||
|
child = Pattern()
|
||||||
|
lib["child"] = child
|
||||||
|
top = Pattern()
|
||||||
|
top.ref("child")
|
||||||
|
lib["top"] = top
|
||||||
|
|
||||||
|
replacement_top = Pattern(ports={"T": Port((1, 2), 0)})
|
||||||
|
replacement_child = Pattern(ports={"C": Port((3, 4), 0)})
|
||||||
|
|
||||||
|
def visit_after(pattern: Pattern, hierarchy: tuple[str | None, ...], **kwargs) -> Pattern: # noqa: ARG001
|
||||||
|
if hierarchy[-1] == "child":
|
||||||
|
return replacement_child
|
||||||
|
if hierarchy[-1] == "top":
|
||||||
|
return replacement_top
|
||||||
|
return pattern
|
||||||
|
|
||||||
|
lib.dfs(lib["top"], visit_after=visit_after, hierarchy=("top",), transform=True)
|
||||||
|
|
||||||
|
assert lib["top"] is replacement_top
|
||||||
|
assert lib["child"] is replacement_child
|
||||||
|
|
||||||
|
|
||||||
|
def test_lazy_library_dfs_can_replace_existing_patterns() -> None:
|
||||||
|
lib = LazyLibrary()
|
||||||
|
lib["child"] = lambda: Pattern()
|
||||||
|
lib["top"] = lambda: Pattern(refs={"child": []})
|
||||||
|
|
||||||
|
top = lib["top"]
|
||||||
|
top.ref("child")
|
||||||
|
|
||||||
|
replacement_top = Pattern(ports={"T": Port((1, 2), 0)})
|
||||||
|
replacement_child = Pattern(ports={"C": Port((3, 4), 0)})
|
||||||
|
|
||||||
|
def visit_after(pattern: Pattern, hierarchy: tuple[str | None, ...], **kwargs) -> Pattern: # noqa: ARG001
|
||||||
|
if hierarchy[-1] == "child":
|
||||||
|
return replacement_child
|
||||||
|
if hierarchy[-1] == "top":
|
||||||
|
return replacement_top
|
||||||
|
return pattern
|
||||||
|
|
||||||
|
lib.dfs(top, visit_after=visit_after, hierarchy=("top",), transform=True)
|
||||||
|
|
||||||
|
assert lib["top"] is replacement_top
|
||||||
|
assert lib["child"] is replacement_child
|
||||||
|
|
||||||
|
|
||||||
def test_library_add_no_duplicates_respects_mutate_other_false() -> None:
|
def test_library_add_no_duplicates_respects_mutate_other_false() -> None:
|
||||||
src_pat = Pattern(ports={"A": Port((0, 0), 0)})
|
src_pat = Pattern(ports={"A": Port((0, 0), 0)})
|
||||||
lib = Library({"a": Pattern()})
|
lib = Library({"a": Pattern()})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue