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