Skip to content

Add some tests for minspan{x,y} in RectangleSelector #22154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,47 @@ def test_rectangle_selector():
check_rectangle(props=dict(fill=True))


@pytest.mark.parametrize('spancoords', ['data', 'pixels'])
@pytest.mark.parametrize('minspanx, x1', [[0, 10], [1, 10.5], [1, 11]])
@pytest.mark.parametrize('minspany, y1', [[0, 10], [1, 10.5], [1, 11]])
def test_rectangle_minspan(spancoords, minspanx, x1, minspany, y1):
ax = get_ax()
# attribute to track number of onselect calls
ax._n_onselect = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICS this dynamically adds an attribute. If so, this needs explanation:

Suggested change
ax._n_onselect = 0
# add an attribute to track the number of onselect calls
ax._n_onselect = 0

OTOH you are only working with a single axis. Couldn't you just use a local variable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a local variable and it gives

    def onselect(epress, erelease):
>       n_onselect += 1
E       UnboundLocalError: local variable 'n_onselect' referenced before assignment

I've added a comment though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would probably need a nonlocal statement. But I can live with a comment as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a use case for Mock? Then assert_not_called, assert_called_once_with, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to look into mocking, but would advocate for this PR to go in first so #21945 can be tested with these tests, and because the current code is how other widget tests do similar checks.


def onselect(epress, erelease):
ax._n_onselect += 1
ax._epress = epress
ax._erelease = erelease

x0, y0 = (10, 10)
if spancoords == 'pixels':
minspanx, minspany = (ax.transData.transform((x1, y1)) -
ax.transData.transform((x0, y0)))

tool = widgets.RectangleSelector(ax, onselect, interactive=True,
spancoords=spancoords,
minspanx=minspanx, minspany=minspany)
# Too small to create a selector
click_and_drag(tool, start=(x0, x1), end=(y0, y1))
assert not tool._selection_completed
assert ax._n_onselect == 0

click_and_drag(tool, start=(20, 20), end=(30, 30))
assert tool._selection_completed
assert ax._n_onselect == 1

# Too small to create a selector. Should clear exising selector, and
# trigger onselect because there was a pre-exisiting selector
click_and_drag(tool, start=(x0, y0), end=(x1, y1))
assert not tool._selection_completed
assert ax._n_onselect == 2
assert ax._epress.xdata == x0
assert ax._epress.ydata == y0
assert ax._erelease.xdata == x1
assert ax._erelease.ydata == y1


@pytest.mark.parametrize('drag_from_anywhere, new_center',
[[True, (60, 75)],
[False, (30, 20)]])
Expand Down