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