##// END OF EJS Templates
Fix failure due to .pyc/.py mismatch detected by test_pr.
Fernando Perez -
Show More
@@ -1,277 +1,285 b''
1 1 """Tests for the object inspection functionality.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (C) 2010-2011 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14 from __future__ import print_function
15 15
16 16 # Stdlib imports
17 17 import os
18 import re
18 19
19 20 # Third-party imports
20 21 import nose.tools as nt
21 22
22 23 # Our own imports
23 24 from .. import oinspect
24 25 from IPython.core.magic import (Magics, magics_class, line_magic,
25 26 cell_magic, line_cell_magic,
26 27 register_line_magic, register_cell_magic,
27 28 register_line_cell_magic)
28 29 from IPython.external.decorator import decorator
29 30 from IPython.utils import py3compat
30 31
31 32
32 33 #-----------------------------------------------------------------------------
33 34 # Globals and constants
34 35 #-----------------------------------------------------------------------------
35 36
36 37 inspector = oinspect.Inspector()
37 38 ip = get_ipython()
38 39
39 40 #-----------------------------------------------------------------------------
40 41 # Local utilities
41 42 #-----------------------------------------------------------------------------
42 43
43 44 # WARNING: since this test checks the line number where a function is
44 45 # defined, if any code is inserted above, the following line will need to be
45 46 # updated. Do NOT insert any whitespace between the next line and the function
46 47 # definition below.
47 THIS_LINE_NUMBER = 47 # Put here the actual number of this line
48 THIS_LINE_NUMBER = 48 # Put here the actual number of this line
48 49 def test_find_source_lines():
49 50 nt.assert_equal(oinspect.find_source_lines(test_find_source_lines),
50 51 THIS_LINE_NUMBER+1)
51 52
52 53
54 # A couple of utilities to ensure these tests work the same from a source or a
55 # binary install
56 def pyfile(fname):
57 return re.sub('.py[co]$', '.py', fname)
58
59
60 def match_pyfiles(f1, f2):
61 nt.assert_equal(pyfile(f1), pyfile(f2))
62
63
53 64 def test_find_file():
54 nt.assert_equal(oinspect.find_file(test_find_file),
55 os.path.abspath(__file__))
65 match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))
56 66
57 67
58 68 def test_find_file_decorated1():
59 69
60 70 @decorator
61 71 def noop1(f):
62 72 def wrapper():
63 73 return f(*a, **kw)
64 74 return wrapper
65 75
66 76 @noop1
67 77 def f(x):
68 78 "My docstring"
69 79
70 nt.assert_equal(oinspect.find_file(f),
71 os.path.abspath(__file__))
80 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
72 81 nt.assert_equal(f.__doc__, "My docstring")
73 82
74 83
75 84 def test_find_file_decorated2():
76 85
77 86 @decorator
78 87 def noop2(f, *a, **kw):
79 88 return f(*a, **kw)
80 89
81 90 @noop2
82 91 def f(x):
83 92 "My docstring 2"
84 93
85 nt.assert_equal(oinspect.find_file(f),
86 os.path.abspath(__file__))
94 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
87 95 nt.assert_equal(f.__doc__, "My docstring 2")
88 96
89 97
90 98 def test_find_file_magic():
91 99 run = ip.find_line_magic('run')
92 100 nt.assert_not_equal(oinspect.find_file(run), None)
93 101
94 102
95 103 # A few generic objects we can then inspect in the tests below
96 104
97 105 class Call(object):
98 106 """This is the class docstring."""
99 107
100 108 def __init__(self, x, y=1):
101 109 """This is the constructor docstring."""
102 110
103 111 def __call__(self, *a, **kw):
104 112 """This is the call docstring."""
105 113
106 114 def method(self, x, z=2):
107 115 """Some method's docstring"""
108 116
109 117
110 118 class OldStyle:
111 119 """An old-style class for testing."""
112 120 pass
113 121
114 122
115 123 def f(x, y=2, *a, **kw):
116 124 """A simple function."""
117 125
118 126
119 127 def g(y, z=3, *a, **kw):
120 128 pass # no docstring
121 129
122 130
123 131 @register_line_magic
124 132 def lmagic(line):
125 133 "A line magic"
126 134
127 135
128 136 @register_cell_magic
129 137 def cmagic(line, cell):
130 138 "A cell magic"
131 139
132 140
133 141 @register_line_cell_magic
134 142 def lcmagic(line, cell=None):
135 143 "A line/cell magic"
136 144
137 145
138 146 @magics_class
139 147 class SimpleMagics(Magics):
140 148 @line_magic
141 149 def Clmagic(self, cline):
142 150 "A class-based line magic"
143 151
144 152 @cell_magic
145 153 def Ccmagic(self, cline, ccell):
146 154 "A class-based cell magic"
147 155
148 156 @line_cell_magic
149 157 def Clcmagic(self, cline, ccell=None):
150 158 "A class-based line/cell magic"
151 159
152 160
153 161 def check_calltip(obj, name, call, docstring):
154 162 """Generic check pattern all calltip tests will use"""
155 163 info = inspector.info(obj, name)
156 164 call_line, ds = oinspect.call_tip(info)
157 165 nt.assert_equal(call_line, call)
158 166 nt.assert_equal(ds, docstring)
159 167
160 168 #-----------------------------------------------------------------------------
161 169 # Tests
162 170 #-----------------------------------------------------------------------------
163 171
164 172 def test_calltip_class():
165 173 check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__)
166 174
167 175
168 176 def test_calltip_instance():
169 177 c = Call(1)
170 178 check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__)
171 179
172 180
173 181 def test_calltip_method():
174 182 c = Call(1)
175 183 check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__)
176 184
177 185
178 186 def test_calltip_function():
179 187 check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__)
180 188
181 189
182 190 def test_calltip_function2():
183 191 check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>')
184 192
185 193
186 194 def test_calltip_builtin():
187 195 check_calltip(sum, 'sum', None, sum.__doc__)
188 196
189 197
190 198 def test_calltip_line_magic():
191 199 check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic")
192 200
193 201
194 202 def test_calltip_cell_magic():
195 203 check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic")
196 204
197 205
198 206 def test_calltip_line_magic():
199 207 check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)',
200 208 "A line/cell magic")
201 209
202 210
203 211 def test_class_magics():
204 212 cm = SimpleMagics(ip)
205 213 ip.register_magics(cm)
206 214 check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)',
207 215 "A class-based line magic")
208 216 check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)',
209 217 "A class-based cell magic")
210 218 check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)',
211 219 "A class-based line/cell magic")
212 220
213 221
214 222 def test_info():
215 223 "Check that Inspector.info fills out various fields as expected."
216 224 i = inspector.info(Call, oname='Call')
217 225 nt.assert_equal(i['type_name'], 'type')
218 226 expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
219 227 nt.assert_equal(i['base_class'], expted_class)
220 228 nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>")
221 229 fname = __file__
222 230 if fname.endswith(".pyc"):
223 231 fname = fname[:-1]
224 232 # case-insensitive comparison needed on some filesystems
225 233 # e.g. Windows:
226 234 nt.assert_equal(i['file'].lower(), fname.lower())
227 235 nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n')
228 236 nt.assert_equal(i['docstring'], Call.__doc__)
229 237 nt.assert_equal(i['source'], None)
230 238 nt.assert_true(i['isclass'])
231 239 nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n")
232 240 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
233 241
234 242 i = inspector.info(Call, detail_level=1)
235 243 nt.assert_not_equal(i['source'], None)
236 244 nt.assert_equal(i['docstring'], None)
237 245
238 246 c = Call(1)
239 247 c.__doc__ = "Modified instance docstring"
240 248 i = inspector.info(c)
241 249 nt.assert_equal(i['type_name'], 'Call')
242 250 nt.assert_equal(i['docstring'], "Modified instance docstring")
243 251 nt.assert_equal(i['class_docstring'], Call.__doc__)
244 252 nt.assert_equal(i['init_docstring'], Call.__init__.__doc__)
245 253 nt.assert_equal(i['call_docstring'], c.__call__.__doc__)
246 254
247 255 # Test old-style classes, which for example may not have an __init__ method.
248 256 if not py3compat.PY3:
249 257 i = inspector.info(OldStyle)
250 258 nt.assert_equal(i['type_name'], 'classobj')
251 259
252 260 i = inspector.info(OldStyle())
253 261 nt.assert_equal(i['type_name'], 'instance')
254 262 nt.assert_equal(i['docstring'], OldStyle.__doc__)
255 263
256 264 def test_getdoc():
257 265 class A(object):
258 266 """standard docstring"""
259 267 pass
260 268
261 269 class B(object):
262 270 """standard docstring"""
263 271 def getdoc(self):
264 272 return "custom docstring"
265 273
266 274 class C(object):
267 275 """standard docstring"""
268 276 def getdoc(self):
269 277 return None
270 278
271 279 a = A()
272 280 b = B()
273 281 c = C()
274 282
275 283 nt.assert_equal(oinspect.getdoc(a), "standard docstring")
276 284 nt.assert_equal(oinspect.getdoc(b), "custom docstring")
277 285 nt.assert_equal(oinspect.getdoc(c), "standard docstring")
General Comments 0
You need to be logged in to leave comments. Login now