bpo-5680: IDLE: Customize running a module (GH-13763) · python/cpython@ae526ee
1-"""Test query, coverage 91%).
1+"""Test query, coverage 93%).
2233Non-gui tests for Query, SectionName, ModuleName, and HelpSource use
44dummy versions that extract the non-gui methods and add other needed
@@ -30,11 +30,9 @@ class Dummy_Query:
3030ok = query.Query.ok
3131cancel = query.Query.cancel
3232# Add attributes and initialization needed for tests.
33-entry = Var()
34-entry_error = {}
3533def __init__(self, dummy_entry):
36-self.entry.set(dummy_entry)
37-self.entry_error['text'] = ''
34+self.entry = Var(value=dummy_entry)
35+self.entry_error = {'text': ''}
3836self.result = None
3937self.destroyed = False
4038def showerror(self, message):
@@ -80,11 +78,9 @@ class SectionNameTest(unittest.TestCase):
8078class Dummy_SectionName:
8179entry_ok = query.SectionName.entry_ok # Function being tested.
8280used_names = ['used']
83-entry = Var()
84-entry_error = {}
8581def __init__(self, dummy_entry):
86-self.entry.set(dummy_entry)
87-self.entry_error['text'] = ''
82+self.entry = Var(value=dummy_entry)
83+self.entry_error = {'text': ''}
8884def showerror(self, message):
8985self.entry_error['text'] = message
9086@@ -115,11 +111,9 @@ class ModuleNameTest(unittest.TestCase):
115111class Dummy_ModuleName:
116112entry_ok = query.ModuleName.entry_ok # Function being tested.
117113text0 = ''
118-entry = Var()
119-entry_error = {}
120114def __init__(self, dummy_entry):
121-self.entry.set(dummy_entry)
122-self.entry_error['text'] = ''
115+self.entry = Var(value=dummy_entry)
116+self.entry_error = {'text': ''}
123117def showerror(self, message):
124118self.entry_error['text'] = message
125119@@ -144,9 +138,7 @@ def test_good_module_name(self):
144138self.assertEqual(dialog.entry_error['text'], '')
145139146140147-# 3 HelpSource test classes each test one function.
148-149-orig_platform = query.platform
141+# 3 HelpSource test classes each test one method.
150142151143class HelpsourceBrowsefileTest(unittest.TestCase):
152144"Test browse_file method of ModuleName subclass of Query."
@@ -178,17 +170,16 @@ class HelpsourcePathokTest(unittest.TestCase):
178170179171class Dummy_HelpSource:
180172path_ok = query.HelpSource.path_ok
181-path = Var()
182-path_error = {}
183173def __init__(self, dummy_path):
184-self.path.set(dummy_path)
185-self.path_error['text'] = ''
174+self.path = Var(value=dummy_path)
175+self.path_error = {'text': ''}
186176def showerror(self, message, widget=None):
187177self.path_error['text'] = message
188178179+orig_platform = query.platform # Set in test_path_ok_file.
189180@classmethod
190181def tearDownClass(cls):
191-query.platform = orig_platform
182+query.platform = cls.orig_platform
192183193184def test_path_ok_blank(self):
194185dialog = self.Dummy_HelpSource(' ')
@@ -242,6 +233,56 @@ def test_entry_ok_helpsource(self):
242233self.assertEqual(dialog.entry_ok(), result)
243234244235236+# 2 CustomRun test classes each test one method.
237+238+class CustomRunCLIargsokTest(unittest.TestCase):
239+"Test cli_ok method of the CustomRun subclass of Query."
240+241+class Dummy_CustomRun:
242+cli_args_ok = query.CustomRun.cli_args_ok
243+def __init__(self, dummy_entry):
244+self.entry = Var(value=dummy_entry)
245+self.entry_error = {'text': ''}
246+def showerror(self, message):
247+self.entry_error['text'] = message
248+249+def test_blank_args(self):
250+dialog = self.Dummy_CustomRun(' ')
251+self.assertEqual(dialog.cli_args_ok(), [])
252+253+def test_invalid_args(self):
254+dialog = self.Dummy_CustomRun("'no-closing-quote")
255+self.assertEqual(dialog.cli_args_ok(), None)
256+self.assertIn('No closing', dialog.entry_error['text'])
257+258+def test_good_args(self):
259+args = ['-n', '10', '--verbose', '-p', '/path', '--name']
260+dialog = self.Dummy_CustomRun(' '.join(args) + ' "my name"')
261+self.assertEqual(dialog.cli_args_ok(), args + ["my name"])
262+self.assertEqual(dialog.entry_error['text'], '')
263+264+265+class CustomRunEntryokTest(unittest.TestCase):
266+"Test entry_ok method of the CustomRun subclass of Query."
267+268+class Dummy_CustomRun:
269+entry_ok = query.CustomRun.entry_ok
270+entry_error = {}
271+restartvar = Var()
272+def cli_args_ok(self):
273+return self.cli_args
274+275+def test_entry_ok_customrun(self):
276+dialog = self.Dummy_CustomRun()
277+for restart in {True, False}:
278+dialog.restartvar.set(restart)
279+for cli_args, result in ((None, None),
280+ (['my arg'], (['my arg'], restart))):
281+with self.subTest(restart=restart, cli_args=cli_args):
282+dialog.cli_args = cli_args
283+self.assertEqual(dialog.entry_ok(), result)
284+285+245286# GUI TESTS
246287247288class QueryGuiTest(unittest.TestCase):
@@ -302,9 +343,7 @@ def test_click_section_name(self):
302343dialog.entry.insert(0, 'okay')
303344dialog.button_ok.invoke()
304345self.assertEqual(dialog.result, 'okay')
305-del dialog
306346root.destroy()
307-del root
308347309348310349class ModulenameGuiTest(unittest.TestCase):
@@ -321,9 +360,7 @@ def test_click_module_name(self):
321360self.assertEqual(dialog.entry.get(), 'idlelib')
322361dialog.button_ok.invoke()
323362self.assertTrue(dialog.result.endswith('__init__.py'))
324-del dialog
325363root.destroy()
326-del root
327364328365329366class HelpsourceGuiTest(unittest.TestCase):
@@ -343,9 +380,23 @@ def test_click_help_source(self):
343380dialog.button_ok.invoke()
344381prefix = "file://" if sys.platform == 'darwin' else ''
345382Equal(dialog.result, ('__test__', prefix + __file__))
346-del dialog
347383root.destroy()
348-del root
384+385+386+class CustomRunGuiTest(unittest.TestCase):
387+388+@classmethod
389+def setUpClass(cls):
390+requires('gui')
391+392+def test_click_args(self):
393+root = Tk()
394+root.withdraw()
395+dialog = query.CustomRun(root, 'Title', _utest=True)
396+dialog.entry.insert(0, 'okay')
397+dialog.button_ok.invoke()
398+self.assertEqual(dialog.result, (['okay'], True))
399+root.destroy()
349400350401351402if __name__ == '__main__':