##// END OF EJS Templates
Tweak test_oinspect to only use a common subset of nt.assert_* functions.
Thomas Kluyver -
Show More
@@ -1,119 +1,119 b''
1 """Tests for the object inspection functionality.
1 """Tests for the object inspection functionality.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (C) 2010 The IPython Development Team.
4 # Copyright (C) 2010 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the BSD License.
6 # Distributed under the terms of the BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 from __future__ import print_function
14 from __future__ import print_function
15
15
16 # Stdlib imports
16 # Stdlib imports
17
17
18 # Third-party imports
18 # Third-party imports
19 import nose.tools as nt
19 import nose.tools as nt
20
20
21 # Our own imports
21 # Our own imports
22 from .. import oinspect
22 from .. import oinspect
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Globals and constants
25 # Globals and constants
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 inspector = oinspect.Inspector()
28 inspector = oinspect.Inspector()
29
29
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31 # Local utilities
31 # Local utilities
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33
33
34 # A few generic objects we can then inspect in the tests below
34 # A few generic objects we can then inspect in the tests below
35
35
36 class Call(object):
36 class Call(object):
37 """This is the class docstring."""
37 """This is the class docstring."""
38
38
39 def __init__(self, x, y=1):
39 def __init__(self, x, y=1):
40 """This is the constructor docstring."""
40 """This is the constructor docstring."""
41
41
42 def __call__(self, *a, **kw):
42 def __call__(self, *a, **kw):
43 """This is the call docstring."""
43 """This is the call docstring."""
44
44
45 def method(self, x, z=2):
45 def method(self, x, z=2):
46 """Some method's docstring"""
46 """Some method's docstring"""
47
47
48 def f(x, y=2, *a, **kw):
48 def f(x, y=2, *a, **kw):
49 """A simple function."""
49 """A simple function."""
50
50
51 def g(y, z=3, *a, **kw):
51 def g(y, z=3, *a, **kw):
52 pass # no docstring
52 pass # no docstring
53
53
54
54
55 def check_calltip(obj, name, call, docstring):
55 def check_calltip(obj, name, call, docstring):
56 """Generic check pattern all calltip tests will use"""
56 """Generic check pattern all calltip tests will use"""
57 info = inspector.info(obj, name)
57 info = inspector.info(obj, name)
58 call_line, ds = oinspect.call_tip(info)
58 call_line, ds = oinspect.call_tip(info)
59 nt.assert_equal(call_line, call)
59 nt.assert_equal(call_line, call)
60 nt.assert_equal(ds, docstring)
60 nt.assert_equal(ds, docstring)
61
61
62 #-----------------------------------------------------------------------------
62 #-----------------------------------------------------------------------------
63 # Tests
63 # Tests
64 #-----------------------------------------------------------------------------
64 #-----------------------------------------------------------------------------
65
65
66 def test_calltip_class():
66 def test_calltip_class():
67 check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
67 check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
68
68
69
69
70 def test_calltip_instance():
70 def test_calltip_instance():
71 c = Call(1)
71 c = Call(1)
72 check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
72 check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
73
73
74
74
75 def test_calltip_method():
75 def test_calltip_method():
76 c = Call(1)
76 c = Call(1)
77 check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
77 check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
78
78
79
79
80 def test_calltip_function():
80 def test_calltip_function():
81 check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
81 check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
82
82
83
83
84 def test_calltip_function2():
84 def test_calltip_function2():
85 check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
85 check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
86
86
87
87
88 def test_calltip_builtin():
88 def test_calltip_builtin():
89 check_calltip(sum, 'sum', None, sum.__doc__)
89 check_calltip(sum, 'sum', None, sum.__doc__)
90
90
91 def test_info():
91 def test_info():
92 "Check that Inspector.info fills out various fields as expected."
92 "Check that Inspector.info fills out various fields as expected."
93 i = inspector.info(Call, oname='Call')
93 i = inspector.info(Call, oname='Call')
94 nt.assert_equal(i['type_name'], 'type')
94 nt.assert_equal(i['type_name'], 'type')
95 nt.assert_equal(i['base_class'], "<type 'type'>")
95 nt.assert_equal(i['base_class'], "<type 'type'>")
96 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
96 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
97 fname = __file__
97 fname = __file__
98 if fname.endswith(".pyc"):
98 if fname.endswith(".pyc"):
99 fname = fname[:-1]
99 fname = fname[:-1]
100 nt.assert_equal(i['file'], fname)
100 nt.assert_equal(i['file'], fname)
101 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
101 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
102 nt.assert_equal(i['docstring'], Call.__doc__)
102 nt.assert_equal(i['docstring'], Call.__doc__)
103 nt.assert_is(i['source'], None)
103 nt.assert_equal(i['source'], None)
104 nt.assert_true(i['isclass'])
104 nt.assert_true(i['isclass'])
105 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
105 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
106 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
106 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
107
107
108 i = inspector.info(Call, detail_level=1)
108 i = inspector.info(Call, detail_level=1)
109 nt.assert_is_not(i['source'], None)
109 nt.assert_not_equal(i['source'], None)
110 nt.assert_is(i['docstring'], None)
110 nt.assert_equal(i['docstring'], None)
111
111
112 c = Call(1)
112 c = Call(1)
113 c.__doc__ = "Modified instance docstring"
113 c.__doc__ = "Modified instance docstring"
114 i = inspector.info(c)
114 i = inspector.info(c)
115 nt.assert_equal(i['type_name'], 'Call')
115 nt.assert_equal(i['type_name'], 'Call')
116 nt.assert_equal(i['docstring'], "Modified instance docstring")
116 nt.assert_equal(i['docstring'], "Modified instance docstring")
117 nt.assert_equal(i['class_docstring'], Call.__doc__)
117 nt.assert_equal(i['class_docstring'], Call.__doc__)
118 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
118 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
119 nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
119 nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
General Comments 0
You need to be logged in to leave comments. Login now