Debian Codegraph @

Lib/idlelib/idle_test/test_query.py

raw
1 """Test query, coverage 93%.
2
3 Non-gui tests for Query, SectionName, ModuleName, and HelpSource use
4 dummy versions that extract the non-gui methods and add other needed
5 attributes. GUI tests create an instance of each class and simulate
6 entries and button clicks. Subclass tests only target the new code in
7 the subclass definition.
8
9 The appearance of the widgets is checked by the Query and
10 HelpSource htests. These are run by running query.py.
11 """
12 from idlelib import query
13 import unittest
14 from test.support import requires
15 from test.support.testcase import ExtraAssertions
16 from tkinter import Tk, END
17
18 import sys
19 from unittest import mock
20 from idlelib.idle_test.mock_tk import Var
21
22
23 # NON-GUI TESTS
24
25 class QueryTest(unittest.TestCase):
26 "Test Query base class."
27
28 class Dummy_Query:
29 # Test the following Query methods.
30 entry_ok = query.Query.entry_ok
31 ok = query.Query.ok
32 cancel = query.Query.cancel
33 # Add attributes and initialization needed for tests.
34 def __init__(self, dummy_entry):
35 self.entry = Var(value=dummy_entry)
36 self.entry_error = {'text': ''}
37 self.result = None
38 self.destroyed = False
39 def showerror(self, message):
40 self.entry_error['text'] = message
41 def destroy(self):
42 self.destroyed = True
43
44 def test_entry_ok_blank(self):
45 dialog = self.Dummy_Query(' ')
46 self.assertEqual(dialog.entry_ok(), None)
47 self.assertEqual((dialog.result, dialog.destroyed), (None, False))
48 self.assertIn('blank line', dialog.entry_error['text'])
49
50 def test_entry_ok_good(self):
51 dialog = self.Dummy_Query(' good ')
52 Equal = self.assertEqual
53 Equal(dialog.entry_ok(), 'good')
54 Equal((dialog.result, dialog.destroyed), (None, False))
55 Equal(dialog.entry_error['text'], '')
56
57 def test_ok_blank(self):
58 dialog = self.Dummy_Query('')
59 dialog.entry.focus_set = mock.Mock()
60 self.assertEqual(dialog.ok(), None)
61 self.assertTrue(dialog.entry.focus_set.called)
62 del dialog.entry.focus_set
63 self.assertEqual((dialog.result, dialog.destroyed), (None, False))
64
65 def test_ok_good(self):
66 dialog = self.Dummy_Query('good')
67 self.assertEqual(dialog.ok(), None)
68 self.assertEqual((dialog.result, dialog.destroyed), ('good', True))
69
70 def test_cancel(self):
71 dialog = self.Dummy_Query('does not matter')
72 self.assertEqual(dialog.cancel(), None)
73 self.assertEqual((dialog.result, dialog.destroyed), (None, True))
74
75
76 class SectionNameTest(unittest.TestCase):
77 "Test SectionName subclass of Query."
78
79 class Dummy_SectionName:
80 entry_ok = query.SectionName.entry_ok # Function being tested.
81 used_names = ['used']
82 def __init__(self, dummy_entry):
83 self.entry = Var(value=dummy_entry)
84 self.entry_error = {'text': ''}
85 def showerror(self, message):
86 self.entry_error['text'] = message
87
88 def test_blank_section_name(self):
89 dialog = self.Dummy_SectionName(' ')
90 self.assertEqual(dialog.entry_ok(), None)
91 self.assertIn('no name', dialog.entry_error['text'])
92
93 def test_used_section_name(self):
94 dialog = self.Dummy_SectionName('used')
95 self.assertEqual(dialog.entry_ok(), None)
96 self.assertIn('use', dialog.entry_error['text'])
97
98 def test_long_section_name(self):
99 dialog = self.Dummy_SectionName('good'*8)
100 self.assertEqual(dialog.entry_ok(), None)
101 self.assertIn('longer than 30', dialog.entry_error['text'])
102
103 def test_good_section_name(self):
104 dialog = self.Dummy_SectionName(' good ')
105 self.assertEqual(dialog.entry_ok(), 'good')
106 self.assertEqual(dialog.entry_error['text'], '')
107
108
109 class ModuleNameTest(unittest.TestCase, ExtraAssertions):
110 "Test ModuleName subclass of Query."
111
112 class Dummy_ModuleName:
113 entry_ok = query.ModuleName.entry_ok # Function being tested.
114 text0 = ''
115 def __init__(self, dummy_entry):
116 self.entry = Var(value=dummy_entry)
117 self.entry_error = {'text': ''}
118 def showerror(self, message):
119 self.entry_error['text'] = message
120
121 def test_blank_module_name(self):
122 dialog = self.Dummy_ModuleName(' ')
123 self.assertEqual(dialog.entry_ok(), None)
124 self.assertIn('no name', dialog.entry_error['text'])
125
126 def test_bogus_module_name(self):
127 dialog = self.Dummy_ModuleName('__name_xyz123_should_not_exist__')
128 self.assertEqual(dialog.entry_ok(), None)
129 self.assertIn('not found', dialog.entry_error['text'])
130
131 def test_c_source_name(self):
132 dialog = self.Dummy_ModuleName('itertools')
133 self.assertEqual(dialog.entry_ok(), None)
134 self.assertIn('source-based', dialog.entry_error['text'])
135
136 def test_good_module_name(self):
137 dialog = self.Dummy_ModuleName('idlelib')
138 self.assertEndsWith(dialog.entry_ok(), '__init__.py')
139 self.assertEqual(dialog.entry_error['text'], '')
140 dialog = self.Dummy_ModuleName('idlelib.idle')
141 self.assertEndsWith(dialog.entry_ok(), 'idle.py')
142 self.assertEqual(dialog.entry_error['text'], '')
143
144
145 class GotoTest(unittest.TestCase):
146 "Test Goto subclass of Query."
147
148 class Dummy_ModuleName:
149 entry_ok = query.Goto.entry_ok # Function being tested.
150 def __init__(self, dummy_entry):
151 self.entry = Var(value=dummy_entry)
152 self.entry_error = {'text': ''}
153 def showerror(self, message):
154 self.entry_error['text'] = message
155
156 def test_bogus_goto(self):
157 dialog = self.Dummy_ModuleName('a')
158 self.assertEqual(dialog.entry_ok(), None)
159 self.assertIn('not a base 10 integer', dialog.entry_error['text'])
160
161 def test_bad_goto(self):
162 dialog = self.Dummy_ModuleName('0')
163 self.assertEqual(dialog.entry_ok(), None)
164 self.assertIn('not a positive integer', dialog.entry_error['text'])
165
166 def test_good_goto(self):
167 dialog = self.Dummy_ModuleName('1')
168 self.assertEqual(dialog.entry_ok(), 1)
169 self.assertEqual(dialog.entry_error['text'], '')
170
171
172 # 3 HelpSource test classes each test one method.
173
174 class HelpsourceBrowsefileTest(unittest.TestCase):
175 "Test browse_file method of ModuleName subclass of Query."
176
177 class Dummy_HelpSource:
178 browse_file = query.HelpSource.browse_file
179 pathvar = Var()
180
181 def test_file_replaces_path(self):
182 dialog = self.Dummy_HelpSource()
183 # Path is widget entry, either '' or something.
184 # Func return is file dialog return, either '' or something.
185 # Func return should override widget entry.
186 # We need all 4 combinations to test all (most) code paths.
187 for path, func, result in (
188 ('', lambda a,b,c:'', ''),
189 ('', lambda a,b,c: __file__, __file__),
190 ('htest', lambda a,b,c:'', 'htest'),
191 ('htest', lambda a,b,c: __file__, __file__)):
192 with self.subTest():
193 dialog.pathvar.set(path)
194 dialog.askfilename = func
195 dialog.browse_file()
196 self.assertEqual(dialog.pathvar.get(), result)
197
198
199 class HelpsourcePathokTest(unittest.TestCase):
200 "Test path_ok method of HelpSource subclass of Query."
201
202 class Dummy_HelpSource:
203 path_ok = query.HelpSource.path_ok
204 def __init__(self, dummy_path):
205 self.path = Var(value=dummy_path)
206 self.path_error = {'text': ''}
207 def showerror(self, message, widget=None):
208 self.path_error['text'] = message
209
210 orig_platform = query.platform # Set in test_path_ok_file.
211 @classmethod
212 def tearDownClass(cls):
213 query.platform = cls.orig_platform
214
215 def test_path_ok_blank(self):
216 dialog = self.Dummy_HelpSource(' ')
217 self.assertEqual(dialog.path_ok(), None)
218 self.assertIn('no help file', dialog.path_error['text'])
219
220 def test_path_ok_bad(self):
221 dialog = self.Dummy_HelpSource(__file__ + 'bad-bad-bad')
222 self.assertEqual(dialog.path_ok(), None)
223 self.assertIn('not exist', dialog.path_error['text'])
224
225 def test_path_ok_web(self):
226 dialog = self.Dummy_HelpSource('')
227 Equal = self.assertEqual
228 for url in 'www.py.org', 'http://py.org':
229 with self.subTest():
230 dialog.path.set(url)
231 self.assertEqual(dialog.path_ok(), url)
232 self.assertEqual(dialog.path_error['text'], '')
233
234 def test_path_ok_file(self):
235 dialog = self.Dummy_HelpSource('')
236 for platform, prefix in ('darwin', 'file://'), ('other', ''):
237 with self.subTest():
238 query.platform = platform
239 dialog.path.set(__file__)
240 self.assertEqual(dialog.path_ok(), prefix + __file__)
241 self.assertEqual(dialog.path_error['text'], '')
242
243
244 class HelpsourceEntryokTest(unittest.TestCase):
245 "Test entry_ok method of HelpSource subclass of Query."
246
247 class Dummy_HelpSource:
248 entry_ok = query.HelpSource.entry_ok
249 entry_error = {}
250 path_error = {}
251 def item_ok(self):
252 return self.name
253 def path_ok(self):
254 return self.path
255
256 def test_entry_ok_helpsource(self):
257 dialog = self.Dummy_HelpSource()
258 for name, path, result in ((None, None, None),
259 (None, 'doc.txt', None),
260 ('doc', None, None),
261 ('doc', 'doc.txt', ('doc', 'doc.txt'))):
262 with self.subTest():
263 dialog.name, dialog.path = name, path
264 self.assertEqual(dialog.entry_ok(), result)
265
266
267 # 2 CustomRun test classes each test one method.
268
269 class CustomRunCLIargsokTest(unittest.TestCase):
270 "Test cli_ok method of the CustomRun subclass of Query."
271
272 class Dummy_CustomRun:
273 cli_args_ok = query.CustomRun.cli_args_ok
274 def __init__(self, dummy_entry):
275 self.entry = Var(value=dummy_entry)
276 self.entry_error = {'text': ''}
277 def showerror(self, message):
278 self.entry_error['text'] = message
279
280 def test_blank_args(self):
281 dialog = self.Dummy_CustomRun(' ')
282 self.assertEqual(dialog.cli_args_ok(), [])
283
284 def test_invalid_args(self):
285 dialog = self.Dummy_CustomRun("'no-closing-quote")
286 self.assertEqual(dialog.cli_args_ok(), None)
287 self.assertIn('No closing', dialog.entry_error['text'])
288
289 def test_good_args(self):
290 args = ['-n', '10', '--verbose', '-p', '/path', '--name']
291 dialog = self.Dummy_CustomRun(' '.join(args) + ' "my name"')
292 self.assertEqual(dialog.cli_args_ok(), args + ["my name"])
293 self.assertEqual(dialog.entry_error['text'], '')
294
295
296 class CustomRunEntryokTest(unittest.TestCase):
297 "Test entry_ok method of the CustomRun subclass of Query."
298
299 class Dummy_CustomRun:
300 entry_ok = query.CustomRun.entry_ok
301 entry_error = {}
302 restartvar = Var()
303 def cli_args_ok(self):
304 return self.cli_args
305
306 def test_entry_ok_customrun(self):
307 dialog = self.Dummy_CustomRun()
308 for restart in {True, False}:
309 dialog.restartvar.set(restart)
310 for cli_args, result in ((None, None),
311 (['my arg'], (['my arg'], restart))):
312 with self.subTest(restart=restart, cli_args=cli_args):
313 dialog.cli_args = cli_args
314 self.assertEqual(dialog.entry_ok(), result)
315
316
317 # GUI TESTS
318
319 class QueryGuiTest(unittest.TestCase):
320
321 @classmethod
322 def setUpClass(cls):
323 requires('gui')
324 cls.root = root = Tk()
325 cls.root.withdraw()
326 cls.dialog = query.Query(root, 'TEST', 'test', _utest=True)
327 cls.dialog.destroy = mock.Mock()
328
329 @classmethod
330 def tearDownClass(cls):
331 del cls.dialog.destroy
332 del cls.dialog
333 cls.root.destroy()
334 del cls.root
335
336 def setUp(self):
337 self.dialog.entry.delete(0, 'end')
338 self.dialog.result = None
339 self.dialog.destroy.reset_mock()
340
341 def test_click_ok(self):
342 dialog = self.dialog
343 dialog.entry.insert(0, 'abc')
344 dialog.button_ok.invoke()
345 self.assertEqual(dialog.result, 'abc')
346 self.assertTrue(dialog.destroy.called)
347
348 def test_click_blank(self):
349 dialog = self.dialog
350 dialog.button_ok.invoke()
351 self.assertEqual(dialog.result, None)
352 self.assertFalse(dialog.destroy.called)
353
354 def test_click_cancel(self):
355 dialog = self.dialog
356 dialog.entry.insert(0, 'abc')
357 dialog.button_cancel.invoke()
358 self.assertEqual(dialog.result, None)
359 self.assertTrue(dialog.destroy.called)
360
361
362 class SectionnameGuiTest(unittest.TestCase):
363
364 @classmethod
365 def setUpClass(cls):
366 requires('gui')
367
368 def test_click_section_name(self):
369 root = Tk()
370 root.withdraw()
371 dialog = query.SectionName(root, 'T', 't', {'abc'}, _utest=True)
372 Equal = self.assertEqual
373 self.assertEqual(dialog.used_names, {'abc'})
374 dialog.entry.insert(0, 'okay')
375 dialog.button_ok.invoke()
376 self.assertEqual(dialog.result, 'okay')
377 root.destroy()
378
379
380 class ModulenameGuiTest(unittest.TestCase, ExtraAssertions):
381
382 @classmethod
383 def setUpClass(cls):
384 requires('gui')
385
386 def test_click_module_name(self):
387 root = Tk()
388 root.withdraw()
389 dialog = query.ModuleName(root, 'T', 't', 'idlelib', _utest=True)
390 self.assertEqual(dialog.text0, 'idlelib')
391 self.assertEqual(dialog.entry.get(), 'idlelib')
392 dialog.button_ok.invoke()
393 self.assertEndsWith(dialog.result, '__init__.py')
394 root.destroy()
395
396
397 class GotoGuiTest(unittest.TestCase):
398
399 @classmethod
400 def setUpClass(cls):
401 requires('gui')
402
403 def test_click_module_name(self):
404 root = Tk()
405 root.withdraw()
406 dialog = query.Goto(root, 'T', 't', _utest=True)
407 dialog.entry.insert(0, '22')
408 dialog.button_ok.invoke()
409 self.assertEqual(dialog.result, 22)
410 root.destroy()
411
412
413 class HelpsourceGuiTest(unittest.TestCase):
414
415 @classmethod
416 def setUpClass(cls):
417 requires('gui')
418
419 def test_click_help_source(self):
420 root = Tk()
421 root.withdraw()
422 dialog = query.HelpSource(root, 'T', menuitem='__test__',
423 filepath=__file__, _utest=True)
424 Equal = self.assertEqual
425 Equal(dialog.entry.get(), '__test__')
426 Equal(dialog.path.get(), __file__)
427 dialog.button_ok.invoke()
428 prefix = "file://" if sys.platform == 'darwin' else ''
429 Equal(dialog.result, ('__test__', prefix + __file__))
430 root.destroy()
431
432
433 class CustomRunGuiTest(unittest.TestCase):
434
435 @classmethod
436 def setUpClass(cls):
437 requires('gui')
438
439 def test_click_args(self):
440 root = Tk()
441 root.withdraw()
442 dialog = query.CustomRun(root, 'Title',
443 cli_args=['a', 'b=1'], _utest=True)
444 self.assertEqual(dialog.entry.get(), 'a b=1')
445 dialog.entry.insert(END, ' c')
446 dialog.button_ok.invoke()
447 self.assertEqual(dialog.result, (['a', 'b=1', 'c'], True))
448 root.destroy()
449
450
451 if __name__ == '__main__':
452 unittest.main(verbosity=2, exit=False)