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