blob: 00ff4b7eeb0b2f758bd4f5e17ed6e3022d5b2499 [file] [log] [blame]
Georg Brandl46b9afc2010-07-30 09:14:20 +00001# A test suite for pdb; not very comprehensive at the moment.
Georg Brandl243ad662009-05-05 09:00:19 +00002
3import imp
Georg Brandl243ad662009-05-05 09:00:19 +00004import sys
Georg Brandl243ad662009-05-05 09:00:19 +00005
6from test import support
7# This little helper class is essential for testing pdb under doctest.
8from test.test_doctest import _FakeInput
9
10
Georg Brandl9fa2e022009-09-16 16:40:45 +000011class PdbTestInput(object):
12 """Context manager that makes testing Pdb in doctests easier."""
13
14 def __init__(self, input):
15 self.input = input
16
17 def __enter__(self):
18 self.real_stdin = sys.stdin
19 sys.stdin = _FakeInput(self.input)
20
21 def __exit__(self, *exc):
22 sys.stdin = self.real_stdin
23
24
25def test_pdb_displayhook():
26 """This tests the custom displayhook for pdb.
27
28 >>> def test_function(foo, bar):
29 ... import pdb; pdb.Pdb().set_trace()
30 ... pass
31
32 >>> with PdbTestInput([
33 ... 'foo',
34 ... 'bar',
35 ... 'for i in range(5): print(i)',
36 ... 'continue',
37 ... ]):
38 ... test_function(1, None)
39 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
40 -> pass
41 (Pdb) foo
42 1
43 (Pdb) bar
44 (Pdb) for i in range(5): print(i)
45 0
46 1
47 2
48 3
49 4
50 (Pdb) continue
51 """
52
53
Georg Brandl243ad662009-05-05 09:00:19 +000054def test_pdb_skip_modules():
55 """This illustrates the simple case of module skipping.
56
57 >>> def skip_module():
58 ... import string
59 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
60 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +000061
Georg Brandl9fa2e022009-09-16 16:40:45 +000062 >>> with PdbTestInput([
63 ... 'step',
64 ... 'continue',
65 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +000066 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +000067 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
68 -> string.capwords('FOO')
69 (Pdb) step
70 --Return--
71 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
72 -> string.capwords('FOO')
73 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +000074 """
Georg Brandl243ad662009-05-05 09:00:19 +000075
76
77# Module for testing skipping of module that makes a callback
78mod = imp.new_module('module_to_skip')
79exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
80
81
82def test_pdb_skip_modules_with_callback():
83 """This illustrates skipping of modules that call into other code.
84
85 >>> def skip_module():
86 ... def callback():
87 ... return None
Georg Brandl9fa2e022009-09-16 16:40:45 +000088 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +000089 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +000090
Georg Brandl9fa2e022009-09-16 16:40:45 +000091 >>> with PdbTestInput([
92 ... 'step',
93 ... 'step',
94 ... 'step',
95 ... 'step',
96 ... 'step',
97 ... 'continue',
98 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +000099 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000100 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000101 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
102 -> mod.foo_pony(callback)
103 (Pdb) step
104 --Call--
105 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
106 -> def callback():
107 (Pdb) step
108 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
109 -> return None
110 (Pdb) step
111 --Return--
112 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
113 -> return None
114 (Pdb) step
115 --Return--
116 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
117 -> mod.foo_pony(callback)
118 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000119 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
120 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000121 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000122 """
Georg Brandl243ad662009-05-05 09:00:19 +0000123
124
Georg Brandl3f940892010-07-30 10:29:19 +0000125def test_pdb_continue_in_bottomframe():
126 """Test that "continue" and "next" work properly in bottom frame (issue #5294).
127
128 >>> def test_function():
129 ... import pdb, sys; inst = pdb.Pdb()
130 ... inst.set_trace()
131 ... inst.botframe = sys._getframe() # hackery to get the right botframe
132 ... print(1)
133 ... print(2)
134 ... print(3)
135 ... print(4)
136
137 >>> with PdbTestInput([
138 ... 'next',
139 ... 'break 7',
140 ... 'continue',
141 ... 'next',
142 ... 'continue',
143 ... 'continue',
144 ... ]):
145 ... test_function()
146 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
147 -> inst.botframe = sys._getframe() # hackery to get the right botframe
148 (Pdb) next
149 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
150 -> print(1)
151 (Pdb) break 7
152 Breakpoint 1 at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
153 (Pdb) continue
154 1
155 2
156 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
157 -> print(3)
158 (Pdb) next
159 3
160 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
161 -> print(4)
162 (Pdb) continue
163 4
164 """
165
166
Georg Brandl46b9afc2010-07-30 09:14:20 +0000167def pdb_invoke(method, arg):
168 """Run pdb.method(arg)."""
169 import pdb; getattr(pdb, method)(arg)
170
171
172def test_pdb_run_with_incorrect_argument():
173 """Testing run and runeval with incorrect first argument.
174
175 >>> pti = PdbTestInput(['continue',])
176 >>> with pti:
177 ... pdb_invoke('run', lambda x: x)
178 Traceback (most recent call last):
179 TypeError: exec() arg 1 must be a string, bytes or code object
180
181 >>> with pti:
182 ... pdb_invoke('runeval', lambda x: x)
183 Traceback (most recent call last):
184 TypeError: eval() arg 1 must be a string, bytes or code object
185 """
186
187
188def test_pdb_run_with_code_object():
189 """Testing run and runeval with code object as a first argument.
190
191 >>> with PdbTestInput(['step','x', 'continue']):
192 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
193 > <string>(1)<module>()
194 (Pdb) step
195 --Return--
196 > <string>(1)<module>()->None
197 (Pdb) x
198 1
199 (Pdb) continue
200
201 >>> with PdbTestInput(['x', 'continue']):
202 ... x=0
203 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
204 > <string>(1)<module>()->None
205 (Pdb) x
206 1
207 (Pdb) continue
208 """
209
210
Georg Brandl243ad662009-05-05 09:00:19 +0000211def test_main():
212 from test import test_pdb
213 support.run_doctest(test_pdb, verbosity=True)
214
215
216if __name__ == '__main__':
217 test_main()