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