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