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