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