##// END OF EJS Templates
Fix debugger doctest to match atual output
Thomas Kluyver -
Show More
@@ -1,184 +1,186 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 t = 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',
128 8 'll',
127 9 'continue',
129 9 'continue',
128 10 ]):
130 10 ]):
129 ---> 11 trigger_ipdb()
131 ---> 11 trigger_ipdb()
130 <BLANKLINE>
132 <BLANKLINE>
131 ipdb> down
133 ipdb> down
132 None
134 None
133 > <doctest ...>(3)trigger_ipdb()
135 > <doctest ...>(3)trigger_ipdb()
134 1 def trigger_ipdb():
136 1 def trigger_ipdb():
135 2 a = ExampleClass()
137 2 a = ExampleClass()
136 ----> 3 debugger.Pdb().set_trace()
138 ----> 3 debugger.Pdb().set_trace()
137 <BLANKLINE>
139 <BLANKLINE>
138 ipdb> list
140 ipdb> list
139 1 def trigger_ipdb():
141 1 def trigger_ipdb():
140 2 a = ExampleClass()
142 2 a = ExampleClass()
141 ----> 3 debugger.Pdb().set_trace()
143 ----> 3 debugger.Pdb().set_trace()
142 <BLANKLINE>
144 <BLANKLINE>
143 ipdb> pinfo a
145 ipdb> pinfo a
144 Type: ExampleClass
146 Type: ExampleClass
145 String form: ExampleClass()
147 String form: ExampleClass()
146 Namespace: Local...
148 Namespace: Local...
147 Docstring: Docstring for ExampleClass.
149 Docstring: Docstring for ExampleClass.
148 Init docstring: Docstring for ExampleClass.__init__
150 Init docstring: Docstring for ExampleClass.__init__
149 ipdb> ll
151 ipdb> ll
150 1 def trigger_ipdb():
152 1 def trigger_ipdb():
151 2 a = ExampleClass()
153 2 a = ExampleClass()
152 ----> 3 debugger.Pdb().set_trace()
154 ----> 3 debugger.Pdb().set_trace()
153 <BLANKLINE>
155 <BLANKLINE>
154 ipdb> continue
156 ipdb> continue
155
157
156 Restore previous trace function, e.g. for coverage.py
158 Restore previous trace function, e.g. for coverage.py
157
159
158 >>> sys.settrace(old_trace)
160 >>> sys.settrace(old_trace)
159 '''
161 '''
160
162
161 def test_ipdb_magics2():
163 def test_ipdb_magics2():
162 '''Test ipdb with a very short function.
164 '''Test ipdb with a very short function.
163
165
164 >>> old_trace = sys.gettrace()
166 >>> old_trace = sys.gettrace()
165
167
166 >>> def bar():
168 >>> def bar():
167 ... pass
169 ... pass
168
170
169 Run ipdb.
171 Run ipdb.
170
172
171 >>> with PdbTestInput([
173 >>> with PdbTestInput([
172 ... 'continue',
174 ... 'continue',
173 ... ]):
175 ... ]):
174 ... debugger.Pdb().runcall(bar)
176 ... debugger.Pdb().runcall(bar)
175 > <doctest ...>(2)bar()
177 > <doctest ...>(2)bar()
176 1 def bar():
178 1 def bar():
177 ----> 2 pass
179 ----> 2 pass
178 <BLANKLINE>
180 <BLANKLINE>
179 ipdb> continue
181 ipdb> continue
180
182
181 Restore previous trace function, e.g. for coverage.py
183 Restore previous trace function, e.g. for coverage.py
182
184
183 >>> sys.settrace(old_trace)
185 >>> sys.settrace(old_trace)
184 '''
186 '''
General Comments 0
You need to be logged in to leave comments. Login now