# Python code coverage for Lib/idlelib/idle_test/test_text.py

# | count | content |
---|---|---|

1 | n/a | ''' Test mock_tk.Text class against tkinter.Text class |

2 | n/a | |

3 | n/a | Run same tests with both by creating a mixin class. |

4 | n/a | ''' |

5 | n/a | import unittest |

6 | n/a | from test.support import requires |

7 | n/a | from _tkinter import TclError |

8 | n/a | |

9 | n/a | class TextTest(object): |

10 | n/a | "Define items common to both sets of tests." |

11 | n/a | |

12 | n/a | hw = 'hello\nworld' # Several tests insert this after after initialization. |

13 | n/a | hwn = hw+'\n' # \n present at initialization, before insert |

14 | n/a | |

15 | n/a | # setUpClass defines cls.Text and maybe cls.root. |

16 | n/a | # setUp defines self.text from Text and maybe root. |

17 | n/a | |

18 | n/a | def test_init(self): |

19 | n/a | self.assertEqual(self.text.get('1.0'), '\n') |

20 | n/a | self.assertEqual(self.text.get('end'), '') |

21 | n/a | |

22 | n/a | def test_index_empty(self): |

23 | n/a | index = self.text.index |

24 | n/a | |

25 | n/a | for dex in (-1.0, 0.3, '1.-1', '1.0', '1.0 lineend', '1.end', '1.33', |

26 | n/a | 'insert'): |

27 | n/a | self.assertEqual(index(dex), '1.0') |

28 | n/a | |

29 | n/a | for dex in 'end', 2.0, '2.1', '33.44': |

30 | n/a | self.assertEqual(index(dex), '2.0') |

31 | n/a | |

32 | n/a | def test_index_data(self): |

33 | n/a | index = self.text.index |

34 | n/a | self.text.insert('1.0', self.hw) |

35 | n/a | |

36 | n/a | for dex in -1.0, 0.3, '1.-1', '1.0': |

37 | n/a | self.assertEqual(index(dex), '1.0') |

38 | n/a | |

39 | n/a | for dex in '1.0 lineend', '1.end', '1.33': |

40 | n/a | self.assertEqual(index(dex), '1.5') |

41 | n/a | |

42 | n/a | for dex in 'end', '33.44': |

43 | n/a | self.assertEqual(index(dex), '3.0') |

44 | n/a | |

45 | n/a | def test_get(self): |

46 | n/a | get = self.text.get |

47 | n/a | Equal = self.assertEqual |

48 | n/a | self.text.insert('1.0', self.hw) |

49 | n/a | |

50 | n/a | Equal(get('end'), '') |

51 | n/a | Equal(get('end', 'end'), '') |

52 | n/a | Equal(get('1.0'), 'h') |

53 | n/a | Equal(get('1.0', '1.1'), 'h') |

54 | n/a | Equal(get('1.0', '1.3'), 'hel') |

55 | n/a | Equal(get('1.1', '1.3'), 'el') |

56 | n/a | Equal(get('1.0', '1.0 lineend'), 'hello') |

57 | n/a | Equal(get('1.0', '1.10'), 'hello') |

58 | n/a | Equal(get('1.0 lineend'), '\n') |

59 | n/a | Equal(get('1.1', '2.3'), 'ello\nwor') |

60 | n/a | Equal(get('1.0', '2.5'), self.hw) |

61 | n/a | Equal(get('1.0', 'end'), self.hwn) |

62 | n/a | Equal(get('0.0', '5.0'), self.hwn) |

63 | n/a | |

64 | n/a | def test_insert(self): |

65 | n/a | insert = self.text.insert |

66 | n/a | get = self.text.get |

67 | n/a | Equal = self.assertEqual |

68 | n/a | |

69 | n/a | insert('1.0', self.hw) |

70 | n/a | Equal(get('1.0', 'end'), self.hwn) |

71 | n/a | |

72 | n/a | insert('1.0', '') # nothing |

73 | n/a | Equal(get('1.0', 'end'), self.hwn) |

74 | n/a | |

75 | n/a | insert('1.0', '*') |

