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