Coverage for idle_test/test_text.py: 95%
157 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-11 13:22 -0700
1''' Test mock_tk.Text class against tkinter.Text class
3Run same tests with both by creating a mixin class.
4'''
5import unittest
6from test.support import requires
7from _tkinter import TclError
9class TextTest:
10 "Define items common to both sets of tests."
12 hw = 'hello\nworld' # Several tests insert this after initialization.
13 hwn = hw+'\n' # \n present at initialization, before insert
15 # setUpClass defines cls.Text and maybe cls.root.
16 # setUp defines self.text from Text and maybe root.
18 def test_init(self):
19 self.assertEqual(self.text.get('1.0'), '\n') 1l
20 self.assertEqual(self.text.get('end'), '') 1l
22 def test_index_empty(self):
23 index = self.text.index 1j
25 for dex in (-1.0, 0.3, '1.-1', '1.0', '1.0 lineend', '1.end', '1.33', 1j
26 'insert'):
27 self.assertEqual(index(dex), '1.0') 1j
29 for dex in 'end', 2.0, '2.1', '33.44': 1j
30 self.assertEqual(index(dex), '2.0') 1j
32 def test_index_data(self):
33 index = self.text.index 1h
34 self.text.insert('1.0', self.hw) 1h
36 for dex in -1.0, 0.3, '1.-1', '1.0': 1h
37 self.assertEqual(index(dex), '1.0') 1h
39 for dex in '1.0 lineend', '1.end', '1.33': 1h
40 self.assertEqual(index(dex), '1.5') 1h
42 for dex in 'end', '33.44': 1h
43 self.assertEqual(index(dex), '3.0') 1h
45 def test_get(self):
46 get = self.text.get 1c
47 Equal = self.assertEqual 1c
48 self.text.insert('1.0', self.hw) 1c
50 Equal(get('end'), '') 1c
51 Equal(get('end', 'end'), '') 1c
52 Equal(get('1.0'), 'h') 1c
53 Equal(get('1.0', '1.1'), 'h') 1c
54 Equal(get('1.0', '1.3'), 'hel') 1c
55 Equal(get('1.1', '1.3'), 'el') 1c
56 Equal(get('1.0', '1.0 lineend'), 'hello') 1c
57 Equal(get('1.0', '1.10'), 'hello') 1c
58 Equal(get('1.0 lineend'), '\n') 1c
59 Equal(get('1.1', '2.3'), 'ello\nwor') 1c
60 Equal(get('1.0', '2.5'), self.hw) 1c
61 Equal(get('1.0', 'end'), self.hwn) 1c
62 Equal(get('0.0', '5.0'), self.hwn) 1c
64 def test_insert(self):
65 insert = self.text.insert 1b
66 get = self.text.get 1b
67 Equal = self.assertEqual 1b
69 insert('1.0', self.hw) 1b
70 Equal(get('1.0', 'end'), self.hwn) 1b
72 insert('1.0', '') # nothing 1b
73 Equal(get('1.0', 'end'), self.hwn) 1b
75 insert('1.0', '*') 1b
76 Equal(get('1.0', 'end'), '*hello\nworld\n') 1b
78 insert('1.0 lineend', '*') 1b
79 Equal(get('1.0', 'end'), '*hello*\nworld\n') 1b
81 insert('2.3', '*') 1b
82 Equal(get('1.0', 'end'), '*hello*\nwor*ld\n') 1b
84 insert('end', 'x') 1b
85 Equal(get('1.0', 'end'), '*hello*\nwor*ldx\n') 1b
87 insert('1.4', 'x\n') 1b
88 Equal(get('1.0', 'end'), '*helx\nlo*\nwor*ldx\n') 1b
90 def test_no_delete(self):
91 # if index1 == 'insert' or 'end' or >= end, there is no deletion
92 delete = self.text.delete 1d
93 get = self.text.get 1d
94 Equal = self.assertEqual 1d
95 self.text.insert('1.0', self.hw) 1d
97 delete('insert') 1d
98 Equal(get('1.0', 'end'), self.hwn) 1d
100 delete('end') 1d
101 Equal(get('1.0', 'end'), self.hwn) 1d
103 delete('insert', 'end') 1d
104 Equal(get('1.0', 'end'), self.hwn) 1d
106 delete('insert', '5.5') 1d
107 Equal(get('1.0', 'end'), self.hwn) 1d
109 delete('1.4', '1.0') 1d
110 Equal(get('1.0', 'end'), self.hwn) 1d
112 delete('1.4', '1.4') 1d
113 Equal(get('1.0', 'end'), self.hwn) 1d
115 def test_delete_char(self):
116 delete = self.text.delete 1e
117 get = self.text.get 1e
118 Equal = self.assertEqual 1e
119 self.text.insert('1.0', self.hw) 1e
121 delete('1.0') 1e
122 Equal(get('1.0', '1.end'), 'ello') 1e
124 delete('1.0', '1.1') 1e
125 Equal(get('1.0', '1.end'), 'llo') 1e
127 # delete \n and combine 2 lines into 1
128 delete('1.end') 1e
129 Equal(get('1.0', '1.end'), 'lloworld') 1e
131 self.text.insert('1.3', '\n') 1e
132 delete('1.10') 1e
133 Equal(get('1.0', '1.end'), 'lloworld') 1e
135 self.text.insert('1.3', '\n') 1e
136 delete('1.3', '2.0') 1e
137 Equal(get('1.0', '1.end'), 'lloworld') 1e
139 def test_delete_slice(self):
140 delete = self.text.delete 1f
141 get = self.text.get 1f
142 Equal = self.assertEqual 1f
143 self.text.insert('1.0', self.hw) 1f
145 delete('1.0', '1.0 lineend') 1f
146 Equal(get('1.0', 'end'), '\nworld\n') 1f
148 delete('1.0', 'end') 1f
149 Equal(get('1.0', 'end'), '\n') 1f
151 self.text.insert('1.0', self.hw) 1f
152 delete('1.0', '2.0') 1f
153 Equal(get('1.0', 'end'), 'world\n') 1f
155 delete('1.0', 'end') 1f
156 Equal(get('1.0', 'end'), '\n') 1f
158 self.text.insert('1.0', self.hw) 1f
159 delete('1.2', '2.3') 1f
160 Equal(get('1.0', 'end'), 'held\n') 1f
162 def test_multiple_lines(self): # insert and delete
163 self.text.insert('1.0', 'hello') 1k
165 self.text.insert('1.3', '1\n2\n3\n4\n5') 1k
166 self.assertEqual(self.text.get('1.0', 'end'), 'hel1\n2\n3\n4\n5lo\n') 1k
168 self.text.delete('1.3', '5.1') 1k
169 self.assertEqual(self.text.get('1.0', 'end'), 'hello\n') 1k
171 def test_compare(self):
172 compare = self.text.compare 1g
173 Equal = self.assertEqual 1g
174 # need data so indexes not squished to 1,0
175 self.text.insert('1.0', 'First\nSecond\nThird\n') 1g
177 self.assertRaises(TclError, compare, '2.2', 'op', '2.2') 1g
179 for op, less1, less0, equal, greater0, greater1 in ( 1g
180 ('<', True, True, False, False, False),
181 ('<=', True, True, True, False, False),
182 ('>', False, False, False, True, True),
183 ('>=', False, False, True, True, True),
184 ('==', False, False, True, False, False),
185 ('!=', True, True, False, True, True),
186 ):
187 Equal(compare('1.1', op, '2.2'), less1, op) 1g
188 Equal(compare('2.1', op, '2.2'), less0, op) 1g
189 Equal(compare('2.2', op, '2.2'), equal, op) 1g
190 Equal(compare('2.3', op, '2.2'), greater0, op) 1g
191 Equal(compare('3.3', op, '2.2'), greater1, op) 1g
194class MockTextTest(TextTest, unittest.TestCase):
196 @classmethod
197 def setUpClass(cls):
198 from idlelib.idle_test.mock_tk import Text
199 cls.Text = Text
201 def setUp(self):
202 self.text = self.Text()
205 def test_decode(self):
206 # test endflags (-1, 0) not tested by test_index (which uses +1)
207 decode = self.text._decode 1i
208 Equal = self.assertEqual 1i
209 self.text.insert('1.0', self.hw) 1i
211 Equal(decode('end', -1), (2, 5)) 1i
212 Equal(decode('3.1', -1), (2, 5)) 1i
213 Equal(decode('end', 0), (2, 6)) 1i
214 Equal(decode('3.1', 0), (2, 6)) 1i
217class TkTextTest(TextTest, unittest.TestCase):
219 @classmethod
220 def setUpClass(cls):
221 requires('gui')
222 from tkinter import Tk, Text
223 cls.Text = Text
224 cls.root = Tk()
226 @classmethod
227 def tearDownClass(cls):
228 cls.root.destroy()
229 del cls.root
231 def setUp(self):
232 self.text = self.Text(self.root)
235if __name__ == '__main__': 235 ↛ 236line 235 didn't jump to line 236, because the condition on line 235 was never true
236 unittest.main(verbosity=2, exit=False)