blob: 7af903d210d1168e0f60c04cfccd4c388e5f1fae [file] [log] [blame]
Georg Brandl243ad662009-05-05 09:00:19 +00001# A test suite for pdb; at the moment, this only validates skipping of
2# specified test modules (RFE #5142).
3
4import imp
Georg Brandl243ad662009-05-05 09:00:19 +00005import sys
Georg Brandl243ad662009-05-05 09:00:19 +00006
7from test import support
8# This little helper class is essential for testing pdb under doctest.
9from test.test_doctest import _FakeInput
10
11
Georg Brandl9fa2e022009-09-16 16:40:45 +000012class PdbTestInput(object):
13 """Context manager that makes testing Pdb in doctests easier."""
14
15 def __init__(self, input):
16 self.input = input
17
18 def __enter__(self):
19 self.real_stdin = sys.stdin
20 sys.stdin = _FakeInput(self.input)
21
22 def __exit__(self, *exc):
23 sys.stdin = self.real_stdin
24
25
26def test_pdb_displayhook():
27 """This tests the custom displayhook for pdb.
28
29 >>> def test_function(foo, bar):
30 ... import pdb; pdb.Pdb().set_trace()
31 ... pass
32
33 >>> with PdbTestInput([
34 ... 'foo',
35 ... 'bar',
36 ... 'for i in range(5): print(i)',
37 ... 'continue',
38 ... ]):
39 ... test_function(1, None)
40 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
41 -> pass
42 (Pdb) foo
43 1
44 (Pdb) bar
45 (Pdb) for i in range(5): print(i)
46 0
47 1
48 2
49 3
50 4
51 (Pdb) continue
52 """
53
54
Georg Brandl243ad662009-05-05 09:00:19 +000055def test_pdb_skip_modules():
56 """This illustrates the simple case of module skipping.
57
58 >>> def skip_module():
59 ... import string
60 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
61 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +000062
Georg Brandl9fa2e022009-09-16 16:40:45 +000063 >>> with PdbTestInput([
64 ... 'step',
65 ... 'continue',
66 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +000067 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +000068 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
69 -> string.capwords('FOO')
70 (Pdb) step
71 --Return--
72 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
73 -> string.capwords('FOO')
74 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +000075 """
Georg Brandl243ad662009-05-05 09:00:19 +000076
77
78# Module for testing skipping of module that makes a callback
79mod = imp.new_module('module_to_skip')
80exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
81
82
83def test_pdb_skip_modules_with_callback():
84 """This illustrates skipping of modules that call into other code.
85
86 >>> def skip_module():
87 ... def callback():
88 ... return None
Georg Brandl9fa2e022009-09-16 16:40:45 +000089 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +000090 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +000091
Georg Brandl9fa2e022009-09-16 16:40:45 +000092 >>> with PdbTestInput([
93 ... 'step',
94 ... 'step',
95 ... 'step',
96 ... 'step',
97 ... 'step',
98 ... 'continue',
99 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000100 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000101 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000102 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
103 -> mod.foo_pony(callback)
104 (Pdb) step
105 --Call--
106 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
107 -> def callback():
108 (Pdb) step
109 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
110 -> return None
111 (Pdb) step
112 --Return--
113 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
114 -> return None
115 (Pdb) step
116 --Return--
117 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
118 -> mod.foo_pony(callback)
119 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000120 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
121 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000122 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000123 """
Georg Brandl243ad662009-05-05 09:00:19 +0000124
125
126def test_main():
127 from test import test_pdb
128 support.run_doctest(test_pdb, verbosity=True)
129
130
131if __name__ == '__main__':
132 test_main()