[PortList] add_port_pair requires unique port names

This commit is contained in:
Jan Petykiewicz 2026-03-30 23:33:33 -07:00
commit d3be6aeba3
2 changed files with 44 additions and 0 deletions

View file

@ -368,6 +368,8 @@ class PortList(metaclass=ABCMeta):
Returns:
self
"""
if names[0] == names[1]:
raise PortError(f'Port names must be distinct: {names[0]!r}')
new_ports = {
names[0]: Port(offset, rotation=rotation, ptype=ptype),
names[1]: Port(offset, rotation=rotation + pi, ptype=ptype),

View file

@ -89,6 +89,25 @@ def test_port_list_rename_missing_port_raises() -> None:
assert set(pl.ports) == {"A"}
def test_port_list_add_port_pair_requires_distinct_names() -> None:
class MyPorts(PortList):
def __init__(self) -> None:
self._ports: dict[str, Port] = {}
@property
def ports(self) -> dict[str, Port]:
return self._ports
@ports.setter
def ports(self, val: dict[str, Port]) -> None:
self._ports = val
pl = MyPorts()
with pytest.raises(PortError, match="Port names must be distinct"):
pl.add_port_pair(names=("A", "A"))
assert not pl.ports
def test_port_list_plugged() -> None:
class MyPorts(PortList):
def __init__(self) -> None:
@ -126,6 +145,29 @@ def test_port_list_plugged_empty_raises() -> None:
assert set(pl.ports) == {"A", "B"}
def test_port_list_plugged_missing_port_raises() -> None:
class MyPorts(PortList):
def __init__(self) -> None:
self._ports = {"A": Port((10, 10), 0), "B": Port((10, 10), pi)}
@property
def ports(self) -> dict[str, Port]:
return self._ports
@ports.setter
def ports(self, val: dict[str, Port]) -> None:
self._ports = val
pl = MyPorts()
with pytest.raises(PortError, match="Connection source ports were not found"):
pl.plugged({"missing": "B"})
assert set(pl.ports) == {"A", "B"}
with pytest.raises(PortError, match="Connection destination ports were not found"):
pl.plugged({"A": "missing"})
assert set(pl.ports) == {"A", "B"}
def test_port_list_plugged_mismatch() -> None:
class MyPorts(PortList):
def __init__(self) -> None: