| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 1 | # A test suite for pdb; at the moment, this only validates skipping of | 
|  | 2 | # specified test modules (RFE #5142). | 
|  | 3 |  | 
|  | 4 | import imp | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 5 | import sys | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 6 |  | 
|  | 7 | from test import test_support | 
|  | 8 | # This little helper class is essential for testing pdb under doctest. | 
|  | 9 | from test_doctest import _FakeInput | 
|  | 10 |  | 
|  | 11 |  | 
| Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 12 | class 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 |  | 
| Georg Brandl | 69dfe8d | 2009-09-16 16:36:39 +0000 | [diff] [blame] | 26 | def write(x): | 
|  | 27 | print x | 
|  | 28 |  | 
|  | 29 | def 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): write(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): write(i) | 
|  | 49 | 0 | 
|  | 50 | 1 | 
|  | 51 | 2 | 
|  | 52 | 3 | 
|  | 53 | 4 | 
|  | 54 | (Pdb) continue | 
|  | 55 | """ | 
|  | 56 |  | 
|  | 57 |  | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 58 | def test_pdb_skip_modules(): | 
|  | 59 | """This illustrates the simple case of module skipping. | 
|  | 60 |  | 
|  | 61 | >>> def skip_module(): | 
|  | 62 | ...     import string | 
| Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 63 | ...     import pdb; pdb.Pdb(skip=['string*']).set_trace() | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 64 | ...     string.lower('FOO') | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 65 |  | 
| Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 66 | >>> with PdbTestInput([ | 
|  | 67 | ...     'step', | 
| Georg Brandl | 69dfe8d | 2009-09-16 16:36:39 +0000 | [diff] [blame] | 68 | ...     'continue', | 
| Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 69 | ... ]): | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 70 | ...     skip_module() | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 71 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() | 
|  | 72 | -> string.lower('FOO') | 
|  | 73 | (Pdb) step | 
|  | 74 | --Return-- | 
|  | 75 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None | 
|  | 76 | -> string.lower('FOO') | 
|  | 77 | (Pdb) continue | 
| Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 78 | """ | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 79 |  | 
|  | 80 |  | 
|  | 81 | # Module for testing skipping of module that makes a callback | 
|  | 82 | mod = imp.new_module('module_to_skip') | 
|  | 83 | exec 'def foo_pony(callback): x = 1; callback(); return None' in mod.__dict__ | 
|  | 84 |  | 
|  | 85 |  | 
|  | 86 | def 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 Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 92 | ...     import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace() | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 93 | ...     mod.foo_pony(callback) | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 94 |  | 
| Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 95 | >>> with PdbTestInput([ | 
|  | 96 | ...     'step', | 
|  | 97 | ...     'step', | 
|  | 98 | ...     'step', | 
|  | 99 | ...     'step', | 
|  | 100 | ...     'step', | 
|  | 101 | ...     'continue', | 
|  | 102 | ... ]): | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 103 | ...     skip_module() | 
| Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 104 | ...     pass  # provides something to "step" to | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 105 | > <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 Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 123 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() | 
|  | 124 | -> pass  # provides something to "step" to | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 125 | (Pdb) continue | 
| Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 126 | """ | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 127 |  | 
|  | 128 |  | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame^] | 129 | def test_pdb_continue_in_bottomframe(): | 
|  | 130 | """Test that "continue" and "next" work properly in bottom frame (issue #5294). | 
|  | 131 |  | 
|  | 132 | >>> def test_function(): | 
|  | 133 | ...     import pdb, sys; inst = pdb.Pdb() | 
|  | 134 | ...     inst.set_trace() | 
|  | 135 | ...     inst.botframe = sys._getframe()  # hackery to get the right botframe | 
|  | 136 | ...     print(1) | 
|  | 137 | ...     print(2) | 
|  | 138 | ...     print(3) | 
|  | 139 | ...     print(4) | 
|  | 140 |  | 
|  | 141 | >>> with PdbTestInput([ | 
|  | 142 | ...     'next', | 
|  | 143 | ...     'break 7', | 
|  | 144 | ...     'continue', | 
|  | 145 | ...     'next', | 
|  | 146 | ...     'continue', | 
|  | 147 | ...     'continue', | 
|  | 148 | ... ]): | 
|  | 149 | ...    test_function() | 
|  | 150 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() | 
|  | 151 | -> inst.botframe = sys._getframe()  # hackery to get the right botframe | 
|  | 152 | (Pdb) next | 
|  | 153 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() | 
|  | 154 | -> print(1) | 
|  | 155 | (Pdb) break 7 | 
|  | 156 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7 | 
|  | 157 | (Pdb) continue | 
|  | 158 | 1 | 
|  | 159 | 2 | 
|  | 160 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() | 
|  | 161 | -> print(3) | 
|  | 162 | (Pdb) next | 
|  | 163 | 3 | 
|  | 164 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() | 
|  | 165 | -> print(4) | 
|  | 166 | (Pdb) continue | 
|  | 167 | 4 | 
|  | 168 | """ | 
|  | 169 |  | 
|  | 170 |  | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 171 | def test_main(): | 
|  | 172 | from test import test_pdb | 
|  | 173 | test_support.run_doctest(test_pdb, verbosity=True) | 
|  | 174 |  | 
|  | 175 |  | 
|  | 176 | if __name__ == '__main__': | 
|  | 177 | test_main() |