76 | n/a | Equal(get('1.0', 'end'), '*hello\nworld\n') |

77 | n/a | |

78 | n/a | insert('1.0 lineend', '*') |

79 | n/a | Equal(get('1.0', 'end'), '*hello*\nworld\n') |

80 | n/a | |

81 | n/a | insert('2.3', '*') |

82 | n/a | Equal(get('1.0', 'end'), '*hello*\nwor*ld\n') |

83 | n/a | |

84 | n/a | insert('end', 'x') |

85 | n/a | Equal(get('1.0', 'end'), '*hello*\nwor*ldx\n') |

86 | n/a | |

87 | n/a | insert('1.4', 'x\n') |

88 | n/a | Equal(get('1.0', 'end'), '*helx\nlo*\nwor*ldx\n') |

89 | n/a | |

90 | n/a | def test_no_delete(self): |

91 | n/a | # if index1 == 'insert' or 'end' or >= end, there is no deletion |

92 | n/a | delete = self.text.delete |

93 | n/a | get = self.text.get |

94 | n/a | Equal = self.assertEqual |

95 | n/a | self.text.insert('1.0', self.hw) |

96 | n/a | |

97 | n/a | delete('insert') |

98 | n/a | Equal(get('1.0', 'end'), self.hwn) |

99 | n/a | |

100 | n/a | delete('end') |

101 | n/a | Equal(get('1.0', 'end'), self.hwn) |

102 | n/a | |

103 | n/a | delete('insert', 'end') |

104 | n/a | Equal(get('1.0', 'end'), self.hwn) |

105 | n/a | |

106 | n/a | delete('insert', '5.5') |

107 | n/a | Equal(get('1.0', 'end'), self.hwn) |

108 | n/a | |

109 | n/a | delete('1.4', '1.0') |

110 | n/a | Equal(get('1.0', 'end'), self.hwn) |

111 | n/a | |

112 | n/a | delete('1.4', '1.4') |

113 | n/a | Equal(get('1.0', 'end'), self.hwn) |

114 | n/a | |

115 | n/a | def test_delete_char(self): |

116 | n/a | delete = self.text.delete |

117 | n/a | get = self.text.get |

118 | n/a | Equal = self.assertEqual |

119 | n/a | self.text.insert('1.0', self.hw) |

120 | n/a | |

121 | n/a | delete('1.0') |

122 | n/a | Equal(get('1.0', '1.end'), 'ello') |

123 | n/a | |

124 | n/a | delete('1.0', '1.1') |

125 | n/a | Equal(get('1.0', '1.end'), 'llo') |

126 | n/a | |

127 | n/a | # delete \n and combine 2 lines into 1 |

128 | n/a | delete('1.end') |

129 | n/a | Equal(get('1.0', '1.end'), 'lloworld') |

130 | n/a | |

131 | n/a | self.text.insert('1.3', '\n') |

132 | n/a | delete('1.10') |

133 | n/a | Equal(get('1.0', '1.end'), 'lloworld') |

134 | n/a | |

135 | n/a | self.text.insert('1.3', '\n') |

136 | n/a | delete('1.3', '2.0') |

137 | n/a | Equal(get('1.0', '1.end'), 'lloworld') |

138 | n/a | |

139 | n/a | def test_delete_slice(self): |

140 | n/a | delete = self.text.delete |

141 | n/a | get = self.text.get |

142 | n/a | Equal = self.assertEqual |

143 | n/a | self.text.insert('1.0', self.hw) |

144 | n/a | |

145 | n/a | delete('1.0', '1.0 lineend') |

146 | n/a | Equal(get('1.0', 'end'), '\nworld\n') |

147 | n/a | |

148 | n/a | delete('1.0', 'end') |

149 | n/a | Equal(get('1.0', 'end'), '\n') |

150 | n/a | |

151 | n/a | self.text.insert('1.0', self.hw) |

152 | n/a | delete('1.0', '2.0') |

153 | n/a | Equal(get('1.0', 'end'), 'world\n') |

154 | n/a | |

155 | n/a | delete('1.0', 'end') |

156 | n/a | Equal(get('1.0', 'end'), '\n') |

