##// END OF EJS Templates
[core][tests][oinspect] Remove nose
Samuel Gaist -
Show More
@@ -9,8 +9,6 b' from inspect import signature, Signature, Parameter'
9 import os
9 import os
10 import re
10 import re
11
11
12 import nose.tools as nt
13
14 from .. import oinspect
12 from .. import oinspect
15
13
16 from decorator import decorator
14 from decorator import decorator
@@ -38,7 +36,7 b' def setup_module():'
38 # defined, if any code is inserted above, the following line will need to be
36 # defined, if any code is inserted above, the following line will need to be
39 # updated. Do NOT insert any whitespace between the next line and the function
37 # updated. Do NOT insert any whitespace between the next line and the function
40 # definition below.
38 # definition below.
41 THIS_LINE_NUMBER = 41 # Put here the actual number of this line
39 THIS_LINE_NUMBER = 39 # Put here the actual number of this line
42
40
43 from unittest import TestCase
41 from unittest import TestCase
44
42
@@ -74,7 +72,7 b' def test_find_file_decorated1():'
74 @noop1
72 @noop1
75 def f(x):
73 def f(x):
76 "My docstring"
74 "My docstring"
77
75
78 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
76 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
79 assert f.__doc__ == "My docstring"
77 assert f.__doc__ == "My docstring"
80
78
@@ -90,14 +88,14 b' def test_find_file_decorated2():'
90 @noop2
88 @noop2
91 def f(x):
89 def f(x):
92 "My docstring 2"
90 "My docstring 2"
93
91
94 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
92 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
95 assert f.__doc__ == "My docstring 2"
93 assert f.__doc__ == "My docstring 2"
96
94
97
95
98 def test_find_file_magic():
96 def test_find_file_magic():
99 run = ip.find_line_magic('run')
97 run = ip.find_line_magic('run')
100 nt.assert_not_equal(oinspect.find_file(run), None)
98 assert oinspect.find_file(run) is not None
101
99
102
100
103 # A few generic objects we can then inspect in the tests below
101 # A few generic objects we can then inspect in the tests below
@@ -169,11 +167,11 b' def test_info():'
169 "Check that Inspector.info fills out various fields as expected."
167 "Check that Inspector.info fills out various fields as expected."
170 i = inspector.info(Call, oname="Call")
168 i = inspector.info(Call, oname="Call")
171 assert i["type_name"] == "type"
169 assert i["type_name"] == "type"
172 expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
170 expected_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
173 assert i["base_class"] == expted_class
171 assert i["base_class"] == expected_class
174 nt.assert_regex(
172 assert re.search(
175 i["string_form"],
176 "<class 'IPython.core.tests.test_oinspect.Call'( at 0x[0-9a-f]{1,9})?>",
173 "<class 'IPython.core.tests.test_oinspect.Call'( at 0x[0-9a-f]{1,9})?>",
174 i["string_form"],
177 )
175 )
178 fname = __file__
176 fname = __file__
179 if fname.endswith(".pyc"):
177 if fname.endswith(".pyc"):
@@ -184,12 +182,12 b' def test_info():'
184 assert i["definition"] == None
182 assert i["definition"] == None
185 assert i["docstring"] == Call.__doc__
183 assert i["docstring"] == Call.__doc__
186 assert i["source"] == None
184 assert i["source"] == None
187 nt.assert_true(i["isclass"])
185 assert i["isclass"] is True
188 assert i["init_definition"] == "Call(x, y=1)"
186 assert i["init_definition"] == "Call(x, y=1)"
189 assert i["init_docstring"] == Call.__init__.__doc__
187 assert i["init_docstring"] == Call.__init__.__doc__
190
188
191 i = inspector.info(Call, detail_level=1)
189 i = inspector.info(Call, detail_level=1)
192 nt.assert_not_equal(i["source"], None)
190 assert i["source"] is not None
193 assert i["docstring"] == None
191 assert i["docstring"] == None
194
192
195 c = Call(1)
193 c = Call(1)
@@ -221,7 +219,7 b' def test_info_serialliar():'
221
219
222 # Nested attribute access should be cut off at 100 levels deep to avoid
220 # Nested attribute access should be cut off at 100 levels deep to avoid
223 # infinite loops: https://github.com/ipython/ipython/issues/9122
221 # infinite loops: https://github.com/ipython/ipython/issues/9122
224 nt.assert_less(fib_tracker[0], 9000)
222 assert fib_tracker[0] < 9000
225
223
226 def support_function_one(x, y=2, *a, **kw):
224 def support_function_one(x, y=2, *a, **kw):
227 """A simple function."""
225 """A simple function."""
@@ -230,7 +228,8 b' def test_calldef_none():'
230 # We should ignore __call__ for all of these.
228 # We should ignore __call__ for all of these.
231 for obj in [support_function_one, SimpleClass().method, any, str.upper]:
229 for obj in [support_function_one, SimpleClass().method, any, str.upper]:
232 i = inspector.info(obj)
230 i = inspector.info(obj)
233 nt.assert_is(i['call_def'], None)
231 assert i["call_def"] is None
232
234
233
235 def f_kwarg(pos, *, kwonly):
234 def f_kwarg(pos, *, kwonly):
236 pass
235 pass
@@ -249,16 +248,16 b' def test_getdoc():'
249 """standard docstring"""
248 """standard docstring"""
250 def getdoc(self):
249 def getdoc(self):
251 return "custom docstring"
250 return "custom docstring"
252
251
253 class C(object):
252 class C(object):
254 """standard docstring"""
253 """standard docstring"""
255 def getdoc(self):
254 def getdoc(self):
256 return None
255 return None
257
256
258 a = A()
257 a = A()
259 b = B()
258 b = B()
260 c = C()
259 c = C()
261
260
262 assert oinspect.getdoc(a) == "standard docstring"
261 assert oinspect.getdoc(a) == "standard docstring"
263 assert oinspect.getdoc(b) == "custom docstring"
262 assert oinspect.getdoc(b) == "custom docstring"
264 assert oinspect.getdoc(c) == "standard docstring"
263 assert oinspect.getdoc(c) == "standard docstring"
@@ -266,17 +265,17 b' def test_getdoc():'
266
265
267 def test_empty_property_has_no_source():
266 def test_empty_property_has_no_source():
268 i = inspector.info(property(), detail_level=1)
267 i = inspector.info(property(), detail_level=1)
269 nt.assert_is(i['source'], None)
268 assert i["source"] is None
270
269
271
270
272 def test_property_sources():
271 def test_property_sources():
273 import posixpath
272 import posixpath
274 # A simple adder whose source and signature stays
273 # A simple adder whose source and signature stays
275 # the same across Python distributions
274 # the same across Python distributions
276 def simple_add(a, b):
275 def simple_add(a, b):
277 "Adds two numbers"
276 "Adds two numbers"
278 return a + b
277 return a + b
279
278
280 class A(object):
279 class A(object):
281 @property
280 @property
282 def foo(self):
281 def foo(self):
@@ -285,17 +284,17 b' def test_property_sources():'
285 foo = foo.setter(lambda self, v: setattr(self, 'bar', v))
284 foo = foo.setter(lambda self, v: setattr(self, 'bar', v))
286
285
287 dname = property(posixpath.dirname)
286 dname = property(posixpath.dirname)
288 adder = property(simple_add)
287 adder = property(simple_add)
289
288
290 i = inspector.info(A.foo, detail_level=1)
289 i = inspector.info(A.foo, detail_level=1)
291 nt.assert_in('def foo(self):', i['source'])
290 assert "def foo(self):" in i["source"]
292 nt.assert_in('lambda self, v:', i['source'])
291 assert "lambda self, v:" in i["source"]
293
292
294 i = inspector.info(A.dname, detail_level=1)
293 i = inspector.info(A.dname, detail_level=1)
295 nt.assert_in('def dirname(p)', i['source'])
294 assert "def dirname(p)" in i["source"]
296
295
297 i = inspector.info(A.adder, detail_level=1)
296 i = inspector.info(A.adder, detail_level=1)
298 nt.assert_in('def simple_add(a, b)', i['source'])
297 assert "def simple_add(a, b)" in i["source"]
299
298
300
299
301 def test_property_docstring_is_in_info_for_detail_level_0():
300 def test_property_docstring_is_in_info_for_detail_level_0():
@@ -367,11 +366,11 b' def test_pinfo_docstring_if_detail_and_no_source():'
367 def bar(self):
366 def bar(self):
368 """ This is a docstring for Foo.bar """
367 """ This is a docstring for Foo.bar """
369 pass
368 pass
370 '''
369 '''
371
370
372 ip.run_cell(obj_def)
371 ip.run_cell(obj_def)
373 ip.run_cell('foo = Foo()')
372 ip.run_cell('foo = Foo()')
374
373
375 with AssertNotPrints("Source:"):
374 with AssertNotPrints("Source:"):
376 with AssertPrints('Docstring:'):
375 with AssertPrints('Docstring:'):
377 ip._inspect('pinfo', 'foo', detail_level=0)
376 ip._inspect('pinfo', 'foo', detail_level=0)
@@ -396,14 +395,14 b' def test_pinfo_magic():'
396 def test_init_colors():
395 def test_init_colors():
397 # ensure colors are not present in signature info
396 # ensure colors are not present in signature info
398 info = inspector.info(HasSignature)
397 info = inspector.info(HasSignature)
399 init_def = info['init_definition']
398 init_def = info["init_definition"]
400 nt.assert_not_in('[0m', init_def)
399 assert "[0m" not in init_def
401
400
402
401
403 def test_builtin_init():
402 def test_builtin_init():
404 info = inspector.info(list)
403 info = inspector.info(list)
405 init_def = info['init_definition']
404 init_def = info['init_definition']
406 nt.assert_is_not_none(init_def)
405 assert init_def is not None
407
406
408
407
409 def test_render_signature_short():
408 def test_render_signature_short():
@@ -428,7 +427,7 b' def test_render_signature_long():'
428 signature(long_function),
427 signature(long_function),
429 long_function.__name__,
428 long_function.__name__,
430 )
429 )
431 nt.assert_in(sig, [
430 assert sig in [
432 # Python >=3.9
431 # Python >=3.9
433 '''\
432 '''\
434 long_function(
433 long_function(
@@ -452,4 +451,4 b' long_function('
452 let_us_make_sure_this_is_looong:Union[str, NoneType]=None,
451 let_us_make_sure_this_is_looong:Union[str, NoneType]=None,
453 ) -> bool\
452 ) -> bool\
454 ''',
453 ''',
455 ])
454 ]
General Comments 0
You need to be logged in to leave comments. Login now