##// END OF EJS Templates
Fix display of SyntaxError in Python 3....
Fix display of SyntaxError in Python 3. Closes gh-1225

File last commit:

r5827:de0ead43
r5827:de0ead43
Show More
test_irunner.py
179 lines | 3.6 KiB | text/x-python | PythonLexer
fperez
New system for interactively running scripts. After a submission by Ken...
r302 """Test suite for the irunner module.
Not the most elegant or fine-grained, but it does cover at least the bulk
functionality."""
# Global to make tests extra verbose and help debugging
VERBOSE = True
# stdlib imports
MinRK
always use StringIO, never cStringIO...
r4794 import StringIO
fperez
Close #147
r635 import sys
fperez
New system for interactively running scripts. After a submission by Ken...
r302 import unittest
# IPython imports
Fernando Perez
Fix up and move old irunner tests into proper test suite.
r2652 from IPython.lib import irunner
Thomas Kluyver
Update irunner to work in Python 3.
r5431 from IPython.utils.py3compat import doctest_refactor_print
fperez
New system for interactively running scripts. After a submission by Ken...
r302
# Testing code begins
class RunnerTestCase(unittest.TestCase):
fperez
Close #147
r635 def setUp(self):
self.out = StringIO.StringIO()
#self.out = sys.stdout
fperez
New system for interactively running scripts. After a submission by Ken...
r302 def _test_runner(self,runner,source,output):
"""Test that a given runner's input/output match."""
runner.run_source(source)
fperez
Close #147
r635 out = self.out.getvalue()
#out = ''
fperez
New system for interactively running scripts. After a submission by Ken...
r302 # this output contains nasty \r\n lineends, and the initial ipython
Fernando Perez
Fix up and move old irunner tests into proper test suite.
r2652 # banner. clean it up for comparison, removing lines of whitespace
output_l = [l for l in output.splitlines() if l and not l.isspace()]
out_l = [l for l in out.splitlines() if l and not l.isspace()]
fperez
New system for interactively running scripts. After a submission by Ken...
r302 mismatch = 0
Fernando Perez
Fix up and move old irunner tests into proper test suite.
r2652 if len(output_l) != len(out_l):
Thomas Kluyver
Put information in failure message for mismatched # lines in irunner.
r3718 message = ("Mismatch in number of lines\n\n"
"Expected:\n"
"~~~~~~~~~\n"
"%s\n\n"
"Got:\n"
"~~~~~~~~~\n"
"%s"
) % ("\n".join(output_l), "\n".join(out_l))
self.fail(message)
fperez
New system for interactively running scripts. After a submission by Ken...
r302 for n in range(len(output_l)):
fperez
Close #147
r635 # Do a line-by-line comparison
fperez
New system for interactively running scripts. After a submission by Ken...
r302 ol1 = output_l[n].strip()
ol2 = out_l[n].strip()
if ol1 != ol2:
mismatch += 1
if VERBOSE:
print '<<< line %s does not match:' % n
print repr(ol1)
print repr(ol2)
print '>>>'
self.assert_(mismatch==0,'Number of mismatched lines: %s' %
mismatch)
def testIPython(self):
"""Test the IPython runner."""
Thomas Kluyver
Update irunner to work in Python 3.
r5431 source = doctest_refactor_print("""
fperez
New system for interactively running scripts. After a submission by Ken...
r302 print 'hello, this is python'
# some more code
x=1;y=2
x+y**2
# An example of autocall functionality
from math import *
autocall 1
cos pi
autocall 0
cos pi
cos(pi)
for i in range(5):
Thomas Kluyver
Update irunner to work in Python 3.
r5431 print i
fperez
New system for interactively running scripts. After a submission by Ken...
r302
print "that's all folks!"
Thomas Kluyver
Remove capitalised exit forms.
r3725 exit
Thomas Kluyver
Update irunner to work in Python 3.
r5431 """)
output = doctest_refactor_print("""\
fperez
New system for interactively running scripts. After a submission by Ken...
r302 In [1]: print 'hello, this is python'
hello, this is python
fperez
Close #147
r635 # some more code
In [2]: x=1;y=2
fperez
New system for interactively running scripts. After a submission by Ken...
r302
fperez
Close #147
r635 In [3]: x+y**2
Out[3]: 5
fperez
New system for interactively running scripts. After a submission by Ken...
r302
fperez
Close #147
r635 # An example of autocall functionality
In [4]: from math import *
fperez
New system for interactively running scripts. After a submission by Ken...
r302
fperez
Close #147
r635 In [5]: autocall 1
fperez
New system for interactively running scripts. After a submission by Ken...
r302 Automatic calling is: Smart
fperez
Close #147
r635 In [6]: cos pi
fperez
New system for interactively running scripts. After a submission by Ken...
r302 ------> cos(pi)
fperez
Close #147
r635 Out[6]: -1.0
fperez
New system for interactively running scripts. After a submission by Ken...
r302
fperez
Close #147
r635 In [7]: autocall 0
fperez
New system for interactively running scripts. After a submission by Ken...
r302 Automatic calling is: OFF
fperez
Close #147
r635 In [8]: cos pi
Thomas Kluyver
Fix subtle bug with error message on Python 2.6 - code to be compiled must end in a newline....
r3850 File "<ipython-input-8-6bd7313dd9a9>", line 1
fperez
New system for interactively running scripts. After a submission by Ken...
r302 cos pi
^
Thomas Kluyver
Fix subtle bug with error message on Python 2.6 - code to be compiled must end in a newline....
r3850 SyntaxError: invalid syntax
fperez
Close #147
r635
fperez
New system for interactively running scripts. After a submission by Ken...
r302
fperez
Close #147
r635 In [9]: cos(pi)
Out[9]: -1.0
fperez
New system for interactively running scripts. After a submission by Ken...
r302
fperez
Close #147
r635 In [10]: for i in range(5):
Thomas Kluyver
Update irunner to work in Python 3.
r5431 ....: print i
fperez
Close #147
r635 ....:
Thomas Kluyver
Update irunner to work in Python 3.
r5431 0
1
2
3
4
fperez
New system for interactively running scripts. After a submission by Ken...
r302
fperez
Close #147
r635 In [11]: print "that's all folks!"
fperez
New system for interactively running scripts. After a submission by Ken...
r302 that's all folks!
fperez
Close #147
r635
Thomas Kluyver
Remove capitalised exit forms.
r3725 In [12]: exit
Thomas Kluyver
Update irunner to work in Python 3.
r5431 """)
fperez
Close #147
r635 runner = irunner.IPythonRunner(out=self.out)
fperez
New system for interactively running scripts. After a submission by Ken...
r302 self._test_runner(runner,source,output)
def testPython(self):
"""Test the Python runner."""
fperez
Close #147
r635 runner = irunner.PythonRunner(out=self.out)
Thomas Kluyver
Update irunner to work in Python 3.
r5431 source = doctest_refactor_print("""
fperez
New system for interactively running scripts. After a submission by Ken...
r302 print 'hello, this is python'
# some more code
x=1;y=2
x+y**2
from math import *
cos(pi)
for i in range(5):
Thomas Kluyver
Update irunner to work in Python 3.
r5431 print i
fperez
New system for interactively running scripts. After a submission by Ken...
r302
print "that's all folks!"
Thomas Kluyver
Update irunner to work in Python 3.
r5431 """)
output = doctest_refactor_print("""\
fperez
New system for interactively running scripts. After a submission by Ken...
r302 >>> print 'hello, this is python'
hello, this is python
fperez
Close #147
r635
# some more code
>>> x=1;y=2
fperez
New system for interactively running scripts. After a submission by Ken...
r302 >>> x+y**2
5
fperez
Close #147
r635
fperez
New system for interactively running scripts. After a submission by Ken...
r302 >>> from math import *
>>> cos(pi)
-1.0
fperez
Close #147
r635
fperez
New system for interactively running scripts. After a submission by Ken...
r302 >>> for i in range(5):
Thomas Kluyver
Update irunner to work in Python 3.
r5431 ... print i
fperez
New system for interactively running scripts. After a submission by Ken...
r302 ...
Thomas Kluyver
Update irunner to work in Python 3.
r5431 0
1
2
3
4
fperez
New system for interactively running scripts. After a submission by Ken...
r302 >>> print "that's all folks!"
fperez
Close #147
r635 that's all folks!
Thomas Kluyver
Update irunner to work in Python 3.
r5431 """)
fperez
New system for interactively running scripts. After a submission by Ken...
r302 self._test_runner(runner,source,output)