157 | n/a | |

158 | n/a | self.text.insert('1.0', self.hw) |

159 | n/a | delete('1.2', '2.3') |

160 | n/a | Equal(get('1.0', 'end'), 'held\n') |

161 | n/a | |

162 | n/a | def test_multiple_lines(self): # insert and delete |

163 | n/a | self.text.insert('1.0', 'hello') |

164 | n/a | |

165 | n/a | self.text.insert('1.3', '1\n2\n3\n4\n5') |

166 | n/a | self.assertEqual(self.text.get('1.0', 'end'), 'hel1\n2\n3\n4\n5lo\n') |

167 | n/a | |

168 | n/a | self.text.delete('1.3', '5.1') |

169 | n/a | self.assertEqual(self.text.get('1.0', 'end'), 'hello\n') |

170 | n/a | |

171 | n/a | def test_compare(self): |

172 | n/a | compare = self.text.compare |

173 | n/a | Equal = self.assertEqual |

174 | n/a | # need data so indexes not squished to 1,0 |

175 | n/a | self.text.insert('1.0', 'First\nSecond\nThird\n') |

176 | n/a | |

177 | n/a | self.assertRaises(TclError, compare, '2.2', 'op', '2.2') |

178 | n/a | |

179 | n/a | for op, less1, less0, equal, greater0, greater1 in ( |

180 | n/a | ('<', True, True, False, False, False), |

181 | n/a | ('<=', True, True, True, False, False), |

182 | n/a | ('>', False, False, False, True, True), |

183 | n/a | ('>=', False, False, True, True, True), |

184 | n/a | ('==', False, False, True, False, False), |

185 | n/a | ('!=', True, True, False, True, True), |

186 | n/a | ): |

187 | n/a | Equal(compare('1.1', op, '2.2'), less1, op) |

188 | n/a | Equal(compare('2.1', op, '2.2'), less0, op) |

189 | n/a | Equal(compare('2.2', op, '2.2'), equal, op) |

190 | n/a | Equal(compare('2.3', op, '2.2'), greater0, op) |

191 | n/a | Equal(compare('3.3', op, '2.2'), greater1, op) |

192 | n/a | |

193 | n/a | |

194 | n/a | class MockTextTest(TextTest, unittest.TestCase): |

195 | n/a | |

196 | n/a | @classmethod |

197 | n/a | def setUpClass(cls): |

198 | n/a | from idlelib.idle_test.mock_tk import Text |

199 | n/a | cls.Text = Text |

200 | n/a | |

201 | n/a | def setUp(self): |

202 | n/a | self.text = self.Text() |

203 | n/a | |

204 | n/a | |

205 | n/a | def test_decode(self): |

206 | n/a | # test endflags (-1, 0) not tested by test_index (which uses +1) |

207 | n/a | decode = self.text._decode |

208 | n/a | Equal = self.assertEqual |

209 | n/a | self.text.insert('1.0', self.hw) |

210 | n/a | |

211 | n/a | Equal(decode('end', -1), (2, 5)) |

212 | n/a | Equal(decode('3.1', -1), (2, 5)) |

213 | n/a | Equal(decode('end', 0), (2, 6)) |

214 | n/a | Equal(decode('3.1', 0), (2, 6)) |

215 | n/a | |

216 | n/a | |

217 | n/a | class TkTextTest(TextTest, unittest.TestCase): |

218 | n/a | |

219 | n/a | @classmethod |

220 | n/a | def setUpClass(cls): |

221 | n/a | requires('gui') |

222 | n/a | from tkinter import Tk, Text |

223 | n/a | cls.Text = Text |

224 | n/a | cls.root = Tk() |

225 | n/a | |

226 | n/a | @classmethod |

227 | n/a | def tearDownClass(cls): |

228 | n/a | cls.root.destroy() |

229 | n/a | del cls.root |

230 | n/a | |

231 | n/a | def setUp(self): |

232 | n/a | self.text = self.Text(self.root) |

233 | n/a | |

234 | n/a | |

235 | n/a | if __name__ == '__main__': |

236 | n/a | unittest.main(verbosity=2, exit=False) |