test_search.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "Test search, coverage 69%."
  2. from idlelib import search
  3. import unittest
  4. from test.support import requires
  5. requires('gui')
  6. from tkinter import Tk, Text, BooleanVar
  7. from idlelib import searchengine
  8. # Does not currently test the event handler wrappers.
  9. # A usage test should simulate clicks and check highlighting.
  10. # Tests need to be coordinated with SearchDialogBase tests
  11. # to avoid duplication.
  12. class SearchDialogTest(unittest.TestCase):
  13. @classmethod
  14. def setUpClass(cls):
  15. cls.root = Tk()
  16. @classmethod
  17. def tearDownClass(cls):
  18. cls.root.destroy()
  19. del cls.root
  20. def setUp(self):
  21. self.engine = searchengine.SearchEngine(self.root)
  22. self.dialog = search.SearchDialog(self.root, self.engine)
  23. self.dialog.bell = lambda: None
  24. self.text = Text(self.root)
  25. self.text.insert('1.0', 'Hello World!')
  26. def test_find_again(self):
  27. # Search for various expressions
  28. text = self.text
  29. self.engine.setpat('')
  30. self.assertFalse(self.dialog.find_again(text))
  31. self.dialog.bell = lambda: None
  32. self.engine.setpat('Hello')
  33. self.assertTrue(self.dialog.find_again(text))
  34. self.engine.setpat('Goodbye')
  35. self.assertFalse(self.dialog.find_again(text))
  36. self.engine.setpat('World!')
  37. self.assertTrue(self.dialog.find_again(text))
  38. self.engine.setpat('Hello World!')
  39. self.assertTrue(self.dialog.find_again(text))
  40. # Regular expression
  41. self.engine.revar = BooleanVar(self.root, True)
  42. self.engine.setpat('W[aeiouy]r')
  43. self.assertTrue(self.dialog.find_again(text))
  44. def test_find_selection(self):
  45. # Select some text and make sure it's found
  46. text = self.text
  47. # Add additional line to find
  48. self.text.insert('2.0', 'Hello World!')
  49. text.tag_add('sel', '1.0', '1.4') # Select 'Hello'
  50. self.assertTrue(self.dialog.find_selection(text))
  51. text.tag_remove('sel', '1.0', 'end')
  52. text.tag_add('sel', '1.6', '1.11') # Select 'World!'
  53. self.assertTrue(self.dialog.find_selection(text))
  54. text.tag_remove('sel', '1.0', 'end')
  55. text.tag_add('sel', '1.0', '1.11') # Select 'Hello World!'
  56. self.assertTrue(self.dialog.find_selection(text))
  57. # Remove additional line
  58. text.delete('2.0', 'end')
  59. if __name__ == '__main__':
  60. unittest.main(verbosity=2, exit=2)