##// END OF EJS Templates
remove non-functional View.kill method...
remove non-functional View.kill method This method cannot be implemented until engines have local nannies that can send signals. closes #2772

File last commit:

r7464:01d7b933 merge
r9036:b02db490
Show More
test_oinspect.py
290 lines | 8.2 KiB | text/x-python | PythonLexer
Fernando Perez
Add function signature info to calltips....
r3051 """Tests for the object inspection functionality.
"""
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2010-2011 The IPython Development Team.
Fernando Perez
Add function signature info to calltips....
r3051 #
# Distributed under the terms of the BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
# Stdlib imports
Fernando Perez
Fix finding of file info for magics and decorated functions....
r7290 import os
Fernando Perez
Fix failure due to .pyc/.py mismatch detected by test_pr.
r7434 import re
Fernando Perez
Add function signature info to calltips....
r3051
# Third-party imports
import nose.tools as nt
# Our own imports
from .. import oinspect
Fernando Perez
Add tests for object inspector with magics of all types.
r6995 from IPython.core.magic import (Magics, magics_class, line_magic,
cell_magic, line_cell_magic,
register_line_magic, register_cell_magic,
register_line_cell_magic)
Fernando Perez
Add extra tests for checking decorator logic with __wrapped__.
r7432 from IPython.external.decorator import decorator
Thomas Kluyver
test_oinspect works with Python 3.
r4760 from IPython.utils import py3compat
Fernando Perez
Add function signature info to calltips....
r3051
Fernando Perez
Add extra tests for checking decorator logic with __wrapped__.
r7432
Fernando Perez
Add function signature info to calltips....
r3051 #-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
inspector = oinspect.Inspector()
Fernando Perez
Add tests for object inspector with magics of all types.
r6995 ip = get_ipython()
Fernando Perez
Add function signature info to calltips....
r3051
#-----------------------------------------------------------------------------
# Local utilities
#-----------------------------------------------------------------------------
Fernando Perez
Fix finding of file info for magics and decorated functions....
r7290 # WARNING: since this test checks the line number where a function is
# defined, if any code is inserted above, the following line will need to be
# updated. Do NOT insert any whitespace between the next line and the function
# definition below.
Fernando Perez
Fix failure due to .pyc/.py mismatch detected by test_pr.
r7434 THIS_LINE_NUMBER = 48 # Put here the actual number of this line
Fernando Perez
Fix finding of file info for magics and decorated functions....
r7290 def test_find_source_lines():
nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
THIS_LINE_NUMBER+1)
Fernando Perez
Fix failure due to .pyc/.py mismatch detected by test_pr.
r7434 # A couple of utilities to ensure these tests work the same from a source or a
# binary install
def pyfile(fname):
Jörgen Stenarson
Moving os.path.normcase call to pyfile function.
r7452 return os.path.normcase(re.sub('.py[co]$', '.py', fname))
Fernando Perez
Fix failure due to .pyc/.py mismatch detected by test_pr.
r7434
def match_pyfiles(f1, f2):
Jörgen Stenarson
Moving os.path.normcase call to pyfile function.
r7452 nt.assert_equal(pyfile(f1), pyfile(f2))
Fernando Perez
Fix failure due to .pyc/.py mismatch detected by test_pr.
r7434
Fernando Perez
Fix finding of file info for magics and decorated functions....
r7290 def test_find_file():
Fernando Perez
Fix failure due to .pyc/.py mismatch detected by test_pr.
r7434 match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))
Fernando Perez
Fix finding of file info for magics and decorated functions....
r7290
Fernando Perez
Add extra tests for checking decorator logic with __wrapped__.
r7432 def test_find_file_decorated1():
@decorator
def noop1(f):
def wrapper():
return f(*a, **kw)
return wrapper
@noop1
def f(x):
"My docstring"
Fernando Perez
Fix failure due to .pyc/.py mismatch detected by test_pr.
r7434 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
Fernando Perez
Add extra tests for checking decorator logic with __wrapped__.
r7432 nt.assert_equal(f.__doc__, "My docstring")
def test_find_file_decorated2():
@decorator
def noop2(f, *a, **kw):
return f(*a, **kw)
@noop2
def f(x):
"My docstring 2"
Fernando Perez
Fix failure due to .pyc/.py mismatch detected by test_pr.
r7434 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
Fernando Perez
Add extra tests for checking decorator logic with __wrapped__.
r7432 nt.assert_equal(f.__doc__, "My docstring 2")
Fernando Perez
Fix finding of file info for magics and decorated functions....
r7290 def test_find_file_magic():
run = ip.find_line_magic('run')
nt.assert_not_equal(oinspect.find_file(run), None)
Fernando Perez
Add function signature info to calltips....
r3051 # A few generic objects we can then inspect in the tests below
class Call(object):
"""This is the class docstring."""
def __init__(self, x, y=1):
"""This is the constructor docstring."""
def __call__(self, *a, **kw):
"""This is the call docstring."""
def method(self, x, z=2):
"""Some method's docstring"""
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Add tests for object inspector with magics of all types.
r6995
Thomas Kluyver
Tweak Inspector.info() so it doesn't fail on old style classes. Added test....
r3867 class OldStyle:
"""An old-style class for testing."""
pass
Fernando Perez
Add function signature info to calltips....
r3051
Fernando Perez
Add tests for object inspector with magics of all types.
r6995
Fernando Perez
Add function signature info to calltips....
r3051 def f(x, y=2, *a, **kw):
"""A simple function."""
Fernando Perez
Add tests for object inspector with magics of all types.
r6995
Fernando Perez
Add function signature info to calltips....
r3051 def g(y, z=3, *a, **kw):
pass # no docstring
Fernando Perez
Add tests for object inspector with magics of all types.
r6995 @register_line_magic
def lmagic(line):
"A line magic"
@register_cell_magic
def cmagic(line, cell):
"A cell magic"
@register_line_cell_magic
def lcmagic(line, cell=None):
"A line/cell magic"
@magics_class
class SimpleMagics(Magics):
@line_magic
def Clmagic(self, cline):
"A class-based line magic"
@cell_magic
def Ccmagic(self, cline, ccell):
"A class-based cell magic"
@line_cell_magic
def Clcmagic(self, cline, ccell=None):
"A class-based line/cell magic"
Fernando Perez
Add function signature info to calltips....
r3051 def check_calltip(obj, name, call, docstring):
"""Generic check pattern all calltip tests will use"""
info = inspector.info(obj, name)
call_line, ds = oinspect.call_tip(info)
nt.assert_equal(call_line, call)
Bernardo B. Marques
remove all trailling spaces
r4872 nt.assert_equal(ds, docstring)
Fernando Perez
Add function signature info to calltips....
r3051
#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
def test_calltip_class():
check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
def test_calltip_instance():
c = Call(1)
check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
def test_calltip_method():
c = Call(1)
check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
def test_calltip_function():
check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
def test_calltip_function2():
check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
def test_calltip_builtin():
check_calltip(sum, 'sum', None, sum.__doc__)
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Add tests for object inspector with magics of all types.
r6995
def test_calltip_line_magic():
check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")
def test_calltip_cell_magic():
check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")
def test_calltip_line_magic():
check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
"A line/cell magic")
def test_class_magics():
cm = SimpleMagics(ip)
ip.register_magics(cm)
check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
"A class-based line magic")
check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
"A class-based cell magic")
check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
"A class-based line/cell magic")
Thomas Kluyver
Add test that Inspector.info fills out various fields as expected.
r3859 def test_info():
"Check that Inspector.info fills out various fields as expected."
i = inspector.info(Call, oname='Call')
nt.assert_equal(i['type_name'], 'type')
Thomas Kluyver
test_oinspect works with Python 3.
r4760 expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
nt.assert_equal(i['base_class'], expted_class)
Thomas Kluyver
Add test that Inspector.info fills out various fields as expected.
r3859 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
fname = __file__
if fname.endswith(".pyc"):
fname = fname[:-1]
Min RK
fix various tests on Windows...
r4105 # case-insensitive comparison needed on some filesystems
# e.g. Windows:
nt.assert_equal(i['file'].lower(), fname.lower())
Thomas Kluyver
Add test that Inspector.info fills out various fields as expected.
r3859 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
nt.assert_equal(i['docstring'], Call.__doc__)
Thomas Kluyver
Tweak test_oinspect to only use a common subset of nt.assert_* functions.
r3869 nt.assert_equal(i['source'], None)
Thomas Kluyver
Add test that Inspector.info fills out various fields as expected.
r3859 nt.assert_true(i['isclass'])
nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Add test that Inspector.info fills out various fields as expected.
r3859 i = inspector.info(Call, detail_level=1)
Thomas Kluyver
Tweak test_oinspect to only use a common subset of nt.assert_* functions.
r3869 nt.assert_not_equal(i['source'], None)
nt.assert_equal(i['docstring'], None)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Add test that Inspector.info fills out various fields as expected.
r3859 c = Call(1)
c.__doc__ = "Modified instance docstring"
i = inspector.info(c)
nt.assert_equal(i['type_name'], 'Call')
nt.assert_equal(i['docstring'], "Modified instance docstring")
nt.assert_equal(i['class_docstring'], Call.__doc__)
nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Tweak Inspector.info() so it doesn't fail on old style classes. Added test....
r3867 # Test old-style classes, which for example may not have an __init__ method.
Thomas Kluyver
test_oinspect works with Python 3.
r4760 if not py3compat.PY3:
i = inspector.info(OldStyle)
nt.assert_equal(i['type_name'], 'classobj')
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
test_oinspect works with Python 3.
r4760 i = inspector.info(OldStyle())
nt.assert_equal(i['type_name'], 'instance')
nt.assert_equal(i['docstring'], OldStyle.__doc__)
Thomas Kluyver
Add test for oinspect.getdoc
r5538
def test_getdoc():
class A(object):
"""standard docstring"""
pass
class B(object):
"""standard docstring"""
def getdoc(self):
return "custom docstring"
Thomas Kluyver
Test for getdoc() returning None.
r5573 class C(object):
"""standard docstring"""
def getdoc(self):
return None
Thomas Kluyver
Add test for oinspect.getdoc
r5538 a = A()
b = B()
Thomas Kluyver
Test for getdoc() returning None.
r5573 c = C()
Thomas Kluyver
Add test for oinspect.getdoc
r5538
nt.assert_equal(oinspect.getdoc(a), "standard docstring")
nt.assert_equal(oinspect.getdoc(b), "custom docstring")
Thomas Kluyver
Test for getdoc() returning None.
r5573 nt.assert_equal(oinspect.getdoc(c), "standard docstring")
Thomas Kluyver
Add failing test for gh-1914
r7460
def test_pdef():
# See gh-1914
def foo(): pass
inspector.pdef(foo, 'foo')