Show More
@@ -12,6 +12,8 b'' | |||||
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
|
15 | import sys | |||
|
16 | ||||
15 | # third-party |
|
17 | # third-party | |
16 | import nose.tools as nt |
|
18 | import nose.tools as nt | |
17 |
|
19 | |||
@@ -19,6 +21,38 b' import nose.tools as nt' | |||||
19 | from IPython.core import debugger |
|
21 | from IPython.core import debugger | |
20 |
|
22 | |||
21 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | # Helper classes, from CPython's Pdb test suite | |||
|
25 | #----------------------------------------------------------------------------- | |||
|
26 | ||||
|
27 | class _FakeInput(object): | |||
|
28 | """ | |||
|
29 | A fake input stream for pdb's interactive debugger. Whenever a | |||
|
30 | line is read, print it (to simulate the user typing it), and then | |||
|
31 | return it. The set of lines to return is specified in the | |||
|
32 | constructor; they should not have trailing newlines. | |||
|
33 | """ | |||
|
34 | def __init__(self, lines): | |||
|
35 | self.lines = iter(lines) | |||
|
36 | ||||
|
37 | def readline(self): | |||
|
38 | line = next(self.lines) | |||
|
39 | print line | |||
|
40 | return line+'\n' | |||
|
41 | ||||
|
42 | class PdbTestInput(object): | |||
|
43 | """Context manager that makes testing Pdb in doctests easier.""" | |||
|
44 | ||||
|
45 | def __init__(self, input): | |||
|
46 | self.input = input | |||
|
47 | ||||
|
48 | def __enter__(self): | |||
|
49 | self.real_stdin = sys.stdin | |||
|
50 | sys.stdin = _FakeInput(self.input) | |||
|
51 | ||||
|
52 | def __exit__(self, *exc): | |||
|
53 | sys.stdin = self.real_stdin | |||
|
54 | ||||
|
55 | #----------------------------------------------------------------------------- | |||
22 | # Tests |
|
56 | # Tests | |
23 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
24 |
|
58 | |||
@@ -33,3 +67,57 b' def test_longer_repr():' | |||||
33 | # in-place, since that global is used directly by the stdlib's pdb module. |
|
67 | # in-place, since that global is used directly by the stdlib's pdb module. | |
34 | t = debugger.Tracer() |
|
68 | t = debugger.Tracer() | |
35 | nt.assert_equal(trepr(a), ar) |
|
69 | nt.assert_equal(trepr(a), ar) | |
|
70 | ||||
|
71 | def test_ipdb_magics(): | |||
|
72 | '''Test calling some IPython magics from ipdb. | |||
|
73 | ||||
|
74 | First, set up some test functions and classes which we can inspect. | |||
|
75 | ||||
|
76 | >>> class ExampleClass(object): | |||
|
77 | ... """Docstring for ExampleClass.""" | |||
|
78 | ... def __init__(self): | |||
|
79 | ... """Docstring for ExampleClass.__init__""" | |||
|
80 | ... pass | |||
|
81 | ... def __str__(self): | |||
|
82 | ... return "ExampleClass()" | |||
|
83 | ||||
|
84 | >>> def example_function(x, y, z="hello"): | |||
|
85 | ... """Docstring for example_function.""" | |||
|
86 | ... pass | |||
|
87 | ||||
|
88 | Create a function which triggers ipdb. | |||
|
89 | ||||
|
90 | >>> def trigger_ipdb(): | |||
|
91 | ... a = ExampleClass() | |||
|
92 | ... debugger.Pdb().set_trace() | |||
|
93 | ||||
|
94 | >>> with PdbTestInput([ | |||
|
95 | ... 'pdef example_function', | |||
|
96 | ... 'pdoc ExampleClass', | |||
|
97 | ... 'pinfo a', | |||
|
98 | ... 'continue', | |||
|
99 | ... ]): | |||
|
100 | ... trigger_ipdb() | |||
|
101 | --Return-- | |||
|
102 | None | |||
|
103 | > <doctest ...>(3)trigger_ipdb() | |||
|
104 | 1 def trigger_ipdb(): | |||
|
105 | 2 a = ExampleClass() | |||
|
106 | ----> 3 debugger.Pdb().set_trace() | |||
|
107 | <BLANKLINE> | |||
|
108 | ipdb> pdef example_function | |||
|
109 | example_function(x, y, z='hello') | |||
|
110 | ipdb> pdoc ExampleClass | |||
|
111 | Class Docstring: | |||
|
112 | Docstring for ExampleClass. | |||
|
113 | Constructor Docstring: | |||
|
114 | Docstring for ExampleClass.__init__ | |||
|
115 | ipdb> pinfo a | |||
|
116 | Type: ExampleClass | |||
|
117 | String Form:ExampleClass() | |||
|
118 | Namespace: Locals | |||
|
119 | File: ... | |||
|
120 | Docstring: Docstring for ExampleClass. | |||
|
121 | Constructor Docstring:Docstring for ExampleClass.__init__ | |||
|
122 | ipdb> continue | |||
|
123 | ''' |
General Comments 0
You need to be logged in to leave comments.
Login now