1 # A test suite for pdb; at the moment, this only validates skipping of
2 # specified test modules (RFE #5142).
10 from test
import test_support
11 # This little helper class is essential for testing pdb under doctest.
12 from test_doctest
import _FakeInput
15 class PdbTestInput(object):
16 """Context manager that makes testing Pdb in doctests easier."""
18 def __init__(self
, input):
22 self
.real_stdin
= sys
.stdin
23 sys
.stdin
= _FakeInput(self
.input)
25 def __exit__(self
, *exc
):
26 sys
.stdin
= self
.real_stdin
32 def test_pdb_displayhook():
33 """This tests the custom displayhook for pdb.
35 >>> def test_function(foo, bar):
36 ... import pdb; pdb.Pdb().set_trace()
39 >>> with PdbTestInput([
42 ... 'for i in range(5): write(i)',
45 ... test_function(1, None)
46 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
51 (Pdb) for i in range(5): write(i)
61 def test_pdb_skip_modules():
62 """This illustrates the simple case of module skipping.
64 >>> def skip_module():
66 ... import pdb; pdb.Pdb(skip=['string*']).set_trace()
67 ... string.lower('FOO')
69 >>> with PdbTestInput([
74 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
75 -> string.lower('FOO')
78 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
79 -> string.lower('FOO')
84 # Module for testing skipping of module that makes a callback
85 mod
= imp
.new_module('module_to_skip')
86 exec 'def foo_pony(callback): x = 1; callback(); return None' in mod
.__dict
__
89 def test_pdb_skip_modules_with_callback():
90 """This illustrates skipping of modules that call into other code.
92 >>> def skip_module():
95 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
96 ... mod.foo_pony(callback)
98 >>> with PdbTestInput([
107 ... pass # provides something to "step" to
108 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
109 -> mod.foo_pony(callback)
112 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
115 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
119 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
123 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
124 -> mod.foo_pony(callback)
126 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
127 -> pass # provides something to "step" to
133 from test
import test_pdb
134 test_support
.run_doctest(test_pdb
, verbosity
=True)
137 if __name__
== '__main__':