blob: 6017edc5e2e9192ac1b371f91d2fcbada0467976 [file] [log] [blame]
Georg Brandl4d4313d2009-05-05 08:54:11 +00001# A test suite for pdb; at the moment, this only validates skipping of
2# specified test modules (RFE #5142).
3
4import imp
5import os
6import sys
7import doctest
8import tempfile
9
10from test import test_support
11# This little helper class is essential for testing pdb under doctest.
12from test_doctest import _FakeInput
13
14
Georg Brandl6c39f062009-09-16 16:22:12 +000015class PdbTestInput(object):
16 """Context manager that makes testing Pdb in doctests easier."""
17
18 def __init__(self, input):
19 self.input = input
20
21 def __enter__(self):
22 self.real_stdin = sys.stdin
23 sys.stdin = _FakeInput(self.input)
24
25 def __exit__(self, *exc):
26 sys.stdin = self.real_stdin
27
28
Georg Brandl69dfe8d2009-09-16 16:36:39 +000029def write(x):
30 print x
31
32def test_pdb_displayhook():
33 """This tests the custom displayhook for pdb.
34
35 >>> def test_function(foo, bar):
36 ... import pdb; pdb.Pdb().set_trace()
37 ... pass
38
39 >>> with PdbTestInput([
40 ... 'foo',
41 ... 'bar',
42 ... 'for i in range(5): write(i)',
43 ... 'continue',
44 ... ]):
45 ... test_function(1, None)
46 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
47 -> pass
48 (Pdb) foo
49 1
50 (Pdb) bar
51 (Pdb) for i in range(5): write(i)
52 0
53 1
54 2
55 3
56 4
57 (Pdb) continue
58 """
59
60
Georg Brandl4d4313d2009-05-05 08:54:11 +000061def test_pdb_skip_modules():
62 """This illustrates the simple case of module skipping.
63
64 >>> def skip_module():
65 ... import string
Georg Brandl6c39f062009-09-16 16:22:12 +000066 ... import pdb; pdb.Pdb(skip=['string*']).set_trace()
Georg Brandl4d4313d2009-05-05 08:54:11 +000067 ... string.lower('FOO')
Georg Brandl4d4313d2009-05-05 08:54:11 +000068
Georg Brandl6c39f062009-09-16 16:22:12 +000069 >>> with PdbTestInput([
70 ... 'step',
Georg Brandl69dfe8d2009-09-16 16:36:39 +000071 ... 'continue',
Georg Brandl6c39f062009-09-16 16:22:12 +000072 ... ]):
Georg Brandl4d4313d2009-05-05 08:54:11 +000073 ... skip_module()
Georg Brandl4d4313d2009-05-05 08:54:11 +000074 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
75 -> string.lower('FOO')
76 (Pdb) step
77 --Return--
78 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
79 -> string.lower('FOO')
80 (Pdb) continue
Georg Brandl6c39f062009-09-16 16:22:12 +000081 """
Georg Brandl4d4313d2009-05-05 08:54:11 +000082
83
84# Module for testing skipping of module that makes a callback
85mod = imp.new_module('module_to_skip')
86exec 'def foo_pony(callback): x = 1; callback(); return None' in mod.__dict__
87
88
89def test_pdb_skip_modules_with_callback():
90 """This illustrates skipping of modules that call into other code.
91
92 >>> def skip_module():
93 ... def callback():
94 ... return None
Georg Brandl6c39f062009-09-16 16:22:12 +000095 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl4d4313d2009-05-05 08:54:11 +000096 ... mod.foo_pony(callback)
Georg Brandl4d4313d2009-05-05 08:54:11 +000097
Georg Brandl6c39f062009-09-16 16:22:12 +000098 >>> with PdbTestInput([
99 ... 'step',
100 ... 'step',
101 ... 'step',
102 ... 'step',
103 ... 'step',
104 ... 'continue',
105 ... ]):
Georg Brandl4d4313d2009-05-05 08:54:11 +0000106 ... skip_module()
Georg Brandl6c39f062009-09-16 16:22:12 +0000107 ... pass # provides something to "step" to
Georg Brandl4d4313d2009-05-05 08:54:11 +0000108 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
109 -> mod.foo_pony(callback)
110 (Pdb) step
111 --Call--
112 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
113 -> def callback():
114 (Pdb) step
115 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
116 -> return None
117 (Pdb) step
118 --Return--
119 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
120 -> return None
121 (Pdb) step
122 --Return--
123 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
124 -> mod.foo_pony(callback)
125 (Pdb) step
Georg Brandl6c39f062009-09-16 16:22:12 +0000126 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
127 -> pass # provides something to "step" to
Georg Brandl4d4313d2009-05-05 08:54:11 +0000128 (Pdb) continue
Georg Brandl6c39f062009-09-16 16:22:12 +0000129 """
Georg Brandl4d4313d2009-05-05 08:54:11 +0000130
131
132def test_main():
133 from test import test_pdb
134 test_support.run_doctest(test_pdb, verbosity=True)
135
136
137if __name__ == '__main__':
138 test_main()