Skip to content

Commit ea18ff3

Browse files
authored
Handle missing figure params and add tests (#186)
* Stub stk and restore parseGCode test * Update figure_editor_test.py * Update gcode_test.py
1 parent ddde18d commit ea18ff3

File tree

3 files changed

+357
-54
lines changed

3 files changed

+357
-54
lines changed

src/figure_editor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def __init__(
5858

5959
self.params_widgets = []
6060
# TODO add implementation of True/False parameters
61-
self.params_dict: Dict[str, float] = dict(
62-
(el, initial_params[el] if initial_params and initial_params[el] else 0)
61+
self.params_dict: Dict[str, float] = {
62+
el: initial_params.get(el, 0) if initial_params else 0
6363
for el in params + self._checkboxes
64-
)
64+
}
6565

6666
for param_idx, param in enumerate(params):
6767
# add label for parameter name

test/figure_editor_test.py

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
import sys
2+
import types
3+
import unittest
4+
5+
# --- Begin stubs for PyQt5 and related modules ---
6+
7+
8+
class Signal:
9+
def __init__(self):
10+
self._callbacks = []
11+
12+
def connect(self, func):
13+
self._callbacks.append(func)
14+
15+
16+
class QStyle:
17+
SP_DialogApplyButton = 0
18+
19+
def standardIcon(self, *args, **kwargs):
20+
return None
21+
22+
23+
class QWidget:
24+
def __init__(self, *args, **kwargs):
25+
pass
26+
27+
def setLayout(self, *args, **kwargs):
28+
pass
29+
30+
def style(self):
31+
return QStyle()
32+
33+
def setWindowTitle(self, *args, **kwargs):
34+
pass
35+
36+
37+
class QGridLayout:
38+
def __init__(self, *args, **kwargs):
39+
pass
40+
41+
def setSpacing(self, *args, **kwargs):
42+
pass
43+
44+
def addWidget(self, *args, **kwargs):
45+
pass
46+
47+
48+
class QHBoxLayout:
49+
def __init__(self, *args, **kwargs):
50+
pass
51+
52+
def addWidget(self, *args, **kwargs):
53+
pass
54+
55+
def addLayout(self, *args, **kwargs):
56+
pass
57+
58+
59+
class QVBoxLayout:
60+
def __init__(self, *args, **kwargs):
61+
pass
62+
63+
def addWidget(self, *args, **kwargs):
64+
pass
65+
66+
def addLayout(self, *args, **kwargs):
67+
pass
68+
69+
70+
class QLabel(QWidget):
71+
def __init__(self, *args, **kwargs):
72+
pass
73+
74+
75+
class QLineEdit(QWidget):
76+
def __init__(self, text=""):
77+
self._text = text
78+
self.textChanged = Signal()
79+
80+
def setSizePolicy(self, *args, **kwargs):
81+
pass
82+
83+
def setMinimumWidth(self, *args, **kwargs):
84+
pass
85+
86+
def setReadOnly(self, *args, **kwargs):
87+
pass
88+
89+
def setText(self, text):
90+
self._text = text
91+
92+
93+
class QSlider(QWidget):
94+
def __init__(self, *args, **kwargs):
95+
self.valueChanged = Signal()
96+
97+
def setOrientation(self, *args, **kwargs):
98+
pass
99+
100+
def setMinimum(self, *args, **kwargs):
101+
pass
102+
103+
def setMaximum(self, *args, **kwargs):
104+
pass
105+
106+
def setValue(self, *args, **kwargs):
107+
pass
108+
109+
def setSizePolicy(self, *args, **kwargs):
110+
pass
111+
112+
def setMinimumWidth(self, *args, **kwargs):
113+
pass
114+
115+
def setEnabled(self, *args, **kwargs):
116+
pass
117+
118+
119+
class QSizePolicy:
120+
Minimum = 0
121+
Fixed = 0
122+
Expanding = 0
123+
124+
125+
class QPushButton(QWidget):
126+
def __init__(self, *args, **kwargs):
127+
self.clicked = Signal()
128+
129+
def setIcon(self, *args, **kwargs):
130+
pass
131+
132+
133+
class QCheckBox(QWidget):
134+
def __init__(self, *args, **kwargs):
135+
self.stateChanged = Signal()
136+
137+
def setChecked(self, *args, **kwargs):
138+
pass
139+
140+
141+
class QScrollArea(QWidget):
142+
def setWidget(self, *args, **kwargs):
143+
pass
144+
145+
def setWidgetResizable(self, *args, **kwargs):
146+
pass
147+
148+
149+
class QComboBox(QWidget):
150+
def addItem(self, *args, **kwargs):
151+
pass
152+
153+
154+
class QApplication:
155+
def __init__(self, *args, **kwargs):
156+
pass
157+
158+
159+
qtwidgets = types.ModuleType("PyQt5.QtWidgets")
160+
qtwidgets.QSlider = QSlider
161+
qtwidgets.QLineEdit = QLineEdit
162+
qtwidgets.QApplication = QApplication
163+
qtwidgets.QGridLayout = QGridLayout
164+
qtwidgets.QWidget = QWidget
165+
qtwidgets.QLabel = QLabel
166+
qtwidgets.QSizePolicy = QSizePolicy
167+
qtwidgets.QPushButton = QPushButton
168+
qtwidgets.QHBoxLayout = QHBoxLayout
169+
qtwidgets.QCheckBox = QCheckBox
170+
qtwidgets.QScrollArea = QScrollArea
171+
qtwidgets.QVBoxLayout = QVBoxLayout
172+
qtwidgets.QComboBox = QComboBox
173+
qtwidgets.QStyle = QStyle
174+
sys.modules["PyQt5.QtWidgets"] = qtwidgets
175+
176+
qtcore = types.ModuleType("PyQt5.QtCore")
177+
qtcore.Qt = types.SimpleNamespace(Horizontal=1, Checked=True)
178+
sys.modules["PyQt5.QtCore"] = qtcore
179+
180+
sip = types.ModuleType("sip")
181+
sip.isdeleted = lambda obj: False
182+
pyqt5 = types.ModuleType("PyQt5")
183+
pyqt5.QtCore = qtcore
184+
pyqt5.sip = sip
185+
sys.modules["PyQt5"] = pyqt5
186+
sys.modules["sip"] = sip
187+
188+
# Stub out project-specific modules that depend on PyQt5
189+
settings_widget_module = types.ModuleType("src.settings_widget")
190+
191+
192+
class SettingsWidget:
193+
def __init__(self, *args, **kwargs):
194+
self.extra_sett_parameters = []
195+
self.translation = {}
196+
197+
def from_settings(self, *args, **kwargs):
198+
return self
199+
200+
def with_delete(self, *args, **kwargs):
201+
return self
202+
203+
def with_sett(self, *args, **kwargs):
204+
return self
205+
206+
207+
settings_widget_module.SettingsWidget = SettingsWidget
208+
sys.modules["src.settings_widget"] = settings_widget_module
209+
210+
settings_module = types.ModuleType("src.settings")
211+
212+
213+
def sett():
214+
return None
215+
216+
217+
settings_module.sett = sett
218+
sys.modules["src.settings"] = settings_module
219+
220+
locales_module = types.ModuleType("src.locales")
221+
locales_module.getLocale = lambda x: x
222+
sys.modules["src.locales"] = locales_module
223+
224+
# --- End stubs ---
225+
226+
from src.figure_editor import FigureEditor
227+
228+
229+
class DummyTab:
230+
def __init__(self):
231+
self._layout = None
232+
233+
def layout(self):
234+
return self._layout
235+
236+
def setLayout(self, layout):
237+
self._layout = layout
238+
239+
240+
class TabsStub:
241+
def __init__(self):
242+
self._tab = DummyTab()
243+
244+
def widget(self, index):
245+
return self._tab
246+
247+
248+
class FigureEditorTest(unittest.TestCase):
249+
def test_missing_initial_params_defaults_to_zero(self):
250+
tabs = TabsStub()
251+
params = ["length", "width"]
252+
constrains = [(0, 10), (0, 10)]
253+
initial_params = {"length": 5} # 'width' is missing
254+
editor = FigureEditor(
255+
tabs,
256+
params,
257+
constrains,
258+
initial_params=initial_params,
259+
settings_provider=lambda: {},
260+
)
261+
self.assertEqual(editor.params_dict["length"], 5)
262+
self.assertEqual(editor.params_dict["width"], 0)
263+
264+
265+
if __name__ == "__main__":
266+
unittest.main()

0 commit comments

Comments
 (0)