Show More
@@ -1,180 +1,180 | |||
|
1 | 1 | """Test suite for the irunner module. |
|
2 | 2 | |
|
3 | 3 | Not the most elegant or fine-grained, but it does cover at least the bulk |
|
4 | 4 | functionality.""" |
|
5 | 5 | from __future__ import print_function |
|
6 | 6 | |
|
7 | 7 | # Global to make tests extra verbose and help debugging |
|
8 | 8 | VERBOSE = True |
|
9 | 9 | |
|
10 | 10 | # stdlib imports |
|
11 | 11 | import StringIO |
|
12 | 12 | import sys |
|
13 | 13 | import unittest |
|
14 | 14 | |
|
15 | 15 | # IPython imports |
|
16 | 16 | from IPython.lib import irunner |
|
17 | 17 | from IPython.utils.py3compat import doctest_refactor_print |
|
18 | 18 | |
|
19 | 19 | # Testing code begins |
|
20 | 20 | class RunnerTestCase(unittest.TestCase): |
|
21 | 21 | |
|
22 | 22 | def setUp(self): |
|
23 | 23 | self.out = StringIO.StringIO() |
|
24 | 24 | #self.out = sys.stdout |
|
25 | 25 | |
|
26 | 26 | def _test_runner(self,runner,source,output): |
|
27 | 27 | """Test that a given runner's input/output match.""" |
|
28 | 28 | |
|
29 | 29 | runner.run_source(source) |
|
30 | 30 | out = self.out.getvalue() |
|
31 | 31 | #out = '' |
|
32 | 32 | # this output contains nasty \r\n lineends, and the initial ipython |
|
33 | 33 | # banner. clean it up for comparison, removing lines of whitespace |
|
34 | 34 | output_l = [l for l in output.splitlines() if l and not l.isspace()] |
|
35 | 35 | out_l = [l for l in out.splitlines() if l and not l.isspace()] |
|
36 | 36 | mismatch = 0 |
|
37 | 37 | if len(output_l) != len(out_l): |
|
38 | 38 | message = ("Mismatch in number of lines\n\n" |
|
39 | 39 | "Expected:\n" |
|
40 | 40 | "~~~~~~~~~\n" |
|
41 | 41 | "%s\n\n" |
|
42 | 42 | "Got:\n" |
|
43 | 43 | "~~~~~~~~~\n" |
|
44 | 44 | "%s" |
|
45 | 45 | ) % ("\n".join(output_l), "\n".join(out_l)) |
|
46 | 46 | self.fail(message) |
|
47 | 47 | for n in range(len(output_l)): |
|
48 | 48 | # Do a line-by-line comparison |
|
49 | 49 | ol1 = output_l[n].strip() |
|
50 | 50 | ol2 = out_l[n].strip() |
|
51 | 51 | if ol1 != ol2: |
|
52 | 52 | mismatch += 1 |
|
53 | 53 | if VERBOSE: |
|
54 | 54 | print('<<< line %s does not match:' % n) |
|
55 | 55 | print(repr(ol1)) |
|
56 | 56 | print(repr(ol2)) |
|
57 | 57 | print('>>>') |
|
58 | 58 | self.assertTrue(mismatch==0,'Number of mismatched lines: %s' % |
|
59 | 59 | mismatch) |
|
60 | 60 | |
|
61 | 61 | def testIPython(self): |
|
62 | 62 | """Test the IPython runner.""" |
|
63 | 63 | source = doctest_refactor_print(""" |
|
64 | 64 | print 'hello, this is python' |
|
65 | 65 | # some more code |
|
66 | 66 | x=1;y=2 |
|
67 | 67 | x+y**2 |
|
68 | 68 | |
|
69 | 69 | # An example of autocall functionality |
|
70 | 70 | from math import * |
|
71 | 71 | autocall 1 |
|
72 | 72 | cos pi |
|
73 | 73 | autocall 0 |
|
74 | 74 | cos pi |
|
75 | 75 | cos(pi) |
|
76 | 76 | |
|
77 | 77 | for i in range(5): |
|
78 | 78 | print i |
|
79 | 79 | |
|
80 | 80 | print "that's all folks!" |
|
81 | 81 | |
|
82 | 82 | exit |
|
83 | 83 | """) |
|
84 | 84 | output = doctest_refactor_print("""\ |
|
85 | 85 | In [1]: print 'hello, this is python' |
|
86 | 86 | hello, this is python |
|
87 | 87 | |
|
88 | 88 | |
|
89 | 89 | # some more code |
|
90 | 90 | In [2]: x=1;y=2 |
|
91 | 91 | |
|
92 | 92 | In [3]: x+y**2 |
|
93 | 93 | Out[3]: 5 |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | # An example of autocall functionality |
|
97 | 97 | In [4]: from math import * |
|
98 | 98 | |
|
99 | 99 | In [5]: autocall 1 |
|
100 | 100 | Automatic calling is: Smart |
|
101 | 101 | |
|
102 | 102 | In [6]: cos pi |
|
103 | 103 | ------> cos(pi) |
|
104 | 104 | Out[6]: -1.0 |
|
105 | 105 | |
|
106 | 106 | In [7]: autocall 0 |
|
107 | 107 | Automatic calling is: OFF |
|
108 | 108 | |
|
109 | 109 | In [8]: cos pi |
|
110 | 110 | File "<ipython-input-8-6bd7313dd9a9>", line 1 |
|
111 | 111 | cos pi |
|
112 | 112 | ^ |
|
113 | 113 | SyntaxError: invalid syntax |
|
114 | 114 | |
|
115 | 115 | |
|
116 | 116 | In [9]: cos(pi) |
|
117 | 117 | Out[9]: -1.0 |
|
118 | 118 | |
|
119 | 119 | |
|
120 | 120 | In [10]: for i in range(5): |
|
121 | 121 | ....: print i |
|
122 | 122 | ....: |
|
123 | 123 | 0 |
|
124 | 124 | 1 |
|
125 | 125 | 2 |
|
126 | 126 | 3 |
|
127 | 127 | 4 |
|
128 | 128 | |
|
129 | 129 | In [11]: print "that's all folks!" |
|
130 | 130 | that's all folks! |
|
131 | 131 | |
|
132 | 132 | |
|
133 | 133 | In [12]: exit |
|
134 | 134 | """) |
|
135 | 135 | runner = irunner.IPythonRunner(out=self.out) |
|
136 | 136 | self._test_runner(runner,source,output) |
|
137 | 137 | |
|
138 | 138 | def testPython(self): |
|
139 | 139 | """Test the Python runner.""" |
|
140 | runner = irunner.PythonRunner(out=self.out) | |
|
140 | runner = irunner.PythonRunner(out=self.out, args=['-E']) | |
|
141 | 141 | source = doctest_refactor_print(""" |
|
142 | 142 | print 'hello, this is python' |
|
143 | 143 | |
|
144 | 144 | # some more code |
|
145 | 145 | x=1;y=2 |
|
146 | 146 | x+y**2 |
|
147 | 147 | |
|
148 | 148 | from math import * |
|
149 | 149 | cos(pi) |
|
150 | 150 | |
|
151 | 151 | for i in range(5): |
|
152 | 152 | print i |
|
153 | 153 | |
|
154 | 154 | print "that's all folks!" |
|
155 | 155 | """) |
|
156 | 156 | output = doctest_refactor_print("""\ |
|
157 | 157 | >>> print 'hello, this is python' |
|
158 | 158 | hello, this is python |
|
159 | 159 | |
|
160 | 160 | # some more code |
|
161 | 161 | >>> x=1;y=2 |
|
162 | 162 | >>> x+y**2 |
|
163 | 163 | 5 |
|
164 | 164 | |
|
165 | 165 | >>> from math import * |
|
166 | 166 | >>> cos(pi) |
|
167 | 167 | -1.0 |
|
168 | 168 | |
|
169 | 169 | >>> for i in range(5): |
|
170 | 170 | ... print i |
|
171 | 171 | ... |
|
172 | 172 | 0 |
|
173 | 173 | 1 |
|
174 | 174 | 2 |
|
175 | 175 | 3 |
|
176 | 176 | 4 |
|
177 | 177 | >>> print "that's all folks!" |
|
178 | 178 | that's all folks! |
|
179 | 179 | """) |
|
180 | 180 | self._test_runner(runner,source,output) |
General Comments 0
You need to be logged in to leave comments.
Login now