Show More
@@ -1,63 +1,128 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.testing import decorators as dec |
|
26 | from IPython.testing import decorators as dec | |
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 | @dec.parametric |
|
41 | @dec.parametric | |
42 | def test_figure_to_svg(): |
|
42 | def test_figure_to_svg(): | |
43 | # simple empty-figure test |
|
43 | # simple empty-figure test | |
44 | fig = plt.figure() |
|
44 | fig = plt.figure() | |
45 | yield nt.assert_equal(pt.print_figure(fig, 'svg'), None) |
|
45 | yield nt.assert_equal(pt.print_figure(fig, 'svg'), None) | |
46 |
|
46 | |||
47 | plt.close('all') |
|
47 | plt.close('all') | |
48 |
|
48 | |||
49 | # simple check for at least svg-looking output |
|
49 | # simple check for at least svg-looking output | |
50 | fig = plt.figure() |
|
50 | fig = plt.figure() | |
51 | ax = fig.add_subplot(1,1,1) |
|
51 | ax = fig.add_subplot(1,1,1) | |
52 | ax.plot([1,2,3]) |
|
52 | ax.plot([1,2,3]) | |
53 | plt.draw() |
|
53 | plt.draw() | |
54 | svg = pt.print_figure(fig, 'svg')[:100].lower() |
|
54 | svg = pt.print_figure(fig, 'svg')[:100].lower() | |
55 | yield nt.assert_true('doctype svg' in svg) |
|
55 | yield nt.assert_true('doctype svg' in svg) | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | def test_import_pylab(): |
|
58 | def test_import_pylab(): | |
59 | ip = get_ipython() |
|
59 | ip = get_ipython() | |
60 | ns = {} |
|
60 | ns = {} | |
61 | pt.import_pylab(ns, import_all=False) |
|
61 | pt.import_pylab(ns, import_all=False) | |
62 | nt.assert_true('plt' in ns) |
|
62 | nt.assert_true('plt' in ns) | |
63 | nt.assert_equal(ns['np'], np) |
|
63 | nt.assert_equal(ns['np'], np) | |
|
64 | ||||
|
65 | ||||
|
66 | class TestPylabSwitch(object): | |||
|
67 | class Shell(object): | |||
|
68 | pylab_gui_select = None | |||
|
69 | ||||
|
70 | def setup(self): | |||
|
71 | self._save_am = pt.activate_matplotlib | |||
|
72 | pt.activate_matplotlib = lambda *a,**kw:None | |||
|
73 | self._save_ip = pt.import_pylab | |||
|
74 | pt.import_pylab = lambda *a,**kw:None | |||
|
75 | self._save_cis = pt.configure_inline_support | |||
|
76 | pt.configure_inline_support = lambda *a,**kw:None | |||
|
77 | ||||
|
78 | def teardown(self): | |||
|
79 | pt.activate_matplotlib = self._save_am | |||
|
80 | pt.import_pylab = self._save_ip | |||
|
81 | pt.configure_inline_support = self._save_cis | |||
|
82 | ||||
|
83 | def test_qt(self): | |||
|
84 | s = self.Shell() | |||
|
85 | gui = pt.pylab_activate(dict(), 'qt', False, s) | |||
|
86 | nt.assert_equal(gui, 'qt') | |||
|
87 | nt.assert_equal(s.pylab_gui_select, 'qt') | |||
|
88 | ||||
|
89 | gui = pt.pylab_activate(dict(), 'inline', False, s) | |||
|
90 | nt.assert_equal(gui, 'inline') | |||
|
91 | nt.assert_equal(s.pylab_gui_select, 'qt') | |||
|
92 | ||||
|
93 | gui = pt.pylab_activate(dict(), None, False, s) | |||
|
94 | nt.assert_equal(gui, 'qt') | |||
|
95 | nt.assert_equal(s.pylab_gui_select, 'qt') | |||
|
96 | ||||
|
97 | gui = pt.pylab_activate(dict(), 'inline', False, s) | |||
|
98 | nt.assert_equal(gui, 'inline') | |||
|
99 | nt.assert_equal(s.pylab_gui_select, 'qt') | |||
|
100 | ||||
|
101 | gui = pt.pylab_activate(dict(), None, False, s) | |||
|
102 | nt.assert_equal(gui, 'qt') | |||
|
103 | nt.assert_equal(s.pylab_gui_select, 'qt') | |||
|
104 | ||||
|
105 | def test_inline(self): | |||
|
106 | s = self.Shell() | |||
|
107 | gui = pt.pylab_activate(dict(), 'inline', False, s) | |||
|
108 | nt.assert_equal(gui, 'inline') | |||
|
109 | nt.assert_equal(s.pylab_gui_select, None) | |||
|
110 | ||||
|
111 | gui = pt.pylab_activate(dict(), 'inline', False, s) | |||
|
112 | nt.assert_equal(gui, 'inline') | |||
|
113 | nt.assert_equal(s.pylab_gui_select, None) | |||
|
114 | ||||
|
115 | gui = pt.pylab_activate(dict(), 'qt', False, s) | |||
|
116 | nt.assert_equal(gui, 'qt') | |||
|
117 | nt.assert_equal(s.pylab_gui_select, 'qt') | |||
|
118 | ||||
|
119 | def test_qt_gtk(self): | |||
|
120 | s = self.Shell() | |||
|
121 | gui = pt.pylab_activate(dict(), 'qt', False, s) | |||
|
122 | nt.assert_equal(gui, 'qt') | |||
|
123 | nt.assert_equal(s.pylab_gui_select, 'qt') | |||
|
124 | ||||
|
125 | gui = pt.pylab_activate(dict(), 'gtk', False, s) | |||
|
126 | nt.assert_equal(gui, 'qt') | |||
|
127 | nt.assert_equal(s.pylab_gui_select, 'qt') | |||
|
128 |
General Comments 0
You need to be logged in to leave comments.
Login now