Show More
@@ -1,119 +1,117 | |||
|
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 | 69 | @decorators.skipif_not_matplotlib |
|
70 | 70 | @decorators.skipif(pylab_not_importable, "Likely a run without X.") |
|
71 | 71 | def test_pylab_import_all_enabled(self): |
|
72 | 72 | "Verify that plot is available when pylab_import_all = True" |
|
73 | 73 | source = """ |
|
74 | 74 | from IPython.config.application import Application |
|
75 | 75 | app = Application.instance() |
|
76 | 76 | app.pylab_import_all = True |
|
77 | 77 | pylab |
|
78 | 78 | ip=get_ipython() |
|
79 | 79 | 'plot' in ip.user_ns |
|
80 | 80 | """ |
|
81 | 81 | output = """ |
|
82 | 82 | In \[1\]: from IPython\.config\.application import Application |
|
83 | 83 | In \[2\]: app = Application\.instance\(\) |
|
84 | 84 | In \[3\]: app\.pylab_import_all = True |
|
85 | 85 | In \[4\]: pylab |
|
86 | ^Welcome to pylab, a matplotlib-based Python environment | |
|
87 | For more information, type 'help\(pylab\)'\. | |
|
86 | ^using matplotlib backend: | |
|
88 | 87 | In \[5\]: ip=get_ipython\(\) |
|
89 | 88 | In \[6\]: \'plot\' in ip\.user_ns |
|
90 | 89 | Out\[6\]: True |
|
91 | 90 | """ |
|
92 | 91 | runner = irunner.IPythonRunner(out=self.out) |
|
93 | 92 | self._test_runner(runner,source,output) |
|
94 | 93 | |
|
95 | 94 | @decorators.skipif_not_matplotlib |
|
96 | 95 | @decorators.skipif(pylab_not_importable, "Likely a run without X.") |
|
97 | 96 | def test_pylab_import_all_disabled(self): |
|
98 | 97 | "Verify that plot is not available when pylab_import_all = False" |
|
99 | 98 | source = """ |
|
100 | 99 | from IPython.config.application import Application |
|
101 | 100 | app = Application.instance() |
|
102 | 101 | app.pylab_import_all = False |
|
103 | 102 | pylab |
|
104 | 103 | ip=get_ipython() |
|
105 | 104 | 'plot' in ip.user_ns |
|
106 | 105 | """ |
|
107 | 106 | output = """ |
|
108 | 107 | In \[1\]: from IPython\.config\.application import Application |
|
109 | 108 | In \[2\]: app = Application\.instance\(\) |
|
110 | 109 | In \[3\]: app\.pylab_import_all = False |
|
111 | 110 | In \[4\]: pylab |
|
112 | ^Welcome to pylab, a matplotlib-based Python environment | |
|
113 | For more information, type 'help\(pylab\)'\. | |
|
111 | ^using matplotlib backend: | |
|
114 | 112 | In \[5\]: ip=get_ipython\(\) |
|
115 | 113 | In \[6\]: \'plot\' in ip\.user_ns |
|
116 | 114 | Out\[6\]: False |
|
117 | 115 | """ |
|
118 | 116 | runner = irunner.IPythonRunner(out=self.out) |
|
119 | 117 | self._test_runner(runner,source,output) |
General Comments 0
You need to be logged in to leave comments.
Login now