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