##// END OF EJS Templates
use testing decorators to enable tests on module availability
Daniel B. Vasquez -
Show More
@@ -1,154 +1,153 b''
1 1 """Tests for pylab tools module.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2011, the IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14 from __future__ import print_function
15 15
16 16 # Stdlib imports
17 17
18 18 # Third-party imports
19 19 import matplotlib; matplotlib.use('Agg')
20 20 import nose.tools as nt
21 21
22 22 from matplotlib import pyplot as plt
23 23 import numpy as np
24 24
25 25 # Our own imports
26 26 from IPython.core.interactiveshell import InteractiveShell
27 27 from .. import pylabtools as pt
28 28
29 from IPython.testing import decorators as dec
30
29 31 #-----------------------------------------------------------------------------
30 32 # Globals and constants
31 33 #-----------------------------------------------------------------------------
32 34
33 35 #-----------------------------------------------------------------------------
34 36 # Local utilities
35 37 #-----------------------------------------------------------------------------
36 38
37 39 #-----------------------------------------------------------------------------
38 40 # Classes and functions
39 41 #-----------------------------------------------------------------------------
40 42
41 43 def test_figure_to_svg():
42 44 # simple empty-figure test
43 45 fig = plt.figure()
44 46 nt.assert_equal(pt.print_figure(fig, 'svg'), None)
45 47
46 48 plt.close('all')
47 49
48 50 # simple check for at least svg-looking output
49 51 fig = plt.figure()
50 52 ax = fig.add_subplot(1,1,1)
51 53 ax.plot([1,2,3])
52 54 plt.draw()
53 55 svg = pt.print_figure(fig, 'svg')[:100].lower()
54 56 nt.assert_in(b'doctype svg', svg)
55 57
56 try:
57 from PIL import Image
58 def test_figure_to_jpg():
59 # simple check for at least jpg-looking output
60 fig = plt.figure()
61 ax = fig.add_subplot(1,1,1)
62 ax.plot([1,2,3])
63 plt.draw()
64 jpg = pt.print_figure(fig, 'jpg')[:100].lower()
65 assert jpg.startswith(b'\xff\xd8')
66 except ImportError:
67 pass
58 @dec.skip_without("PIL.Image")
59 def test_figure_to_jpg():
60 # simple check for at least jpg-looking output
61 fig = plt.figure()
62 ax = fig.add_subplot(1,1,1)
63 ax.plot([1,2,3])
64 plt.draw()
65 jpg = pt.print_figure(fig, 'jpg')[:100].lower()
66 assert jpg.startswith(b'\xff\xd8')
68 67
69 68
70 69 def test_import_pylab():
71 70 ip = get_ipython()
72 71 ns = {}
73 72 pt.import_pylab(ns, import_all=False)
74 73 nt.assert_true('plt' in ns)
75 74 nt.assert_equal(ns['np'], np)
76 75
77 76 class TestPylabSwitch(object):
78 77 class Shell(InteractiveShell):
79 78 def enable_gui(self, gui):
80 79 pass
81 80
82 81 def setup(self):
83 82 import matplotlib
84 83 def act_mpl(backend):
85 84 matplotlib.rcParams['backend'] = backend
86 85
87 86 # Save rcParams since they get modified
88 87 self._saved_rcParams = matplotlib.rcParams
89 88 self._saved_rcParamsOrig = matplotlib.rcParamsOrig
90 89 matplotlib.rcParams = dict(backend='Qt4Agg')
91 90 matplotlib.rcParamsOrig = dict(backend='Qt4Agg')
92 91
93 92 # Mock out functions
94 93 self._save_am = pt.activate_matplotlib
95 94 pt.activate_matplotlib = act_mpl
96 95 self._save_ip = pt.import_pylab
97 96 pt.import_pylab = lambda *a,**kw:None
98 97 self._save_cis = pt.configure_inline_support
99 98 pt.configure_inline_support = lambda *a,**kw:None
100 99
101 100 def teardown(self):
102 101 pt.activate_matplotlib = self._save_am
103 102 pt.import_pylab = self._save_ip
104 103 pt.configure_inline_support = self._save_cis
105 104 import matplotlib
106 105 matplotlib.rcParams = self._saved_rcParams
107 106 matplotlib.rcParamsOrig = self._saved_rcParamsOrig
108 107
109 108 def test_qt(self):
110 109 s = self.Shell()
111 110 gui, backend = s.enable_matplotlib(None)
112 111 nt.assert_equal(gui, 'qt')
113 112 nt.assert_equal(s.pylab_gui_select, 'qt')
114 113
115 114 gui, backend = s.enable_matplotlib('inline')
116 115 nt.assert_equal(gui, 'inline')
117 116 nt.assert_equal(s.pylab_gui_select, 'qt')
118 117
119 118 gui, backend = s.enable_matplotlib('qt')
120 119 nt.assert_equal(gui, 'qt')
121 120 nt.assert_equal(s.pylab_gui_select, 'qt')
122 121
123 122 gui, backend = s.enable_matplotlib('inline')
124 123 nt.assert_equal(gui, 'inline')
125 124 nt.assert_equal(s.pylab_gui_select, 'qt')
126 125
127 126 gui, backend = s.enable_matplotlib()
128 127 nt.assert_equal(gui, 'qt')
129 128 nt.assert_equal(s.pylab_gui_select, 'qt')
130 129
131 130 def test_inline(self):
132 131 s = self.Shell()
133 132 gui, backend = s.enable_matplotlib('inline')
134 133 nt.assert_equal(gui, 'inline')
135 134 nt.assert_equal(s.pylab_gui_select, None)
136 135
137 136 gui, backend = s.enable_matplotlib('inline')
138 137 nt.assert_equal(gui, 'inline')
139 138 nt.assert_equal(s.pylab_gui_select, None)
140 139
141 140 gui, backend = s.enable_matplotlib('qt')
142 141 nt.assert_equal(gui, 'qt')
143 142 nt.assert_equal(s.pylab_gui_select, 'qt')
144 143
145 144 def test_qt_gtk(self):
146 145 s = self.Shell()
147 146 gui, backend = s.enable_matplotlib('qt')
148 147 nt.assert_equal(gui, 'qt')
149 148 nt.assert_equal(s.pylab_gui_select, 'qt')
150 149
151 150 gui, backend = s.enable_matplotlib('gtk')
152 151 nt.assert_equal(gui, 'qt')
153 152 nt.assert_equal(s.pylab_gui_select, 'qt')
154 153
General Comments 0
You need to be logged in to leave comments. Login now