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