test_query.py 15 KB

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