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