Skip to content

Commit f565ee6

Browse files
committed
documentation and some code cleaning
1 parent 77ebfdf commit f565ee6

File tree

1 file changed

+72
-15
lines changed

1 file changed

+72
-15
lines changed

plotpy/tests/unit/test_manipulate_selection.py

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Callable, TypeVar
3+
from typing import TYPE_CHECKING, Callable, TypeVar
44

55
import numpy as np
66
import pytest
@@ -13,9 +13,6 @@
1313
IShapeItemType,
1414
)
1515
from plotpy.items.image.base import BaseImageItem
16-
from plotpy.items.image.transform import TrImageItem
17-
from plotpy.plot.base import BasePlot
18-
from plotpy.plot.plotwidget import PlotWindow
1916
from plotpy.tests.data import gen_image4
2017
from plotpy.tests.unit.utils import (
2118
create_window,
@@ -25,34 +22,59 @@
2522
)
2623
from plotpy.tools import RectangularSelectionTool, SelectTool
2724

28-
# guitest: show
25+
if TYPE_CHECKING:
26+
from plotpy.items.image.transform import TrImageItem
27+
from plotpy.plot.base import BasePlot
28+
from plotpy.plot.plotwidget import PlotWindow
2929

3030
BaseImageItemT = TypeVar("BaseImageItemT", bound=BaseImageItem)
3131

3232

3333
def _assert_images_angle(
3434
images: list[TrImageItem], ref_angle: float, target_angle: float | None = None
3535
) -> None:
36-
angle = images[0].param.pos_angle
36+
"""Used to assert the angle of a list of images (factorization).
37+
38+
Args:
39+
images: list of transformable image items
40+
ref_angle: reference angle
41+
target_angle: Expected angle. Defaults to None.
42+
"""
43+
angle = images[0].param.pos_angle # type: ignore
3744
assert angle > ref_angle
3845
if target_angle is not None:
3946
assert np.isclose(angle, target_angle, 0.5)
4047
for img in images:
41-
assert np.isclose(angle, img.param.pos_angle)
48+
assert np.isclose(angle, img.param.pos_angle) # type: ignore
4249

4350

4451
def _assert_images_pos(images: list[TrImageItem], x0: float, y0: float) -> None:
45-
x = images[0].param.pos_x0
46-
y = images[0].param.pos_y0
52+
"""Used to assert the position of a list of images (factorization).
53+
54+
Args:
55+
images: lst of transformable image items
56+
x0: reference x position
57+
y0: reference y position
58+
"""
59+
x = images[0].param.pos_x0 # type: ignore
60+
y = images[0].param.pos_y0 # type: ignore
4761
assert x > x0
4862
assert y > y0
4963
for img in images:
50-
assert img.param.pos_angle == 0
51-
assert np.isclose(x, img.param.pos_x0)
52-
assert np.isclose(y, img.param.pos_y0)
64+
assert img.param.pos_angle == 0 # type: ignore
65+
assert np.isclose(x, img.param.pos_x0) # type: ignore
66+
assert np.isclose(y, img.param.pos_y0) # type: ignore
5367

5468

5569
def _get_xy_coords(tr_img: BaseImageItem) -> tuple[float, float, float, float]:
70+
"""Asserts and returns the coordinates of a transformable image item.
71+
72+
Args:
73+
tr_img: transformable image item
74+
75+
Returns:
76+
coordinates of the image item from its bounding rectangle.
77+
"""
5678
x1, y1, x2, y2 = tr_img.boundingRect().getCoords()
5779
assert isinstance(x1, float)
5880
assert isinstance(y1, float)
@@ -64,6 +86,12 @@ def _get_xy_coords(tr_img: BaseImageItem) -> tuple[float, float, float, float]:
6486
def _setup_plot(
6587
img_item: BaseImageItem | None = None,
6688
) -> tuple[PlotWindow, SelectTool, BasePlot, BaseImageItem]:
89+
"""Setup the plot for the tests with the given image item.
90+
91+
Args:
92+
img_item: image item to use. If None, will create a default image item.
93+
Defaults to None.
94+
"""
6795
if img_item is None:
6896
img_item = make.trimage(gen_image4(100, 100), x0=100, y0=100)
6997
win, tool = create_window(SelectTool, IImageItemType, None, [img_item])
@@ -89,11 +117,15 @@ def _setup_plot(
89117
),
90118
],
91119
)
92-
def test_move_with_mouse(img_item_builder: Callable[[], BaseImageItem] | None):
93-
"""Test the select tool."""
120+
def test_move_with_mouse(img_item_factory: Callable[[], BaseImageItem] | None):
121+
"""Test the select tool.
122+
123+
Arg:
124+
img_item_factory: image item factory function. Defaults to None.
125+
"""
94126

