forked from matplotlib/napari-matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.py
76 lines (59 loc) · 2.63 KB
/
test_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import importlib
import sys
import pytest
from qtpy.QtCore import QSize
from napari_matplotlib.util import Interval, from_napari_css_get_size_of
def test_version_fallback(mocker):
"""Test the versioning fallback (in case setuptools_scm didn't work)"""
import napari_matplotlib # fmt: skip
assert napari_matplotlib.__version__ != "unknown" # type: ignore[attr-defined]
mocker.patch.dict(sys.modules, {"napari_matplotlib._version": None})
importlib.reload(napari_matplotlib)
assert napari_matplotlib.__version__ == "unknown" # type: ignore[attr-defined]
def test_interval():
interval = Interval(4, 9)
for i in range(4, 10):
assert i in interval
assert 3 not in interval
assert 10 not in interval
with pytest.raises(ValueError, match="must be an integer"):
"string" in interval # type: ignore
@pytest.mark.parametrize(
"lower, upper, text",
[
(None, None, None),
(1, None, "Select at least 1 layer to generate plot"),
(4, None, "Select at least 4 layers to generate plot"),
(None, 1, "Select at most 1 layer to generate plot"),
(None, 5939, "Select at most 5939 layers to generate plot"),
(1, 1, "Select 1 layer to generate plot"),
(2, 2, "Select 2 layers to generate plot"),
(1, 2, "Select between 1 and 2 layers to generate plot"),
],
)
def test_interval_helper_text(lower, upper, text):
assert Interval(lower, upper)._helper_text == text
def test_get_size_from_css(mocker):
"""Test getting the max-width and max-height from something in css"""
test_css = """
Flibble {
min-width : 0;
max-width : 123px;
min-height : 0px;
max-height : 456px;
padding: 0px;
}
"""
mocker.patch("napari.qt.get_current_stylesheet").return_value = test_css
assert from_napari_css_get_size_of("Flibble", (1, 2)) == QSize(123, 456)
def test_fallback_if_missing_dimensions(mocker):
"""Test fallback if given something that doesn't have dimensions"""
test_css = " Flobble { background-color: rgb(0, 97, 163); } "
mocker.patch("napari.qt.get_current_stylesheet").return_value = test_css
with pytest.warns(RuntimeWarning, match="Unable to find DimensionToken"):
assert from_napari_css_get_size_of("Flobble", (1, 2)) == QSize(1, 2)
def test_fallback_if_prelude_not_in_css():
"""Test fallback if given something not in the css"""
doesntexist = "AQButtonThatDoesntExist"
with pytest.warns(RuntimeWarning, match=f"Unable to find {doesntexist}"):
assert from_napari_css_get_size_of(doesntexist, (1, 2)) == QSize(1, 2)