test_pyshell.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "Test pyshell, coverage 12%."
  2. # Plus coverage of test_warning. Was 20% with test_openshell.
  3. from idlelib import pyshell
  4. import unittest
  5. from test.support import requires
  6. from tkinter import Tk
  7. class FunctionTest(unittest.TestCase):
  8. # Test stand-alone module level non-gui functions.
  9. def test_restart_line_wide(self):
  10. eq = self.assertEqual
  11. for file, mul, extra in (('', 22, ''), ('finame', 21, '=')):
  12. width = 60
  13. bar = mul * '='
  14. with self.subTest(file=file, bar=bar):
  15. file = file or 'Shell'
  16. line = pyshell.restart_line(width, file)
  17. eq(len(line), width)
  18. eq(line, f"{bar+extra} RESTART: {file} {bar}")
  19. def test_restart_line_narrow(self):
  20. expect, taglen = "= RESTART: Shell", 16
  21. for width in (taglen-1, taglen, taglen+1):
  22. with self.subTest(width=width):
  23. self.assertEqual(pyshell.restart_line(width, ''), expect)
  24. self.assertEqual(pyshell.restart_line(taglen+2, ''), expect+' =')
  25. class PyShellFileListTest(unittest.TestCase):
  26. @classmethod
  27. def setUpClass(cls):
  28. requires('gui')
  29. cls.root = Tk()
  30. cls.root.withdraw()
  31. @classmethod
  32. def tearDownClass(cls):
  33. #cls.root.update_idletasks()
  34. ## for id in cls.root.tk.call('after', 'info'):
  35. ## cls.root.after_cancel(id) # Need for EditorWindow.
  36. cls.root.destroy()
  37. del cls.root
  38. def test_init(self):
  39. psfl = pyshell.PyShellFileList(self.root)
  40. self.assertEqual(psfl.EditorWindow, pyshell.PyShellEditorWindow)
  41. self.assertIsNone(psfl.pyshell)
  42. # The following sometimes causes 'invalid command name "109734456recolorize"'.
  43. # Uncommenting after_cancel above prevents this, but results in
  44. # TclError: bad window path name ".!listedtoplevel.!frame.text"
  45. # which is normally prevented by after_cancel.
  46. ## def test_openshell(self):
  47. ## pyshell.use_subprocess = False
  48. ## ps = pyshell.PyShellFileList(self.root).open_shell()
  49. ## self.assertIsInstance(ps, pyshell.PyShell)
  50. class PyShellRemoveLastNewlineAndSurroundingWhitespaceTest(unittest.TestCase):
  51. regexp = pyshell.PyShell._last_newline_re
  52. def all_removed(self, text):
  53. self.assertEqual('', self.regexp.sub('', text))
  54. def none_removed(self, text):
  55. self.assertEqual(text, self.regexp.sub('', text))
  56. def check_result(self, text, expected):
  57. self.assertEqual(expected, self.regexp.sub('', text))
  58. def test_empty(self):
  59. self.all_removed('')
  60. def test_newline(self):
  61. self.all_removed('\n')
  62. def test_whitespace_no_newline(self):
  63. self.all_removed(' ')
  64. self.all_removed(' ')
  65. self.all_removed(' ')
  66. self.all_removed(' ' * 20)
  67. self.all_removed('\t')
  68. self.all_removed('\t\t')
  69. self.all_removed('\t\t\t')
  70. self.all_removed('\t' * 20)
  71. self.all_removed('\t ')
  72. self.all_removed(' \t')
  73. self.all_removed(' \t \t ')
  74. self.all_removed('\t \t \t')
  75. def test_newline_with_whitespace(self):
  76. self.all_removed(' \n')
  77. self.all_removed('\t\n')
  78. self.all_removed(' \t\n')
  79. self.all_removed('\t \n')
  80. self.all_removed('\n ')
  81. self.all_removed('\n\t')
  82. self.all_removed('\n \t')
  83. self.all_removed('\n\t ')
  84. self.all_removed(' \n ')
  85. self.all_removed('\t\n ')
  86. self.all_removed(' \n\t')
  87. self.all_removed('\t\n\t')
  88. self.all_removed('\t \t \t\n')
  89. self.all_removed(' \t \t \n')
  90. self.all_removed('\n\t \t \t')
  91. self.all_removed('\n \t \t ')
  92. def test_multiple_newlines(self):
  93. self.check_result('\n\n', '\n')
  94. self.check_result('\n' * 5, '\n' * 4)
  95. self.check_result('\n' * 5 + '\t', '\n' * 4)
  96. self.check_result('\n' * 20, '\n' * 19)
  97. self.check_result('\n' * 20 + ' ', '\n' * 19)
  98. self.check_result(' \n \n ', ' \n')
  99. self.check_result(' \n\n ', ' \n')
  100. self.check_result(' \n\n', ' \n')
  101. self.check_result('\t\n\n', '\t\n')
  102. self.check_result('\n\n ', '\n')
  103. self.check_result('\n\n\t', '\n')
  104. self.check_result(' \n \n ', ' \n')
  105. self.check_result('\t\n\t\n\t', '\t\n')
  106. def test_non_whitespace(self):
  107. self.none_removed('a')
  108. self.check_result('a\n', 'a')
  109. self.check_result('a\n ', 'a')
  110. self.check_result('a \n ', 'a')
  111. self.check_result('a \n\t', 'a')
  112. self.none_removed('-')
  113. self.check_result('-\n', '-')
  114. self.none_removed('.')
  115. self.check_result('.\n', '.')
  116. def test_unsupported_whitespace(self):
  117. self.none_removed('\v')
  118. self.none_removed('\n\v')
  119. self.check_result('\v\n', '\v')
  120. self.none_removed(' \n\v')
  121. self.check_result('\v\n ', '\v')
  122. if __name__ == '__main__':
  123. unittest.main(verbosity=2)