test_irunner.py
168 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
/ test / test_irunner.py
fperez
|
r302 | #!/usr/bin/env python | ||
"""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 | ||||
import cStringIO as StringIO | ||||
fperez
|
r635 | import sys | ||
fperez
|
r302 | import unittest | ||
# IPython imports | ||||
from IPython import irunner | ||||
# Testing code begins | ||||
class RunnerTestCase(unittest.TestCase): | ||||
fperez
|
r635 | def setUp(self): | ||
self.out = StringIO.StringIO() | ||||
#self.out = sys.stdout | ||||
fperez
|
r302 | def _test_runner(self,runner,source,output): | ||
"""Test that a given runner's input/output match.""" | ||||
runner.run_source(source) | ||||
fperez
|
r635 | out = self.out.getvalue() | ||
#out = '' | ||||
fperez
|
r302 | # this output contains nasty \r\n lineends, and the initial ipython | ||
# banner. clean it up for comparison | ||||
output_l = output.split() | ||||
out_l = out.split() | ||||
mismatch = 0 | ||||
fperez
|
r635 | #if len(output_l) != len(out_l): | ||
# self.fail('mismatch in number of lines') | ||||
fperez
|
r302 | for n in range(len(output_l)): | ||
fperez
|
r635 | # Do a line-by-line comparison | ||
fperez
|
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.""" | ||||
source = """ | ||||
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): | ||||
print i, | ||||
print "that's all folks!" | ||||
%Exit | ||||
""" | ||||
output = """\ | ||||
In [1]: print 'hello, this is python' | ||||
hello, this is python | ||||
fperez
|
r635 | # some more code | ||
In [2]: x=1;y=2 | ||||
fperez
|
r302 | |||
fperez
|
r635 | In [3]: x+y**2 | ||
Out[3]: 5 | ||||
fperez
|
r302 | |||
fperez
|
r635 | # An example of autocall functionality | ||
In [4]: from math import * | ||||
fperez
|
r302 | |||
fperez
|
r635 | In [5]: autocall 1 | ||
fperez
|
r302 | Automatic calling is: Smart | ||
fperez
|
r635 | In [6]: cos pi | ||
fperez
|
r302 | ------> cos(pi) | ||
fperez
|
r635 | Out[6]: -1.0 | ||
fperez
|
r302 | |||
fperez
|
r635 | In [7]: autocall 0 | ||
fperez
|
r302 | Automatic calling is: OFF | ||
fperez
|
r635 | In [8]: cos pi | ||
fperez
|
r302 | ------------------------------------------------------------ | ||
File "<ipython console>", line 1 | ||||
cos pi | ||||
^ | ||||
fperez
|
r635 | <type 'exceptions.SyntaxError'>: invalid syntax | ||
fperez
|
r302 | |||
fperez
|
r635 | In [9]: cos(pi) | ||
Out[9]: -1.0 | ||||
fperez
|
r302 | |||
fperez
|
r635 | In [10]: for i in range(5): | ||
....: print i, | ||||
....: | ||||
fperez
|
r302 | 0 1 2 3 4 | ||
fperez
|
r635 | In [11]: print "that's all folks!" | ||
fperez
|
r302 | that's all folks! | ||
fperez
|
r635 | |||
In [12]: %Exit | ||||
""" | ||||
runner = irunner.IPythonRunner(out=self.out) | ||||
fperez
|
r302 | self._test_runner(runner,source,output) | ||
def testPython(self): | ||||
"""Test the Python runner.""" | ||||
fperez
|
r635 | runner = irunner.PythonRunner(out=self.out) | ||
fperez
|
r302 | source = """ | ||
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): | ||||
print i, | ||||
print "that's all folks!" | ||||
""" | ||||
output = """\ | ||||
>>> print 'hello, this is python' | ||||
hello, this is python | ||||
fperez
|
r635 | |||
# some more code | ||||
>>> x=1;y=2 | ||||
fperez
|
r302 | >>> x+y**2 | ||
5 | ||||
fperez
|
r635 | |||
fperez
|
r302 | >>> from math import * | ||
>>> cos(pi) | ||||
-1.0 | ||||
fperez
|
r635 | |||
fperez
|
r302 | >>> for i in range(5): | ||
... print i, | ||||
... | ||||
0 1 2 3 4 | ||||
>>> print "that's all folks!" | ||||
fperez
|
r635 | that's all folks! | ||
""" | ||||
fperez
|
r302 | self._test_runner(runner,source,output) | ||
if __name__ == '__main__': | ||||
unittest.main() | ||||