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