Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e838a25

Browse files
committed
MNT: put test helper functions into their proper place
1 parent 61eb622 commit e838a25

File tree

3 files changed

+70
-69
lines changed

3 files changed

+70
-69
lines changed

lib/matplotlib/cbook.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ def is_string_like(obj):
708708
return False
709709
return True
710710

711+
def is_list_like(obj):
712+
"""Returns whether the obj is iterable and not a string"""
713+
return not is_string_like(obj) and iterable(obj)
711714

712715
def is_sequence_of_strings(obj):
713716
"""

lib/matplotlib/testing/__init__.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,68 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
3+
4+
5+
from contextlib import contextmanager
6+
7+
8+
# stolen from pandas
9+
@contextmanager
10+
def assert_produces_warning(expected_warning=Warning, filter_level="always",
11+
clear=None):
12+
"""
13+
Context manager for running code that expects to raise (or not raise)
14+
warnings. Checks that code raises the expected warning and only the
15+
expected warning. Pass ``False`` or ``None`` to check that it does *not*
16+
raise a warning. Defaults to ``exception.Warning``, baseclass of all
17+
Warnings. (basically a wrapper around ``warnings.catch_warnings``).
18+
19+
>>> import warnings
20+
>>> with assert_produces_warning():
21+
... warnings.warn(UserWarning())
22+
...
23+
>>> with assert_produces_warning(False):
24+
... warnings.warn(RuntimeWarning())
25+
...
26+
Traceback (most recent call last):
27+
...
28+
AssertionError: Caused unexpected warning(s): ['RuntimeWarning'].
29+
>>> with assert_produces_warning(UserWarning):
30+
... warnings.warn(RuntimeWarning())
31+
Traceback (most recent call last):
32+
...
33+
AssertionError: Did not see expected warning of class 'UserWarning'.
34+
35+
..warn:: This is *not* thread-safe.
36+
"""
37+
import warnings
38+
from matplotlib.cbook import is_list_like
39+
40+
with warnings.catch_warnings(record=True) as w:
41+
42+
if clear is not None:
43+
# make sure that we are clearning these warnings
44+
# if they have happened before
45+
# to guarantee that we will catch them
46+
if not is_list_like(clear):
47+
clear = [clear]
48+
for m in clear:
49+
try:
50+
m.__warningregistry__.clear()
51+
except:
52+
pass
53+
54+
saw_warning = False
55+
warnings.simplefilter(filter_level)
56+
yield w
57+
extra_warnings = []
58+
for actual_warning in w:
59+
if (expected_warning and issubclass(actual_warning.category,
60+
expected_warning)):
61+
saw_warning = True
62+
else:
63+
extra_warnings.append(actual_warning.category.__name__)
64+
if expected_warning:
65+
assert saw_warning, ("Did not see expected warning of class %r."
66+
% expected_warning.__name__)
67+
assert not extra_warnings, ("Caused unexpected warning(s): %r."
68+
% extra_warnings)

lib/matplotlib/tests/test_labeled_data_unpacking.py

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -19,85 +19,17 @@ def noop(txt, regex):
1919
assert_regex = noop
2020
assert_not_regex = noop
2121

22-
23-
from matplotlib.cbook import is_string_like, iterable
24-
25-
from contextlib import contextmanager
22+
from ..testing import assert_produces_warning
2623

2724
from .. import unpack_labeled_data
2825

2926

30-
def is_list_like(obj):
31-
return not is_string_like(obj) and iterable(obj)
32-
3327
# Notes on testing the plotting functions itself
3428
# * the individual decorated plotting functions are tested in 'test_axes.py'
3529
# * that pyplot functions accept a data kwarg is only tested in
3630
# test_axes.test_pie_linewidth_0
3731

3832

39-
# stolen from pandas
40-
@contextmanager
41-
def assert_produces_warning(expected_warning=Warning, filter_level="always",
42-
clear=None):
43-
"""
44-
Context manager for running code that expects to raise (or not raise)
45-
warnings. Checks that code raises the expected warning and only the
46-
expected warning. Pass ``False`` or ``None`` to check that it does *not*
47-
raise a warning. Defaults to ``exception.Warning``, baseclass of all
48-
Warnings. (basically a wrapper around ``warnings.catch_warnings``).
49-
50-
>>> import warnings
51-
>>> with assert_produces_warning():
52-
... warnings.warn(UserWarning())
53-
...
54-
>>> with assert_produces_warning(False):
55-
... warnings.warn(RuntimeWarning())
56-
...
57-
Traceback (most recent call last):
58-
...
59-
AssertionError: Caused unexpected warning(s): ['RuntimeWarning'].
60-
>>> with assert_produces_warning(UserWarning):
61-
... warnings.warn(RuntimeWarning())
62-
Traceback (most recent call last):
63-
...
64-
AssertionError: Did not see expected warning of class 'UserWarning'.
65-
66-
..warn:: This is *not* thread-safe.
67-
"""
68-
import warnings
69-
70-
with warnings.catch_warnings(record=True) as w:
71-
72-
if clear is not None:
73-
# make sure that we are clearning these warnings
74-
# if they have happened before
75-
# to guarantee that we will catch them
76-
if not is_list_like(clear):
77-
clear = [clear]
78-
for m in clear:
79-
try:
80-
m.__warningregistry__.clear()
81-
except:
82-
pass
83-
84-
saw_warning = False
85-
warnings.simplefilter(filter_level)
86-
yield w
87-
extra_warnings = []
88-
for actual_warning in w:
89-
if (expected_warning and issubclass(actual_warning.category,
90-
expected_warning)):
91-
saw_warning = True
92-
else:
93-
extra_warnings.append(actual_warning.category.__name__)
94-
if expected_warning:
95-
assert saw_warning, ("Did not see expected warning of class %r."
96-
% expected_warning.__name__)
97-
assert not extra_warnings, ("Caused unexpected warning(s): %r."
98-
% extra_warnings)
99-
100-
10133
# these two get used in multiple tests, so define them here
10234
@unpack_labeled_data(replace_names=["x", "y"], label_namer="y")
10335
def plot_func(ax, x, y, ls="x", label=None, w="xyz"):

0 commit comments

Comments
 (0)