test_zzdummy.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "Test zzdummy, coverage 100%."
  2. from idlelib import zzdummy
  3. import unittest
  4. from test.support import requires
  5. from tkinter import Tk, Text
  6. from unittest import mock
  7. from idlelib import config
  8. from idlelib import editor
  9. from idlelib import format
  10. usercfg = zzdummy.idleConf.userCfg
  11. testcfg = {
  12. 'main': config.IdleUserConfParser(''),
  13. 'highlight': config.IdleUserConfParser(''),
  14. 'keys': config.IdleUserConfParser(''),
  15. 'extensions': config.IdleUserConfParser(''),
  16. }
  17. code_sample = """\
  18. class C1:
  19. # Class comment.
  20. def __init__(self, a, b):
  21. self.a = a
  22. self.b = b
  23. """
  24. class DummyEditwin:
  25. get_selection_indices = editor.EditorWindow.get_selection_indices
  26. def __init__(self, root, text):
  27. self.root = root
  28. self.top = root
  29. self.text = text
  30. self.fregion = format.FormatRegion(self)
  31. self.text.undo_block_start = mock.Mock()
  32. self.text.undo_block_stop = mock.Mock()
  33. class ZZDummyTest(unittest.TestCase):
  34. @classmethod
  35. def setUpClass(cls):
  36. requires('gui')
  37. root = cls.root = Tk()
  38. root.withdraw()
  39. text = cls.text = Text(cls.root)
  40. cls.editor = DummyEditwin(root, text)
  41. zzdummy.idleConf.userCfg = testcfg
  42. @classmethod
  43. def tearDownClass(cls):
  44. zzdummy.idleConf.userCfg = usercfg
  45. del cls.editor, cls.text
  46. cls.root.update_idletasks()
  47. for id in cls.root.tk.call('after', 'info'):
  48. cls.root.after_cancel(id) # Need for EditorWindow.
  49. cls.root.destroy()
  50. del cls.root
  51. def setUp(self):
  52. text = self.text
  53. text.insert('1.0', code_sample)
  54. text.undo_block_start.reset_mock()
  55. text.undo_block_stop.reset_mock()
  56. zz = self.zz = zzdummy.ZzDummy(self.editor)
  57. zzdummy.ZzDummy.ztext = '# ignore #'
  58. def tearDown(self):
  59. self.text.delete('1.0', 'end')
  60. del self.zz
  61. def checklines(self, text, value):
  62. # Verify that there are lines being checked.
  63. end_line = int(float(text.index('end')))
  64. # Check each line for the starting text.
  65. actual = []
  66. for line in range(1, end_line):
  67. txt = text.get(f'{line}.0', f'{line}.end')
  68. actual.append(txt.startswith(value))
  69. return actual
  70. def test_init(self):
  71. zz = self.zz
  72. self.assertEqual(zz.editwin, self.editor)
  73. self.assertEqual(zz.text, self.editor.text)
  74. def test_reload(self):
  75. self.assertEqual(self.zz.ztext, '# ignore #')
  76. testcfg['extensions'].SetOption('ZzDummy', 'z-text', 'spam')
  77. zzdummy.ZzDummy.reload()
  78. self.assertEqual(self.zz.ztext, 'spam')
  79. def test_z_in_event(self):
  80. eq = self.assertEqual
  81. zz = self.zz
  82. text = zz.text
  83. eq(self.zz.ztext, '# ignore #')
  84. # No lines have the leading text.
  85. expected = [False, False, False, False, False, False, False]
  86. actual = self.checklines(text, zz.ztext)
  87. eq(expected, actual)
  88. text.tag_add('sel', '2.0', '4.end')
  89. eq(zz.z_in_event(), 'break')
  90. expected = [False, True, True, True, False, False, False]
  91. actual = self.checklines(text, zz.ztext)
  92. eq(expected, actual)
  93. text.undo_block_start.assert_called_once()
  94. text.undo_block_stop.assert_called_once()
  95. def test_z_out_event(self):
  96. eq = self.assertEqual
  97. zz = self.zz
  98. text = zz.text
  99. eq(self.zz.ztext, '# ignore #')
  100. # Prepend text.
  101. text.tag_add('sel', '2.0', '5.end')
  102. zz.z_in_event()
  103. text.undo_block_start.reset_mock()
  104. text.undo_block_stop.reset_mock()
  105. # Select a few lines to remove text.
  106. text.tag_remove('sel', '1.0', 'end')
  107. text.tag_add('sel', '3.0', '4.end')
  108. eq(zz.z_out_event(), 'break')
  109. expected = [False, True, False, False, True, False, False]
  110. actual = self.checklines(text, zz.ztext)
  111. eq(expected, actual)
  112. text.undo_block_start.assert_called_once()
  113. text.undo_block_stop.assert_called_once()
  114. def test_roundtrip(self):
  115. # Insert and remove to all code should give back original text.
  116. zz = self.zz
  117. text = zz.text
  118. text.tag_add('sel', '1.0', 'end-1c')
  119. zz.z_in_event()
  120. zz.z_out_event()
  121. self.assertEqual(text.get('1.0', 'end-1c'), code_sample)
  122. if __name__ == '__main__':
  123. unittest.main(verbosity=2)