@@ -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
2724from .. 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" )
10335def plot_func (ax , x , y , ls = "x" , label = None , w = "xyz" ):
0 commit comments