89 lines
2.3 KiB
Python
89 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from bedit_core.bondgraph import flatten_bondgraph
|
|
from bedit_core.models import (
|
|
BondConnection,
|
|
BondPort,
|
|
Component,
|
|
ComponentID,
|
|
ConnectionID,
|
|
EquationImplementation,
|
|
Graph,
|
|
GraphImplementation,
|
|
Interface,
|
|
PortID,
|
|
SignalDirection,
|
|
)
|
|
|
|
|
|
def _leaf(name: str, port_id: PortID) -> Component:
|
|
return Component(
|
|
name=name,
|
|
interface=Interface(
|
|
{port_id: BondPort(name="p", direction=SignalDirection.INPUT)}
|
|
),
|
|
parameters={},
|
|
implementation=EquationImplementation(),
|
|
)
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_flattens_bonds_across_a_component_boundary() -> None:
|
|
boundary_id = PortID("boundary")
|
|
inner_id = PortID("inner")
|
|
outer_id = PortID("outer")
|
|
inner = _leaf("Inner", inner_id)
|
|
subsystem = Component(
|
|
name="Subsystem",
|
|
interface=Interface(
|
|
{
|
|
boundary_id: BondPort(
|
|
name="boundary",
|
|
direction=SignalDirection.INPUT,
|
|
)
|
|
}
|
|
),
|
|
parameters={},
|
|
implementation=GraphImplementation(
|
|
Graph(
|
|
components={ComponentID("inner"): inner},
|
|
connections={
|
|
ConnectionID("inside"): BondConnection(
|
|
boundary_id,
|
|
inner_id,
|
|
)
|
|
},
|
|
)
|
|
),
|
|
)
|
|
outer = _leaf("Outer", outer_id)
|
|
root = Component(
|
|
name="Root",
|
|
interface=Interface(),
|
|
parameters={},
|
|
implementation=GraphImplementation(
|
|
Graph(
|
|
components={
|
|
ComponentID("subsystem"): subsystem,
|
|
ComponentID("outer"): outer,
|
|
},
|
|
connections={
|
|
ConnectionID("outside"): BondConnection(
|
|
outer_id,
|
|
boundary_id,
|
|
)
|
|
},
|
|
)
|
|
),
|
|
)
|
|
|
|
network = flatten_bondgraph(root)
|
|
boundary = next(port for port in network.ports if port.port_id == boundary_id)
|
|
|
|
assert len(network.components) == 4
|
|
assert len(network.ports) == 3
|
|
assert len(network.bonds) == 2
|
|
assert len(network.bonds_for(boundary)) == 2
|