##// END OF EJS Templates
Fix pylabtools test for Python 3
Thomas Kluyver -
Show More
@@ -1,138 +1,138
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 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Globals and constants
30 # Globals and constants
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 # Local utilities
34 # Local utilities
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36
36
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38 # Classes and functions
38 # Classes and functions
39 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
40
40
41 def test_figure_to_svg():
41 def test_figure_to_svg():
42 # simple empty-figure test
42 # simple empty-figure test
43 fig = plt.figure()
43 fig = plt.figure()
44 nt.assert_equal(pt.print_figure(fig, 'svg'), None)
44 nt.assert_equal(pt.print_figure(fig, 'svg'), None)
45
45
46 plt.close('all')
46 plt.close('all')
47
47
48 # simple check for at least svg-looking output
48 # simple check for at least svg-looking output
49 fig = plt.figure()
49 fig = plt.figure()
50 ax = fig.add_subplot(1,1,1)
50 ax = fig.add_subplot(1,1,1)
51 ax.plot([1,2,3])
51 ax.plot([1,2,3])
52 plt.draw()
52 plt.draw()
53 svg = pt.print_figure(fig, 'svg')[:100].lower()
53 svg = pt.print_figure(fig, 'svg')[:100].lower()
54 nt.assert_in('doctype svg', svg)
54 nt.assert_in(b'doctype svg', svg)
55
55
56
56
57 def test_import_pylab():
57 def test_import_pylab():
58 ip = get_ipython()
58 ip = get_ipython()
59 ns = {}
59 ns = {}
60 pt.import_pylab(ns, import_all=False)
60 pt.import_pylab(ns, import_all=False)
61 nt.assert_true('plt' in ns)
61 nt.assert_true('plt' in ns)
62 nt.assert_equal(ns['np'], np)
62 nt.assert_equal(ns['np'], np)
63
63
64 class TestPylabSwitch(object):
64 class TestPylabSwitch(object):
65 class Shell(InteractiveShell):
65 class Shell(InteractiveShell):
66 def enable_gui(self, gui):
66 def enable_gui(self, gui):
67 pass
67 pass
68
68
69 def setup(self):
69 def setup(self):
70 import matplotlib
70 import matplotlib
71 def act_mpl(backend):
71 def act_mpl(backend):
72 matplotlib.rcParams['backend'] = backend
72 matplotlib.rcParams['backend'] = backend
73
73
74 # Save rcParams since they get modified
74 # Save rcParams since they get modified
75 self._saved_rcParams = matplotlib.rcParams
75 self._saved_rcParams = matplotlib.rcParams
76 matplotlib.rcParams = dict(backend='Qt4Agg')
76 matplotlib.rcParams = dict(backend='Qt4Agg')
77
77
78 # Mock out functions
78 # Mock out functions
79 self._save_am = pt.activate_matplotlib
79 self._save_am = pt.activate_matplotlib
80 pt.activate_matplotlib = act_mpl
80 pt.activate_matplotlib = act_mpl
81 self._save_ip = pt.import_pylab
81 self._save_ip = pt.import_pylab
82 pt.import_pylab = lambda *a,**kw:None
82 pt.import_pylab = lambda *a,**kw:None
83 self._save_cis = pt.configure_inline_support
83 self._save_cis = pt.configure_inline_support
84 pt.configure_inline_support = lambda *a,**kw:None
84 pt.configure_inline_support = lambda *a,**kw:None
85
85
86 def teardown(self):
86 def teardown(self):
87 pt.activate_matplotlib = self._save_am
87 pt.activate_matplotlib = self._save_am
88 pt.import_pylab = self._save_ip
88 pt.import_pylab = self._save_ip
89 pt.configure_inline_support = self._save_cis
89 pt.configure_inline_support = self._save_cis
90 import matplotlib
90 import matplotlib
91 matplotlib.rcParams = self._saved_rcParams
91 matplotlib.rcParams = self._saved_rcParams
92
92
93 def test_qt(self):
93 def test_qt(self):
94 s = self.Shell()
94 s = self.Shell()
95 gui, backend = s.enable_matplotlib(None)
95 gui, backend = s.enable_matplotlib(None)
96 nt.assert_equal(gui, 'qt')
96 nt.assert_equal(gui, 'qt')
97 nt.assert_equal(s.pylab_gui_select, 'qt')
97 nt.assert_equal(s.pylab_gui_select, 'qt')
98
98
99 gui, backend = s.enable_matplotlib('inline')
99 gui, backend = s.enable_matplotlib('inline')
100 nt.assert_equal(gui, 'inline')
100 nt.assert_equal(gui, 'inline')
101 nt.assert_equal(s.pylab_gui_select, 'qt')
101 nt.assert_equal(s.pylab_gui_select, 'qt')
102
102
103 gui, backend = s.enable_matplotlib('qt')
103 gui, backend = s.enable_matplotlib('qt')
104 nt.assert_equal(gui, 'qt')
104 nt.assert_equal(gui, 'qt')
105 nt.assert_equal(s.pylab_gui_select, 'qt')
105 nt.assert_equal(s.pylab_gui_select, 'qt')
106
106
107 gui, backend = s.enable_matplotlib('inline')
107 gui, backend = s.enable_matplotlib('inline')
108 nt.assert_equal(gui, 'inline')
108 nt.assert_equal(gui, 'inline')
109 nt.assert_equal(s.pylab_gui_select, 'qt')
109 nt.assert_equal(s.pylab_gui_select, 'qt')
110
110
111 gui, backend = s.enable_matplotlib()
111 gui, backend = s.enable_matplotlib()
112 nt.assert_equal(gui, 'qt')
112 nt.assert_equal(gui, 'qt')
113 nt.assert_equal(s.pylab_gui_select, 'qt')
113 nt.assert_equal(s.pylab_gui_select, 'qt')
114
114
115 def test_inline(self):
115 def test_inline(self):
116 s = self.Shell()
116 s = self.Shell()
117 gui, backend = s.enable_matplotlib('inline')
117 gui, backend = s.enable_matplotlib('inline')
118 nt.assert_equal(gui, 'inline')
118 nt.assert_equal(gui, 'inline')
119 nt.assert_equal(s.pylab_gui_select, None)
119 nt.assert_equal(s.pylab_gui_select, None)
120
120
121 gui, backend = s.enable_matplotlib('inline')
121 gui, backend = s.enable_matplotlib('inline')
122 nt.assert_equal(gui, 'inline')
122 nt.assert_equal(gui, 'inline')
123 nt.assert_equal(s.pylab_gui_select, None)
123 nt.assert_equal(s.pylab_gui_select, None)
124
124
125 gui, backend = s.enable_matplotlib('qt')
125 gui, backend = s.enable_matplotlib('qt')
126 nt.assert_equal(gui, 'qt')
126 nt.assert_equal(gui, 'qt')
127 nt.assert_equal(s.pylab_gui_select, 'qt')
127 nt.assert_equal(s.pylab_gui_select, 'qt')
128
128
129 def test_qt_gtk(self):
129 def test_qt_gtk(self):
130 s = self.Shell()
130 s = self.Shell()
131 gui, backend = s.enable_matplotlib('qt')
131 gui, backend = s.enable_matplotlib('qt')
132 nt.assert_equal(gui, 'qt')
132 nt.assert_equal(gui, 'qt')
133 nt.assert_equal(s.pylab_gui_select, 'qt')
133 nt.assert_equal(s.pylab_gui_select, 'qt')
134
134
135 gui, backend = s.enable_matplotlib('gtk')
135 gui, backend = s.enable_matplotlib('gtk')
136 nt.assert_equal(gui, 'qt')
136 nt.assert_equal(gui, 'qt')
137 nt.assert_equal(s.pylab_gui_select, 'qt')
137 nt.assert_equal(s.pylab_gui_select, 'qt')
138
138
General Comments 0
You need to be logged in to leave comments. Login now