test_autoexpand.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. "Test autoexpand, coverage 100%."
  2. from idlelib.autoexpand import AutoExpand
  3. import unittest
  4. from test.support import requires
  5. from tkinter import Text, Tk
  6. class DummyEditwin:
  7. # AutoExpand.__init__ only needs .text
  8. def __init__(self, text):
  9. self.text = text
  10. class AutoExpandTest(unittest.TestCase):
  11. @classmethod
  12. def setUpClass(cls):
  13. requires('gui')
  14. cls.tk = Tk()
  15. cls.text = Text(cls.tk)
  16. cls.auto_expand = AutoExpand(DummyEditwin(cls.text))
  17. cls.auto_expand.bell = lambda: None
  18. # If mock_tk.Text._decode understood indexes 'insert' with suffixed 'linestart',
  19. # 'wordstart', and 'lineend', used by autoexpand, we could use the following
  20. # to run these test on non-gui machines (but check bell).
  21. ## try:
  22. ## requires('gui')
  23. ## #raise ResourceDenied() # Uncomment to test mock.
  24. ## except ResourceDenied:
  25. ## from idlelib.idle_test.mock_tk import Text
  26. ## cls.text = Text()
  27. ## cls.text.bell = lambda: None
  28. ## else:
  29. ## from tkinter import Tk, Text
  30. ## cls.tk = Tk()
  31. ## cls.text = Text(cls.tk)
  32. @classmethod
  33. def tearDownClass(cls):
  34. del cls.text, cls.auto_expand
  35. if hasattr(cls, 'tk'):
  36. cls.tk.destroy()
  37. del cls.tk
  38. def tearDown(self):
  39. self.text.delete('1.0', 'end')
  40. def test_get_prevword(self):
  41. text = self.text
  42. previous = self.auto_expand.getprevword
  43. equal = self.assertEqual
  44. equal(previous(), '')
  45. text.insert('insert', 't')
  46. equal(previous(), 't')
  47. text.insert('insert', 'his')
  48. equal(previous(), 'this')
  49. text.insert('insert', ' ')
  50. equal(previous(), '')
  51. text.insert('insert', 'is')
  52. equal(previous(), 'is')
  53. text.insert('insert', '\nsample\nstring')
  54. equal(previous(), 'string')
  55. text.delete('3.0', 'insert')
  56. equal(previous(), '')
  57. text.delete('1.0', 'end')
  58. equal(previous(), '')
  59. def test_before_only(self):
  60. previous = self.auto_expand.getprevword
  61. expand = self.auto_expand.expand_word_event
  62. equal = self.assertEqual
  63. self.text.insert('insert', 'ab ac bx ad ab a')
  64. equal(self.auto_expand.getwords(), ['ab', 'ad', 'ac', 'a'])
  65. expand('event')
  66. equal(previous(), 'ab')
  67. expand('event')
  68. equal(previous(), 'ad')
  69. expand('event')
  70. equal(previous(), 'ac')
  71. expand('event')
  72. equal(previous(), 'a')
  73. def test_after_only(self):
  74. # Also add punctuation 'noise' that should be ignored.
  75. text = self.text
  76. previous = self.auto_expand.getprevword
  77. expand = self.auto_expand.expand_word_event
  78. equal = self.assertEqual
  79. text.insert('insert', 'a, [ab] ac: () bx"" cd ac= ad ya')
  80. text.mark_set('insert', '1.1')
  81. equal(self.auto_expand.getwords(), ['ab', 'ac', 'ad', 'a'])
  82. expand('event')
  83. equal(previous(), 'ab')
  84. expand('event')
  85. equal(previous(), 'ac')
  86. expand('event')
  87. equal(previous(), 'ad')
  88. expand('event')
  89. equal(previous(), 'a')
  90. def test_both_before_after(self):
  91. text = self.text
  92. previous = self.auto_expand.getprevword
  93. expand = self.auto_expand.expand_word_event
  94. equal = self.assertEqual
  95. text.insert('insert', 'ab xy yz\n')
  96. text.insert('insert', 'a ac by ac')
  97. text.mark_set('insert', '2.1')
  98. equal(self.auto_expand.getwords(), ['ab', 'ac', 'a'])
  99. expand('event')
  100. equal(previous(), 'ab')
  101. expand('event')
  102. equal(previous(), 'ac')
  103. expand('event')
  104. equal(previous(), 'a')
  105. def test_other_expand_cases(self):
  106. text = self.text
  107. expand = self.auto_expand.expand_word_event
  108. equal = self.assertEqual
  109. # no expansion candidate found
  110. equal(self.auto_expand.getwords(), [])
  111. equal(expand('event'), 'break')
  112. text.insert('insert', 'bx cy dz a')
  113. equal(self.auto_expand.getwords(), [])
  114. # reset state by successfully expanding once
  115. # move cursor to another position and expand again
  116. text.insert('insert', 'ac xy a ac ad a')
  117. text.mark_set('insert', '1.7')
  118. expand('event')
  119. initial_state = self.auto_expand.state
  120. text.mark_set('insert', '1.end')
  121. expand('event')
  122. new_state = self.auto_expand.state
  123. self.assertNotEqual(initial_state, new_state)
  124. if __name__ == '__main__':
  125. unittest.main(verbosity=2)