Show More
@@ -1,133 +1,136 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 | from IPython.utils import py3compat | |||
23 |
|
24 | |||
24 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
25 | # Globals and constants |
|
26 | # Globals and constants | |
26 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
27 |
|
28 | |||
28 | inspector = oinspect.Inspector() |
|
29 | inspector = oinspect.Inspector() | |
29 |
|
30 | |||
30 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
31 | # Local utilities |
|
32 | # Local utilities | |
32 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
33 |
|
34 | |||
34 | # A few generic objects we can then inspect in the tests below |
|
35 | # A few generic objects we can then inspect in the tests below | |
35 |
|
36 | |||
36 | class Call(object): |
|
37 | class Call(object): | |
37 | """This is the class docstring.""" |
|
38 | """This is the class docstring.""" | |
38 |
|
39 | |||
39 | def __init__(self, x, y=1): |
|
40 | def __init__(self, x, y=1): | |
40 | """This is the constructor docstring.""" |
|
41 | """This is the constructor docstring.""" | |
41 |
|
42 | |||
42 | def __call__(self, *a, **kw): |
|
43 | def __call__(self, *a, **kw): | |
43 | """This is the call docstring.""" |
|
44 | """This is the call docstring.""" | |
44 |
|
45 | |||
45 | def method(self, x, z=2): |
|
46 | def method(self, x, z=2): | |
46 | """Some method's docstring""" |
|
47 | """Some method's docstring""" | |
47 |
|
48 | |||
48 | class OldStyle: |
|
49 | class OldStyle: | |
49 | """An old-style class for testing.""" |
|
50 | """An old-style class for testing.""" | |
50 | pass |
|
51 | pass | |
51 |
|
52 | |||
52 | def f(x, y=2, *a, **kw): |
|
53 | def f(x, y=2, *a, **kw): | |
53 | """A simple function.""" |
|
54 | """A simple function.""" | |
54 |
|
55 | |||
55 | def g(y, z=3, *a, **kw): |
|
56 | def g(y, z=3, *a, **kw): | |
56 | pass # no docstring |
|
57 | pass # no docstring | |
57 |
|
58 | |||
58 |
|
59 | |||
59 | def check_calltip(obj, name, call, docstring): |
|
60 | def check_calltip(obj, name, call, docstring): | |
60 | """Generic check pattern all calltip tests will use""" |
|
61 | """Generic check pattern all calltip tests will use""" | |
61 | info = inspector.info(obj, name) |
|
62 | info = inspector.info(obj, name) | |
62 | call_line, ds = oinspect.call_tip(info) |
|
63 | call_line, ds = oinspect.call_tip(info) | |
63 | nt.assert_equal(call_line, call) |
|
64 | nt.assert_equal(call_line, call) | |
64 | nt.assert_equal(ds, docstring) |
|
65 | nt.assert_equal(ds, docstring) | |
65 |
|
66 | |||
66 | #----------------------------------------------------------------------------- |
|
67 | #----------------------------------------------------------------------------- | |
67 | # Tests |
|
68 | # Tests | |
68 | #----------------------------------------------------------------------------- |
|
69 | #----------------------------------------------------------------------------- | |
69 |
|
70 | |||
70 | def test_calltip_class(): |
|
71 | def test_calltip_class(): | |
71 | check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__) |
|
72 | check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__) | |
72 |
|
73 | |||
73 |
|
74 | |||
74 | def test_calltip_instance(): |
|
75 | def test_calltip_instance(): | |
75 | c = Call(1) |
|
76 | c = Call(1) | |
76 | check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__) |
|
77 | check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__) | |
77 |
|
78 | |||
78 |
|
79 | |||
79 | def test_calltip_method(): |
|
80 | def test_calltip_method(): | |
80 | c = Call(1) |
|
81 | c = Call(1) | |
81 | check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__) |
|
82 | check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__) | |
82 |
|
83 | |||
83 |
|
84 | |||
84 | def test_calltip_function(): |
|
85 | def test_calltip_function(): | |
85 | check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__) |
|
86 | check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__) | |
86 |
|
87 | |||
87 |
|
88 | |||
88 | def test_calltip_function2(): |
|
89 | def test_calltip_function2(): | |
89 | check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>') |
|
90 | check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>') | |
90 |
|
91 | |||
91 |
|
92 | |||
92 | def test_calltip_builtin(): |
|
93 | def test_calltip_builtin(): | |
93 | check_calltip(sum, 'sum', None, sum.__doc__) |
|
94 | check_calltip(sum, 'sum', None, sum.__doc__) | |
94 |
|
95 | |||
95 | def test_info(): |
|
96 | def test_info(): | |
96 | "Check that Inspector.info fills out various fields as expected." |
|
97 | "Check that Inspector.info fills out various fields as expected." | |
97 | i = inspector.info(Call, oname='Call') |
|
98 | i = inspector.info(Call, oname='Call') | |
98 | nt.assert_equal(i['type_name'], 'type') |
|
99 | nt.assert_equal(i['type_name'], 'type') | |
99 | nt.assert_equal(i['base_class'], "<type 'type'>") |
|
100 | expted_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'> | |
|
101 | nt.assert_equal(i['base_class'], expted_class) | |||
100 | nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>") |
|
102 | nt.assert_equal(i['string_form'], "<class 'IPython.core.tests.test_oinspect.Call'>") | |
101 | fname = __file__ |
|
103 | fname = __file__ | |
102 | if fname.endswith(".pyc"): |
|
104 | if fname.endswith(".pyc"): | |
103 | fname = fname[:-1] |
|
105 | fname = fname[:-1] | |
104 | # case-insensitive comparison needed on some filesystems |
|
106 | # case-insensitive comparison needed on some filesystems | |
105 | # e.g. Windows: |
|
107 | # e.g. Windows: | |
106 | nt.assert_equal(i['file'].lower(), fname.lower()) |
|
108 | nt.assert_equal(i['file'].lower(), fname.lower()) | |
107 | nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n') |
|
109 | nt.assert_equal(i['definition'], 'Call(self, *a, **kw)\n') | |
108 | nt.assert_equal(i['docstring'], Call.__doc__) |
|
110 | nt.assert_equal(i['docstring'], Call.__doc__) | |
109 | nt.assert_equal(i['source'], None) |
|
111 | nt.assert_equal(i['source'], None) | |
110 | nt.assert_true(i['isclass']) |
|
112 | nt.assert_true(i['isclass']) | |
111 | nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n") |
|
113 | nt.assert_equal(i['init_definition'], "Call(self, x, y=1)\n") | |
112 | nt.assert_equal(i['init_docstring'], Call.__init__.__doc__) |
|
114 | nt.assert_equal(i['init_docstring'], Call.__init__.__doc__) | |
113 |
|
115 | |||
114 | i = inspector.info(Call, detail_level=1) |
|
116 | i = inspector.info(Call, detail_level=1) | |
115 | nt.assert_not_equal(i['source'], None) |
|
117 | nt.assert_not_equal(i['source'], None) | |
116 | nt.assert_equal(i['docstring'], None) |
|
118 | nt.assert_equal(i['docstring'], None) | |
117 |
|
119 | |||
118 | c = Call(1) |
|
120 | c = Call(1) | |
119 | c.__doc__ = "Modified instance docstring" |
|
121 | c.__doc__ = "Modified instance docstring" | |
120 | i = inspector.info(c) |
|
122 | i = inspector.info(c) | |
121 | nt.assert_equal(i['type_name'], 'Call') |
|
123 | nt.assert_equal(i['type_name'], 'Call') | |
122 | nt.assert_equal(i['docstring'], "Modified instance docstring") |
|
124 | nt.assert_equal(i['docstring'], "Modified instance docstring") | |
123 | nt.assert_equal(i['class_docstring'], Call.__doc__) |
|
125 | nt.assert_equal(i['class_docstring'], Call.__doc__) | |
124 | nt.assert_equal(i['init_docstring'], Call.__init__.__doc__) |
|
126 | nt.assert_equal(i['init_docstring'], Call.__init__.__doc__) | |
125 | nt.assert_equal(i['call_docstring'], c.__call__.__doc__) |
|
127 | nt.assert_equal(i['call_docstring'], c.__call__.__doc__) | |
126 |
|
128 | |||
127 | # Test old-style classes, which for example may not have an __init__ method. |
|
129 | # Test old-style classes, which for example may not have an __init__ method. | |
|
130 | if not py3compat.PY3: | |||
128 | i = inspector.info(OldStyle) |
|
131 | i = inspector.info(OldStyle) | |
129 | nt.assert_equal(i['type_name'], 'classobj') |
|
132 | nt.assert_equal(i['type_name'], 'classobj') | |
130 |
|
133 | |||
131 | i = inspector.info(OldStyle()) |
|
134 | i = inspector.info(OldStyle()) | |
132 | nt.assert_equal(i['type_name'], 'instance') |
|
135 | nt.assert_equal(i['type_name'], 'instance') | |
133 | nt.assert_equal(i['docstring'], OldStyle.__doc__) |
|
136 | nt.assert_equal(i['docstring'], OldStyle.__doc__) |
General Comments 0
You need to be logged in to leave comments.
Login now