##// END OF EJS Templates
test_pylabtools also needs to modify matplotlib.rcParamsOrig...
Thomas Kluyver -
Show More
@@ -1,138 +1,141 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 #-----------------------------------------------------------------------------
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(b'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 self._saved_rcParamsOrig = matplotlib.rcParamsOrig
76 matplotlib.rcParams = dict(backend='Qt4Agg')
77 matplotlib.rcParams = dict(backend='Qt4Agg')
78 matplotlib.rcParamsOrig = dict(backend='Qt4Agg')
77
79
78 # Mock out functions
80 # Mock out functions
79 self._save_am = pt.activate_matplotlib
81 self._save_am = pt.activate_matplotlib
80 pt.activate_matplotlib = act_mpl
82 pt.activate_matplotlib = act_mpl
81 self._save_ip = pt.import_pylab
83 self._save_ip = pt.import_pylab
82 pt.import_pylab = lambda *a,**kw:None
84 pt.import_pylab = lambda *a,**kw:None
83 self._save_cis = pt.configure_inline_support
85 self._save_cis = pt.configure_inline_support
84 pt.configure_inline_support = lambda *a,**kw:None
86 pt.configure_inline_support = lambda *a,**kw:None
85
87
86 def teardown(self):
88 def teardown(self):
87 pt.activate_matplotlib = self._save_am
89 pt.activate_matplotlib = self._save_am
88 pt.import_pylab = self._save_ip
90 pt.import_pylab = self._save_ip
89 pt.configure_inline_support = self._save_cis
91 pt.configure_inline_support = self._save_cis
90 import matplotlib
92 import matplotlib
91 matplotlib.rcParams = self._saved_rcParams
93 matplotlib.rcParams = self._saved_rcParams
94 matplotlib.rcParamsOrig = self._saved_rcParamsOrig
92
95
93 def test_qt(self):
96 def test_qt(self):
94 s = self.Shell()
97 s = self.Shell()
95 gui, backend = s.enable_matplotlib(None)
98 gui, backend = s.enable_matplotlib(None)
96 nt.assert_equal(gui, 'qt')
99 nt.assert_equal(gui, 'qt')
97 nt.assert_equal(s.pylab_gui_select, 'qt')
100 nt.assert_equal(s.pylab_gui_select, 'qt')
98
101
99 gui, backend = s.enable_matplotlib('inline')
102 gui, backend = s.enable_matplotlib('inline')
100 nt.assert_equal(gui, 'inline')
103 nt.assert_equal(gui, 'inline')
101 nt.assert_equal(s.pylab_gui_select, 'qt')
104 nt.assert_equal(s.pylab_gui_select, 'qt')
102
105
103 gui, backend = s.enable_matplotlib('qt')
106 gui, backend = s.enable_matplotlib('qt')
104 nt.assert_equal(gui, 'qt')
107 nt.assert_equal(gui, 'qt')
105 nt.assert_equal(s.pylab_gui_select, 'qt')
108 nt.assert_equal(s.pylab_gui_select, 'qt')
106
109
107 gui, backend = s.enable_matplotlib('inline')
110 gui, backend = s.enable_matplotlib('inline')
108 nt.assert_equal(gui, 'inline')
111 nt.assert_equal(gui, 'inline')
109 nt.assert_equal(s.pylab_gui_select, 'qt')
112 nt.assert_equal(s.pylab_gui_select, 'qt')
110
113
111 gui, backend = s.enable_matplotlib()
114 gui, backend = s.enable_matplotlib()
112 nt.assert_equal(gui, 'qt')
115 nt.assert_equal(gui, 'qt')
113 nt.assert_equal(s.pylab_gui_select, 'qt')
116 nt.assert_equal(s.pylab_gui_select, 'qt')
114
117
115 def test_inline(self):
118 def test_inline(self):
116 s = self.Shell()
119 s = self.Shell()
117 gui, backend = s.enable_matplotlib('inline')
120 gui, backend = s.enable_matplotlib('inline')
118 nt.assert_equal(gui, 'inline')
121 nt.assert_equal(gui, 'inline')
119 nt.assert_equal(s.pylab_gui_select, None)
122 nt.assert_equal(s.pylab_gui_select, None)
120
123
121 gui, backend = s.enable_matplotlib('inline')
124 gui, backend = s.enable_matplotlib('inline')
122 nt.assert_equal(gui, 'inline')
125 nt.assert_equal(gui, 'inline')
123 nt.assert_equal(s.pylab_gui_select, None)
126 nt.assert_equal(s.pylab_gui_select, None)
124
127
125 gui, backend = s.enable_matplotlib('qt')
128 gui, backend = s.enable_matplotlib('qt')
126 nt.assert_equal(gui, 'qt')
129 nt.assert_equal(gui, 'qt')
127 nt.assert_equal(s.pylab_gui_select, 'qt')
130 nt.assert_equal(s.pylab_gui_select, 'qt')
128
131
129 def test_qt_gtk(self):
132 def test_qt_gtk(self):
130 s = self.Shell()
133 s = self.Shell()
131 gui, backend = s.enable_matplotlib('qt')
134 gui, backend = s.enable_matplotlib('qt')
132 nt.assert_equal(gui, 'qt')
135 nt.assert_equal(gui, 'qt')
133 nt.assert_equal(s.pylab_gui_select, 'qt')
136 nt.assert_equal(s.pylab_gui_select, 'qt')
134
137
135 gui, backend = s.enable_matplotlib('gtk')
138 gui, backend = s.enable_matplotlib('gtk')
136 nt.assert_equal(gui, 'qt')
139 nt.assert_equal(gui, 'qt')
137 nt.assert_equal(s.pylab_gui_select, 'qt')
140 nt.assert_equal(s.pylab_gui_select, 'qt')
138
141
General Comments 0
You need to be logged in to leave comments. Login now