95127
with qt_app_context(exec_loop=False) as qapp:
96-
img_item = None if img_item_builder is None else img_item_builder()
128+
img_item = None if img_item_factory is None else img_item_factory()
97129
win, tool, plot, img_item = _setup_plot(img_item)
98130
x1, y1, *_ = _get_xy_coords(img_item)
99131

@@ -120,6 +152,11 @@ def test_move_with_mouse(img_item_builder: Callable[[], BaseImageItem] | None):
120152
def test_move_with_arrows(
121153
mod: QC.Qt.KeyboardModifier,
122154
):
155+
"""Test moving an image item with the arrow keys.
156+
157+
Args:
158+
mod: keyboard modifier to use.
159+
"""
123160
with qt_app_context(exec_loop=False) as qapp:
124161
win, tool, plot, tr_img = _setup_plot()
125162
x1, y1, *_ = _get_xy_coords(tr_img)
@@ -169,6 +206,11 @@ def test_move_with_arrows(
169206
def test_rotate_with_arrow(
170207
mod: QC.Qt.KeyboardModifier,
171208
):
209+
"""Test rotating an image item with the arrow keys.
210+
211+
Args:
212+
mod: keyboard modifier to use.
213+
"""
172214
with qt_app_context(exec_loop=False) as qapp:
173215
win, tool, plot, tr_img = _setup_plot()
174216
x1_a, y1_a, x1_b, y1_b = _get_xy_coords(tr_img)
@@ -202,6 +244,7 @@ def test_rotate_with_arrow(
202244

203245

204246
def test_select_all_items():
247+
"""Test selecting all items in the plot using the keyboard."""
205248
with qt_app_context() as qapp:
206249
n = 100
207250
x = np.arange(n)
@@ -228,6 +271,7 @@ def test_select_all_items():
228271

229272

230273
def test_rotate_with_mouse():
274+
"""Test rotating an image item with the mouse."""
231275
with qt_app_context(exec_loop=False) as qapp:
232276
win, tool, plot, tr_img = _setup_plot()
233277
init_angle = tr_img.param.pos_angle # type: ignore
@@ -244,6 +288,7 @@ def test_rotate_with_mouse():
244288

245289

246290
def test_rectangular_selection():
291+
"""Test selecting items with the rectangular selection tool."""
247292
with qt_app_context(exec_loop=False) as qapp:
248293
items = [
249294
make.image(gen_image4(100, 100)),
@@ -290,6 +335,12 @@ def test_rectangular_selection():
290335
def test_multi_rotate_move_with_mouse(
291336
mouse_path: tuple[np.ndarray, np.ndarray], rotation: bool
292337
):
338+
"""Test rotating and moving multiple images with the mouse.
339+
340+
Args:
341+
mouse_path: path of the mouse (drag).
342+
rotation: whether the image should rotate or not.
343+
"""
293344
n = 100
294345
x0 = n
295346
y0 = 0
@@ -333,6 +384,12 @@ def test_multi_rotate_move_with_mouse(
333384
def test_multi_rotate_move_with_keyboard(
334385
keymod: QC.Qt.KeyboardModifier, rotation: bool
335386
):
387+
"""Test rotating and moving multiple images with the keyboard.
388+
389+
Args:
390+
keymod: keyboard modifier to use.
391+
rotation: whether the image should rotate or not.
392+
"""
336393
n = 100
337394
x0 = n
338395
y0 = 0

0 commit comments

Comments
 (0)