Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 1 | # A test suite for pdb; not very comprehensive at the moment. |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 2 | |
| 3 | import imp |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 4 | import sys |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 5 | |
| 6 | from test import support |
| 7 | # This little helper class is essential for testing pdb under doctest. |
| 8 | from test.test_doctest import _FakeInput |
| 9 | |
| 10 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 11 | class PdbTestInput(object): |
| 12 | """Context manager that makes testing Pdb in doctests easier.""" |
| 13 | |
| 14 | def __init__(self, input): |
| 15 | self.input = input |
| 16 | |
| 17 | def __enter__(self): |
| 18 | self.real_stdin = sys.stdin |
| 19 | sys.stdin = _FakeInput(self.input) |
| 20 | |
| 21 | def __exit__(self, *exc): |
| 22 | sys.stdin = self.real_stdin |
| 23 | |
| 24 | |
| 25 | def test_pdb_displayhook(): |
| 26 | """This tests the custom displayhook for pdb. |
| 27 | |
| 28 | >>> def test_function(foo, bar): |
| 29 | ... import pdb; pdb.Pdb().set_trace() |
| 30 | ... pass |
| 31 | |
| 32 | >>> with PdbTestInput([ |
| 33 | ... 'foo', |
| 34 | ... 'bar', |
| 35 | ... 'for i in range(5): print(i)', |
| 36 | ... 'continue', |
| 37 | ... ]): |
| 38 | ... test_function(1, None) |
| 39 | > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function() |
| 40 | -> pass |
| 41 | (Pdb) foo |
| 42 | 1 |
| 43 | (Pdb) bar |
| 44 | (Pdb) for i in range(5): print(i) |
| 45 | 0 |
| 46 | 1 |
| 47 | 2 |
| 48 | 3 |
| 49 | 4 |
| 50 | (Pdb) continue |
| 51 | """ |
| 52 | |
| 53 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 54 | def test_pdb_skip_modules(): |
| 55 | """This illustrates the simple case of module skipping. |
| 56 | |
| 57 | >>> def skip_module(): |
| 58 | ... import string |
| 59 | ... import pdb; pdb.Pdb(skip=['stri*']).set_trace() |
| 60 | ... string.capwords('FOO') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 62 | >>> with PdbTestInput([ |
| 63 | ... 'step', |
| 64 | ... 'continue', |
| 65 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 66 | ... skip_module() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 67 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 68 | -> string.capwords('FOO') |
| 69 | (Pdb) step |
| 70 | --Return-- |
| 71 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 72 | -> string.capwords('FOO') |
| 73 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 74 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | # Module for testing skipping of module that makes a callback |
| 78 | mod = imp.new_module('module_to_skip') |
| 79 | exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) |
| 80 | |
| 81 | |
| 82 | def test_pdb_skip_modules_with_callback(): |
| 83 | """This illustrates skipping of modules that call into other code. |
| 84 | |
| 85 | >>> def skip_module(): |
| 86 | ... def callback(): |
| 87 | ... return None |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 88 | ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 89 | ... mod.foo_pony(callback) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 90 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 91 | >>> with PdbTestInput([ |
| 92 | ... 'step', |
| 93 | ... 'step', |
| 94 | ... 'step', |
| 95 | ... 'step', |
| 96 | ... 'step', |
| 97 | ... 'continue', |
| 98 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 99 | ... skip_module() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 100 | ... pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 101 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 102 | -> mod.foo_pony(callback) |
| 103 | (Pdb) step |
| 104 | --Call-- |
| 105 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 106 | -> def callback(): |
| 107 | (Pdb) step |
| 108 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 109 | -> return None |
| 110 | (Pdb) step |
| 111 | --Return-- |
| 112 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 113 | -> return None |
| 114 | (Pdb) step |
| 115 | --Return-- |
| 116 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 117 | -> mod.foo_pony(callback) |
| 118 | (Pdb) step |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 119 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() |
| 120 | -> pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 121 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 122 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 123 | |
| 124 | |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 125 | def test_pdb_continue_in_bottomframe(): |
| 126 | """Test that "continue" and "next" work properly in bottom frame (issue #5294). |
| 127 | |
| 128 | >>> def test_function(): |
| 129 | ... import pdb, sys; inst = pdb.Pdb() |
| 130 | ... inst.set_trace() |
| 131 | ... inst.botframe = sys._getframe() # hackery to get the right botframe |
| 132 | ... print(1) |
| 133 | ... print(2) |
| 134 | ... print(3) |
| 135 | ... print(4) |
| 136 | |
| 137 | >>> with PdbTestInput([ |
| 138 | ... 'next', |
| 139 | ... 'break 7', |
| 140 | ... 'continue', |
| 141 | ... 'next', |
| 142 | ... 'continue', |
| 143 | ... 'continue', |
| 144 | ... ]): |
| 145 | ... test_function() |
| 146 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() |
| 147 | -> inst.botframe = sys._getframe() # hackery to get the right botframe |
| 148 | (Pdb) next |
| 149 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() |
| 150 | -> print(1) |
| 151 | (Pdb) break 7 |
| 152 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7 |
| 153 | (Pdb) continue |
| 154 | 1 |
| 155 | 2 |
| 156 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() |
| 157 | -> print(3) |
| 158 | (Pdb) next |
| 159 | 3 |
| 160 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() |
| 161 | -> print(4) |
| 162 | (Pdb) continue |
| 163 | 4 |
| 164 | """ |
| 165 | |
| 166 | |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 167 | def pdb_invoke(method, arg): |
| 168 | """Run pdb.method(arg).""" |
| 169 | import pdb; getattr(pdb, method)(arg) |
| 170 | |
| 171 | |
| 172 | def test_pdb_run_with_incorrect_argument(): |
| 173 | """Testing run and runeval with incorrect first argument. |
| 174 | |
| 175 | >>> pti = PdbTestInput(['continue',]) |
| 176 | >>> with pti: |
| 177 | ... pdb_invoke('run', lambda x: x) |
| 178 | Traceback (most recent call last): |
| 179 | TypeError: exec() arg 1 must be a string, bytes or code object |
| 180 | |
| 181 | >>> with pti: |
| 182 | ... pdb_invoke('runeval', lambda x: x) |
| 183 | Traceback (most recent call last): |
| 184 | TypeError: eval() arg 1 must be a string, bytes or code object |
| 185 | """ |
| 186 | |
| 187 | |
| 188 | def test_pdb_run_with_code_object(): |
| 189 | """Testing run and runeval with code object as a first argument. |
| 190 | |
| 191 | >>> with PdbTestInput(['step','x', 'continue']): |
| 192 | ... pdb_invoke('run', compile('x=1', '<string>', 'exec')) |
| 193 | > <string>(1)<module>() |
| 194 | (Pdb) step |
| 195 | --Return-- |
| 196 | > <string>(1)<module>()->None |
| 197 | (Pdb) x |
| 198 | 1 |
| 199 | (Pdb) continue |
| 200 | |
| 201 | >>> with PdbTestInput(['x', 'continue']): |
| 202 | ... x=0 |
| 203 | ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval')) |
| 204 | > <string>(1)<module>()->None |
| 205 | (Pdb) x |
| 206 | 1 |
| 207 | (Pdb) continue |
| 208 | """ |
| 209 | |
| 210 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 211 | def test_main(): |
| 212 | from test import test_pdb |
| 213 | support.run_doctest(test_pdb, verbosity=True) |
| 214 | |
| 215 | |
| 216 | if __name__ == '__main__': |
| 217 | test_main() |