test_searchbase.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "Test searchbase, coverage 98%."
  2. # The only thing not covered is inconsequential --
  3. # testing skipping of suite when self.needwrapbutton is false.
  4. import unittest
  5. from test.support import requires
  6. from tkinter import Text, Tk, Toplevel
  7. from tkinter.ttk import Frame
  8. from idlelib import searchengine as se
  9. from idlelib import searchbase as sdb
  10. from idlelib.idle_test.mock_idle import Func
  11. ## from idlelib.idle_test.mock_tk import Var
  12. # The ## imports above & following could help make some tests gui-free.
  13. # However, they currently make radiobutton tests fail.
  14. ##def setUpModule():
  15. ## # Replace tk objects used to initialize se.SearchEngine.
  16. ## se.BooleanVar = Var
  17. ## se.StringVar = Var
  18. ##
  19. ##def tearDownModule():
  20. ## se.BooleanVar = BooleanVar
  21. ## se.StringVar = StringVar
  22. class SearchDialogBaseTest(unittest.TestCase):
  23. @classmethod
  24. def setUpClass(cls):
  25. requires('gui')
  26. cls.root = Tk()
  27. @classmethod
  28. def tearDownClass(cls):
  29. cls.root.update_idletasks()
  30. cls.root.destroy()
  31. del cls.root
  32. def setUp(self):
  33. self.engine = se.SearchEngine(self.root) # None also seems to work
  34. self.dialog = sdb.SearchDialogBase(root=self.root, engine=self.engine)
  35. def tearDown(self):
  36. self.dialog.close()
  37. def test_open_and_close(self):
  38. # open calls create_widgets, which needs default_command
  39. self.dialog.default_command = None
  40. toplevel = Toplevel(self.root)
  41. text = Text(toplevel)
  42. self.dialog.open(text)
  43. self.assertEqual(self.dialog.top.state(), 'normal')
  44. self.dialog.close()
  45. self.assertEqual(self.dialog.top.state(), 'withdrawn')
  46. self.dialog.open(text, searchphrase="hello")
  47. self.assertEqual(self.dialog.ent.get(), 'hello')
  48. toplevel.update_idletasks()
  49. toplevel.destroy()
  50. def test_create_widgets(self):
  51. self.dialog.create_entries = Func()
  52. self.dialog.create_option_buttons = Func()
  53. self.dialog.create_other_buttons = Func()
  54. self.dialog.create_command_buttons = Func()
  55. self.dialog.default_command = None
  56. self.dialog.create_widgets()
  57. self.assertTrue(self.dialog.create_entries.called)
  58. self.assertTrue(self.dialog.create_option_buttons.called)
  59. self.assertTrue(self.dialog.create_other_buttons.called)
  60. self.assertTrue(self.dialog.create_command_buttons.called)
  61. def test_make_entry(self):
  62. equal = self.assertEqual
  63. self.dialog.row = 0
  64. self.dialog.frame = Frame(self.root)
  65. entry, label = self.dialog.make_entry("Test:", 'hello')
  66. equal(label['text'], 'Test:')
  67. self.assertIn(entry.get(), 'hello')
  68. egi = entry.grid_info()
  69. equal(int(egi['row']), 0)
  70. equal(int(egi['column']), 1)
  71. equal(int(egi['rowspan']), 1)
  72. equal(int(egi['columnspan']), 1)
  73. equal(self.dialog.row, 1)
  74. def test_create_entries(self):
  75. self.dialog.frame = Frame(self.root)
  76. self.dialog.row = 0
  77. self.engine.setpat('hello')
  78. self.dialog.create_entries()
  79. self.assertIn(self.dialog.ent.get(), 'hello')
  80. def test_make_frame(self):
  81. self.dialog.row = 0
  82. self.dialog.frame = Frame(self.root)
  83. frame, label = self.dialog.make_frame()
  84. self.assertEqual(label, '')
  85. self.assertEqual(str(type(frame)), "<class 'tkinter.ttk.Frame'>")
  86. # self.assertIsInstance(frame, Frame) fails when test is run by
  87. # test_idle not run from IDLE editor. See issue 33987 PR.
  88. frame, label = self.dialog.make_frame('testlabel')
  89. self.assertEqual(label['text'], 'testlabel')
  90. def btn_test_setup(self, meth):
  91. self.dialog.frame = Frame(self.root)
  92. self.dialog.row = 0
  93. return meth()
  94. def test_create_option_buttons(self):
  95. e = self.engine
  96. for state in (0, 1):
  97. for var in (e.revar, e.casevar, e.wordvar, e.wrapvar):
  98. var.set(state)
  99. frame, options = self.btn_test_setup(
  100. self.dialog.create_option_buttons)
  101. for spec, button in zip (options, frame.pack_slaves()):
  102. var, label = spec
  103. self.assertEqual(button['text'], label)
  104. self.assertEqual(var.get(), state)
  105. def test_create_other_buttons(self):
  106. for state in (False, True):
  107. var = self.engine.backvar
  108. var.set(state)
  109. frame, others = self.btn_test_setup(
  110. self.dialog.create_other_buttons)
  111. buttons = frame.pack_slaves()
  112. for spec, button in zip(others, buttons):
  113. val, label = spec
  114. self.assertEqual(button['text'], label)
  115. if val == state:
  116. # hit other button, then this one
  117. # indexes depend on button order
  118. self.assertEqual(var.get(), state)
  119. def test_make_button(self):
  120. self.dialog.frame = Frame(self.root)
  121. self.dialog.buttonframe = Frame(self.dialog.frame)
  122. btn = self.dialog.make_button('Test', self.dialog.close)
  123. self.assertEqual(btn['text'], 'Test')
  124. def test_create_command_buttons(self):
  125. self.dialog.frame = Frame(self.root)
  126. self.dialog.create_command_buttons()
  127. # Look for close button command in buttonframe
  128. closebuttoncommand = ''
  129. for child in self.dialog.buttonframe.winfo_children():
  130. if child['text'] == 'Close':
  131. closebuttoncommand = child['command']
  132. self.assertIn('close', closebuttoncommand)
  133. if __name__ == '__main__':
  134. unittest.main(verbosity=2, exit=2)