blob: 5d52707f408345f1499e48cbadb22568e37d5458 [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
5import os
6import sys
7import doctest
8import tempfile
9
10from test import support
11# This little helper class is essential for testing pdb under doctest.
12from test.test_doctest import _FakeInput
13
14
Georg Brandl9fa2e022009-09-16 16:40:45 +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
29def test_pdb_displayhook():
30 """This tests the custom displayhook for pdb.
31
32 >>> def test_function(foo, bar):
33 ... import pdb; pdb.Pdb().set_trace()
34 ... pass
35
36 >>> with PdbTestInput([
37 ... 'foo',
38 ... 'bar',
39 ... 'for i in range(5): print(i)',
40 ... 'continue',
41 ... ]):
42 ... test_function(1, None)
43 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
44 -> pass
45 (Pdb) foo
46 1
47 (Pdb) bar
48 (Pdb) for i in range(5): print(i)
49 0
50 1
51 2
52 3
53 4
54 (Pdb) continue
55 """
56
57
Georg Brandl243ad662009-05-05 09:00:19 +000058def test_pdb_skip_modules():
59 """This illustrates the simple case of module skipping.
60
61 >>> def skip_module():
62 ... import string
63 ... import pdb; pdb.Pdb(skip=['stri*']).set_trace()
64 ... string.capwords('FOO')
Georg Brandl243ad662009-05-05 09:00:19 +000065
Georg Brandl9fa2e022009-09-16 16:40:45 +000066 >>> with PdbTestInput([
67 ... 'step',
68 ... 'continue',
69 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +000070 ... skip_module()
Georg Brandl243ad662009-05-05 09:00:19 +000071 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
72 -> string.capwords('FOO')
73 (Pdb) step
74 --Return--
75 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
76 -> string.capwords('FOO')
77 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +000078 """
Georg Brandl243ad662009-05-05 09:00:19 +000079
80
81# Module for testing skipping of module that makes a callback
82mod = imp.new_module('module_to_skip')
83exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
84
85
86def test_pdb_skip_modules_with_callback():
87 """This illustrates skipping of modules that call into other code.
88
89 >>> def skip_module():
90 ... def callback():
91 ... return None
Georg Brandl9fa2e022009-09-16 16:40:45 +000092 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl243ad662009-05-05 09:00:19 +000093 ... mod.foo_pony(callback)
Georg Brandl243ad662009-05-05 09:00:19 +000094
Georg Brandl9fa2e022009-09-16 16:40:45 +000095 >>> with PdbTestInput([
96 ... 'step',
97 ... 'step',
98 ... 'step',
99 ... 'step',
100 ... 'step',
101 ... 'continue',
102 ... ]):
Georg Brandl243ad662009-05-05 09:00:19 +0000103 ... skip_module()
Georg Brandl9fa2e022009-09-16 16:40:45 +0000104 ... pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000105 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
106 -> mod.foo_pony(callback)
107 (Pdb) step
108 --Call--
109 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
110 -> def callback():
111 (Pdb) step
112 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
113 -> return None
114 (Pdb) step
115 --Return--
116 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
117 -> return None
118 (Pdb) step
119 --Return--
120 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
121 -> mod.foo_pony(callback)
122 (Pdb) step
Georg Brandl9fa2e022009-09-16 16:40:45 +0000123 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
124 -> pass # provides something to "step" to
Georg Brandl243ad662009-05-05 09:00:19 +0000125 (Pdb) continue
Georg Brandl9fa2e022009-09-16 16:40:45 +0000126 """
Georg Brandl243ad662009-05-05 09:00:19 +0000127
128
129def test_main():
130 from test import test_pdb
131 support.run_doctest(test_pdb, verbosity=True)
132
133
134if __name__ == '__main__':
135 test_main()