| 1 |
import unittest |
| 2 |
import tkinter |
| 3 |
from tkinter import TclError |
| 4 |
from test.support import requires |
| 5 |
from test.test_tkinter.support import setUpModule # noqa: F401 |
| 6 |
from test.test_tkinter.support import AbstractTkTest, requires_tk |
| 7 |
|
| 8 |
requires('gui') |
| 9 |
|
| 10 |
class TextTest(AbstractTkTest, unittest.TestCase): |
| 11 |
|
| 12 |
def setUp(self): |
| 13 |
super().setUp() |
| 14 |
self.text = tkinter.Text(self.root) |
| 15 |
self.text.pack() |
| 16 |
|
| 17 |
def test_debug(self): |
| 18 |
text = self.text |
| 19 |
olddebug = text.debug() |
| 20 |
try: |
| 21 |
text.debug(False) |
| 22 |
self.assertIs(text.debug(), False) |
| 23 |
text.debug(True) |
| 24 |
self.assertIs(text.debug(), True) |
| 25 |
finally: |
| 26 |
text.debug(olddebug) |
| 27 |
self.assertEqual(text.debug(), olddebug) |
| 28 |
|
| 29 |
def test_index(self): |
| 30 |
text = self.text |
| 31 |
text.insert('1.0', 'Lorem ipsum\ndolor sit amet') |
| 32 |
self.assertEqual(text.index('1.0'), '1.0') |
| 33 |
self.assertEqual(text.index('1.end'), '1.11') |
| 34 |
self.assertEqual(text.index('end'), '3.0') |
| 35 |
self.assertEqual(text.index('2.5'), '2.5') |
| 36 |
# Index past the end of a line is clamped to its end. |
| 37 |
self.assertEqual(text.index('1.100'), '1.11') |
| 38 |
self.assertRaises(TclError, text.index, 'invalid') |
| 39 |
self.assertRaises(TypeError, text.index) |
| 40 |
self.assertRaises(TypeError, text.index, '1.0', '2.5') |
| 41 |
|
| 42 |
def test_compare(self): |
| 43 |
text = self.text |
| 44 |
text.insert('1.0', 'Lorem ipsum\ndolor sit amet') |
| 45 |
self.assertIs(text.compare('1.0', '<', '2.0'), True) |
| 46 |
self.assertIs(text.compare('2.0', '<', '1.0'), False) |
| 47 |
self.assertIs(text.compare('1.5', '==', '1.5'), True) |
| 48 |
self.assertIs(text.compare('1.5', '!=', '1.5'), False) |
| 49 |
self.assertIs(text.compare('2.0', '>=', '2.0'), True) |
| 50 |
self.assertIs(text.compare('1.0', '<=', 'end'), True) |
| 51 |
self.assertRaises(TclError, text.compare, '1.0', 'invalid', '2.0') |
| 52 |
self.assertRaises(TypeError, text.compare, '1.0', '<') |
| 53 |
self.assertRaises(TypeError, text.compare, '1.0', '<', '2.0', '3.0') |
| 54 |
|
| 55 |
def test_insert_get_delete(self): |
| 56 |
text = self.text |
| 57 |
text.insert('1.0', 'Lorem ipsum') |
| 58 |
self.assertEqual(text.get('1.0', 'end'), 'Lorem ipsum\n') |
| 59 |
self.assertEqual(text.get('1.0', '1.5'), 'Lorem') |
| 60 |
self.assertEqual(text.get('1.6'), 'i') # single character |
| 61 |
|
| 62 |
# insert before an existing index |
| 63 |
text.insert('1.0', '*** ') |
| 64 |
self.assertEqual(text.get('1.0', 'end'), '*** Lorem ipsum\n') |
| 65 |
|
| 66 |
text.delete('1.0', '1.4') |
| 67 |
self.assertEqual(text.get('1.0', 'end'), 'Lorem ipsum\n') |
| 68 |
text.delete('1.5') # delete a single character |
| 69 |
self.assertEqual(text.get('1.0', 'end'), 'Loremipsum\n') |
| 70 |
self.assertRaises(TypeError, text.get) |
| 71 |
self.assertRaises(TypeError, text.get, '1.0', '1.5', 'end') |
| 72 |
self.assertRaises(TypeError, text.delete, '1.0', '1.5', 'end') |
| 73 |
|
| 74 |
def test_insert_with_tags(self): |
| 75 |
text = self.text |
| 76 |
text.insert('1.0', 'hello', 'a', ' ', ('a', 'b'), 'world', 'b') |
| 77 |
self.assertEqual(text.get('1.0', 'end'), 'hello world\n') |
| 78 |
self.assertEqual([str(i) for i in text.tag_ranges('a')], |
| 79 |
['1.0', '1.6']) |
| 80 |
self.assertEqual([str(i) for i in text.tag_ranges('b')], |
| 81 |
['1.5', '1.11']) |
| 82 |
|
| 83 |
def test_replace(self): |
| 84 |
text = self.text |
| 85 |
text.insert('1.0', 'Lorem ipsum') |
| 86 |
text.replace('1.0', '1.5', 'Hello') |
| 87 |
self.assertEqual(text.get('1.0', 'end'), 'Hello ipsum\n') |
| 88 |
text.replace('1.6', 'end - 1 char', 'world') |
| 89 |
self.assertEqual(text.get('1.0', 'end'), 'Hello world\n') |
| 90 |
self.assertRaises(TclError, text.replace, 'invalid', '1.5', 'x') |
| 91 |
# The first index must not be after the second. |
| 92 |
self.assertRaises(TclError, text.replace, '1.5', '1.0', 'x') |
| 93 |
self.assertRaises(TypeError, text.replace, '1.0', '1.5') |
| 94 |
|
| 95 |
def test_mark(self): |
| 96 |
text = self.text |
| 97 |
text.insert('1.0', 'Lorem ipsum\ndolor sit amet') |
| 98 |
# 'insert' and 'current' marks always exist. |
| 99 |
self.assertIn('insert', text.mark_names()) |
| 100 |
self.assertIn('current', text.mark_names()) |
| 101 |
|
| 102 |
text.mark_set('here', '1.5') |
| 103 |
self.assertIn('here', text.mark_names()) |
| 104 |
self.assertEqual(text.index('here'), '1.5') |
| 105 |
text.mark_set('here', '2.3') |
| 106 |
self.assertEqual(text.index('here'), '2.3') |
| 107 |
|
| 108 |
text.mark_unset('here') |
| 109 |
self.assertNotIn('here', text.mark_names()) |
| 110 |
|
| 111 |
# Unsetting a non-existent mark is not an error. |
| 112 |
text.mark_unset('nonexistent') |
| 113 |
self.assertRaises(TclError, text.mark_set, 'm', 'invalid') |
| 114 |
self.assertRaises(TypeError, text.mark_set, 'm') |
| 115 |
self.assertRaises(TypeError, text.mark_set, 'm', '1.0', '2.0') |
| 116 |
|
| 117 |
def test_mark_gravity(self): |
| 118 |
text = self.text |
| 119 |
text.insert('1.0', 'Lorem ipsum') |
| 120 |
text.mark_set('here', '1.5') |
| 121 |
# The default gravity is right. |
| 122 |
self.assertEqual(text.mark_gravity('here'), 'right') |
| 123 |
text.mark_gravity('here', 'left') |
| 124 |
self.assertEqual(text.mark_gravity('here'), 'left') |
| 125 |
# With left gravity the mark stays before inserted text. |
| 126 |
text.insert('here', 'XXX') |
| 127 |
self.assertEqual(text.index('here'), '1.5') |
| 128 |
# With right gravity the mark moves after inserted text. |
| 129 |
text.mark_gravity('here', 'right') |
| 130 |
text.insert('here', 'YYY') |
| 131 |
self.assertEqual(text.index('here'), '1.8') |
| 132 |
|
| 133 |
self.assertRaises(TclError, text.mark_gravity, 'nonexistent') |
| 134 |
self.assertRaises(TclError, text.mark_gravity, 'here', 'invalid') |
| 135 |
self.assertRaises(TypeError, text.mark_gravity) |
| 136 |
self.assertRaises(TypeError, text.mark_gravity, 'here', 'left', 'extra') |
| 137 |
|
| 138 |
def test_mark_next_previous(self): |
| 139 |
text = self.text |
| 140 |
text.insert('1.0', 'Lorem ipsum') |
| 141 |
# Keep the builtin 'insert' and 'current' marks at 1.0 so they do |
| 142 |
# not interfere with the queries below. |
| 143 |
text.mark_set('insert', '1.0') |
| 144 |
text.mark_set('current', '1.0') |
| 145 |
text.mark_set('m1', '1.3') |
| 146 |
text.mark_set('m2', '1.7') |
| 147 |
# mark_next finds a mark at a position at or after the index, |
| 148 |
# mark_previous finds one at a position strictly before it. |
| 149 |
self.assertEqual(text.mark_next('1.1'), 'm1') |
| 150 |
self.assertEqual(text.mark_next('1.3'), 'm1') |
| 151 |
self.assertEqual(text.mark_next('1.4'), 'm2') |
| 152 |
self.assertIsNone(text.mark_next('1.8')) |
| 153 |
self.assertEqual(text.mark_previous('1.4'), 'm1') |
| 154 |
self.assertEqual(text.mark_previous('1.7'), 'm1') |
| 155 |
self.assertEqual(text.mark_previous('end'), 'm2') |
| 156 |
self.assertIsNone(text.mark_previous('1.0')) |
| 157 |
|
| 158 |
self.assertRaises(TclError, text.mark_next, 'invalid') |
| 159 |
self.assertRaises(TclError, text.mark_previous, 'invalid') |
| 160 |
self.assertRaises(TypeError, text.mark_next) |
| 161 |
self.assertRaises(TypeError, text.mark_previous) |
| 162 |
self.assertRaises(TypeError, text.mark_next, '1.0', '2.0') |
| 163 |
self.assertRaises(TypeError, text.mark_previous, '1.0', '2.0') |
| 164 |
|
| 165 |
def test_tag_add_remove_ranges(self): |
| 166 |
text = self.text |
| 167 |
text.insert('1.0', 'Lorem ipsum\ndolor sit amet') |
| 168 |
self.assertEqual(text.tag_ranges('sel'), ()) |
| 169 |
|
| 170 |
text.tag_add('big', '1.0', '1.5') |
| 171 |
self.assertEqual([str(i) for i in text.tag_ranges('big')], |
| 172 |
['1.0', '1.5']) |
| 173 |
# Add a second, disjoint range. |
| 174 |
text.tag_add('big', '2.0', '2.5') |
| 175 |
self.assertEqual([str(i) for i in text.tag_ranges('big')], |
| 176 |
['1.0', '1.5', '2.0', '2.5']) |
| 177 |
|
| 178 |
text.tag_remove('big', '1.0', '1.3') |
| 179 |
self.assertEqual([str(i) for i in text.tag_ranges('big')], |
| 180 |
['1.3', '1.5', '2.0', '2.5']) |
| 181 |
|
| 182 |
# tag_ranges of an undefined tag is empty, not an error. |
| 183 |
self.assertEqual(text.tag_ranges('nonexistent'), ()) |
| 184 |
self.assertRaises(TclError, text.tag_add, 'big', 'invalid') |
| 185 |
self.assertRaises(TypeError, text.tag_add, 'big') |
| 186 |
self.assertRaises(TclError, text.tag_remove, 'big', 'invalid') |
| 187 |
self.assertRaises(TypeError, text.tag_remove, 'big', '1.0', '2.0', '3.0') |
| 188 |
self.assertRaises(TypeError, text.tag_ranges, 'big', 'extra') |
| 189 |
|
| 190 |
def test_tag_names(self): |
| 191 |
text = self.text |
| 192 |
text.insert('1.0', 'Lorem ipsum') |
| 193 |
text.tag_add('a', '1.0', '1.5') |
| 194 |
text.tag_add('b', '1.3', '1.8') |
| 195 |
# 'sel' always exists; order reflects priority (creation order). |
| 196 |
self.assertEqual(set(text.tag_names()), {'sel', 'a', 'b'}) |
| 197 |
# Tags applied to the character at the given index. |
| 198 |
self.assertEqual(set(text.tag_names('1.4')), {'a', 'b'}) |
| 199 |
self.assertEqual(set(text.tag_names('1.0')), {'a'}) |
| 200 |
self.assertEqual(set(text.tag_names('1.10')), set()) |
| 201 |
self.assertRaises(TclError, text.tag_names, 'invalid') |
| 202 |
self.assertRaises(TypeError, text.tag_names, '1.0', '2.0') |
| 203 |
|
| 204 |
def test_tag_nextrange_prevrange(self): |
| 205 |
text = self.text |
| 206 |
text.insert('1.0', 'Lorem ipsum dolor') |
| 207 |
text.tag_add('a', '1.0', '1.5') |
| 208 |
text.tag_add('a', '1.12', '1.17') |
| 209 |
|
| 210 |
self.assertEqual([str(i) for i in text.tag_nextrange('a', '1.0')], |
| 211 |
['1.0', '1.5']) |
| 212 |
self.assertEqual([str(i) for i in text.tag_nextrange('a', '1.5')], |
| 213 |
['1.12', '1.17']) |
| 214 |
self.assertEqual(text.tag_nextrange('a', '1.17'), ()) |
| 215 |
|
| 216 |
self.assertEqual([str(i) for i in text.tag_prevrange('a', 'end')], |
| 217 |
['1.12', '1.17']) |
| 218 |
self.assertEqual([str(i) for i in text.tag_prevrange('a', '1.12')], |
| 219 |
['1.0', '1.5']) |
| 220 |
self.assertEqual(text.tag_prevrange('a', '1.0'), ()) |
| 221 |
|
| 222 |
# An undefined tag has no ranges, but the index must be valid. |
| 223 |
self.assertEqual(text.tag_nextrange('nonexistent', '1.0'), ()) |
| 224 |
self.assertRaises(TclError, text.tag_nextrange, 'a', 'invalid') |
| 225 |
self.assertRaises(TclError, text.tag_prevrange, 'a', 'invalid') |
| 226 |
self.assertRaises(TypeError, text.tag_nextrange, 'a') |
| 227 |
self.assertRaises(TypeError, text.tag_prevrange, 'a') |
| 228 |
self.assertRaises(TypeError, text.tag_nextrange, 'a', '1.0', '2.0', '3.0') |
| 229 |
self.assertRaises(TypeError, text.tag_prevrange, 'a', '1.0', '2.0', '3.0') |
| 230 |
|
| 231 |
def test_tag_delete(self): |
| 232 |
text = self.text |
| 233 |
text.insert('1.0', 'Lorem ipsum') |
| 234 |
text.tag_add('a', '1.0', '1.5') |
| 235 |
text.tag_add('b', '1.6', '1.11') |
| 236 |
self.assertEqual(set(text.tag_names()), {'sel', 'a', 'b'}) |
| 237 |
text.tag_delete('a', 'b') |
| 238 |
self.assertEqual(set(text.tag_names()), {'sel'}) |
| 239 |
self.assertEqual(text.tag_ranges('a'), ()) |
| 240 |
|
| 241 |
def test_tag_raise_lower(self): |
| 242 |
text = self.text |
| 243 |
text.tag_configure('a', foreground='red') |
| 244 |
text.tag_configure('b', foreground='green') |
| 245 |
text.tag_configure('c', foreground='blue') |
| 246 |
# Creation order is lowest-to-highest priority; tag_names lists |
| 247 |
# them in increasing priority order. |
| 248 |
self.assertEqual(text.tag_names(), ('sel', 'a', 'b', 'c')) |
| 249 |
text.tag_raise('a') |
| 250 |
self.assertEqual(text.tag_names(), ('sel', 'b', 'c', 'a')) |
| 251 |
text.tag_lower('a') |
| 252 |
self.assertEqual(text.tag_names(), ('a', 'sel', 'b', 'c')) |
| 253 |
text.tag_raise('a', 'b') |
| 254 |
self.assertEqual(text.tag_names(), ('sel', 'b', 'a', 'c')) |
| 255 |
text.tag_lower('c', 'b') |
| 256 |
self.assertEqual(text.tag_names(), ('sel', 'c', 'b', 'a')) |
| 257 |
|
| 258 |
self.assertRaises(TclError, text.tag_raise, 'nonexistent') |
| 259 |
self.assertRaises(TclError, text.tag_lower, 'nonexistent') |
| 260 |
self.assertRaises(TclError, text.tag_raise, 'a', 'nonexistent') |
| 261 |
self.assertRaises(TypeError, text.tag_raise) |
| 262 |
self.assertRaises(TypeError, text.tag_raise, 'a', 'b', 'c') |
| 263 |
self.assertRaises(TypeError, text.tag_lower, 'a', 'b', 'c') |
| 264 |
|
| 265 |
def test_tag_cget_configure(self): |
| 266 |
text = self.text |
| 267 |
text.tag_configure('a', foreground='red', underline=True) |
| 268 |
self.assertEqual(str(text.tag_cget('a', 'foreground')), 'red') |
| 269 |
self.assertIn(text.tag_cget('a', 'underline'), (1, '1', True)) |
| 270 |
# tag_cget normalizes the option name (no leading hyphen needed). |
| 271 |
self.assertEqual(str(text.tag_cget('a', '-foreground')), 'red') |
| 272 |
# configure() query returns the full option spec. |
| 273 |
self.assertEqual(text.tag_configure('a', 'foreground')[-1], 'red') |
| 274 |
text.tag_configure('a', foreground='blue') |
| 275 |
self.assertEqual(str(text.tag_cget('a', 'foreground')), 'blue') |
| 276 |
|
| 277 |
self.assertRaises(TclError, text.tag_cget, 'nonexistent', |
| 278 |
'foreground') |
| 279 |
self.assertRaises(TypeError, text.tag_cget, 'a') |
| 280 |
self.assertRaises(TypeError, text.tag_cget, 'a', 'foreground', 'extra') |
| 281 |
|
| 282 |
def test_edit_modified(self): |
| 283 |
text = self.text |
| 284 |
self.assertEqual(text.edit_modified(), 0) |
| 285 |
text.insert('1.0', 'spam') |
| 286 |
self.assertEqual(text.edit_modified(), 1) |
| 287 |
text.edit_modified(False) |
| 288 |
self.assertEqual(text.edit_modified(), 0) |
| 289 |
text.edit_modified(True) |
| 290 |
self.assertEqual(text.edit_modified(), 1) |
| 291 |
|
| 292 |
def test_edit_undo_redo(self): |
| 293 |
text = self.text |
| 294 |
text.configure(undo=True) |
| 295 |
text.insert('1.0', 'spam') |
| 296 |
text.edit_separator() |
| 297 |
text.insert('end', ' eggs') |
| 298 |
self.assertEqual(text.get('1.0', 'end'), 'spam eggs\n') |
| 299 |
|
| 300 |
text.edit_undo() |
| 301 |
self.assertEqual(text.get('1.0', 'end'), 'spam\n') |
| 302 |
text.edit_redo() |
| 303 |
self.assertEqual(text.get('1.0', 'end'), 'spam eggs\n') |
| 304 |
|
| 305 |
# An empty undo stack raises. |
| 306 |
text.edit_reset() |
| 307 |
self.assertRaises(TclError, text.edit_undo) |
| 308 |
|
| 309 |
def test_dump(self): |
| 310 |
text = self.text |
| 311 |
text.insert('1.0', 'hello') |
| 312 |
text.tag_add('a', '1.0', '1.3') |
| 313 |
text.mark_set('here', '1.2') |
| 314 |
|
| 315 |
dump = text.dump('1.0', 'end') |
| 316 |
self.assertIsInstance(dump, list) |
| 317 |
for item in dump: |
| 318 |
self.assertIsInstance(item, tuple) |
| 319 |
self.assertEqual(len(item), 3) |
| 320 |
kinds = {item[0] for item in dump} |
| 321 |
self.assertIn('text', kinds) |
| 322 |
self.assertIn('tagon', kinds) |
| 323 |
self.assertIn('mark', kinds) |
| 324 |
|
| 325 |
# Filtering by kind. |
| 326 |
text_only = text.dump('1.0', 'end', text=True) |
| 327 |
self.assertEqual({item[0] for item in text_only}, {'text'}) |
| 328 |
self.assertEqual(''.join(item[1] for item in text_only), 'hello\n') |
| 329 |
|
| 330 |
# The command callback receives each triple and suppresses the result. |
| 331 |
collected = [] |
| 332 |
result = text.dump('1.0', 'end', text=True, |
| 333 |
command=lambda *a: collected.append(a)) |
| 334 |
self.assertIsNone(result) |
| 335 |
self.assertEqual(''.join(key_value_index[1] |
| 336 |
for key_value_index in collected), 'hello\n') |
| 337 |
|
| 338 |
self.assertRaises(TclError, text.dump, 'invalid') |
| 339 |
self.assertRaises(TypeError, text.dump) |
| 340 |
self.assertRaises(TypeError, text.dump, '1.0', 'end', None, 'extra') |
| 341 |
|
| 342 |
def test_image(self): |
| 343 |
text = self.text |
| 344 |
image = tkinter.PhotoImage(master=self.root, width=10, height=10) |
| 345 |
text.insert('1.0', 'AB') |
| 346 |
name = text.image_create('1.1', image=image) |
| 347 |
self.assertIsInstance(name, str) |
| 348 |
self.assertIn(name, self.root.splitlist(text.image_names())) |
| 349 |
self.assertEqual(str(text.image_cget(name, 'image')), str(image)) |
| 350 |
|
| 351 |
text.image_configure(name, align='top') |
| 352 |
self.assertEqual(str(text.image_cget(name, 'align')), 'top') |
| 353 |
|
| 354 |
# An embedded image occupies a single index position. |
| 355 |
self.assertEqual(text.index('end - 1 char'), '1.3') |
| 356 |
|
| 357 |
# Either a name or an image is required, and the index must be valid. |
| 358 |
self.assertRaises(TclError, text.image_create, '1.0') |
| 359 |
self.assertRaises(TclError, text.image_create, 'invalid', |
| 360 |
image=image) |
| 361 |
self.assertRaises(TypeError, text.image_cget, name) |
| 362 |
self.assertRaises(TypeError, text.image_cget, name, 'image', 'extra') |
| 363 |
|
| 364 |
def test_window(self): |
| 365 |
text = self.text |
| 366 |
button = tkinter.Button(text, text='ok') |
| 367 |
text.insert('1.0', 'AB') |
| 368 |
text.window_create('1.1', window=button) |
| 369 |
self.assertEqual(self.root.splitlist(text.window_names()), |
| 370 |
(str(button),)) |
| 371 |
self.assertEqual(text.window_cget('1.1', 'window'), str(button)) |
| 372 |
|
| 373 |
text.window_configure('1.1', padx=5) |
| 374 |
self.assertEqual(text.window_cget('1.1', 'padx'), 5) |
| 375 |
|
| 376 |
# No embedded window at this index, and the index must be valid. |
| 377 |
self.assertRaises(TclError, text.window_cget, '1.0', 'window') |
| 378 |
self.assertRaises(TclError, text.window_cget, 'invalid', 'window') |
| 379 |
self.assertRaises(TypeError, text.window_cget, '1.1') |
| 380 |
self.assertRaises(TypeError, text.window_cget, '1.1', 'window', 'extra') |
| 381 |
button.destroy() |
| 382 |
|
| 383 |
def test_tag_configure(self): |
| 384 |
text = self.text |
| 385 |
tag = 'a' |
| 386 |
getint = text.tk.getint |
| 387 |
getboolean = text.tk.getboolean |
| 388 |
|
| 389 |
# Color options. |
| 390 |
for opt in ('foreground', 'background'): |
| 391 |
text.tag_configure(tag, **{opt: '#ff0000'}) |
| 392 |
self.assertEqual(str(text.tag_cget(tag, opt)), '#ff0000') |
| 393 |
|
| 394 |
# Stipple (bitmap) options. |
| 395 |
for opt in ('bgstipple', 'fgstipple'): |
| 396 |
text.tag_configure(tag, **{opt: 'gray50'}) |
| 397 |
self.assertEqual(str(text.tag_cget(tag, opt)), 'gray50') |
| 398 |
|
| 399 |
# Enumerated options. |
| 400 |
for opt, value in (('justify', 'center'), ('wrap', 'word'), |
| 401 |
('relief', 'raised'), ('tabstyle', 'wordprocessor'), |
| 402 |
('offset', '5')): |
| 403 |
text.tag_configure(tag, **{opt: value}) |
| 404 |
self.assertEqual(str(text.tag_cget(tag, 'justify')), 'center') |
| 405 |
self.assertEqual(str(text.tag_cget(tag, 'wrap')), 'word') |
| 406 |
self.assertEqual(str(text.tag_cget(tag, 'relief')), 'raised') |
| 407 |
self.assertEqual(str(text.tag_cget(tag, 'tabstyle')), 'wordprocessor') |
| 408 |
|
| 409 |
# Boolean options. |
| 410 |
for opt in ('underline', 'overstrike', 'elide'): |
| 411 |
text.tag_configure(tag, **{opt: True}) |
| 412 |
self.assertIs(getboolean(text.tag_cget(tag, opt)), True) |
| 413 |
text.tag_configure(tag, **{opt: False}) |
| 414 |
self.assertIs(getboolean(text.tag_cget(tag, opt)), False) |
| 415 |
|
| 416 |
# Screen-distance (pixel) options. |
| 417 |
for opt in ('borderwidth', 'lmargin1', 'lmargin2', 'rmargin', |
| 418 |
'spacing1', 'spacing2', 'spacing3'): |
| 419 |
text.tag_configure(tag, **{opt: 7}) |
| 420 |
self.assertEqual(getint(text.tag_cget(tag, opt)), 7) |
| 421 |
|
| 422 |
# Other options. |
| 423 |
text.tag_configure(tag, font='Helvetica 12') |
| 424 |
self.assertEqual(str(text.tag_cget(tag, 'font')), 'Helvetica 12') |
| 425 |
text.tag_configure(tag, tabs=(10.2, '1i')) |
| 426 |
self.assertEqual([str(x) for x in text.tag_ranges('sel')], []) |
| 427 |
|
| 428 |
self.assertRaises(TclError, text.tag_cget, tag, 'spam') |
| 429 |
|
| 430 |
@requires_tk(8, 6, 6) |
| 431 |
def test_tag_configure_colors(self): |
| 432 |
# Tag color options added in Tk 8.6.6. |
| 433 |
text = self.text |
| 434 |
tag = 'a' |
| 435 |
for opt in ('selectforeground', 'selectbackground', |
| 436 |
'lmargincolor', 'rmargincolor', |
| 437 |
'underlinefg', 'overstrikefg'): |
| 438 |
text.tag_configure(tag, **{opt: '#00ff00'}) |
| 439 |
self.assertEqual(str(text.tag_cget(tag, opt)), '#00ff00') |
| 440 |
|
| 441 |
def test_tag_configure_query(self): |
| 442 |
text = self.text |
| 443 |
tag = 'a' |
| 444 |
# Querying all options returns a dict keyed by option name. |
| 445 |
cnf = text.tag_configure(tag) |
| 446 |
self.assertIsInstance(cnf, dict) |
| 447 |
self.assertIn('foreground', cnf) |
| 448 |
# The value is the full 5-tuple option specification. |
| 449 |
self.assertEqual(len(cnf['foreground']), 5) |
| 450 |
|
| 451 |
# Querying a single option returns its specification. |
| 452 |
spec = text.tag_configure(tag, 'foreground') |
| 453 |
self.assertEqual(spec[0], 'foreground') |
| 454 |
self.assertEqual(spec[-1], '') # unset by default |
| 455 |
|
| 456 |
# Setting via keyword arguments and via a dict are equivalent. |
| 457 |
text.tag_configure(tag, foreground='red') |
| 458 |
self.assertEqual(str(text.tag_configure(tag, 'foreground')[-1]), 'red') |
| 459 |
text.tag_configure(tag, {'foreground': 'blue'}) |
| 460 |
self.assertEqual(str(text.tag_cget(tag, 'foreground')), 'blue') |
| 461 |
|
| 462 |
# tag_config is an alias of tag_configure. |
| 463 |
self.assertEqual(text.tag_config, text.tag_configure) |
| 464 |
|
| 465 |
def test_image_configure(self): |
| 466 |
text = self.text |
| 467 |
image = tkinter.PhotoImage(master=self.root, width=10, height=10) |
| 468 |
text.insert('1.0', 'AB') |
| 469 |
name = text.image_create('1.1', image=image, name='img') |
| 470 |
self.assertEqual(name, 'img') |
| 471 |
|
| 472 |
self.assertEqual(str(text.image_cget(name, 'name')), 'img') |
| 473 |
self.assertEqual(str(text.image_cget(name, 'image')), str(image)) |
| 474 |
for value in ('top', 'center', 'bottom', 'baseline'): |
| 475 |
text.image_configure(name, align=value) |
| 476 |
self.assertEqual(str(text.image_cget(name, 'align')), value) |
| 477 |
text.image_configure(name, padx=3, pady=4) |
| 478 |
self.assertEqual(text.tk.getint(text.image_cget(name, 'padx')), 3) |
| 479 |
self.assertEqual(text.tk.getint(text.image_cget(name, 'pady')), 4) |
| 480 |
|
| 481 |
# Querying returns the full option set. |
| 482 |
cnf = text.image_configure(name) |
| 483 |
self.assertIsInstance(cnf, dict) |
| 484 |
self.assertIn('align', cnf) |
| 485 |
self.assertRaises(TclError, text.image_cget, name, 'spam') |
| 486 |
|
| 487 |
def test_window_configure(self): |
| 488 |
text = self.text |
| 489 |
button = tkinter.Button(text, text='ok') |
| 490 |
text.insert('1.0', 'AB') |
| 491 |
text.window_create('1.1', window=button) |
| 492 |
|
| 493 |
self.assertEqual(text.window_cget('1.1', 'window'), str(button)) |
| 494 |
for value in ('top', 'center', 'bottom', 'baseline'): |
| 495 |
text.window_configure('1.1', align=value) |
| 496 |
self.assertEqual(str(text.window_cget('1.1', 'align')), value) |
| 497 |
text.window_configure('1.1', padx=3, pady=4) |
| 498 |
self.assertEqual(text.tk.getint(text.window_cget('1.1', 'padx')), 3) |
| 499 |
self.assertEqual(text.tk.getint(text.window_cget('1.1', 'pady')), 4) |
| 500 |
text.window_configure('1.1', stretch=True) |
| 501 |
self.assertIs(text.tk.getboolean(text.window_cget('1.1', 'stretch')), |
| 502 |
True) |
| 503 |
|
| 504 |
cnf = text.window_configure('1.1') |
| 505 |
self.assertIsInstance(cnf, dict) |
| 506 |
self.assertIn('stretch', cnf) |
| 507 |
self.assertRaises(TclError, text.window_cget, '1.1', 'spam') |
| 508 |
self.assertEqual(text.window_config, text.window_configure) |
| 509 |
button.destroy() |
| 510 |
|
| 511 |
def test_peer(self): |
| 512 |
text = self.text |
| 513 |
text.insert('1.0', 'Lorem ipsum') |
| 514 |
self.assertEqual(text.peer_names(), ()) |
| 515 |
|
| 516 |
text.peer_create('.peer1') |
| 517 |
names = self.root.splitlist(text.tk.call('winfo', 'children', '.')) |
| 518 |
self.assertIn('.peer1', [str(n) for n in names]) |
| 519 |
self.assertEqual([str(p) for p in text.peer_names()], ['.peer1']) |
| 520 |
# Peers share content. |
| 521 |
self.assertEqual(text.tk.call('.peer1', 'get', '1.0', 'end'), |
| 522 |
'Lorem ipsum\n') |
| 523 |
text.tk.call('destroy', '.peer1') |
| 524 |
|
| 525 |
def test_bbox(self): |
| 526 |
text = self.text |
| 527 |
text.insert('1.0', 'hello') |
| 528 |
text.update() |
| 529 |
bbox = text.bbox('1.0') |
| 530 |
self.assertIsInstance(bbox, tuple) |
| 531 |
self.assertEqual(len(bbox), 4) |
| 532 |
for v in bbox: |
| 533 |
self.assertIsInstance(v, int) |
| 534 |
# A character that is not displayed has no bounding box. |
| 535 |
self.assertIsNone(text.bbox('end')) |
| 536 |
self.assertRaises(TclError, text.bbox, 'invalid') |
| 537 |
self.assertRaises(TypeError, text.bbox) |
| 538 |
self.assertRaises(TypeError, text.bbox, '1.0', '2.0') |
| 539 |
|
| 540 |
def test_dlineinfo(self): |
| 541 |
text = self.text |
| 542 |
text.insert('1.0', 'hello\nworld') |
| 543 |
text.update() |
| 544 |
info = text.dlineinfo('1.0') |
| 545 |
self.assertIsInstance(info, tuple) |
| 546 |
self.assertEqual(len(info), 5) |
| 547 |
for v in info: |
| 548 |
self.assertIsInstance(v, int) |
| 549 |
self.assertRaises(TclError, text.dlineinfo, 'invalid') |
| 550 |
self.assertRaises(TypeError, text.dlineinfo) |
| 551 |
self.assertRaises(TypeError, text.dlineinfo, '1.0', '2.0') |
| 552 |
|
| 553 |
def test_see(self): |
| 554 |
text = self.text |
| 555 |
text.insert('1.0', '\n'.join('line %d' % i for i in range(200))) |
| 556 |
text.update() |
| 557 |
# Initially the last line is not visible. |
| 558 |
self.assertIsNone(text.bbox('200.0')) |
| 559 |
text.see('200.0') |
| 560 |
text.update() |
| 561 |
self.assertIsNotNone(text.bbox('200.0')) |
| 562 |
self.assertRaises(TclError, text.see, 'invalid') |
| 563 |
self.assertRaises(TypeError, text.see) |
| 564 |
self.assertRaises(TypeError, text.see, '1.0', '2.0') |
| 565 |
|
| 566 |
# yview_pickplace is a deprecated way to make an index visible. |
| 567 |
text.yview_pickplace('1.0') |
| 568 |
text.update() |
| 569 |
self.assertIsNotNone(text.bbox('1.0')) |
| 570 |
|
| 571 |
def test_search(self): |
| 572 |
text = self.text |
| 573 |
|
| 574 |
# pattern and index are obligatory arguments. |
| 575 |
self.assertRaises(TclError, text.search, None, '1.0') |
| 576 |
self.assertRaises(TclError, text.search, 'a', None) |
| 577 |
self.assertRaises(TclError, text.search, None, None) |
| 578 |
|
| 579 |
# Invalid text index. |
| 580 |
self.assertRaises(TclError, text.search, '', 0) |
| 581 |
self.assertRaises(TclError, text.search, '', '') |
| 582 |
self.assertRaises(TclError, text.search, '', 'invalid') |
| 583 |
self.assertRaises(TclError, text.search, '', '1.0', 'invalid') |
| 584 |
|
| 585 |
text.insert('1.0', |
| 586 |
'This is a test. This is only a test.\n' |
| 587 |
'Another line.\n' |
| 588 |
'Yet another line.\n' |
| 589 |
'64-bit') |
| 590 |
|
| 591 |
self.assertEqual(text.search('test', '1.0'), '1.10') |
| 592 |
self.assertEqual(text.search('test', '1.0', 'end'), '1.10') |
| 593 |
self.assertEqual(text.search('test', '1.0', '1.10'), '') |
| 594 |
self.assertEqual(text.search('test', '1.11'), '1.31') |
| 595 |
self.assertEqual(text.search('test', '1.32', 'end'), '') |
| 596 |
self.assertEqual(text.search('test', '1.32'), '1.10') |
| 597 |
|
| 598 |
self.assertEqual(text.search('', '1.0'), '1.0') # empty pattern |
| 599 |
self.assertEqual(text.search('nonexistent', '1.0'), '') |
| 600 |
self.assertEqual(text.search('-bit', '1.0'), '4.2') # starts with a hyphen |
| 601 |
|
| 602 |
self.assertEqual(text.search('line', '3.0'), '3.12') |
| 603 |
self.assertEqual(text.search('line', '3.0', forwards=True), '3.12') |
| 604 |
self.assertEqual(text.search('line', '3.0', backwards=True), '2.8') |
| 605 |
self.assertEqual(text.search('line', '3.0', forwards=True, backwards=True), '2.8') |
| 606 |
|
| 607 |
self.assertEqual(text.search('t.', '1.0'), '1.13') |
| 608 |
self.assertEqual(text.search('t.', '1.0', exact=True), '1.13') |
| 609 |
self.assertEqual(text.search('t.', '1.0', regexp=True), '1.10') |
| 610 |
self.assertEqual(text.search('t.', '1.0', exact=True, regexp=True), '1.10') |
| 611 |
|
| 612 |
self.assertEqual(text.search('TEST', '1.0'), '') |
| 613 |
self.assertEqual(text.search('TEST', '1.0', nocase=True), '1.10') |
| 614 |
|
| 615 |
var = tkinter.Variable(self.root) |
| 616 |
self.assertEqual(text.search('test', '1.0', count=var), '1.10') |
| 617 |
self.assertEqual(var.get(), 4 if self.wantobjects else '4') |
| 618 |
|
| 619 |
# TODO: Add test for elide=True |
| 620 |
|
| 621 |
def test_count(self): |
| 622 |
text = self.text |
| 623 |
text.insert('1.0', |
| 624 |
'Lorem ipsum dolor sit amet,\n' |
| 625 |
'consectetur adipiscing elit,\n' |
| 626 |
'sed do eiusmod tempor incididunt\n' |
| 627 |
'ut labore et dolore magna aliqua.') |
| 628 |
|
| 629 |
options = ('chars', 'indices', 'lines', |
| 630 |
'displaychars', 'displayindices', 'displaylines', |
| 631 |
'xpixels', 'ypixels') |
| 632 |
self.assertEqual(len(text.count('1.0', 'end', *options, return_ints=True)), 8) |
| 633 |
self.assertEqual(len(text.count('1.0', 'end', *options)), 8) |
| 634 |
self.assertEqual(text.count('1.0', 'end', 'chars', 'lines', return_ints=True), |
| 635 |
(124, 4)) |
| 636 |
self.assertEqual(text.count('1.3', '4.5', 'chars', 'lines'), (92, 3)) |
| 637 |
self.assertEqual(text.count('4.5', '1.3', 'chars', 'lines', return_ints=True), |
| 638 |
(-92, -3)) |
| 639 |
self.assertEqual(text.count('4.5', '1.3', 'chars', 'lines'), (-92, -3)) |
| 640 |
self.assertEqual(text.count('1.3', '1.3', 'chars', 'lines', return_ints=True), |
| 641 |
(0, 0)) |
| 642 |
self.assertEqual(text.count('1.3', '1.3', 'chars', 'lines'), (0, 0)) |
| 643 |
self.assertEqual(text.count('1.0', 'end', 'lines', return_ints=True), 4) |
| 644 |
self.assertEqual(text.count('1.0', 'end', 'lines'), (4,)) |
| 645 |
self.assertEqual(text.count('end', '1.0', 'lines', return_ints=True), -4) |
| 646 |
self.assertEqual(text.count('end', '1.0', 'lines'), (-4,)) |
| 647 |
self.assertEqual(text.count('1.3', '1.5', 'lines', return_ints=True), 0) |
| 648 |
self.assertEqual(text.count('1.3', '1.5', 'lines'), None) |
| 649 |
self.assertEqual(text.count('1.3', '1.3', 'lines', return_ints=True), 0) |
| 650 |
self.assertEqual(text.count('1.3', '1.3', 'lines'), None) |
| 651 |
# Count 'indices' by default. |
| 652 |
self.assertEqual(text.count('1.0', 'end', return_ints=True), 124) |
| 653 |
self.assertEqual(text.count('1.0', 'end'), (124,)) |
| 654 |
self.assertEqual(text.count('1.0', 'end', 'indices', return_ints=True), 124) |
| 655 |
self.assertEqual(text.count('1.0', 'end', 'indices'), (124,)) |
| 656 |
self.assertRaises(TclError, text.count, '1.0', 'end', 'spam') |
| 657 |
self.assertRaises(TclError, text.count, '1.0', 'end', '-lines') |
| 658 |
|
| 659 |
self.assertIsInstance(text.count('1.3', '1.5', 'ypixels', return_ints=True), int) |
| 660 |
self.assertIsInstance(text.count('1.3', '1.5', 'ypixels'), tuple) |
| 661 |
self.assertIsInstance(text.count('1.3', '1.5', 'update', 'ypixels', return_ints=True), int) |
| 662 |
self.assertIsInstance(text.count('1.3', '1.5', 'update', 'ypixels'), int) |
| 663 |
self.assertEqual(text.count('1.3', '1.3', 'update', 'ypixels', return_ints=True), 0) |
| 664 |
self.assertEqual(text.count('1.3', '1.3', 'update', 'ypixels'), None) |
| 665 |
self.assertEqual(text.count('1.3', '1.5', 'update', 'indices', return_ints=True), 2) |
| 666 |
self.assertEqual(text.count('1.3', '1.5', 'update', 'indices'), 2) |
| 667 |
self.assertEqual(text.count('1.3', '1.3', 'update', 'indices', return_ints=True), 0) |
| 668 |
self.assertEqual(text.count('1.3', '1.3', 'update', 'indices'), None) |
| 669 |
self.assertEqual(text.count('1.3', '1.5', 'update', return_ints=True), 2) |
| 670 |
self.assertEqual(text.count('1.3', '1.5', 'update'), (2,)) |
| 671 |
self.assertEqual(text.count('1.3', '1.3', 'update', return_ints=True), 0) |
| 672 |
self.assertEqual(text.count('1.3', '1.3', 'update'), None) |
| 673 |
|
| 674 |
|
| 675 |
if __name__ == "__main__": |
| 676 |
unittest.main() |