test_iomenu.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "Test , coverage 17%."
  2. from idlelib import iomenu
  3. import unittest
  4. from test.support import requires
  5. from tkinter import Tk
  6. from idlelib.editor import EditorWindow
  7. from idlelib import util
  8. from idlelib.idle_test.mock_idle import Func
  9. # Fail if either tokenize.open and t.detect_encoding does not exist.
  10. # These are used in loadfile and encode.
  11. # Also used in pyshell.MI.execfile and runscript.tabnanny.
  12. from tokenize import open, detect_encoding
  13. # Remove when we have proper tests that use both.
  14. class IOBindingTest(unittest.TestCase):
  15. @classmethod
  16. def setUpClass(cls):
  17. requires('gui')
  18. cls.root = Tk()
  19. cls.root.withdraw()
  20. cls.editwin = EditorWindow(root=cls.root)
  21. cls.io = iomenu.IOBinding(cls.editwin)
  22. @classmethod
  23. def tearDownClass(cls):
  24. cls.io.close()
  25. cls.editwin._close()
  26. del cls.editwin
  27. cls.root.update_idletasks()
  28. for id in cls.root.tk.call('after', 'info'):
  29. cls.root.after_cancel(id) # Need for EditorWindow.
  30. cls.root.destroy()
  31. del cls.root
  32. def test_init(self):
  33. self.assertIs(self.io.editwin, self.editwin)
  34. def test_fixnewlines_end(self):
  35. eq = self.assertEqual
  36. io = self.io
  37. fix = io.fixnewlines
  38. text = io.editwin.text
  39. # Make the editor temporarily look like Shell.
  40. self.editwin.interp = None
  41. shelltext = '>>> if 1'
  42. self.editwin.get_prompt_text = Func(result=shelltext)
  43. eq(fix(), shelltext) # Get... call and '\n' not added.
  44. del self.editwin.interp, self.editwin.get_prompt_text
  45. text.insert(1.0, 'a')
  46. eq(fix(), 'a'+io.eol_convention)
  47. eq(text.get('1.0', 'end-1c'), 'a\n')
  48. eq(fix(), 'a'+io.eol_convention)
  49. def _extension_in_filetypes(extension):
  50. return any(
  51. f'*{extension}' in filetype_tuple[1]
  52. for filetype_tuple in iomenu.IOBinding.filetypes
  53. )
  54. class FiletypesTest(unittest.TestCase):
  55. def test_python_source_files(self):
  56. for extension in util.py_extensions:
  57. with self.subTest(extension=extension):
  58. self.assertTrue(
  59. _extension_in_filetypes(extension)
  60. )
  61. def test_text_files(self):
  62. self.assertTrue(_extension_in_filetypes('.txt'))
  63. def test_all_files(self):
  64. self.assertTrue(_extension_in_filetypes(''))
  65. if __name__ == '__main__':
  66. unittest.main(verbosity=2)