| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ | |||
| 14 | 14 | import nose | |
| 15 | 15 | import numpy as np | |
| 16 | 16 | ||
| 17 | + import matplotlib as mpl | ||
| 17 | 18 | import matplotlib.tests | |
| 18 | 19 | import matplotlib.units | |
| 19 | 20 | from matplotlib import cbook | |
@@ -329,3 +330,19 @@ def find_dotted_module(module_name, path=None): | |||
| 329 | 330 | cbook.mkdirs(result_dir) | |
| 330 | 331 | ||
| 331 | 332 | return baseline_dir, result_dir | |
| 333 | + | ||
| 334 | + | ||
| 335 | + def switch_backend(backend): | ||
| 336 | + def switch_backend_decorator(func): | ||
| 337 | + def backend_switcher(*args, **kwargs): | ||
| 338 | + try: | ||
| 339 | + prev_backend = mpl.get_backend() | ||
| 340 | + matplotlib.tests.setup() | ||
| 341 | + plt.switch_backend(backend) | ||
| 342 | + result = func(*args, **kwargs) | ||
| 343 | + finally: | ||
| 344 | + plt.switch_backend(prev_backend) | ||
| 345 | + return result | ||
| 346 | + | ||
| 347 | + return nose.tools.make_decorator(func)(backend_switcher) | ||
| 348 | + return switch_backend_decorator | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ | |||
| 15 | 15 | import matplotlib.pyplot as plt | |
| 16 | 16 | from matplotlib.compat import subprocess | |
| 17 | 17 | from matplotlib.testing.compare import compare_images, ImageComparisonFailure | |
| 18 | - from matplotlib.testing.decorators import _image_directories | ||
| 18 | + from matplotlib.testing.decorators import _image_directories, switch_backend | ||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | 21 | baseline_dir, result_dir = _image_directories(lambda: 'dummy func') | |
@@ -41,23 +41,6 @@ def check_for(texsystem): | |||
| 41 | 41 | return latex.returncode == 0 | |
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | - def switch_backend(backend): | ||
| 45 | - | ||
| 46 | - def switch_backend_decorator(func): | ||
| 47 | - def backend_switcher(*args, **kwargs): | ||
| 48 | - try: | ||
| 49 | - prev_backend = mpl.get_backend() | ||
| 50 | - mpl.rcdefaults() | ||
| 51 | - plt.switch_backend(backend) | ||
| 52 | - result = func(*args, **kwargs) | ||
| 53 | - finally: | ||
| 54 | - plt.switch_backend(prev_backend) | ||
| 55 | - return result | ||
| 56 | - | ||
| 57 | - return nose.tools.make_decorator(func)(backend_switcher) | ||
| 58 | - return switch_backend_decorator | ||
| 59 | - | ||
| 60 | - | ||
| 61 | 44 | def compare_figure(fname, savefig_kwargs={}): | |
| 62 | 45 | actual = os.path.join(result_dir, fname) | |
| 63 | 46 | plt.savefig(actual, **savefig_kwargs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | from matplotlib.externals import six | |
| 5 | 5 | from matplotlib.externals.six import unichr | |
| 6 | 6 | from matplotlib import pyplot as plt | |
| 7 | - from matplotlib.testing.decorators import cleanup | ||
| 7 | + from matplotlib.testing.decorators import cleanup, switch_backend | ||
| 8 | 8 | from matplotlib.testing.decorators import knownfailureif | |
| 9 | 9 | from matplotlib._pylab_helpers import Gcf | |
| 10 | 10 | import copy | |
@@ -31,10 +31,8 @@ | |||
| 31 | 31 | ||
| 32 | 32 | @cleanup | |
| 33 | 33 | @knownfailureif(not HAS_QT) | |
| 34 | + @switch_backend('Qt4Agg') | ||
| 34 | 35 | def test_fig_close(): | |
| 35 | - # force switch to the Qt4 backend | ||
| 36 | - plt.switch_backend('Qt4Agg') | ||
| 37 | - | ||
| 38 | 36 | #save the state of Gcf.figs | |
| 39 | 37 | init_figs = copy.copy(Gcf.figs) | |
| 40 | 38 | ||
@@ -50,14 +48,14 @@ def test_fig_close(): | |||
| 50 | 48 | assert(init_figs == Gcf.figs) | |
| 51 | 49 | ||
| 52 | 50 | ||
| 51 | + @switch_backend('Qt4Agg') | ||
| 53 | 52 | def assert_correct_key(qt_key, qt_mods, answer): | |
| 54 | 53 | """ | |
| 55 | 54 | Make a figure | |
| 56 | 55 | Send a key_press_event event (using non-public, qt4 backend specific api) | |
| 57 | 56 | Catch the event | |
| 58 | 57 | Assert sent and caught keys are the same | |
| 59 | 58 | """ | |
| 60 | - plt.switch_backend('Qt4Agg') | ||
| 61 | 59 | qt_canvas = plt.figure().canvas | |
| 62 | 60 | ||
| 63 | 61 | event = mock.Mock() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments