Show More
@@ -1,119 +1,121 b'' | |||
|
1 | 1 | """Test suite for pylab_import_all magic |
|
2 | 2 | Modified from the irunner module but using regex. |
|
3 | 3 | """ |
|
4 | 4 | from __future__ import print_function |
|
5 | 5 | |
|
6 | 6 | # Global to make tests extra verbose and help debugging |
|
7 | 7 | VERBOSE = True |
|
8 | 8 | |
|
9 | 9 | # stdlib imports |
|
10 | 10 | import StringIO |
|
11 | 11 | import sys |
|
12 | 12 | import unittest |
|
13 | 13 | import re |
|
14 | 14 | |
|
15 | 15 | # IPython imports |
|
16 | 16 | from IPython.lib import irunner |
|
17 | 17 | from IPython.testing import decorators |
|
18 | 18 | |
|
19 | 19 | def pylab_not_importable(): |
|
20 | 20 | """Test if importing pylab fails. (For example, when having no display)""" |
|
21 | 21 | try: |
|
22 | 22 | import pylab |
|
23 | 23 | return False |
|
24 | 24 | except: |
|
25 | 25 | return True |
|
26 | 26 | |
|
27 | 27 | # Testing code begins |
|
28 | 28 | class RunnerTestCase(unittest.TestCase): |
|
29 | 29 | |
|
30 | 30 | def setUp(self): |
|
31 | 31 | self.out = StringIO.StringIO() |
|
32 | 32 | #self.out = sys.stdout |
|
33 | 33 | |
|
34 | 34 | def _test_runner(self,runner,source,output): |
|
35 | 35 | """Test that a given runner's input/output match.""" |
|
36 | 36 | |
|
37 | 37 | runner.run_source(source) |
|
38 | 38 | out = self.out.getvalue() |
|
39 | 39 | #out = '' |
|
40 | 40 | # this output contains nasty \r\n lineends, and the initial ipython |
|
41 | 41 | # banner. clean it up for comparison, removing lines of whitespace |
|
42 | 42 | output_l = [l for l in output.splitlines() if l and not l.isspace()] |
|
43 | 43 | out_l = [l for l in out.splitlines() if l and not l.isspace()] |
|
44 | 44 | mismatch = 0 |
|
45 | 45 | if len(output_l) != len(out_l): |
|
46 | 46 | message = ("Mismatch in number of lines\n\n" |
|
47 | 47 | "Expected:\n" |
|
48 | 48 | "~~~~~~~~~\n" |
|
49 | 49 | "%s\n\n" |
|
50 | 50 | "Got:\n" |
|
51 | 51 | "~~~~~~~~~\n" |
|
52 | 52 | "%s" |
|
53 | 53 | ) % ("\n".join(output_l), "\n".join(out_l)) |
|
54 | 54 | self.fail(message) |
|
55 | 55 | for n in range(len(output_l)): |
|
56 | 56 | # Do a line-by-line comparison |
|
57 | 57 | ol1 = output_l[n].strip() |
|
58 | 58 | ol2 = out_l[n].strip() |
|
59 | 59 | if not re.match(ol1,ol2): |
|
60 | 60 | mismatch += 1 |
|
61 | 61 | if VERBOSE: |
|
62 | 62 | print('<<< line %s does not match:' % n) |
|
63 | 63 | print(repr(ol1)) |
|
64 | 64 | print(repr(ol2)) |
|
65 | 65 | print('>>>') |
|
66 | 66 | self.assertTrue(mismatch==0,'Number of mismatched lines: %s' % |
|
67 | 67 | mismatch) |
|
68 | 68 | |
|
69 | @decorators.skip_if_no_x11 | |
|
69 | 70 | @decorators.skipif_not_matplotlib |
|
70 | 71 | @decorators.skipif(pylab_not_importable, "Likely a run without X.") |
|
71 | 72 | def test_pylab_import_all_enabled(self): |
|
72 | 73 | "Verify that plot is available when pylab_import_all = True" |
|
73 | 74 | source = """ |
|
74 | 75 | from IPython.config.application import Application |
|
75 | 76 | app = Application.instance() |
|
76 | 77 | app.pylab_import_all = True |
|
77 | 78 | pylab |
|
78 | 79 | ip=get_ipython() |
|
79 | 80 | 'plot' in ip.user_ns |
|
80 | 81 | """ |
|
81 | 82 | output = """ |
|
82 | 83 | In \[1\]: from IPython\.config\.application import Application |
|
83 | 84 | In \[2\]: app = Application\.instance\(\) |
|
84 | 85 | In \[3\]: app\.pylab_import_all = True |
|
85 | 86 | In \[4\]: pylab |
|
86 | 87 | ^Using matplotlib backend: |
|
87 | 88 | Populating the interactive namespace from numpy and matplotlib |
|
88 | 89 | In \[5\]: ip=get_ipython\(\) |
|
89 | 90 | In \[6\]: \'plot\' in ip\.user_ns |
|
90 | 91 | Out\[6\]: True |
|
91 | 92 | """ |
|
92 | 93 | runner = irunner.IPythonRunner(out=self.out) |
|
93 | 94 | self._test_runner(runner,source,output) |
|
94 | 95 | |
|
96 | @decorators.skip_if_no_x11 | |
|
95 | 97 | @decorators.skipif_not_matplotlib |
|
96 | 98 | @decorators.skipif(pylab_not_importable, "Likely a run without X.") |
|
97 | 99 | def test_pylab_import_all_disabled(self): |
|
98 | 100 | "Verify that plot is not available when pylab_import_all = False" |
|
99 | 101 | source = """ |
|
100 | 102 | from IPython.config.application import Application |
|
101 | 103 | app = Application.instance() |
|
102 | 104 | app.pylab_import_all = False |
|
103 | 105 | pylab |
|
104 | 106 | ip=get_ipython() |
|
105 | 107 | 'plot' in ip.user_ns |
|
106 | 108 | """ |
|
107 | 109 | output = """ |
|
108 | 110 | In \[1\]: from IPython\.config\.application import Application |
|
109 | 111 | In \[2\]: app = Application\.instance\(\) |
|
110 | 112 | In \[3\]: app\.pylab_import_all = False |
|
111 | 113 | In \[4\]: pylab |
|
112 | 114 | ^Using matplotlib backend: |
|
113 | 115 | Populating the interactive namespace from numpy and matplotlib |
|
114 | 116 | In \[5\]: ip=get_ipython\(\) |
|
115 | 117 | In \[6\]: \'plot\' in ip\.user_ns |
|
116 | 118 | Out\[6\]: False |
|
117 | 119 | """ |
|
118 | 120 | runner = irunner.IPythonRunner(out=self.out) |
|
119 | 121 | self._test_runner(runner,source,output) |
General Comments 0
You need to be logged in to leave comments.
Login now