##// END OF EJS Templates
Add tests for object inspector with magics of all types.
Fernando Perez -
Show More
@@ -1,159 +1,222 b''
1 """Tests for the object inspection functionality.
1 """Tests for the object inspection functionality.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (C) 2010-2011 The IPython Development Team.
4 # Copyright (C) 2010-2011 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 from IPython.core.magic import (Magics, magics_class, line_magic,
24 cell_magic, line_cell_magic,
25 register_line_magic, register_cell_magic,
26 register_line_cell_magic)
23 from IPython.utils import py3compat
27 from IPython.utils import py3compat
24
28
25 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
26 # Globals and constants
30 # Globals and constants
27 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
28
32
29 inspector = oinspect.Inspector()
33 inspector = oinspect.Inspector()
34 ip = get_ipython()
30
35
31 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
32 # Local utilities
37 # Local utilities
33 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
34
39
35 # A few generic objects we can then inspect in the tests below
40 # A few generic objects we can then inspect in the tests below
36
41
37 class Call(object):
42 class Call(object):
38 """This is the class docstring."""
43 """This is the class docstring."""
39
44
40 def __init__(self, x, y=1):
45 def __init__(self, x, y=1):
41 """This is the constructor docstring."""
46 """This is the constructor docstring."""
42
47
43 def __call__(self, *a, **kw):
48 def __call__(self, *a, **kw):
44 """This is the call docstring."""
49 """This is the call docstring."""
45
50
46 def method(self, x, z=2):
51 def method(self, x, z=2):
47 """Some method's docstring"""
52 """Some method's docstring"""
48
53
54
49 class OldStyle:
55 class OldStyle:
50 """An old-style class for testing."""
56 """An old-style class for testing."""
51 pass
57 pass
52
58
59
53 def f(x, y=2, *a, **kw):
60 def f(x, y=2, *a, **kw):
54 """A simple function."""
61 """A simple function."""
55
62
63
56 def g(y, z=3, *a, **kw):
64 def g(y, z=3, *a, **kw):
57 pass # no docstring
65 pass # no docstring
58
66
59
67
68 @register_line_magic
69 def lmagic(line):
70 "A line magic"
71
72
73 @register_cell_magic
74 def cmagic(line, cell):
75 "A cell magic"
76
77
78 @register_line_cell_magic
79 def lcmagic(line, cell=None):
80 "A line/cell magic"
81
82
83 @magics_class
84 class SimpleMagics(Magics):
85 @line_magic
86 def Clmagic(self, cline):
87 "A class-based line magic"
88
89 @cell_magic
90 def Ccmagic(self, cline, ccell):
91 "A class-based cell magic"
92
93 @line_cell_magic
94 def Clcmagic(self, cline, ccell=None):
95 "A class-based line/cell magic"
96
97
60 def check_calltip(obj, name, call, docstring):
98 def check_calltip(obj, name, call, docstring):
61 """Generic check pattern all calltip tests will use"""
99 """Generic check pattern all calltip tests will use"""
62 info = inspector.info(obj, name)
100 info = inspector.info(obj, name)
63 call_line, ds = oinspect.call_tip(info)
101 call_line, ds = oinspect.call_tip(info)
64 nt.assert_equal(call_line, call)
102 nt.assert_equal(call_line, call)
65 nt.assert_equal(ds, docstring)
103 nt.assert_equal(ds, docstring)
66
104
67 #-----------------------------------------------------------------------------
105 #-----------------------------------------------------------------------------
68 # Tests
106 # Tests
69 #-----------------------------------------------------------------------------
107 #-----------------------------------------------------------------------------
70
108
71 def test_calltip_class():
109 def test_calltip_class():
72 check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
110 check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
73
111
74
112
75 def test_calltip_instance():
113 def test_calltip_instance():
76 c = Call(1)
114 c = Call(1)
77 check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
115 check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
78
116
79
117
80 def test_calltip_method():
118 def test_calltip_method():
81 c = Call(1)
119 c = Call(1)
82 check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
120 check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
83
121
84
122
85 def test_calltip_function():
123 def test_calltip_function():
86 check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
124 check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
87
125
88
126
89 def test_calltip_function2():
127 def test_calltip_function2():
90 check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
128 check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
91
129
92
130
93 def test_calltip_builtin():
131 def test_calltip_builtin():
94 check_calltip(sum, 'sum', None, sum.__doc__)
132 check_calltip(sum, 'sum', None, sum.__doc__)
95
133
134
135 def test_calltip_line_magic():
136 check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")
137
138
139 def test_calltip_cell_magic():
140 check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")
141
142
143 def test_calltip_line_magic():
144 check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
145 "A line/cell magic")
146
147
148 def test_class_magics():
149 cm = SimpleMagics(ip)
150 ip.register_magics(cm)
151 check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
152 "A class-based line magic")
153 check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
154 "A class-based cell magic")
155 check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
156 "A class-based line/cell magic")
157
158
96 def test_info():
159 def test_info():
97 "Check that Inspector.info fills out various fields as expected."
160 "Check that Inspector.info fills out various fields as expected."
98 i = inspector.info(Call, oname='Call')
161 i = inspector.info(Call, oname='Call')
99 nt.assert_equal(i['type_name'], 'type')
162 nt.assert_equal(i['type_name'], 'type')
100 expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
163 expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
101 nt.assert_equal(i['base_class'], expted_class)
164 nt.assert_equal(i['base_class'], expted_class)
102 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
165 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
103 fname = __file__
166 fname = __file__
104 if fname.endswith(".pyc"):
167 if fname.endswith(".pyc"):
105 fname = fname[:-1]
168 fname = fname[:-1]
106 # case-insensitive comparison needed on some filesystems
169 # case-insensitive comparison needed on some filesystems
107 # e.g. Windows:
170 # e.g. Windows:
108 nt.assert_equal(i['file'].lower(), fname.lower())
171 nt.assert_equal(i['file'].lower(), fname.lower())
109 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
172 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
110 nt.assert_equal(i['docstring'], Call.__doc__)
173 nt.assert_equal(i['docstring'], Call.__doc__)
111 nt.assert_equal(i['source'], None)
174 nt.assert_equal(i['source'], None)
112 nt.assert_true(i['isclass'])
175 nt.assert_true(i['isclass'])
113 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
176 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
114 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
177 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
115
178
116 i = inspector.info(Call, detail_level=1)
179 i = inspector.info(Call, detail_level=1)
117 nt.assert_not_equal(i['source'], None)
180 nt.assert_not_equal(i['source'], None)
118 nt.assert_equal(i['docstring'], None)
181 nt.assert_equal(i['docstring'], None)
119
182
120 c = Call(1)
183 c = Call(1)
121 c.__doc__ = "Modified instance docstring"
184 c.__doc__ = "Modified instance docstring"
122 i = inspector.info(c)
185 i = inspector.info(c)
123 nt.assert_equal(i['type_name'], 'Call')
186 nt.assert_equal(i['type_name'], 'Call')
124 nt.assert_equal(i['docstring'], "Modified instance docstring")
187 nt.assert_equal(i['docstring'], "Modified instance docstring")
125 nt.assert_equal(i['class_docstring'], Call.__doc__)
188 nt.assert_equal(i['class_docstring'], Call.__doc__)
126 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
189 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
127 nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
190 nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
128
191
129 # Test old-style classes, which for example may not have an __init__ method.
192 # Test old-style classes, which for example may not have an __init__ method.
130 if not py3compat.PY3:
193 if not py3compat.PY3:
131 i = inspector.info(OldStyle)
194 i = inspector.info(OldStyle)
132 nt.assert_equal(i['type_name'], 'classobj')
195 nt.assert_equal(i['type_name'], 'classobj')
133
196
134 i = inspector.info(OldStyle())
197 i = inspector.info(OldStyle())
135 nt.assert_equal(i['type_name'], 'instance')
198 nt.assert_equal(i['type_name'], 'instance')
136 nt.assert_equal(i['docstring'], OldStyle.__doc__)
199 nt.assert_equal(i['docstring'], OldStyle.__doc__)
137
200
138 def test_getdoc():
201 def test_getdoc():
139 class A(object):
202 class A(object):
140 """standard docstring"""
203 """standard docstring"""
141 pass
204 pass
142
205
143 class B(object):
206 class B(object):
144 """standard docstring"""
207 """standard docstring"""
145 def getdoc(self):
208 def getdoc(self):
146 return "custom docstring"
209 return "custom docstring"
147
210
148 class C(object):
211 class C(object):
149 """standard docstring"""
212 """standard docstring"""
150 def getdoc(self):
213 def getdoc(self):
151 return None
214 return None
152
215
153 a = A()
216 a = A()
154 b = B()
217 b = B()
155 c = C()
218 c = C()
156
219
157 nt.assert_equal(oinspect.getdoc(a), "standard docstring")
220 nt.assert_equal(oinspect.getdoc(a), "standard docstring")
158 nt.assert_equal(oinspect.getdoc(b), "custom docstring")
221 nt.assert_equal(oinspect.getdoc(b), "custom docstring")
159 nt.assert_equal(oinspect.getdoc(c), "standard docstring")
222 nt.assert_equal(oinspect.getdoc(c), "standard docstring")
General Comments 0
You need to be logged in to leave comments. Login now