Coverage for idle_test/test_pyshell.py: 93%

109 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-11 13:22 -0700

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 1f

15 for file, mul, extra in (('', 22, ''), ('finame', 21, '=')): 1f

16 width = 60 1f

17 bar = mul * '=' 1f

18 with self.subTest(file=file, bar=bar): 1f

19 file = file or 'Shell' 1f

20 line = pyshell.restart_line(width, file) 1f

21 eq(len(line), width) 1f

22 eq(line, f"{bar+extra} RESTART: {file} {bar}") 1f

23 

24 def test_restart_line_narrow(self): 

25 expect, taglen = "= RESTART: Shell", 16 1h

26 for width in (taglen-1, taglen, taglen+1): 1h

27 with self.subTest(width=width): 1h

28 self.assertEqual(pyshell.restart_line(width, ''), expect) 1h

29 self.assertEqual(pyshell.restart_line(taglen+2, ''), expect+' =') 1h

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)) 1ijbd

68 

69 def none_removed(self, text): 

70 self.assertEqual(text, self.regexp.sub('', text)) 1eg

71 

72 def check_result(self, text, expected): 

73 self.assertEqual(expected, self.regexp.sub('', text)) 1ceg

74 

75 def test_empty(self): 

76 self.all_removed('') 1i

77 

78 def test_newline(self): 

79 self.all_removed('\n') 1j

80 

81 def test_whitespace_no_newline(self): 

82 self.all_removed(' ') 1d

83 self.all_removed(' ') 1d

84 self.all_removed(' ') 1d

85 self.all_removed(' ' * 20) 1d

86 self.all_removed('\t') 1d

87 self.all_removed('\t\t') 1d

88 self.all_removed('\t\t\t') 1d

89 self.all_removed('\t' * 20) 1d

90 self.all_removed('\t ') 1d

91 self.all_removed(' \t') 1d

92 self.all_removed(' \t \t ') 1d

93 self.all_removed('\t \t \t') 1d

94 

95 def test_newline_with_whitespace(self): 

96 self.all_removed(' \n') 1b

97 self.all_removed('\t\n') 1b

98 self.all_removed(' \t\n') 1b

99 self.all_removed('\t \n') 1b

100 self.all_removed('\n ') 1b

101 self.all_removed('\n\t') 1b

102 self.all_removed('\n \t') 1b

103 self.all_removed('\n\t ') 1b

104 self.all_removed(' \n ') 1b

105 self.all_removed('\t\n ') 1b

106 self.all_removed(' \n\t') 1b

107 self.all_removed('\t\n\t') 1b

108 self.all_removed('\t \t \t\n') 1b

109 self.all_removed(' \t \t \n') 1b

110 self.all_removed('\n\t \t \t') 1b

111 self.all_removed('\n \t \t ') 1b

112 

113 def test_multiple_newlines(self): 

114 self.check_result('\n\n', '\n') 1c

115 self.check_result('\n' * 5, '\n' * 4) 1c

116 self.check_result('\n' * 5 + '\t', '\n' * 4) 1c

117 self.check_result('\n' * 20, '\n' * 19) 1c

118 self.check_result('\n' * 20 + ' ', '\n' * 19) 1c

119 self.check_result(' \n \n ', ' \n') 1c

120 self.check_result(' \n\n ', ' \n') 1c

121 self.check_result(' \n\n', ' \n') 1c

122 self.check_result('\t\n\n', '\t\n') 1c

123 self.check_result('\n\n ', '\n') 1c

124 self.check_result('\n\n\t', '\n') 1c

125 self.check_result(' \n \n ', ' \n') 1c

126 self.check_result('\t\n\t\n\t', '\t\n') 1c

127 

128 def test_non_whitespace(self): 

129 self.none_removed('a') 1e

130 self.check_result('a\n', 'a') 1e

131 self.check_result('a\n ', 'a') 1e

132 self.check_result('a \n ', 'a') 1e

133 self.check_result('a \n\t', 'a') 1e

134 self.none_removed('-') 1e

135 self.check_result('-\n', '-') 1e

136 self.none_removed('.') 1e

137 self.check_result('.\n', '.') 1e

138 

139 def test_unsupported_whitespace(self): 

140 self.none_removed('\v') 1g

141 self.none_removed('\n\v') 1g

142 self.check_result('\v\n', '\v') 1g

143 self.none_removed(' \n\v') 1g

144 self.check_result('\v\n ', '\v') 1g

145 

146 

147if __name__ == '__main__': 147 ↛ 148line 147 didn't jump to line 148, because the condition on line 147 was never true

148 unittest.main(verbosity=2)