Debian Codegraph @

Lib/idlelib/idle_test/test_debugger.py

raw
1 """Test debugger, coverage 66%
2
3 Try to make tests pass with draft bdbx, which may replace bdb in 3.13+.
4 """
5
6 from idlelib import debugger
7 from collections import namedtuple
8 from textwrap import dedent
9 from tkinter import Tk
10
11 from test.support import requires
12 from test.support.testcase import ExtraAssertions
13 import unittest
14 from unittest import mock
15 from unittest.mock import Mock, patch
16
17 """A test python script for the debug tests."""
18 TEST_CODE = dedent("""
19 i = 1
20 i += 2
21 if i == 3:
22 print(i)
23 """)
24
25
26 class MockFrame:
27 "Minimal mock frame."
28
29 def __init__(self, code, lineno):
30 self.f_code = code
31 self.f_lineno = lineno
32
33
34 class IdbTest(unittest.TestCase):
35
36 @classmethod
37 def setUpClass(cls):
38 cls.gui = Mock()
39 cls.idb = debugger.Idb(cls.gui)
40
41 # Create test and code objects to simulate a debug session.
42 code_obj = compile(TEST_CODE, 'idlelib/file.py', mode='exec')
43 frame1 = MockFrame(code_obj, 1)
44 frame1.f_back = None
45 frame2 = MockFrame(code_obj, 2)
46 frame2.f_back = frame1
47 cls.frame = frame2
48 cls.msg = 'file.py:2: <module>()'
49
50 def test_init(self):
51 self.assertIs(self.idb.gui, self.gui)
52 # Won't test super call since two Bdbs are very different.
53
54 def test_user_line(self):
55 # Test that .user_line() creates a string message for a frame.
56 self.gui.interaction = Mock()
57 self.idb.user_line(self.frame)
58 self.gui.interaction.assert_called_once_with(self.msg, self.frame)
59
60 def test_user_exception(self):
61 # Test that .user_exception() creates a string message for a frame.
62 exc_info = (type(ValueError), ValueError(), None)
63 self.gui.interaction = Mock()
64 self.idb.user_exception(self.frame, exc_info)
65 self.gui.interaction.assert_called_once_with(
66 self.msg, self.frame, exc_info)
67
68
69 class FunctionTest(unittest.TestCase):
70 # Test module functions together.
71
72 def test_functions(self):
73 rpc_obj = compile(TEST_CODE,'rpc.py', mode='exec')
74 rpc_frame = MockFrame(rpc_obj, 2)
75 rpc_frame.f_back = rpc_frame
76 self.assertTrue(debugger._in_rpc_code(rpc_frame))
77 self.assertEqual(debugger._frame2message(rpc_frame),
78 'rpc.py:2: <module>()')
79
80 code_obj = compile(TEST_CODE, 'idlelib/debugger.py', mode='exec')
81 code_frame = MockFrame(code_obj, 1)
82 code_frame.f_back = None
83 self.assertFalse(debugger._in_rpc_code(code_frame))
84 self.assertEqual(debugger._frame2message(code_frame),
85 'debugger.py:1: <module>()')
86
87 code_frame.f_back = code_frame
88 self.assertFalse(debugger._in_rpc_code(code_frame))
89 code_frame.f_back = rpc_frame
90 self.assertTrue(debugger._in_rpc_code(code_frame))
91
92
93 class DebuggerTest(unittest.TestCase):
94 "Tests for Debugger that do not need a real root."
95
96 @classmethod
97 def setUpClass(cls):
98 cls.pyshell = Mock()
99 cls.pyshell.root = Mock()
100 cls.idb = Mock()
101 with patch.object(debugger.Debugger, 'make_gui'):
102 cls.debugger = debugger.Debugger(cls.pyshell, cls.idb)
103 cls.debugger.root = Mock()
104
105 def test_cont(self):
106 self.debugger.cont()
107 self.idb.set_continue.assert_called_once()
108
109 def test_step(self):
110 self.debugger.step()
111 self.idb.set_step.assert_called_once()
112
113 def test_quit(self):
114 self.debugger.quit()
115 self.idb.set_quit.assert_called_once()
116
117 def test_next(self):
118 with patch.object(self.debugger, 'frame') as frame:
119 self.debugger.next()
120 self.idb.set_next.assert_called_once_with(frame)
121
122 def test_ret(self):
123 with patch.object(self.debugger, 'frame') as frame:
124 self.debugger.ret()
125 self.idb.set_return.assert_called_once_with(frame)
126
127 def test_clear_breakpoint(self):
128 self.debugger.clear_breakpoint('test.py', 4)
129 self.idb.clear_break.assert_called_once_with('test.py', 4)
130
131 def test_clear_file_breaks(self):
132 self.debugger.clear_file_breaks('test.py')
133 self.idb.clear_all_file_breaks.assert_called_once_with('test.py')
134
135 def test_set_load_breakpoints(self):
136 # Test the .load_breakpoints() method calls idb.
137 FileIO = namedtuple('FileIO', 'filename')
138
139 class MockEditWindow(object):
140 def __init__(self, fn, breakpoints):
141 self.io = FileIO(fn)
142 self.breakpoints = breakpoints
143
144 self.pyshell.flist = Mock()
145 self.pyshell.flist.inversedict = (
146 MockEditWindow('test1.py', [4, 4]),
147 MockEditWindow('test2.py', [13, 44, 45]),
148 )
149 self.debugger.set_breakpoint('test0.py', 1)
150 self.idb.set_break.assert_called_once_with('test0.py', 1)
151 self.debugger.load_breakpoints() # Call set_breakpoint 5 times.
152 self.idb.set_break.assert_has_calls(
153 [mock.call('test0.py', 1),
154 mock.call('test1.py', 4),
155 mock.call('test1.py', 4),
156 mock.call('test2.py', 13),
157 mock.call('test2.py', 44),
158 mock.call('test2.py', 45)])
159
160 def test_sync_source_line(self):
161 # Test that .sync_source_line() will set the flist.gotofileline with fixed frame.
162 test_code = compile(TEST_CODE, 'test_sync.py', 'exec')
163 test_frame = MockFrame(test_code, 1)
164 self.debugger.frame = test_frame
165
166 self.debugger.flist = Mock()
167 with patch('idlelib.debugger.os.path.exists', return_value=True):
168 self.debugger.sync_source_line()
169 self.debugger.flist.gotofileline.assert_called_once_with('test_sync.py', 1)
170
171
172 class DebuggerGuiTest(unittest.TestCase):
173 """Tests for debugger.Debugger that need tk root.
174
175 close needs debugger.top set in make_gui.
176 """
177
178 @classmethod
179 def setUpClass(cls):
180 requires('gui')
181 cls.root = root = Tk()
182 root.withdraw()
183 cls.pyshell = Mock()
184 cls.pyshell.root = root
185 cls.idb = Mock()
186 # stack tests fail with debugger here.
187 ## cls.debugger = debugger.Debugger(cls.pyshell, cls.idb)
188 ## cls.debugger.root = root
189 ## # real root needed for real make_gui
190 ## # run, interacting, abort_loop
191
192 @classmethod
193 def tearDownClass(cls):
194 cls.root.destroy()
195 del cls.root
196
197 def setUp(self):
198 self.debugger = debugger.Debugger(self.pyshell, self.idb)
199 self.debugger.root = self.root
200 # real root needed for real make_gui
201 # run, interacting, abort_loop
202
203 def test_run_debugger(self):
204 self.debugger.run(1, 'two')
205 self.idb.run.assert_called_once_with(1, 'two')
206 self.assertEqual(self.debugger.interacting, 0)
207
208 def test_close(self):
209 # Test closing the window in an idle state.
210 self.debugger.close()
211 self.pyshell.close_debugger.assert_called_once()
212
213 def test_show_stack(self):
214 self.debugger.show_stack()
215 self.assertEqual(self.debugger.stackviewer.gui, self.debugger)
216
217 def test_show_stack_with_frame(self):
218 test_frame = MockFrame(None, None)
219 self.debugger.frame = test_frame
220
221 # Reset the stackviewer to force it to be recreated.
222 self.debugger.stackviewer = None
223 self.idb.get_stack.return_value = ([], 0)
224 self.debugger.show_stack()
225
226 # Check that the newly created stackviewer has the test gui as a field.
227 self.assertEqual(self.debugger.stackviewer.gui, self.debugger)
228 self.idb.get_stack.assert_called_once_with(test_frame, None)
229
230
231 class StackViewerTest(unittest.TestCase, ExtraAssertions):
232
233 @classmethod
234 def setUpClass(cls):
235 requires('gui')
236 cls.root = Tk()
237 cls.root.withdraw()
238
239 @classmethod
240 def tearDownClass(cls):
241 cls.root.destroy()
242 del cls.root
243
244 def setUp(self):
245 self.code = compile(TEST_CODE, 'test_stackviewer.py', 'exec')
246 self.stack = [
247 (MockFrame(self.code, 1), 1),
248 (MockFrame(self.code, 2), 2)
249 ]
250 # Create a stackviewer and load the test stack.
251 self.sv = debugger.StackViewer(self.root, None, None)
252 self.sv.load_stack(self.stack)
253
254 def test_init(self):
255 # Test creation of StackViewer.
256 gui = None
257 flist = None
258 master_window = self.root
259 sv = debugger.StackViewer(master_window, flist, gui)
260 self.assertHasAttr(sv, 'stack')
261
262 def test_load_stack(self):
263 # Test the .load_stack() method against a fixed test stack.
264 # Check the test stack is assigned and the list contains the repr of them.
265 self.assertEqual(self.sv.stack, self.stack)
266 self.assertTrue('?.<module>(), line 1:' in self.sv.get(0))
267 self.assertEqual(self.sv.get(1), '?.<module>(), line 2: ')
268
269 def test_show_source(self):
270 # Test the .show_source() method against a fixed test stack.
271 # Patch out the file list to monitor it
272 self.sv.flist = Mock()
273 # Patch out isfile to pretend file exists.
274 with patch('idlelib.debugger.os.path.isfile', return_value=True) as isfile:
275 self.sv.show_source(1)
276 isfile.assert_called_once_with('test_stackviewer.py')
277 self.sv.flist.open.assert_called_once_with('test_stackviewer.py')
278
279
280 class NameSpaceTest(unittest.TestCase):
281
282 @classmethod
283 def setUpClass(cls):
284 requires('gui')
285 cls.root = Tk()
286 cls.root.withdraw()
287
288 @classmethod
289 def tearDownClass(cls):
290 cls.root.destroy()
291 del cls.root
292
293 def test_init(self):
294 debugger.NamespaceViewer(self.root, 'Test')
295
296
297 if __name__ == '__main__':
298 unittest.main(verbosity=2)