test_history.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. " Test history, coverage 100%."
  2. from idlelib.history import History
  3. import unittest
  4. from test.support import requires
  5. import tkinter as tk
  6. from tkinter import Text as tkText
  7. from idlelib.idle_test.mock_tk import Text as mkText
  8. from idlelib.config import idleConf
  9. line1 = 'a = 7'
  10. line2 = 'b = a'
  11. class StoreTest(unittest.TestCase):
  12. '''Tests History.__init__ and History.store with mock Text'''
  13. @classmethod
  14. def setUpClass(cls):
  15. cls.text = mkText()
  16. cls.history = History(cls.text)
  17. def tearDown(self):
  18. self.text.delete('1.0', 'end')
  19. self.history.history = []
  20. def test_init(self):
  21. self.assertIs(self.history.text, self.text)
  22. self.assertEqual(self.history.history, [])
  23. self.assertIsNone(self.history.prefix)
  24. self.assertIsNone(self.history.pointer)
  25. self.assertEqual(self.history.cyclic,
  26. idleConf.GetOption("main", "History", "cyclic", 1, "bool"))
  27. def test_store_short(self):
  28. self.history.store('a')
  29. self.assertEqual(self.history.history, [])
  30. self.history.store(' a ')
  31. self.assertEqual(self.history.history, [])
  32. def test_store_dup(self):
  33. self.history.store(line1)
  34. self.assertEqual(self.history.history, [line1])
  35. self.history.store(line2)
  36. self.assertEqual(self.history.history, [line1, line2])
  37. self.history.store(line1)
  38. self.assertEqual(self.history.history, [line2, line1])
  39. def test_store_reset(self):
  40. self.history.prefix = line1
  41. self.history.pointer = 0
  42. self.history.store(line2)
  43. self.assertIsNone(self.history.prefix)
  44. self.assertIsNone(self.history.pointer)
  45. class TextWrapper:
  46. def __init__(self, master):
  47. self.text = tkText(master=master)
  48. self._bell = False
  49. def __getattr__(self, name):
  50. return getattr(self.text, name)
  51. def bell(self):
  52. self._bell = True
  53. class FetchTest(unittest.TestCase):
  54. '''Test History.fetch with wrapped tk.Text.
  55. '''
  56. @classmethod
  57. def setUpClass(cls):
  58. requires('gui')
  59. cls.root = tk.Tk()
  60. cls.root.withdraw()
  61. def setUp(self):
  62. self.text = text = TextWrapper(self.root)
  63. text.insert('1.0', ">>> ")
  64. text.mark_set('iomark', '1.4')
  65. text.mark_gravity('iomark', 'left')
  66. self.history = History(text)
  67. self.history.history = [line1, line2]
  68. @classmethod
  69. def tearDownClass(cls):
  70. cls.root.destroy()
  71. del cls.root
  72. def fetch_test(self, reverse, line, prefix, index, *, bell=False):
  73. # Perform one fetch as invoked by Alt-N or Alt-P
  74. # Test the result. The line test is the most important.
  75. # The last two are diagnostic of fetch internals.
  76. History = self.history
  77. History.fetch(reverse)
  78. Equal = self.assertEqual
  79. Equal(self.text.get('iomark', 'end-1c'), line)
  80. Equal(self.text._bell, bell)
  81. if bell:
  82. self.text._bell = False
  83. Equal(History.prefix, prefix)
  84. Equal(History.pointer, index)
  85. Equal(self.text.compare("insert", '==', "end-1c"), 1)
  86. def test_fetch_prev_cyclic(self):
  87. prefix = ''
  88. test = self.fetch_test
  89. test(True, line2, prefix, 1)
  90. test(True, line1, prefix, 0)
  91. test(True, prefix, None, None, bell=True)
  92. def test_fetch_next_cyclic(self):
  93. prefix = ''
  94. test = self.fetch_test
  95. test(False, line1, prefix, 0)
  96. test(False, line2, prefix, 1)
  97. test(False, prefix, None, None, bell=True)
  98. # Prefix 'a' tests skip line2, which starts with 'b'
  99. def test_fetch_prev_prefix(self):
  100. prefix = 'a'
  101. self.text.insert('iomark', prefix)
  102. self.fetch_test(True, line1, prefix, 0)
  103. self.fetch_test(True, prefix, None, None, bell=True)
  104. def test_fetch_next_prefix(self):
  105. prefix = 'a'
  106. self.text.insert('iomark', prefix)
  107. self.fetch_test(False, line1, prefix, 0)
  108. self.fetch_test(False, prefix, None, None, bell=True)
  109. def test_fetch_prev_noncyclic(self):
  110. prefix = ''
  111. self.history.cyclic = False
  112. test = self.fetch_test
  113. test(True, line2, prefix, 1)
  114. test(True, line1, prefix, 0)
  115. test(True, line1, prefix, 0, bell=True)
  116. def test_fetch_next_noncyclic(self):
  117. prefix = ''
  118. self.history.cyclic = False
  119. test = self.fetch_test
  120. test(False, prefix, None, None, bell=True)
  121. test(True, line2, prefix, 1)
  122. test(False, prefix, None, None, bell=True)
  123. test(False, prefix, None, None, bell=True)
  124. def test_fetch_cursor_move(self):
  125. # Move cursor after fetch
  126. self.history.fetch(reverse=True) # initialization
  127. self.text.mark_set('insert', 'iomark')
  128. self.fetch_test(True, line2, None, None, bell=True)
  129. def test_fetch_edit(self):
  130. # Edit after fetch
  131. self.history.fetch(reverse=True) # initialization
  132. self.text.delete('iomark', 'insert', )
  133. self.text.insert('iomark', 'a =')
  134. self.fetch_test(True, line1, 'a =', 0) # prefix is reset
  135. def test_history_prev_next(self):
  136. # Minimally test functions bound to events
  137. self.history.history_prev('dummy event')
  138. self.assertEqual(self.history.pointer, 1)
  139. self.history.history_next('dummy event')
  140. self.assertEqual(self.history.pointer, None)
  141. if __name__ == '__main__':
  142. unittest.main(verbosity=2, exit=2)