Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +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 | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 5 | import sys |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 6 | |
| 7 | from test import support |
| 8 | # This little helper class is essential for testing pdb under doctest. |
| 9 | from test.test_doctest import _FakeInput |
| 10 | |
| 11 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +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 | |
| 26 | def test_pdb_displayhook(): |
| 27 | """This tests the custom displayhook for pdb. |
| 28 | |
| 29 | >>> def test_function(foo, bar): |
| 30 | ... import pdb; pdb.Pdb().set_trace() |
| 31 | ... pass |
| 32 | |
| 33 | >>> with PdbTestInput([ |
| 34 | ... 'foo', |
| 35 | ... 'bar', |
| 36 | ... 'for i in range(5): print(i)', |
| 37 | ... 'continue', |
| 38 | ... ]): |
| 39 | ... test_function(1, None) |
| 40 | > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function() |
| 41 | -> pass |
| 42 | (Pdb) foo |
| 43 | 1 |
| 44 | (Pdb) bar |
| 45 | (Pdb) for i in range(5): print(i) |
| 46 | 0 |
| 47 | 1 |
| 48 | 2 |
| 49 | 3 |
| 50 | 4 |
| 51 | (Pdb) continue |
| 52 | """ |
| 53 | |
| 54 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 55 | def test_pdb_skip_modules(): |
| 56 | """This illustrates the simple case of module skipping. |
| 57 | |
| 58 | >>> def skip_module(): |
| 59 | ... import string |
| 60 | ... import pdb; pdb.Pdb(skip=['stri*']).set_trace() |
| 61 | ... string.capwords('FOO') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 62 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 63 | >>> with PdbTestInput([ |
| 64 | ... 'step', |
| 65 | ... 'continue', |
| 66 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 67 | ... skip_module() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 68 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 69 | -> string.capwords('FOO') |
| 70 | (Pdb) step |
| 71 | --Return-- |
| 72 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 73 | -> string.capwords('FOO') |
| 74 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 75 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | # Module for testing skipping of module that makes a callback |
| 79 | mod = imp.new_module('module_to_skip') |
| 80 | exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) |
| 81 | |
| 82 | |
| 83 | def test_pdb_skip_modules_with_callback(): |
| 84 | """This illustrates skipping of modules that call into other code. |
| 85 | |
| 86 | >>> def skip_module(): |
| 87 | ... def callback(): |
| 88 | ... return None |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 89 | ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 90 | ... mod.foo_pony(callback) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 91 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 92 | >>> with PdbTestInput([ |
| 93 | ... 'step', |
| 94 | ... 'step', |
| 95 | ... 'step', |
| 96 | ... 'step', |
| 97 | ... 'step', |
| 98 | ... 'continue', |
| 99 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 100 | ... skip_module() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 101 | ... pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 102 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 103 | -> mod.foo_pony(callback) |
| 104 | (Pdb) step |
| 105 | --Call-- |
| 106 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 107 | -> def callback(): |
| 108 | (Pdb) step |
| 109 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 110 | -> return None |
| 111 | (Pdb) step |
| 112 | --Return-- |
| 113 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 114 | -> return None |
| 115 | (Pdb) step |
| 116 | --Return-- |
| 117 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 118 | -> mod.foo_pony(callback) |
| 119 | (Pdb) step |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 120 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() |
| 121 | -> pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 122 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 123 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | def test_main(): |
| 127 | from test import test_pdb |
| 128 | support.run_doctest(test_pdb, verbosity=True) |
| 129 | |
| 130 | |
| 131 | if __name__ == '__main__': |
| 132 | test_main() |