blob: a15538cbf7e2ba84b51b953e0ccd39a8d90e72e2 [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 Brandl4d4313d2009-05-05 08:54:11 +000029def test_pdb_skip_modules():
30 """This illustrates the simple case of module skipping.
31
32 >>> def skip_module():
33 ... import string
Georg Brandl6c39f062009-09-16 16:22:12 +000034 ... import pdb; pdb.Pdb(skip=['string*']).set_trace()
Georg Brandl4d4313d2009-05-05 08:54:11 +000035 ... string.lower('FOO')
Georg Brandl4d4313d2009-05-05 08:54:11 +000036
Georg Brandl6c39f062009-09-16 16:22:12 +000037 >>> with PdbTestInput([
38 ... 'step',
39 ... 'continue'
40 ... ]):
Georg Brandl4d4313d2009-05-05 08:54:11 +000041 ... skip_module()
Georg Brandl4d4313d2009-05-05 08:54:11 +000042 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
43 -> string.lower('FOO')
44 (Pdb) step
45 --Return--
46 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
47 -> string.lower('FOO')
48 (Pdb) continue
Georg Brandl6c39f062009-09-16 16:22:12 +000049 """
Georg Brandl4d4313d2009-05-05 08:54:11 +000050
51
52# Module for testing skipping of module that makes a callback
53mod = imp.new_module('module_to_skip')
54exec 'def foo_pony(callback): x = 1; callback(); return None' in mod.__dict__
55
56
57def test_pdb_skip_modules_with_callback():
58 """This illustrates skipping of modules that call into other code.
59
60 >>> def skip_module():
61 ... def callback():
62 ... return None
Georg Brandl6c39f062009-09-16 16:22:12 +000063 ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace()
Georg Brandl4d4313d2009-05-05 08:54:11 +000064 ... mod.foo_pony(callback)
Georg Brandl4d4313d2009-05-05 08:54:11 +000065
Georg Brandl6c39f062009-09-16 16:22:12 +000066 >>> with PdbTestInput([
67 ... 'step',
68 ... 'step',
69 ... 'step',
70 ... 'step',
71 ... 'step',
72 ... 'continue',
73 ... ]):
Georg Brandl4d4313d2009-05-05 08:54:11 +000074 ... skip_module()
Georg Brandl6c39f062009-09-16 16:22:12 +000075 ... pass # provides something to "step" to
Georg Brandl4d4313d2009-05-05 08:54:11 +000076 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
77 -> mod.foo_pony(callback)
78 (Pdb) step
79 --Call--
80 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
81 -> def callback():
82 (Pdb) step
83 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
84 -> return None
85 (Pdb) step
86 --Return--
87 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
88 -> return None
89 (Pdb) step
90 --Return--
91 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
92 -> mod.foo_pony(callback)
93 (Pdb) step
Georg Brandl6c39f062009-09-16 16:22:12 +000094 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
95 -> pass # provides something to "step" to
Georg Brandl4d4313d2009-05-05 08:54:11 +000096 (Pdb) continue
Georg Brandl6c39f062009-09-16 16:22:12 +000097 """
Georg Brandl4d4313d2009-05-05 08:54:11 +000098
99
100def test_main():
101 from test import test_pdb
102 test_support.run_doctest(test_pdb, verbosity=True)
103
104
105if __name__ == '__main__':
106 test_main()