test_text.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. ''' Test mock_tk.Text class against tkinter.Text class
  2. Run same tests with both by creating a mixin class.
  3. '''
  4. import unittest
  5. from test.support import requires
  6. from _tkinter import TclError
  7. class TextTest:
  8. "Define items common to both sets of tests."
  9. hw = 'hello\nworld' # Several tests insert this after initialization.
  10. hwn = hw+'\n' # \n present at initialization, before insert
  11. # setUpClass defines cls.Text and maybe cls.root.
  12. # setUp defines self.text from Text and maybe root.
  13. def test_init(self):
  14. self.assertEqual(self.text.get('1.0'), '\n')
  15. self.assertEqual(self.text.get('end'), '')
  16. def test_index_empty(self):
  17. index = self.text.index
  18. for dex in (-1.0, 0.3, '1.-1', '1.0', '1.0 lineend', '1.end', '1.33',
  19. 'insert'):
  20. self.assertEqual(index(dex), '1.0')
  21. for dex in 'end', 2.0, '2.1', '33.44':
  22. self.assertEqual(index(dex), '2.0')
  23. def test_index_data(self):
  24. index = self.text.index
  25. self.text.insert('1.0', self.hw)
  26. for dex in -1.0, 0.3, '1.-1', '1.0':
  27. self.assertEqual(index(dex), '1.0')
  28. for dex in '1.0 lineend', '1.end', '1.33':
  29. self.assertEqual(index(dex), '1.5')
  30. for dex in 'end', '33.44':
  31. self.assertEqual(index(dex), '3.0')
  32. def test_get(self):
  33. get = self.text.get
  34. Equal = self.assertEqual
  35. self.text.insert('1.0', self.hw)
  36. Equal(get('end'), '')
  37. Equal(get('end', 'end'), '')
  38. Equal(get('1.0'), 'h')
  39. Equal(get('1.0', '1.1'), 'h')
  40. Equal(get('1.0', '1.3'), 'hel')
  41. Equal(get('1.1', '1.3'), 'el')
  42. Equal(get('1.0', '1.0 lineend'), 'hello')
  43. Equal(get('1.0', '1.10'), 'hello')
  44. Equal(get('1.0 lineend'), '\n')
  45. Equal(get('1.1', '2.3'), 'ello\nwor')
  46. Equal(get('1.0', '2.5'), self.hw)
  47. Equal(get('1.0', 'end'), self.hwn)
  48. Equal(get('0.0', '5.0'), self.hwn)
  49. def test_insert(self):
  50. insert = self.text.insert
  51. get = self.text.get
  52. Equal = self.assertEqual
  53. insert('1.0', self.hw)
  54. Equal(get('1.0', 'end'), self.hwn)
  55. insert('1.0', '') # nothing
  56. Equal(get('1.0', 'end'), self.hwn)
  57. insert('1.0', '*')
  58. Equal(get('1.0', 'end'), '*hello\nworld\n')
  59. insert('1.0 lineend', '*')
  60. Equal(get('1.0', 'end'), '*hello*\nworld\n')
  61. insert('2.3', '*')
  62. Equal(get('1.0', 'end'), '*hello*\nwor*ld\n')
  63. insert('end', 'x')
  64. Equal(get('1.0', 'end'), '*hello*\nwor*ldx\n')
  65. insert('1.4', 'x\n')
  66. Equal(get('1.0', 'end'), '*helx\nlo*\nwor*ldx\n')
  67. def test_no_delete(self):
  68. # if index1 == 'insert' or 'end' or >= end, there is no deletion
  69. delete = self.text.delete
  70. get = self.text.get
  71. Equal = self.assertEqual
  72. self.text.insert('1.0', self.hw)
  73. delete('insert')
  74. Equal(get('1.0', 'end'), self.hwn)
  75. delete('end')
  76. Equal(get('1.0', 'end'), self.hwn)
  77. delete('insert', 'end')
  78. Equal(get('1.0', 'end'), self.hwn)
  79. delete('insert', '5.5')
  80. Equal(get('1.0', 'end'), self.hwn)
  81. delete('1.4', '1.0')
  82. Equal(get('1.0', 'end'), self.hwn)
  83. delete('1.4', '1.4')
  84. Equal(get('1.0', 'end'), self.hwn)
  85. def test_delete_char(self):
  86. delete = self.text.delete
  87. get = self.text.get
  88. Equal = self.assertEqual
  89. self.text.insert('1.0', self.hw)
  90. delete('1.0')
  91. Equal(get('1.0', '1.end'), 'ello')
  92. delete('1.0', '1.1')
  93. Equal(get('1.0', '1.end'), 'llo')
  94. # delete \n and combine 2 lines into 1
  95. delete('1.end')
  96. Equal(get('1.0', '1.end'), 'lloworld')
  97. self.text.insert('1.3', '\n')
  98. delete('1.10')
  99. Equal(get('1.0', '1.end'), 'lloworld')
  100. self.text.insert('1.3', '\n')
  101. delete('1.3', '2.0')
  102. Equal(get('1.0', '1.end'), 'lloworld')
  103. def test_delete_slice(self):
  104. delete = self.text.delete
  105. get = self.text.get
  106. Equal = self.assertEqual
  107. self.text.insert('1.0', self.hw)
  108. delete('1.0', '1.0 lineend')
  109. Equal(get('1.0', 'end'), '\nworld\n')
  110. delete('1.0', 'end')
  111. Equal(get('1.0', 'end'), '\n')
  112. self.text.insert('1.0', self.hw)
  113. delete('1.0', '2.0')
  114. Equal(get('1.0', 'end'), 'world\n')
  115. delete('1.0', 'end')
  116. Equal(get('1.0', 'end'), '\n')
  117. self.text.insert('1.0', self.hw)
  118. delete('1.2', '2.3')
  119. Equal(get('1.0', 'end'), 'held\n')
  120. def test_multiple_lines(self): # insert and delete
  121. self.text.insert('1.0', 'hello')
  122. self.text.insert('1.3', '1\n2\n3\n4\n5')
  123. self.assertEqual(self.text.get('1.0', 'end'), 'hel1\n2\n3\n4\n5lo\n')
  124. self.text.delete('1.3', '5.1')
  125. self.assertEqual(self.text.get('1.0', 'end'), 'hello\n')
  126. def test_compare(self):
  127. compare = self.text.compare
  128. Equal = self.assertEqual
  129. # need data so indexes not squished to 1,0
  130. self.text.insert('1.0', 'First\nSecond\nThird\n')
  131. self.assertRaises(TclError, compare, '2.2', 'op', '2.2')
  132. for op, less1, less0, equal, greater0, greater1 in (
  133. ('<', True, True, False, False, False),
  134. ('<=', True, True, True, False, False),
  135. ('>', False, False, False, True, True),
  136. ('>=', False, False, True, True, True),
  137. ('==', False, False, True, False, False),
  138. ('!=', True, True, False, True, True),
  139. ):
  140. Equal(compare('1.1', op, '2.2'), less1, op)
  141. Equal(compare('2.1', op, '2.2'), less0, op)
  142. Equal(compare('2.2', op, '2.2'), equal, op)
  143. Equal(compare('2.3', op, '2.2'), greater0, op)
  144. Equal(compare('3.3', op, '2.2'), greater1, op)
  145. class MockTextTest(TextTest, unittest.TestCase):
  146. @classmethod
  147. def setUpClass(cls):
  148. from idlelib.idle_test.mock_tk import Text
  149. cls.Text = Text
  150. def setUp(self):
  151. self.text = self.Text()
  152. def test_decode(self):
  153. # test endflags (-1, 0) not tested by test_index (which uses +1)
  154. decode = self.text._decode
  155. Equal = self.assertEqual
  156. self.text.insert('1.0', self.hw)
  157. Equal(decode('end', -1), (2, 5))
  158. Equal(decode('3.1', -1), (2, 5))
  159. Equal(decode('end', 0), (2, 6))
  160. Equal(decode('3.1', 0), (2, 6))
  161. class TkTextTest(TextTest, unittest.TestCase):
  162. @classmethod
  163. def setUpClass(cls):
  164. requires('gui')
  165. from tkinter import Tk, Text
  166. cls.Text = Text
  167. cls.root = Tk()
  168. @classmethod
  169. def tearDownClass(cls):
  170. cls.root.destroy()
  171. del cls.root
  172. def setUp(self):
  173. self.text = self.Text(self.root)
  174. if __name__ == '__main__':
  175. unittest.main(verbosity=2, exit=False)