blob: 4af8516bd98c1c262be8f780d6239c919165ef40 [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 Brandl46b9afc2010-07-30 09:14:20 +0000125def pdb_invoke(method, arg):
126 """Run pdb.method(arg)."""
127 import pdb; getattr(pdb, method)(arg)
128
129
130def test_pdb_run_with_incorrect_argument():
131 """Testing run and runeval with incorrect first argument.
132
133 >>> pti = PdbTestInput(['continue',])
134 >>> with pti:
135 ... pdb_invoke('run', lambda x: x)
136 Traceback (most recent call last):
137 TypeError: exec() arg 1 must be a string, bytes or code object
138
139 >>> with pti:
140 ... pdb_invoke('runeval', lambda x: x)
141 Traceback (most recent call last):
142 TypeError: eval() arg 1 must be a string, bytes or code object
143 """
144
145
146def test_pdb_run_with_code_object():
147 """Testing run and runeval with code object as a first argument.
148
149 >>> with PdbTestInput(['step','x', 'continue']):
150 ... pdb_invoke('run', compile('x=1', '<string>', 'exec'))
151 > <string>(1)<module>()
152 (Pdb) step
153 --Return--
154 > <string>(1)<module>()->None
155 (Pdb) x
156 1
157 (Pdb) continue
158
159 >>> with PdbTestInput(['x', 'continue']):
160 ... x=0
161 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
162 > <string>(1)<module>()->None
163 (Pdb) x
164 1
165 (Pdb) continue
166 """
167
168
Georg Brandl243ad662009-05-05 09:00:19 +0000169def test_main():
170 from test import test_pdb
171 support.run_doctest(test_pdb, verbosity=True)
172
173
174if __name__ == '__main__':
175 test_main()