116 lines
3.2 KiB
Python
116 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
from copy import deepcopy
|
|
|
|
import pytest
|
|
from PySide6.QtWidgets import QApplication
|
|
|
|
from bedit_core import models
|
|
from bedit_core.models import (
|
|
BondPort,
|
|
Component,
|
|
Interface,
|
|
PortID,
|
|
SignalDirection,
|
|
SignalPort,
|
|
ValueType,
|
|
)
|
|
from bedit_gui.documents import Document
|
|
from bedit_gui.views.port_editor_widget import PortEditorWidget
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def qt_app() -> QApplication:
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_port_editor_uses_a_detached_draft(qt_app: QApplication) -> None:
|
|
port_id = PortID("input")
|
|
original = {
|
|
port_id: SignalPort(
|
|
name="Input",
|
|
direction=SignalDirection.INPUT,
|
|
)
|
|
}
|
|
editor = PortEditorWidget()
|
|
editor.set_ports(original)
|
|
|
|
editor.ui.nameEdit.setText("Changed")
|
|
editor.ui.nameEdit.textEdited.emit("Changed")
|
|
|
|
assert original[port_id].name == "Input"
|
|
assert editor.ports()[port_id].name == "Changed"
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_port_type_controls_option_visibility(qt_app: QApplication) -> None:
|
|
editor = PortEditorWidget()
|
|
editor.set_ports(
|
|
{
|
|
PortID("port"): SignalPort(
|
|
name="Port",
|
|
direction=SignalDirection.INPUT,
|
|
value_type=ValueType.REAL,
|
|
)
|
|
}
|
|
)
|
|
|
|
assert not editor.ui.signalTypeComboBox.isHidden()
|
|
assert editor.ui.domainComboBox.isHidden()
|
|
|
|
editor.ui.typeBond.setChecked(True)
|
|
|
|
assert editor.ui.signalTypeComboBox.isHidden()
|
|
assert not editor.ui.domainComboBox.isHidden()
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_bond_causality_is_loaded_and_changed(qt_app: QApplication) -> None:
|
|
port_id = PortID("bond")
|
|
editor = PortEditorWidget()
|
|
editor.set_ports({port_id: BondPort("Bond", SignalDirection.INPUT, causality_preference=models.PortCausality.PREFERRED_FLOW_OUT)})
|
|
|
|
assert editor.ui.causalityComboBox.currentData() is models.PortCausality.PREFERRED_FLOW_OUT
|
|
|
|
index = editor.ui.causalityComboBox.findData(models.PortCausality.FIXED_EFFORT_OUT)
|
|
editor.ui.causalityComboBox.setCurrentIndex(index)
|
|
|
|
assert editor.ports()[port_id].causality_preference is models.PortCausality.FIXED_EFFORT_OUT
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_port_changes_are_one_undoable_operation(
|
|
qt_app: QApplication,
|
|
minimal_document,
|
|
) -> None:
|
|
component: Component = next(iter(minimal_document.root.values()))
|
|
removed_id = PortID("removed")
|
|
changed_id = PortID("changed")
|
|
added_id = PortID("added")
|
|
component.interface = Interface(
|
|
{
|
|
removed_id: SignalPort("Remove", SignalDirection.INPUT),
|
|
changed_id: SignalPort("Before", SignalDirection.INPUT),
|
|
}
|
|
)
|
|
original = deepcopy(component.interface.ports)
|
|
updated = {
|
|
changed_id: BondPort(
|
|
"After",
|
|
SignalDirection.OUTPUT,
|
|
domain="electrical",
|
|
),
|
|
added_id: SignalPort("Added", SignalDirection.INPUT),
|
|
}
|
|
document = Document(qt_app)
|
|
|
|
document.update_component_ports(component, updated)
|
|
assert component.interface.ports == updated
|
|
|
|
document.undo_stack.undo()
|
|
assert component.interface.ports == original
|
|
|
|
document.undo_stack.redo()
|
|
assert component.interface.ports == updated
|