Skip to content

Commit 2e21419

Browse files
author
MShekow
committed
Fixed various smaller issues:
* placed new parameters at the end * renamed "force_order" to "order" * renamed "callback" to "check_params_cb" and "callbacks" to "check_params_cbs" * improved various parts of the documentation * replaced assert with raising a ValueError
1 parent a8cee4c commit 2e21419

File tree

3 files changed

+67
-60
lines changed

3 files changed

+67
-60
lines changed

pytestqt/qtbot.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def stopForInteraction(self):
234234

235235
stop = stopForInteraction
236236

237-
def waitSignal(self, signal=None, timeout=1000, callback=None, raising=None):
237+
def waitSignal(self, signal=None, timeout=1000, raising=None, check_params_cb=None):
238238
"""
239239
.. versionadded:: 1.2
240240
@@ -265,13 +265,15 @@ def waitSignal(self, signal=None, timeout=1000, callback=None, raising=None):
265265
A signal to wait for. Set to ``None`` to just use timeout.
266266
:param int timeout:
267267
How many milliseconds to wait before resuming control flow.
268-
:param Callable callback:
269-
Optional callable(*args) that returns True if the parameters are as expected, or False otherwise.
270268
:param bool raising:
271269
If :class:`QtBot.SignalTimeoutError <pytestqt.plugin.SignalTimeoutError>`
272270
should be raised if a timeout occurred.
273271
This defaults to ``True`` unless ``qt_wait_signal_raising = false``
274272
is set in the config.
273+
:param Callable check_params_cb:
274+
Optional callable(*parameters) that compares the provided signal parameters to some expected parameters.
275+
It has to match the signature of ``signal`` (just like a slot function would) and return ``True`` if
276+
parameters match, ``False`` otherwise.
275277
:returns:
276278
``SignalBlocker`` object. Call ``SignalBlocker.wait()`` to wait.
277279
@@ -287,14 +289,14 @@ def waitSignal(self, signal=None, timeout=1000, callback=None, raising=None):
287289
raising = True
288290
else:
289291
raising = _parse_ini_boolean(raising_val)
290-
blocker = SignalBlocker(timeout=timeout, raising=raising, callback=callback)
292+
blocker = SignalBlocker(timeout=timeout, raising=raising, check_params_cb=check_params_cb)
291293
if signal is not None:
292294
blocker.connect(signal)
293295
return blocker
294296

295297
wait_signal = waitSignal # pep-8 alias
296298

297-
def waitSignals(self, signals=None, timeout=1000, force_order = "none", callbacks = None, raising=None):
299+
def waitSignals(self, signals=None, timeout=1000, raising=None, check_params_cbs=None, order="none"):
298300
"""
299301
.. versionadded:: 1.4
300302
@@ -317,24 +319,28 @@ def waitSignals(self, signals=None, timeout=1000, force_order = "none", callback
317319
blocker.wait()
318320
319321
:param list signals:
320-
A list of :class:`Signal`s objects to wait for. Set to ``None`` to just use
322+
A list of :class:`Signal` objects to wait for. Set to ``None`` to just use
321323
timeout.
322324
:param int timeout:
323325
How many milliseconds to wait before resuming control flow.
324-
:param str force_order:
325-
"none": no order is enforced
326-
"strict": the signals have to be emitted in the provided order (and no intermediate signals may be emitted)
327-
"simple": like "strict", but signals may be emitted inbetween the provided ones, e.g. suppose
328-
signals=[a, b, c] and the tested object emits the signals a, a, b, a, c, the test will pass with "simple"
329-
but not with "strict"
330-
:param list callbacks:
331-
optional list of Callable functions that evaluate the parameters of the signal. Each element of the list
332-
can be a callable or None. Make sure that the number of callbacks matches the number of signals.
333326
:param bool raising:
334327
If :class:`QtBot.SignalTimeoutError <pytestqt.plugin.SignalTimeoutError>`
335328
should be raised if a timeout occurred.
336329
This defaults to ``True`` unless ``qt_wait_signal_raising = false``
337330
is set in the config.
331+
:param list check_params_cbs:
332+
optional list of callables that compare the provided signal parameters to some expected parameters.
333+
Each callable has to match the signature of the corresponding signal in ``signals`` (just like a slot
334+
function would) and return ``True`` if parameters match, ``False`` otherwise.
335+
Instead of a specific callable, ``None`` can be provided, to disable parameter checking for the
336+
corresponding signal.
337+
If the number of callbacks doesn't match the number of signals ``ValueError`` will be raised.
338+
:param str order: determines the order in which to expect signals
339+
* ``"none"``: no order is enforced
340+
* ``"strict"``: signals have to be emitted strictly in the provided order
341+
(e.g. fails when expecting signals [a, b] and [a, a, b] is emitted)
342+
* ``"simple"``: like "strict", but signals may be emitted in-between the provided ones, e.g. expected
343+
``signals`` == [a, b, c] and actually emitted signals = [a, a, b, a, c] works (would fail with "strict")
338344
:returns:
339345
``MultiSignalBlocker`` object. Call ``MultiSignalBlocker.wait()``
340346
to wait.
@@ -345,16 +351,17 @@ def waitSignals(self, signals=None, timeout=1000, force_order = "none", callback
345351
346352
.. note:: This method is also available as ``wait_signals`` (pep-8 alias)
347353
"""
348-
assert force_order in ["none", "simple", "strict"], "force_order has to be set to 'none', 'simple' or 'strict'"
354+
if order not in ["none", "simple", "strict"]:
355+
raise ValueError("order has to be set to 'none', 'simple' or 'strict'")
349356

350357
if raising is None:
351358
raising = self._request.config.getini('qt_wait_signal_raising')
352359

353-
if callbacks:
354-
if len(callbacks) != len(signals):
360+
if check_params_cbs:
361+
if len(check_params_cbs) != len(signals):
355362
raise ValueError("Number of callbacks ({}) does not "
356-
"match number of signals ({})!".format(len(callbacks), len(signals)))
357-
blocker = MultiSignalBlocker(timeout=timeout, raising=raising, force_order=force_order, callbacks=callbacks)
363+
"match number of signals ({})!".format(len(check_params_cbs), len(signals)))
364+
blocker = MultiSignalBlocker(timeout=timeout, raising=raising, order=order, check_params_cbs=check_params_cbs)
358365
if signals is not None:
359366
blocker.add_signals(signals)
360367
return blocker

pytestqt/wait_signal.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ class SignalBlocker(_AbstractSignalBlocker):
9999
.. automethod:: connect
100100
"""
101101

102-
def __init__(self, timeout=1000, raising=True, callback=None):
102+
def __init__(self, timeout=1000, raising=True, check_params_cb=None):
103103
super(SignalBlocker, self).__init__(timeout, raising=raising)
104104
self._signals = []
105105
self.args = None
106-
self.callback = callback
106+
self.check_params_callback = check_params_cb
107107

108108
def connect(self, signal):
109109
"""
@@ -122,8 +122,8 @@ def _quit_loop_by_signal(self, *args):
122122
"""
123123
quits the event loop and marks that we finished because of a signal.
124124
"""
125-
if self.callback:
126-
if not self.callback(*args):
125+
if self.check_params_callback:
126+
if not self.check_params_callback(*args):
127127
return # parameter check did not pass
128128
try:
129129
self.signal_triggered = True
@@ -152,10 +152,10 @@ class MultiSignalBlocker(_AbstractSignalBlocker):
152152
.. automethod:: wait
153153
"""
154154

155-
def __init__(self, timeout=1000, force_order="none", callbacks=None, raising=True):
155+
def __init__(self, timeout=1000, raising=True, check_params_cbs=None, order="none"):
156156
super(MultiSignalBlocker, self).__init__(timeout, raising=raising)
157-
self.force_order = force_order
158-
self.callbacks = callbacks
157+
self.order = order
158+
self.check_params_callbacks = check_params_cbs
159159
self._signals_emitted = [] # list of booleans, indicates whether the signal was already emitted
160160
self._signals_map = {} # maps from a unique Signal to a list of indices where to expect signal instance emits
161161
self._signals = [] # list of all Signals (for compatibility with _AbstractSignalBlocker)
@@ -171,7 +171,7 @@ def add_signals(self, signals):
171171
:param list signals: list of QtCore.Signal`s
172172
"""
173173
# determine uniqueness of signals, creating a map that maps from a unique signal to a list of indices
174-
# (positions) where this signal is expected (in case force_order matters)
174+
# (positions) where this signal is expected (in case order matters)
175175
signals_as_str = [str(signal) for signal in signals]
176176
signal_str_to_signal = {} # maps from a signal-string to one of the signal instances (the first one found)
177177
for index, signal_str in enumerate(signals_as_str):
@@ -200,7 +200,7 @@ def _signal_emitted(self, signal, *args):
200200
If all expected signals have been emitted, quits the event loop and
201201
marks that we finished because signals.
202202
"""
203-
if self.force_order == "none":
203+
if self.order == "none":
204204
# perform the test for every matching index (stop after the first one that matches)
205205
successfully_emitted = False
206206
successful_index = -1
@@ -213,14 +213,14 @@ def _signal_emitted(self, signal, *args):
213213

214214
if successfully_emitted:
215215
self._signals_emitted[successful_index] = True
216-
elif self.force_order == "simple":
216+
elif self.order == "simple":
217217
potential_indices = self._get_unemitted_signal_indices(signal)
218218
if potential_indices:
219219
if self._signal_expected_index == potential_indices[0]:
220220
if self._check_callback(self._signal_expected_index, *args):
221221
self._signals_emitted[self._signal_expected_index] = True
222222
self._signal_expected_index += 1
223-
else: # self.force_order == "strict"
223+
else: # self.order == "strict"
224224
if not self._strict_order_violated:
225225
# only do the check if the strict order has not been violated yet
226226
self._strict_order_violated = True # assume the order has been violated this time
@@ -244,8 +244,8 @@ def _check_callback(self, index, *args):
244244
Checks if there's a callback that evaluates the validity of the parameters. Returns False if there is one
245245
and its evaluation revealed that the parameters were invalid. Returns True otherwise.
246246
"""
247-
if self.callbacks:
248-
callback_func = self.callbacks[index]
247+
if self.check_params_callbacks:
248+
callback_func = self.check_params_callbacks[index]
249249
if callback_func:
250250
if not callback_func(*args):
251251
return False

tests/test_wait_signal.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -396,29 +396,29 @@ def test_signal_identity(signaller):
396396
assert str(x) != str(z)
397397

398398

399-
def get_waitsignals_cases_all(force_order):
399+
def get_waitsignals_cases_all(order):
400400
"""
401401
Returns the list of tuples (emitted-signal-list, expected-signal-list, expect_signal_triggered) for the
402-
given force_order parameter of waitSignals().
402+
given order parameter of waitSignals().
403403
"""
404-
cases = get_waitsignals_cases(force_order, working=True)
405-
cases.extend(get_waitsignals_cases(force_order, working=False))
404+
cases = get_waitsignals_cases(order, working=True)
405+
cases.extend(get_waitsignals_cases(order, working=False))
406406
return cases
407407

408408

409-
def get_waitsignals_cases(force_order, working):
409+
def get_waitsignals_cases(order, working):
410410
"""
411411
Builds combinations for signals to be emitted and expected for working cases (i.e. blocker.signal_triggered == True)
412-
and non-working cases, depending on the force_order.
412+
and non-working cases, depending on the order.
413413
414414
Note:
415-
The force_order ("none", "simple", "strict") becomes stricter from left to right.
415+
The order ("none", "simple", "strict") becomes stricter from left to right.
416416
Working cases of stricter cases also work in less stricter cases.
417417
Non-working cases in less stricter cases also are non-working in stricter cases.
418418
"""
419-
if force_order == "none":
419+
if order == "none":
420420
if working:
421-
cases = get_waitsignals_cases(force_order="simple", working=True)
421+
cases = get_waitsignals_cases(order="simple", working=True)
422422
cases.extend([
423423
# allow even out-of-order signals
424424
(('A1', 'A2'), ('A2', 'A1'), True),
@@ -440,9 +440,9 @@ def get_waitsignals_cases(force_order, working):
440440
(('A1', 'B1'), ('B1', 'B1'), False),
441441
(('A1', 'B1', 'B1'), ('A1', 'A1', 'B1'), False),
442442
]
443-
elif force_order == "simple":
443+
elif order == "simple":
444444
if working:
445-
cases = get_waitsignals_cases(force_order="strict", working=True)
445+
cases = get_waitsignals_cases(order="strict", working=True)
446446
cases.extend([
447447
# allow signals that occur in-between, before or after the expected signals
448448
(('B1', 'A1', 'A1', 'B1', 'A1'), ('A1', 'B1'), True),
@@ -452,7 +452,7 @@ def get_waitsignals_cases(force_order, working):
452452
])
453453
return cases
454454
else:
455-
cases = get_waitsignals_cases(force_order="none", working=False)
455+
cases = get_waitsignals_cases(order="none", working=False)
456456
cases.extend([
457457
# don't allow out-of-order signals
458458
(('A1', 'B1'), ('B1', 'A1'), False),
@@ -461,7 +461,7 @@ def get_waitsignals_cases(force_order, working):
461461
(('A1', 'B1', 'B1'), ('B1', 'B1', 'A1'), False),
462462
])
463463
return cases
464-
elif force_order == "strict":
464+
elif order == "strict":
465465
if working:
466466
return [
467467
# only allow exactly the same signals to be emitted that were also expected
@@ -479,7 +479,7 @@ def get_waitsignals_cases(force_order, working):
479479
(('A1', 'B1', 'A1'), ('Ax', 'A1'), True),
480480
]
481481
else:
482-
cases = get_waitsignals_cases(force_order="simple", working=False)
482+
cases = get_waitsignals_cases(order="simple", working=False)
483483
cases.extend([
484484
# don't allow in-between signals
485485
(('A1', 'A1', 'A2', 'B1'), ('A1', 'A2', 'B1'), False),
@@ -573,47 +573,47 @@ def test_wait_signal(self, qtbot, signaller, emitted_signal_codes, expected_sign
573573
expected_signal_triggered):
574574
"""Tests that waitSignal() correctly checks the signal parameters using the provided callback"""
575575
signals_to_expect, callbacks = TestCallback.get_signals_and_callbacks(signaller, expected_signal_codes)
576-
with qtbot.waitSignal(signal=signals_to_expect[0], callback=callbacks[0], timeout=200,
576+
with qtbot.waitSignal(signal=signals_to_expect[0], check_params_cb=callbacks[0], timeout=200,
577577
raising=False) as blocker:
578578
TestCallback.emit_parametrized_signals(signaller, emitted_signal_codes)
579579

580580
assert blocker.signal_triggered == expected_signal_triggered
581581

582582
@pytest.mark.parametrize(
583583
('emitted_signal_codes', 'expected_signal_codes', 'expected_signal_triggered'),
584-
get_waitsignals_cases_all(force_order="none")
584+
get_waitsignals_cases_all(order="none")
585585
)
586586
def test_wait_signals_none_order(self, qtbot, signaller, emitted_signal_codes, expected_signal_codes,
587587
expected_signal_triggered):
588-
"""Tests waitSignals() with force_order="none"."""
588+
"""Tests waitSignals() with order="none"."""
589589
self._test_wait_signals(qtbot, signaller, emitted_signal_codes, expected_signal_codes,
590-
expected_signal_triggered, force_order="none")
590+
expected_signal_triggered, order="none")
591591

592592
@pytest.mark.parametrize(
593593
('emitted_signal_codes', 'expected_signal_codes', 'expected_signal_triggered'),
594-
get_waitsignals_cases_all(force_order="simple")
594+
get_waitsignals_cases_all(order="simple")
595595
)
596596
def test_wait_signals_simple_order(self, qtbot, signaller, emitted_signal_codes, expected_signal_codes,
597597
expected_signal_triggered):
598-
"""Tests waitSignals() with force_order="simple"."""
598+
"""Tests waitSignals() with order="simple"."""
599599
self._test_wait_signals(qtbot, signaller, emitted_signal_codes, expected_signal_codes,
600-
expected_signal_triggered, force_order="simple")
600+
expected_signal_triggered, order="simple")
601601

602602
@pytest.mark.parametrize(
603603
('emitted_signal_codes', 'expected_signal_codes', 'expected_signal_triggered'),
604-
get_waitsignals_cases_all(force_order="strict")
604+
get_waitsignals_cases_all(order="strict")
605605
)
606606
def test_wait_signals_strict_order(self, qtbot, signaller, emitted_signal_codes, expected_signal_codes,
607607
expected_signal_triggered):
608-
"""Tests waitSignals() with force_order="strict"."""
608+
"""Tests waitSignals() with order="strict"."""
609609
self._test_wait_signals(qtbot, signaller, emitted_signal_codes, expected_signal_codes,
610-
expected_signal_triggered, force_order="strict")
610+
expected_signal_triggered, order="strict")
611611

612612
@staticmethod
613613
def _test_wait_signals(qtbot, signaller, emitted_signal_codes, expected_signal_codes,
614-
expected_signal_triggered, force_order):
614+
expected_signal_triggered, order):
615615
signals_to_expect, callbacks = TestCallback.get_signals_and_callbacks(signaller, expected_signal_codes)
616-
with qtbot.waitSignals(signals=signals_to_expect, force_order=force_order, callbacks=callbacks,
616+
with qtbot.waitSignals(signals=signals_to_expect, order=order, check_params_cbs=callbacks,
617617
timeout=200, raising=False) as blocker:
618618
TestCallback.emit_parametrized_signals(signaller, emitted_signal_codes)
619619

@@ -628,7 +628,7 @@ def test_signals_and_callbacks_length_mismatch(self, qtbot, signaller):
628628
signals_to_expect, callbacks = TestCallback.get_signals_and_callbacks(signaller, expected_signal_codes)
629629
callbacks.append(None)
630630
with pytest.raises(ValueError):
631-
with qtbot.waitSignals(signals=signals_to_expect, force_order="none", callbacks=callbacks,
631+
with qtbot.waitSignals(signals=signals_to_expect, order="none", check_params_cbs=callbacks,
632632
raising=False):
633633
pass
634634

0 commit comments

Comments
 (0)