test_undo.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "Test undo, coverage 77%."
  2. # Only test UndoDelegator so far.
  3. from idlelib.undo import UndoDelegator
  4. import unittest
  5. from test.support import requires
  6. requires('gui')
  7. from unittest.mock import Mock
  8. from tkinter import Text, Tk
  9. from idlelib.percolator import Percolator
  10. class UndoDelegatorTest(unittest.TestCase):
  11. @classmethod
  12. def setUpClass(cls):
  13. cls.root = Tk()
  14. cls.text = Text(cls.root)
  15. cls.percolator = Percolator(cls.text)
  16. @classmethod
  17. def tearDownClass(cls):
  18. cls.percolator.redir.close()
  19. del cls.percolator, cls.text
  20. cls.root.destroy()
  21. del cls.root
  22. def setUp(self):
  23. self.delegator = UndoDelegator()
  24. self.delegator.bell = Mock()
  25. self.percolator.insertfilter(self.delegator)
  26. def tearDown(self):
  27. self.percolator.removefilter(self.delegator)
  28. self.text.delete('1.0', 'end')
  29. self.delegator.resetcache()
  30. def test_undo_event(self):
  31. text = self.text
  32. text.insert('insert', 'foobar')
  33. text.insert('insert', 'h')
  34. text.event_generate('<<undo>>')
  35. self.assertEqual(text.get('1.0', 'end'), '\n')
  36. text.insert('insert', 'foo')
  37. text.insert('insert', 'bar')
  38. text.delete('1.2', '1.4')
  39. text.insert('insert', 'hello')
  40. text.event_generate('<<undo>>')
  41. self.assertEqual(text.get('1.0', '1.4'), 'foar')
  42. text.event_generate('<<undo>>')
  43. self.assertEqual(text.get('1.0', '1.6'), 'foobar')
  44. text.event_generate('<<undo>>')
  45. self.assertEqual(text.get('1.0', '1.3'), 'foo')
  46. text.event_generate('<<undo>>')
  47. self.delegator.undo_event('event')
  48. self.assertTrue(self.delegator.bell.called)
  49. def test_redo_event(self):
  50. text = self.text
  51. text.insert('insert', 'foo')
  52. text.insert('insert', 'bar')
  53. text.delete('1.0', '1.3')
  54. text.event_generate('<<undo>>')
  55. text.event_generate('<<redo>>')
  56. self.assertEqual(text.get('1.0', '1.3'), 'bar')
  57. text.event_generate('<<redo>>')
  58. self.assertTrue(self.delegator.bell.called)
  59. def test_dump_event(self):
  60. """
  61. Dump_event cannot be tested directly without changing
  62. environment variables. So, test statements in dump_event
  63. indirectly
  64. """
  65. text = self.text
  66. d = self.delegator
  67. text.insert('insert', 'foo')
  68. text.insert('insert', 'bar')
  69. text.delete('1.2', '1.4')
  70. self.assertTupleEqual((d.pointer, d.can_merge), (3, True))
  71. text.event_generate('<<undo>>')
  72. self.assertTupleEqual((d.pointer, d.can_merge), (2, False))
  73. def test_get_set_saved(self):
  74. # test the getter method get_saved
  75. # test the setter method set_saved
  76. # indirectly test check_saved
  77. d = self.delegator
  78. self.assertTrue(d.get_saved())
  79. self.text.insert('insert', 'a')
  80. self.assertFalse(d.get_saved())
  81. d.saved_change_hook = Mock()
  82. d.set_saved(True)
  83. self.assertEqual(d.pointer, d.saved)
  84. self.assertTrue(d.saved_change_hook.called)
  85. d.set_saved(False)
  86. self.assertEqual(d.saved, -1)
  87. self.assertTrue(d.saved_change_hook.called)
  88. def test_undo_start_stop(self):
  89. # test the undo_block_start and undo_block_stop methods
  90. text = self.text
  91. text.insert('insert', 'foo')
  92. self.delegator.undo_block_start()
  93. text.insert('insert', 'bar')
  94. text.insert('insert', 'bar')
  95. self.delegator.undo_block_stop()
  96. self.assertEqual(text.get('1.0', '1.3'), 'foo')
  97. # test another code path
  98. self.delegator.undo_block_start()
  99. text.insert('insert', 'bar')
  100. self.delegator.undo_block_stop()
  101. self.assertEqual(text.get('1.0', '1.3'), 'foo')
  102. def test_addcmd(self):
  103. text = self.text
  104. # when number of undo operations exceeds max_undo
  105. self.delegator.max_undo = max_undo = 10
  106. for i in range(max_undo + 10):
  107. text.insert('insert', 'foo')
  108. self.assertLessEqual(len(self.delegator.undolist), max_undo)
  109. if __name__ == '__main__':
  110. unittest.main(verbosity=2, exit=False)