##// END OF EJS Templates
Merge branch 'master' into Carreau-patch-4
Merge branch 'master' into Carreau-patch-4

File last commit:

r27639:c55dbf99
r27644:c272c021 merge Carreau-patch-4
Show More
test_iplib.py
250 lines | 6.7 KiB | text/x-python | PythonLexer
Brian Granger
Moving and renaming in preparation of subclassing InteractiveShell....
r2760 """Tests for the key interactiveshell module, where the main ipython class is defined.
Fernando Perez
Fix https://bugs.launchpad.net/ipython/+bug/239054...
r1859 """
Fernando Perez
Make user_setup a top-level function in iplib and add better tests....
r1908 #-----------------------------------------------------------------------------
# Module imports
#-----------------------------------------------------------------------------
Fernando Perez
Fix https://bugs.launchpad.net/ipython/+bug/239054...
r1859
Fernando Perez
Make user_setup a top-level function in iplib and add better tests....
r1908 # third party
Tomasz Kłoczko
nose2pytest migration batch 1...
r26749 import pytest
Fernando Perez
Fix https://bugs.launchpad.net/ipython/+bug/239054...
r1859
Fernando Perez
Make user_setup a top-level function in iplib and add better tests....
r1908 # our own packages
#-----------------------------------------------------------------------------
# Test functions
#-----------------------------------------------------------------------------
Fernando Perez
Fix problems with multiline doctests and add docs about testing....
r1868
Fernando Perez
Fix https://bugs.launchpad.net/ipython/+bug/239054...
r1859 def test_reset():
"""reset must clear most namespaces."""
Fernando Perez
Work in multiple places to improve state of the test suite....
r2398
# Check that reset runs without error
ip.reset()
# Once we've reset it (to clear of any junk that might have been there from
# other tests, we can count how many variables are in the user's namespace
nvars_user_ns = len(ip.user_ns)
Thomas Kluyver
Minor improvements to how namespaces are handled.
r5458 nvars_hidden = len(ip.user_ns_hidden)
Fernando Perez
Work in multiple places to improve state of the test suite....
r2398
# Now add a few variables to user_ns, and check that reset clears them
ip.user_ns['x'] = 1
ip.user_ns['y'] = 1
ip.reset()
# Finally, check that all namespaces have only as many variables as we
# expect to find in them:
Tomasz Kłoczko
nose2pytest migration batch 1...
r26749 assert len(ip.user_ns) == nvars_user_ns
assert len(ip.user_ns_hidden) == nvars_hidden
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440
# Tests for reporting of exceptions in various modes, handling of SystemExit,
Brian Granger
Moving and renaming in preparation of subclassing InteractiveShell....
r2760 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440
def doctest_tb_plain():
"""
Matthias Bussonnier
manual reformat
r27052 In [18]: xmode plain
Exception reporting mode: Plain
In [19]: run simpleerr.py
Traceback (most recent call last):
Matthias Bussonnier
Add line number next to the file in the traceback...
r27356 File ...:... in <module>
Matthias Bussonnier
manual reformat
r27052 bar(mode)
Matthias Bussonnier
Add line number next to the file in the traceback...
r27356 File ...:... in bar
Matthias Bussonnier
manual reformat
r27052 div0()
Matthias Bussonnier
Add line number next to the file in the traceback...
r27356 File ...:... in div0
Matthias Bussonnier
manual reformat
r27052 x/y
ZeroDivisionError: ...
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440 """
def doctest_tb_context():
"""
Matthias Bussonnier
manual reformat
r27052 In [3]: xmode context
Exception reporting mode: Context
In [4]: run simpleerr.py
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<BLANKLINE>
... in <module>
30 except IndexError:
31 mode = 'div'
---> 33 bar(mode)
<BLANKLINE>
... in bar(mode)
15 "bar"
16 if mode=='div':
---> 17 div0()
18 elif mode=='exit':
19 try:
<BLANKLINE>
... in div0()
6 x = 1
7 y = 0
----> 8 x/y
<BLANKLINE>
ZeroDivisionError: ..."""
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440
def doctest_tb_verbose():
"""
Blazej Michalik
Darker
r26750 In [5]: xmode verbose
Exception reporting mode: Verbose
In [6]: run simpleerr.py
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<BLANKLINE>
... in <module>
Matthias Bussonnier
rectify line numbers
r27005 30 except IndexError:
31 mode = 'div'
---> 33 bar(mode)
Blazej Michalik
Darker
r26750 mode = 'div'
<BLANKLINE>
... in bar(mode='div')
Matthias Bussonnier
rectify line numbers
r27005 15 "bar"
16 if mode=='div':
---> 17 div0()
18 elif mode=='exit':
19 try:
Blazej Michalik
Darker
r26750 <BLANKLINE>
... in div0()
6 x = 1
7 y = 0
----> 8 x/y
x = 1
y = 0
<BLANKLINE>
ZeroDivisionError: ...
"""
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 def doctest_tb_sysexit():
"""
In [17]: %xmode plain
Exception reporting mode: Plain
In [18]: %run simpleerr.py exit
An exception has occurred, use %tb to see the full traceback.
SystemExit: (1, 'Mode = exit')
In [19]: %run simpleerr.py exit 2
An exception has occurred, use %tb to see the full traceback.
SystemExit: (2, 'Mode = exit')
In [20]: %tb
Traceback (most recent call last):
Matthias Bussonnier
Add line number next to the file in the traceback...
r27356 File ...:... in execfile
Matthias Bussonnier
remove some dead code especially wrt py3compat
r27224 exec(compiler(f.read(), fname, "exec"), glob, loc)
Matthias Bussonnier
Add line number next to the file in the traceback...
r27356 File ...:... in <module>
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 bar(mode)
Matthias Bussonnier
Add line number next to the file in the traceback...
r27356 File ...:... in bar
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 sysexit(stat, mode)
Matthias Bussonnier
Add line number next to the file in the traceback...
r27356 File ...:... in sysexit
Matthias Bussonnier
rectify line numbers
r27005 raise SystemExit(stat, f"Mode = {mode}")
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 SystemExit: (2, 'Mode = exit')
In [21]: %xmode context
Exception reporting mode: Context
In [22]: %tb
---------------------------------------------------------------------------
SystemExit Traceback (most recent call last)
Matthias Bussonnier
rectify line numbers
r27005 File ..., in execfile(fname, glob, loc, compiler)
Matthias Bussonnier
remove some dead code especially wrt py3compat
r27224 ... with open(fname, "rb") as f:
... compiler = compiler or compile
---> ... exec(compiler(f.read(), fname, "exec"), glob, loc)
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 ...<module>
Matthias Bussonnier
rectify line numbers
r27005 30 except IndexError:
31 mode = 'div'
---> 33 bar(mode)
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 <BLANKLINE>
...bar(mode)
Matthias Bussonnier
rectify line numbers
r27005 21 except:
22 stat = 1
---> 23 sysexit(stat, mode)
24 else:
25 raise ValueError('Unknown mode')
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 <BLANKLINE>
...sysexit(stat, mode)
10 def sysexit(stat, mode):
Matthias Bussonnier
rectify line numbers
r27005 ---> 11 raise SystemExit(stat, f"Mode = {mode}")
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 <BLANKLINE>
SystemExit: (2, 'Mode = exit')
Matthias Bussonnier
rectify line numbers
r27005 """
def doctest_tb_sysexit_verbose():
"""
In [18]: %run simpleerr.py exit
An exception has occurred, use %tb to see the full traceback.
SystemExit: (1, 'Mode = exit')
In [19]: %run simpleerr.py exit 2
An exception has occurred, use %tb to see the full traceback.
SystemExit: (2, 'Mode = exit')
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003
In [23]: %xmode verbose
Exception reporting mode: Verbose
In [24]: %tb
---------------------------------------------------------------------------
SystemExit Traceback (most recent call last)
<BLANKLINE>
... in <module>
Matthias Bussonnier
rectify line numbers
r27005 30 except IndexError:
31 mode = 'div'
---> 33 bar(mode)
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 mode = 'exit'
<BLANKLINE>
... in bar(mode='exit')
Matthias Bussonnier
rectify line numbers
r27005 ... except:
... stat = 1
---> ... sysexit(stat, mode)
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 mode = 'exit'
stat = 2
Matthias Bussonnier
rectify line numbers
r27005 ... else:
... raise ValueError('Unknown mode')
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 <BLANKLINE>
... in sysexit(stat=2, mode='exit')
10 def sysexit(stat, mode):
Matthias Bussonnier
rectify line numbers
r27005 ---> 11 raise SystemExit(stat, f"Mode = {mode}")
Nikita Kniazev
Revive doctest_tb_sysexit...
r27003 stat = 2
<BLANKLINE>
SystemExit: (2, 'Mode = exit')
"""
Fernando Perez
Move a few test into proper suite, clean up unused ones....
r2656
Thomas Kluyver
Remove runlines method and calls to it.
r3752 def test_run_cell():
Fernando Perez
Move a few test into proper suite, clean up unused ones....
r2656 import textwrap
Blazej Michalik
Darker
r26750 ip.run_cell("a = 10\na+=1")
ip.run_cell("assert a == 11\nassert 1")
assert ip.user_ns["a"] == 11
complex = textwrap.dedent(
"""
Fernando Perez
Move a few test into proper suite, clean up unused ones....
r2656 if 1:
print "hello"
if 1:
print "world"
if 2:
print "foo"
if 3:
print "bar"
if 4:
print "bar"
Matthias Bussonnier
typo and reformat
r27639 """
)
Fernando Perez
Move a few test into proper suite, clean up unused ones....
r2656 # Simply verifies that this kind of input is run
Thomas Kluyver
Remove runlines method and calls to it.
r3752 ip.run_cell(complex)
Fernando Perez
Move a few test into proper suite, clean up unused ones....
r2656
def test_db():
"""Test the internal database used for variable persistence."""
Blazej Michalik
Darker
r26750 ip.db["__unittest_"] = 12
assert ip.db["__unittest_"] == 12
del ip.db["__unittest_"]
assert "__unittest_" not in ip.db