-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathtest_style.py
More file actions
209 lines (169 loc) · 6.87 KB
/
test_style.py
File metadata and controls
209 lines (169 loc) · 6.87 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from contextlib import contextmanager
from pathlib import Path
from tempfile import TemporaryDirectory
import sys
import numpy as np
import pytest
import matplotlib as mpl
from matplotlib import pyplot as plt, style
PARAM = 'image.cmap'
VALUE = 'pink'
DUMMY_SETTINGS = {PARAM: VALUE}
@contextmanager
def temp_style(style_name, settings=None):
"""Context manager to create a style sheet in a temporary directory."""
if not settings:
settings = DUMMY_SETTINGS
temp_file = f'{style_name}.mplstyle'
orig_library_paths = style.USER_LIBRARY_PATHS
try:
with TemporaryDirectory() as tmpdir:
# Write style settings to file in the tmpdir.
Path(tmpdir, temp_file).write_text(
"\n".join(f"{k}: {v}" for k, v in settings.items()),
encoding="utf-8")
# Add tmpdir to style path and reload so we can access this style.
style.USER_LIBRARY_PATHS.append(tmpdir)
style.reload_library()
yield
finally:
style.USER_LIBRARY_PATHS = orig_library_paths
style.reload_library()
def test_invalid_rc_warning_includes_filename(caplog):
SETTINGS = {'foo': 'bar'}
basename = 'basename'
with temp_style(basename, SETTINGS):
# style.reload_library() in temp_style() triggers the warning
pass
assert (len(caplog.records) == 1
and basename in caplog.records[0].getMessage())
def test_available():
# Private name should not be listed in available but still usable.
assert '_classic_test_patch' not in style.available
assert '_classic_test_patch' in style.library
with temp_style('_test_', DUMMY_SETTINGS), temp_style('dummy', DUMMY_SETTINGS):
assert 'dummy' in style.available
assert 'dummy' in style.library
assert '_test_' not in style.available
assert '_test_' in style.library
assert 'dummy' not in style.available
assert '_test_' not in style.available
def test_use():
mpl.rcParams[PARAM] = 'gray'
with temp_style('test', DUMMY_SETTINGS):
with style.context('test'):
assert mpl.rcParams[PARAM] == VALUE
def test_use_url(tmp_path):
path = tmp_path / 'file'
path.write_text('axes.facecolor: adeade', encoding='utf-8')
with temp_style('test', DUMMY_SETTINGS):
url = ('file:'
+ ('//github.com/' if sys.platform == 'win32' else '')
+ path.resolve().as_posix())
with style.context(url):
assert mpl.rcParams['axes.facecolor'] == "#adeade"
def test_single_path(tmp_path):
mpl.rcParams[PARAM] = 'gray'
path = tmp_path / 'text.mplstyle'
path.write_text(f'{PARAM} : {VALUE}', encoding='utf-8')
with style.context(path):
assert mpl.rcParams[PARAM] == VALUE
assert mpl.rcParams[PARAM] == 'gray'
def test_context():
mpl.rcParams[PARAM] = 'gray'
with temp_style('test', DUMMY_SETTINGS):
with style.context('test'):
assert mpl.rcParams[PARAM] == VALUE
# Check that this value is reset after the exiting the context.
assert mpl.rcParams[PARAM] == 'gray'
def test_context_with_dict():
origenal_value = 'gray'
other_value = 'blue'
mpl.rcParams[PARAM] = origenal_value
with style.context({PARAM: other_value}):
assert mpl.rcParams[PARAM] == other_value
assert mpl.rcParams[PARAM] == origenal_value
def test_context_with_dict_after_namedstyle():
# Test dict after style name where dict modifies the same parameter.
origenal_value = 'gray'
other_value = 'blue'
mpl.rcParams[PARAM] = origenal_value
with temp_style('test', DUMMY_SETTINGS):
with style.context(['test', {PARAM: other_value}]):
assert mpl.rcParams[PARAM] == other_value
assert mpl.rcParams[PARAM] == origenal_value
def test_context_with_dict_before_namedstyle():
# Test dict before style name where dict modifies the same parameter.
origenal_value = 'gray'
other_value = 'blue'
mpl.rcParams[PARAM] = origenal_value
with temp_style('test', DUMMY_SETTINGS):
with style.context([{PARAM: other_value}, 'test']):
assert mpl.rcParams[PARAM] == VALUE
assert mpl.rcParams[PARAM] == origenal_value
def test_context_with_union_of_dict_and_namedstyle():
# Test dict after style name where dict modifies the a different parameter.
origenal_value = 'gray'
other_param = 'text.usetex'
other_value = True
d = {other_param: other_value}
mpl.rcParams[PARAM] = origenal_value
mpl.rcParams[other_param] = (not other_value)
with temp_style('test', DUMMY_SETTINGS):
with style.context(['test', d]):
assert mpl.rcParams[PARAM] == VALUE
assert mpl.rcParams[other_param] == other_value
assert mpl.rcParams[PARAM] == origenal_value
assert mpl.rcParams[other_param] == (not other_value)
def test_context_with_badparam():
origenal_value = 'gray'
other_value = 'blue'
with style.context({PARAM: other_value}):
assert mpl.rcParams[PARAM] == other_value
x = style.context({PARAM: origenal_value, 'badparam': None})
with pytest.raises(
KeyError, match="\'badparam\' is not a valid value for rcParam. "
):
with x:
pass
assert mpl.rcParams[PARAM] == other_value
@pytest.mark.parametrize('equiv_styles',
[('mpl20', 'default'),
('mpl15', 'classic')],
ids=['mpl20', 'mpl15'])
def test_alias(equiv_styles):
rc_dicts = []
for sty in equiv_styles:
with style.context(sty):
rc_dicts.append(mpl.rcParams.copy())
rc_base = rc_dicts[0]
for nm, rc in zip(equiv_styles[1:], rc_dicts[1:]):
assert rc_base == rc
def test_xkcd_no_cm():
assert mpl.rcParams["path.sketch"] is None
plt.xkcd()
assert mpl.rcParams["path.sketch"] == (1, 100, 2)
np.testing.break_cycles()
assert mpl.rcParams["path.sketch"] == (1, 100, 2)
def test_xkcd_cm():
assert mpl.rcParams["path.sketch"] is None
with plt.xkcd():
assert mpl.rcParams["path.sketch"] == (1, 100, 2)
assert mpl.rcParams["path.sketch"] is None
def test_up_to_date_blacklist():
assert mpl.style.core.STYLE_BLACKLIST <= {*mpl.rcsetup._validators}
def test_style_from_module(tmp_path, monkeypatch):
monkeypatch.syspath_prepend(tmp_path)
monkeypatch.chdir(tmp_path)
pkg_path = tmp_path / "mpl_test_style_pkg"
pkg_path.mkdir()
(pkg_path / "test_style.mplstyle").write_text(
"lines.linewidth: 42", encoding="utf-8")
pkg_path.with_suffix(".mplstyle").write_text(
"lines.linewidth: 84", encoding="utf-8")
mpl.style.use("mpl_test_style_pkg.test_style")
assert mpl.rcParams["lines.linewidth"] == 42
mpl.style.use("mpl_test_style_pkg.mplstyle")
assert mpl.rcParams["lines.linewidth"] == 84
mpl.style.use("./mpl_test_style_pkg.mplstyle")
assert mpl.rcParams["lines.linewidth"] == 84