##// END OF EJS Templates
Add test that quit and exit work in debugger....
Matthias Bussonnier -
Show More
@@ -1,186 +1,233 b''
1 """Tests for debugging machinery.
1 """Tests for debugging machinery.
2 """
2 """
3 from __future__ import print_function
3 from __future__ import print_function
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2012, The IPython Development Team.
5 # Copyright (c) 2012, The IPython Development Team.
6 #
6 #
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 #
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 import sys
16 import sys
17
17
18 # third-party
18 # third-party
19 import nose.tools as nt
19 import nose.tools as nt
20
20
21 # Our own
21 # Our own
22 from IPython.core import debugger
22 from IPython.core import debugger
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Helper classes, from CPython's Pdb test suite
25 # Helper classes, from CPython's Pdb test suite
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 class _FakeInput(object):
28 class _FakeInput(object):
29 """
29 """
30 A fake input stream for pdb's interactive debugger. Whenever a
30 A fake input stream for pdb's interactive debugger. Whenever a
31 line is read, print it (to simulate the user typing it), and then
31 line is read, print it (to simulate the user typing it), and then
32 return it. The set of lines to return is specified in the
32 return it. The set of lines to return is specified in the
33 constructor; they should not have trailing newlines.
33 constructor; they should not have trailing newlines.
34 """
34 """
35 def __init__(self, lines):
35 def __init__(self, lines):
36 self.lines = iter(lines)
36 self.lines = iter(lines)
37
37
38 def readline(self):
38 def readline(self):
39 line = next(self.lines)
39 line = next(self.lines)
40 print(line)
40 print(line)
41 return line+'\n'
41 return line+'\n'
42
42
43 class PdbTestInput(object):
43 class PdbTestInput(object):
44 """Context manager that makes testing Pdb in doctests easier."""
44 """Context manager that makes testing Pdb in doctests easier."""
45
45
46 def __init__(self, input):
46 def __init__(self, input):
47 self.input = input
47 self.input = input
48
48
49 def __enter__(self):
49 def __enter__(self):
50 self.real_stdin = sys.stdin
50 self.real_stdin = sys.stdin
51 sys.stdin = _FakeInput(self.input)
51 sys.stdin = _FakeInput(self.input)
52
52
53 def __exit__(self, *exc):
53 def __exit__(self, *exc):
54 sys.stdin = self.real_stdin
54 sys.stdin = self.real_stdin
55
55
56 #-----------------------------------------------------------------------------
56 #-----------------------------------------------------------------------------
57 # Tests
57 # Tests
58 #-----------------------------------------------------------------------------
58 #-----------------------------------------------------------------------------
59
59
60 def test_longer_repr():
60 def test_longer_repr():
61 try:
61 try:
62 from reprlib import repr as trepr # Py 3
62 from reprlib import repr as trepr # Py 3
63 except ImportError:
63 except ImportError:
64 from repr import repr as trepr # Py 2
64 from repr import repr as trepr # Py 2
65
65
66 a = '1234567890'* 7
66 a = '1234567890'* 7
67 ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'"
67 ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'"
68 a_trunc = "'123456789012...8901234567890'"
68 a_trunc = "'123456789012...8901234567890'"
69 nt.assert_equal(trepr(a), a_trunc)
69 nt.assert_equal(trepr(a), a_trunc)
70 # The creation of our tracer modifies the repr module's repr function
70 # The creation of our tracer modifies the repr module's repr function
71 # in-place, since that global is used directly by the stdlib's pdb module.
71 # in-place, since that global is used directly by the stdlib's pdb module.
72 t = debugger.Tracer()
72 debugger.Tracer()
73 nt.assert_equal(trepr(a), ar)
73 nt.assert_equal(trepr(a), ar)
74
74
75 def test_ipdb_magics():
75 def test_ipdb_magics():
76 '''Test calling some IPython magics from ipdb.
76 '''Test calling some IPython magics from ipdb.
77
77
78 First, set up some test functions and classes which we can inspect.
78 First, set up some test functions and classes which we can inspect.
79
79
80 >>> class ExampleClass(object):
80 >>> class ExampleClass(object):
81 ... """Docstring for ExampleClass."""
81 ... """Docstring for ExampleClass."""
82 ... def __init__(self):
82 ... def __init__(self):
83 ... """Docstring for ExampleClass.__init__"""
83 ... """Docstring for ExampleClass.__init__"""
84 ... pass
84 ... pass
85 ... def __str__(self):
85 ... def __str__(self):
86 ... return "ExampleClass()"
86 ... return "ExampleClass()"
87
87
88 >>> def example_function(x, y, z="hello"):
88 >>> def example_function(x, y, z="hello"):
89 ... """Docstring for example_function."""
89 ... """Docstring for example_function."""
90 ... pass
90 ... pass
91
91
92 >>> old_trace = sys.gettrace()
92 >>> old_trace = sys.gettrace()
93
93
94 Create a function which triggers ipdb.
94 Create a function which triggers ipdb.
95
95
96 >>> def trigger_ipdb():
96 >>> def trigger_ipdb():
97 ... a = ExampleClass()
97 ... a = ExampleClass()
98 ... debugger.Pdb().set_trace()
98 ... debugger.Pdb().set_trace()
99
99
100 >>> with PdbTestInput([
100 >>> with PdbTestInput([
101 ... 'pdef example_function',
101 ... 'pdef example_function',
102 ... 'pdoc ExampleClass',
102 ... 'pdoc ExampleClass',
103 ... 'up',
103 ... 'up',
104 ... 'down',
104 ... 'down',
105 ... 'list',
105 ... 'list',
106 ... 'pinfo a',
106 ... 'pinfo a',
107 ... 'll',
107 ... 'll',
108 ... 'continue',
108 ... 'continue',
109 ... ]):
109 ... ]):
110 ... trigger_ipdb()
110 ... trigger_ipdb()
111 --Return--
111 --Return--
112 None
112 None
113 > <doctest ...>(3)trigger_ipdb()
113 > <doctest ...>(3)trigger_ipdb()
114 1 def trigger_ipdb():
114 1 def trigger_ipdb():
115 2 a = ExampleClass()
115 2 a = ExampleClass()
116 ----> 3 debugger.Pdb().set_trace()
116 ----> 3 debugger.Pdb().set_trace()
117 <BLANKLINE>
117 <BLANKLINE>
118 ipdb> pdef example_function
118 ipdb> pdef example_function
119 example_function(x, y, z='hello')
119 example_function(x, y, z='hello')
120 ipdb> pdoc ExampleClass
120 ipdb> pdoc ExampleClass
121 Class docstring:
121 Class docstring:
122 Docstring for ExampleClass.
122 Docstring for ExampleClass.
123 Init docstring:
123 Init docstring:
124 Docstring for ExampleClass.__init__
124 Docstring for ExampleClass.__init__
125 ipdb> up
125 ipdb> up
126 > <doctest ...>(11)<module>()
126 > <doctest ...>(11)<module>()
127 7 'pinfo a',
127 7 'pinfo a',
128 8 'll',
128 8 'll',
129 9 'continue',
129 9 'continue',
130 10 ]):
130 10 ]):
131 ---> 11 trigger_ipdb()
131 ---> 11 trigger_ipdb()
132 <BLANKLINE>
132 <BLANKLINE>
133 ipdb> down
133 ipdb> down
134 None
134 None
135 > <doctest ...>(3)trigger_ipdb()
135 > <doctest ...>(3)trigger_ipdb()
136 1 def trigger_ipdb():
136 1 def trigger_ipdb():
137 2 a = ExampleClass()
137 2 a = ExampleClass()
138 ----> 3 debugger.Pdb().set_trace()
138 ----> 3 debugger.Pdb().set_trace()
139 <BLANKLINE>
139 <BLANKLINE>
140 ipdb> list
140 ipdb> list
141 1 def trigger_ipdb():
141 1 def trigger_ipdb():
142 2 a = ExampleClass()
142 2 a = ExampleClass()
143 ----> 3 debugger.Pdb().set_trace()
143 ----> 3 debugger.Pdb().set_trace()
144 <BLANKLINE>
144 <BLANKLINE>
145 ipdb> pinfo a
145 ipdb> pinfo a
146 Type: ExampleClass
146 Type: ExampleClass
147 String form: ExampleClass()
147 String form: ExampleClass()
148 Namespace: Local...
148 Namespace: Local...
149 Docstring: Docstring for ExampleClass.
149 Docstring: Docstring for ExampleClass.
150 Init docstring: Docstring for ExampleClass.__init__
150 Init docstring: Docstring for ExampleClass.__init__
151 ipdb> ll
151 ipdb> ll
152 1 def trigger_ipdb():
152 1 def trigger_ipdb():
153 2 a = ExampleClass()
153 2 a = ExampleClass()
154 ----> 3 debugger.Pdb().set_trace()
154 ----> 3 debugger.Pdb().set_trace()
155 <BLANKLINE>
155 <BLANKLINE>
156 ipdb> continue
156 ipdb> continue
157
157
158 Restore previous trace function, e.g. for coverage.py
158 Restore previous trace function, e.g. for coverage.py
159
159
160 >>> sys.settrace(old_trace)
160 >>> sys.settrace(old_trace)
161 '''
161 '''
162
162
163 def test_ipdb_magics2():
163 def test_ipdb_magics2():
164 '''Test ipdb with a very short function.
164 '''Test ipdb with a very short function.
165
165
166 >>> old_trace = sys.gettrace()
166 >>> old_trace = sys.gettrace()
167
167
168 >>> def bar():
168 >>> def bar():
169 ... pass
169 ... pass
170
170
171 Run ipdb.
171 Run ipdb.
172
172
173 >>> with PdbTestInput([
173 >>> with PdbTestInput([
174 ... 'continue',
174 ... 'continue',
175 ... ]):
175 ... ]):
176 ... debugger.Pdb().runcall(bar)
176 ... debugger.Pdb().runcall(bar)
177 > <doctest ...>(2)bar()
177 > <doctest ...>(2)bar()
178 1 def bar():
178 1 def bar():
179 ----> 2 pass
179 ----> 2 pass
180 <BLANKLINE>
180 <BLANKLINE>
181 ipdb> continue
181 ipdb> continue
182
182
183 Restore previous trace function, e.g. for coverage.py
183 Restore previous trace function, e.g. for coverage.py
184
184
185 >>> sys.settrace(old_trace)
185 >>> sys.settrace(old_trace)
186 '''
186 '''
187
188 def can_quit():
189 '''Test that quit work in ipydb
190
191 >>> old_trace = sys.gettrace()
192
193 >>> def bar():
194 ... pass
195
196 >>> with PdbTestInput([
197 ... 'quit',
198 ... ]):
199 ... debugger.Pdb().runcall(bar)
200 > <doctest ...>(2)bar()
201 1 def bar():
202 ----> 2 pass
203 <BLANKLINE>
204 ipdb> quit
205
206 Restore previous trace function, e.g. for coverage.py
207
208 >>> sys.settrace(old_trace)
209 '''
210
211
212 def can_exit():
213 '''Test that quit work in ipydb
214
215 >>> old_trace = sys.gettrace()
216
217 >>> def bar():
218 ... pass
219
220 >>> with PdbTestInput([
221 ... 'exit',
222 ... ]):
223 ... debugger.Pdb().runcall(bar)
224 > <doctest ...>(2)bar()
225 1 def bar():
226 ----> 2 pass
227 <BLANKLINE>
228 ipdb> exit
229
230 Restore previous trace function, e.g. for coverage.py
231
232 >>> sys.settrace(old_trace)
233 '''
General Comments 0
You need to be logged in to leave comments. Login now