| 1 |
"Test colorizer, coverage 99%." |
| 2 |
from idlelib import colorizer |
| 3 |
from test.support import requires |
| 4 |
import unittest |
| 5 |
from unittest import mock |
| 6 |
from idlelib.idle_test.tkinter_testing_utils import run_in_tk_mainloop |
| 7 |
|
| 8 |
from functools import partial |
| 9 |
import textwrap |
| 10 |
from tkinter import Tk, Text |
| 11 |
from idlelib import config |
| 12 |
from idlelib.percolator import Percolator |
| 13 |
|
| 14 |
|
| 15 |
usercfg = colorizer.idleConf.userCfg |
| 16 |
testcfg = { |
| 17 |
'main': config.IdleUserConfParser(''), |
| 18 |
'highlight': config.IdleUserConfParser(''), |
| 19 |
'keys': config.IdleUserConfParser(''), |
| 20 |
'extensions': config.IdleUserConfParser(''), |
| 21 |
} |
| 22 |
|
| 23 |
source = textwrap.dedent("""\ |
| 24 |
if True: int ('1') # keyword, builtin, string, comment |
| 25 |
elif False: print(0) # 'string' in comment |
| 26 |
else: float(None) # if in comment |
| 27 |
if iF + If + IF: 'keyword matching must respect case' |
| 28 |
if'': x or'' # valid keyword-string no-space combinations |
| 29 |
async def f(): await g() |
| 30 |
# Strings should be entirely colored, including quotes. |
| 31 |
'x', '''x''', "x", \"""x\""" |
| 32 |
'abc\\ |
| 33 |
def' |
| 34 |
'''abc\\ |
| 35 |
def''' |
| 36 |
# All valid prefixes for unicode and byte strings should be colored. |
| 37 |
r'x', u'x', R'x', U'x', f'x', F'x' |
| 38 |
fr'x', Fr'x', fR'x', FR'x', rf'x', rF'x', Rf'x', RF'x' |
| 39 |
tr'x', Tr'x', tR'x', TR'x', rt'x', rT'x', Rt'x', RT'x' |
| 40 |
b'x',B'x', br'x',Br'x',bR'x',BR'x', rb'x', rB'x',Rb'x',RB'x' |
| 41 |
# Invalid combinations of legal characters should be half colored. |
| 42 |
ur'x', ru'x', uf'x', fu'x', UR'x', ufr'x', rfu'x', xf'x', fx'x' |
| 43 |
match point: |
| 44 |
case (x, 0) as _: |
| 45 |
print(f"X={x}") |
| 46 |
case [_, [_], "_", |
| 47 |
_]: |
| 48 |
pass |
| 49 |
case _ if ("a" if _ else set()): pass |
| 50 |
case _: |
| 51 |
raise ValueError("Not a point _") |
| 52 |
''' |
| 53 |
case _:''' |
| 54 |
"match x:" |
| 55 |
""") |
| 56 |
|
| 57 |
|
| 58 |
def setUpModule(): |
| 59 |
colorizer.idleConf.userCfg = testcfg |
| 60 |
|
| 61 |
|
| 62 |
def tearDownModule(): |
| 63 |
colorizer.idleConf.userCfg = usercfg |
| 64 |
|
| 65 |
|
| 66 |
class FunctionTest(unittest.TestCase): |
| 67 |
|
| 68 |
def test_any(self): |
| 69 |
self.assertEqual(colorizer.any('test', ('a', 'b', 'cd')), |
| 70 |
'(?P<test>a|b|cd)') |
| 71 |
|
| 72 |
def test_make_pat(self): |
| 73 |
# Tested in more detail by testing prog. |
| 74 |
self.assertTrue(colorizer.make_pat()) |
| 75 |
|
| 76 |
def test_prog(self): |
| 77 |
prog = colorizer.prog |
| 78 |
eq = self.assertEqual |
| 79 |
line = 'def f():\n print("hello")\n' |
| 80 |
m = prog.search(line) |
| 81 |
eq(m.groupdict()['KEYWORD'], 'def') |
| 82 |
m = prog.search(line, m.end()) |
| 83 |
eq(m.groupdict()['SYNC'], '\n') |
| 84 |
m = prog.search(line, m.end()) |
| 85 |
eq(m.groupdict()['BUILTIN'], 'print') |
| 86 |
m = prog.search(line, m.end()) |
| 87 |
eq(m.groupdict()['STRING'], '"hello"') |
| 88 |
m = prog.search(line, m.end()) |
| 89 |
eq(m.groupdict()['SYNC'], '\n') |
| 90 |
|
| 91 |
def test_idprog(self): |
| 92 |
idprog = colorizer.idprog |
| 93 |
m = idprog.match('nospace') |
| 94 |
self.assertIsNone(m) |
| 95 |
m = idprog.match(' space') |
| 96 |
self.assertEqual(m.group(0), ' space') |
| 97 |
|
| 98 |
|
| 99 |
class ColorConfigTest(unittest.TestCase): |
| 100 |
|
| 101 |
@classmethod |
| 102 |
def setUpClass(cls): |
| 103 |
requires('gui') |
| 104 |
root = cls.root = Tk() |
| 105 |
root.withdraw() |
| 106 |
cls.text = Text(root) |
| 107 |
|
| 108 |
@classmethod |
| 109 |
def tearDownClass(cls): |
| 110 |
del cls.text |
| 111 |
cls.root.update_idletasks() |
| 112 |
cls.root.destroy() |
| 113 |
del cls.root |
| 114 |
|
| 115 |
def test_color_config(self): |
| 116 |
text = self.text |
| 117 |
eq = self.assertEqual |
| 118 |
colorizer.color_config(text) |
| 119 |
# Uses IDLE Classic theme as default. |
| 120 |
eq(text['background'], '#ffffff') |
| 121 |
eq(text['foreground'], '#000000') |
| 122 |
eq(text['selectbackground'], 'gray') |
| 123 |
eq(text['selectforeground'], '#000000') |
| 124 |
eq(text['insertbackground'], 'black') |
| 125 |
eq(text['inactiveselectbackground'], 'gray') |
| 126 |
|
| 127 |
|
| 128 |
class ColorDelegatorInstantiationTest(unittest.TestCase): |
| 129 |
|
| 130 |
@classmethod |
| 131 |
def setUpClass(cls): |
| 132 |
requires('gui') |
| 133 |
root = cls.root = Tk() |
| 134 |
root.withdraw() |
| 135 |
cls.text = Text(root) |
| 136 |
|
| 137 |
@classmethod |
| 138 |
def tearDownClass(cls): |
| 139 |
del cls.text |
| 140 |
cls.root.update_idletasks() |
| 141 |
cls.root.destroy() |
| 142 |
del cls.root |
| 143 |
|
| 144 |
def setUp(self): |
| 145 |
self.color = colorizer.ColorDelegator() |
| 146 |
|
| 147 |
def tearDown(self): |
| 148 |
self.color.close() |
| 149 |
self.text.delete('1.0', 'end') |
| 150 |
self.color.resetcache() |
| 151 |
del self.color |
| 152 |
|
| 153 |
def test_init(self): |
| 154 |
color = self.color |
| 155 |
self.assertIsInstance(color, colorizer.ColorDelegator) |
| 156 |
|
| 157 |
def test_init_state(self): |
| 158 |
# init_state() is called during the instantiation of |
| 159 |
# ColorDelegator in setUp(). |
| 160 |
color = self.color |
| 161 |
self.assertIsNone(color.after_id) |
| 162 |
self.assertTrue(color.allow_colorizing) |
| 163 |
self.assertFalse(color.colorizing) |
| 164 |
self.assertFalse(color.stop_colorizing) |
| 165 |
|
| 166 |
|
| 167 |
class ColorDelegatorTest(unittest.TestCase): |
| 168 |
|
| 169 |
@classmethod |
| 170 |
def setUpClass(cls): |
| 171 |
requires('gui') |
| 172 |
root = cls.root = Tk() |
| 173 |
root.withdraw() |
| 174 |
text = cls.text = Text(root) |
| 175 |
cls.percolator = Percolator(text) |
| 176 |
# Delegator stack = [Delegator(text)] |
| 177 |
|
| 178 |
@classmethod |
| 179 |
def tearDownClass(cls): |
| 180 |
cls.percolator.close() |
| 181 |
del cls.percolator, cls.text |
| 182 |
cls.root.update_idletasks() |
| 183 |
cls.root.destroy() |
| 184 |
del cls.root |
| 185 |
|
| 186 |
def setUp(self): |
| 187 |
self.color = colorizer.ColorDelegator() |
| 188 |
self.percolator.insertfilter(self.color) |
| 189 |
# Calls color.setdelegate(Delegator(text)). |
| 190 |
|
| 191 |
def tearDown(self): |
| 192 |
self.color.close() |
| 193 |
self.percolator.removefilter(self.color) |
| 194 |
self.text.delete('1.0', 'end') |
| 195 |
self.color.resetcache() |
| 196 |
del self.color |
| 197 |
|
| 198 |
def test_setdelegate(self): |
| 199 |
# Called in setUp when filter is attached to percolator. |
| 200 |
color = self.color |
| 201 |
self.assertIsInstance(color.delegate, colorizer.Delegator) |
| 202 |
# It is too late to mock notify_range, so test side effect. |
| 203 |
self.assertEqual(self.root.tk.call( |
| 204 |
'after', 'info', color.after_id)[1], 'timer') |
| 205 |
|
| 206 |
def test_LoadTagDefs(self): |
| 207 |
highlight = partial(config.idleConf.GetHighlight, theme='IDLE Classic') |
| 208 |
for tag, colors in self.color.tagdefs.items(): |
| 209 |
with self.subTest(tag=tag): |
| 210 |
self.assertIn('background', colors) |
| 211 |
self.assertIn('foreground', colors) |
| 212 |
if tag not in ('SYNC', 'TODO'): |
| 213 |
self.assertEqual(colors, highlight(element=tag.lower())) |
| 214 |
|
| 215 |
def test_config_colors(self): |
| 216 |
text = self.text |
| 217 |
highlight = partial(config.idleConf.GetHighlight, theme='IDLE Classic') |
| 218 |
for tag in self.color.tagdefs: |
| 219 |
for plane in ('background', 'foreground'): |
| 220 |
with self.subTest(tag=tag, plane=plane): |
| 221 |
if tag in ('SYNC', 'TODO'): |
| 222 |
self.assertEqual(text.tag_cget(tag, plane), '') |
| 223 |
else: |
| 224 |
self.assertEqual(text.tag_cget(tag, plane), |
| 225 |
highlight(element=tag.lower())[plane]) |
| 226 |
# 'sel' is marked as the highest priority. |
| 227 |
self.assertEqual(text.tag_names()[-1], 'sel') |
| 228 |
|
| 229 |
@mock.patch.object(colorizer.ColorDelegator, 'notify_range') |
| 230 |
def test_insert(self, mock_notify): |
| 231 |
text = self.text |
| 232 |
# Initial text. |
| 233 |
text.insert('insert', 'foo') |
| 234 |
self.assertEqual(text.get('1.0', 'end'), 'foo\n') |
| 235 |
mock_notify.assert_called_with('1.0', '1.0+3c') |
| 236 |
# Additional text. |
| 237 |
text.insert('insert', 'barbaz') |
| 238 |
self.assertEqual(text.get('1.0', 'end'), 'foobarbaz\n') |
| 239 |
mock_notify.assert_called_with('1.3', '1.3+6c') |
| 240 |
|
| 241 |
@mock.patch.object(colorizer.ColorDelegator, 'notify_range') |
| 242 |
def test_delete(self, mock_notify): |
| 243 |
text = self.text |
| 244 |
# Initialize text. |
| 245 |
text.insert('insert', 'abcdefghi') |
| 246 |
self.assertEqual(text.get('1.0', 'end'), 'abcdefghi\n') |
| 247 |
# Delete single character. |
| 248 |
text.delete('1.7') |
| 249 |
self.assertEqual(text.get('1.0', 'end'), 'abcdefgi\n') |
| 250 |
mock_notify.assert_called_with('1.7') |
| 251 |
# Delete multiple characters. |
| 252 |
text.delete('1.3', '1.6') |
| 253 |
self.assertEqual(text.get('1.0', 'end'), 'abcgi\n') |
| 254 |
mock_notify.assert_called_with('1.3') |
| 255 |
|
| 256 |
def test_notify_range(self): |
| 257 |
text = self.text |
| 258 |
color = self.color |
| 259 |
eq = self.assertEqual |
| 260 |
|
| 261 |
# Colorizing already scheduled. |
| 262 |
save_id = color.after_id |
| 263 |
eq(self.root.tk.call('after', 'info', save_id)[1], 'timer') |
| 264 |
self.assertFalse(color.colorizing) |
| 265 |
self.assertFalse(color.stop_colorizing) |
| 266 |
self.assertTrue(color.allow_colorizing) |
| 267 |
|
| 268 |
# Coloring scheduled and colorizing in progress. |
| 269 |
color.colorizing = True |
| 270 |
color.notify_range('1.0', 'end') |
| 271 |
self.assertFalse(color.stop_colorizing) |
| 272 |
eq(color.after_id, save_id) |
| 273 |
|
| 274 |
# No colorizing scheduled and colorizing in progress. |
| 275 |
text.after_cancel(save_id) |
| 276 |
color.after_id = None |
| 277 |
color.notify_range('1.0', '1.0+3c') |
| 278 |
self.assertTrue(color.stop_colorizing) |
| 279 |
self.assertIsNotNone(color.after_id) |
| 280 |
eq(self.root.tk.call('after', 'info', color.after_id)[1], 'timer') |
| 281 |
# New event scheduled. |
| 282 |
self.assertNotEqual(color.after_id, save_id) |
| 283 |
|
| 284 |
# No colorizing scheduled and colorizing off. |
| 285 |
text.after_cancel(color.after_id) |
| 286 |
color.after_id = None |
| 287 |
color.allow_colorizing = False |
| 288 |
color.notify_range('1.4', '1.4+10c') |
| 289 |
# Nothing scheduled when colorizing is off. |
| 290 |
self.assertIsNone(color.after_id) |
| 291 |
|
| 292 |
def test_toggle_colorize_event(self): |
| 293 |
color = self.color |
| 294 |
eq = self.assertEqual |
| 295 |
|
| 296 |
# Starts with colorizing allowed and scheduled. |
| 297 |
self.assertFalse(color.colorizing) |
| 298 |
self.assertFalse(color.stop_colorizing) |
| 299 |
self.assertTrue(color.allow_colorizing) |
| 300 |
eq(self.root.tk.call('after', 'info', color.after_id)[1], 'timer') |
| 301 |
|
| 302 |
# Toggle colorizing off. |
| 303 |
color.toggle_colorize_event() |
| 304 |
self.assertIsNone(color.after_id) |
| 305 |
self.assertFalse(color.colorizing) |
| 306 |
self.assertFalse(color.stop_colorizing) |
| 307 |
self.assertFalse(color.allow_colorizing) |
| 308 |
|
| 309 |
# Toggle on while colorizing in progress (doesn't add timer). |
| 310 |
color.colorizing = True |
| 311 |
color.toggle_colorize_event() |
| 312 |
self.assertIsNone(color.after_id) |
| 313 |
self.assertTrue(color.colorizing) |
| 314 |
self.assertFalse(color.stop_colorizing) |
| 315 |
self.assertTrue(color.allow_colorizing) |
| 316 |
|
| 317 |
# Toggle off while colorizing in progress. |
| 318 |
color.toggle_colorize_event() |
| 319 |
self.assertIsNone(color.after_id) |
| 320 |
self.assertTrue(color.colorizing) |
| 321 |
self.assertTrue(color.stop_colorizing) |
| 322 |
self.assertFalse(color.allow_colorizing) |
| 323 |
|
| 324 |
# Toggle on while colorizing not in progress. |
| 325 |
color.colorizing = False |
| 326 |
color.toggle_colorize_event() |
| 327 |
eq(self.root.tk.call('after', 'info', color.after_id)[1], 'timer') |
| 328 |
self.assertFalse(color.colorizing) |
| 329 |
self.assertTrue(color.stop_colorizing) |
| 330 |
self.assertTrue(color.allow_colorizing) |
| 331 |
|
| 332 |
@mock.patch.object(colorizer.ColorDelegator, 'recolorize_main') |
| 333 |
def test_recolorize(self, mock_recmain): |
| 334 |
text = self.text |
| 335 |
color = self.color |
| 336 |
eq = self.assertEqual |
| 337 |
# Call recolorize manually and not scheduled. |
| 338 |
text.after_cancel(color.after_id) |
| 339 |
|
| 340 |
# No delegate. |
| 341 |
save_delegate = color.delegate |
| 342 |
color.delegate = None |
| 343 |
color.recolorize() |
| 344 |
mock_recmain.assert_not_called() |
| 345 |
color.delegate = save_delegate |
| 346 |
|
| 347 |
# Toggle off colorizing. |
| 348 |
color.allow_colorizing = False |
| 349 |
color.recolorize() |
| 350 |
mock_recmain.assert_not_called() |
| 351 |
color.allow_colorizing = True |
| 352 |
|
| 353 |
# Colorizing in progress. |
| 354 |
color.colorizing = True |
| 355 |
color.recolorize() |
| 356 |
mock_recmain.assert_not_called() |
| 357 |
color.colorizing = False |
| 358 |
|
| 359 |
# Colorizing is done, but not completed, so rescheduled. |
| 360 |
color.recolorize() |
| 361 |
self.assertFalse(color.stop_colorizing) |
| 362 |
self.assertFalse(color.colorizing) |
| 363 |
mock_recmain.assert_called() |
| 364 |
eq(mock_recmain.call_count, 1) |
| 365 |
# Rescheduled when TODO tag still exists. |
| 366 |
eq(self.root.tk.call('after', 'info', color.after_id)[1], 'timer') |
| 367 |
|
| 368 |
# No changes to text, so no scheduling added. |
| 369 |
text.tag_remove('TODO', '1.0', 'end') |
| 370 |
color.recolorize() |
| 371 |
self.assertFalse(color.stop_colorizing) |
| 372 |
self.assertFalse(color.colorizing) |
| 373 |
mock_recmain.assert_called() |
| 374 |
eq(mock_recmain.call_count, 2) |
| 375 |
self.assertIsNone(color.after_id) |
| 376 |
|
| 377 |
@mock.patch.object(colorizer.ColorDelegator, 'notify_range') |
| 378 |
def test_recolorize_main(self, mock_notify): |
| 379 |
text = self.text |
| 380 |
color = self.color |
| 381 |
eq = self.assertEqual |
| 382 |
|
| 383 |
text.insert('insert', source) |
| 384 |
expected = (('1.0', ('KEYWORD',)), ('1.2', ()), ('1.3', ('KEYWORD',)), |
| 385 |
('1.7', ()), ('1.9', ('BUILTIN',)), ('1.14', ('STRING',)), |
| 386 |
('1.19', ('COMMENT',)), |
| 387 |
('2.1', ('KEYWORD',)), ('2.18', ()), ('2.25', ('COMMENT',)), |
| 388 |
('3.6', ('BUILTIN',)), ('3.12', ('KEYWORD',)), ('3.21', ('COMMENT',)), |
| 389 |
('4.0', ('KEYWORD',)), ('4.3', ()), ('4.6', ()), |
| 390 |
('5.2', ('STRING',)), ('5.8', ('KEYWORD',)), ('5.10', ('STRING',)), |
| 391 |
('6.0', ('KEYWORD',)), ('6.10', ('DEFINITION',)), ('6.11', ()), |
| 392 |
('8.0', ('STRING',)), ('8.4', ()), ('8.5', ('STRING',)), |
| 393 |
('8.12', ()), ('8.14', ('STRING',)), |
| 394 |
('20.0', ('KEYWORD',)), |
| 395 |
('21.4', ('KEYWORD',)), ('21.16', ('KEYWORD',)),# ('21.19', ('KEYWORD',)), |
| 396 |
#('23.4', ('KEYWORD',)), ('23.10', ('KEYWORD',)), ('23.14', ('KEYWORD',)), ('23.19', ('STRING',)), |
| 397 |
#('24.12', ('KEYWORD',)), |
| 398 |
('25.8', ('KEYWORD',)), |
| 399 |
('26.4', ('KEYWORD',)), ('26.9', ('KEYWORD',)), |
| 400 |
('26.11', ('KEYWORD',)), ('26.15', ('STRING',)), |
| 401 |
('26.19', ('KEYWORD',)), ('26.22', ()), |
| 402 |
('26.24', ('KEYWORD',)), ('26.29', ('BUILTIN',)), ('26.37', ('KEYWORD',)), |
| 403 |
('27.4', ('KEYWORD',)), ('27.9', ('KEYWORD',)),# ('27.11', ('KEYWORD',)), ('27.14', (),), |
| 404 |
('28.25', ('STRING',)), ('28.38', ('STRING',)), |
| 405 |
('30.0', ('STRING',)), |
| 406 |
('31.1', ('STRING',)), |
| 407 |
# SYNC at the end of every line. |
| 408 |
('1.55', ('SYNC',)), ('2.50', ('SYNC',)), ('3.34', ('SYNC',)), |
| 409 |
) |
| 410 |
|
| 411 |
# Nothing marked to do therefore no tags in text. |
| 412 |
text.tag_remove('TODO', '1.0', 'end') |
| 413 |
color.recolorize_main() |
| 414 |
for tag in text.tag_names(): |
| 415 |
with self.subTest(tag=tag): |
| 416 |
eq(text.tag_ranges(tag), ()) |
| 417 |
|
| 418 |
# Source marked for processing. |
| 419 |
text.tag_add('TODO', '1.0', 'end') |
| 420 |
# Check some indexes. |
| 421 |
color.recolorize_main() |
| 422 |
for index, expected_tags in expected: |
| 423 |
with self.subTest(index=index): |
| 424 |
eq(text.tag_names(index), expected_tags) |
| 425 |
|
| 426 |
# Check for some tags for ranges. |
| 427 |
eq(text.tag_nextrange('TODO', '1.0'), ()) |
| 428 |
eq(text.tag_nextrange('KEYWORD', '1.0'), ('1.0', '1.2')) |
| 429 |
eq(text.tag_nextrange('COMMENT', '2.0'), ('2.22', '2.43')) |
| 430 |
eq(text.tag_nextrange('SYNC', '2.0'), ('2.43', '3.0')) |
| 431 |
eq(text.tag_nextrange('STRING', '2.0'), ('4.17', '4.53')) |
| 432 |
eq(text.tag_nextrange('STRING', '8.0'), ('8.0', '8.3')) |
| 433 |
eq(text.tag_nextrange('STRING', '8.3'), ('8.5', '8.12')) |
| 434 |
eq(text.tag_nextrange('STRING', '8.12'), ('8.14', '8.17')) |
| 435 |
eq(text.tag_nextrange('STRING', '8.17'), ('8.19', '8.26')) |
| 436 |
eq(text.tag_nextrange('SYNC', '8.0'), ('8.26', '9.0')) |
| 437 |
eq(text.tag_nextrange('SYNC', '31.0'), ('31.10', '33.0')) |
| 438 |
|
| 439 |
def _assert_highlighting(self, source, tag_ranges): |
| 440 |
"""Check highlighting of a given piece of code. |
| 441 |
|
| 442 |
This inserts just this code into the Text widget. It will then |
| 443 |
check that the resulting highlighting tag ranges exactly match |
| 444 |
those described in the given `tag_ranges` dict. |
| 445 |
|
| 446 |
Note that the irrelevant tags 'sel', 'TODO' and 'SYNC' are |
| 447 |
ignored. |
| 448 |
""" |
| 449 |
text = self.text |
| 450 |
|
| 451 |
with mock.patch.object(colorizer.ColorDelegator, 'notify_range'): |
| 452 |
text.delete('1.0', 'end-1c') |
| 453 |
text.insert('insert', source) |
| 454 |
text.tag_add('TODO', '1.0', 'end-1c') |
| 455 |
self.color.recolorize_main() |
| 456 |
|
| 457 |
# Make a dict with highlighting tag ranges in the Text widget. |
| 458 |
text_tag_ranges = {} |
| 459 |
for tag in set(text.tag_names()) - {'sel', 'TODO', 'SYNC'}: |
| 460 |
indexes = [rng.string for rng in text.tag_ranges(tag)] |
| 461 |
for index_pair in zip(indexes[::2], indexes[1::2]): |
| 462 |
text_tag_ranges.setdefault(tag, []).append(index_pair) |
| 463 |
|
| 464 |
self.assertEqual(text_tag_ranges, tag_ranges) |
| 465 |
|
| 466 |
with mock.patch.object(colorizer.ColorDelegator, 'notify_range'): |
| 467 |
text.delete('1.0', 'end-1c') |
| 468 |
|
| 469 |
def test_def_statement(self): |
| 470 |
# empty def |
| 471 |
self._assert_highlighting('def', {'KEYWORD': [('1.0', '1.3')]}) |
| 472 |
|
| 473 |
# def followed by identifier |
| 474 |
self._assert_highlighting('def foo:', {'KEYWORD': [('1.0', '1.3')], |
| 475 |
'DEFINITION': [('1.4', '1.7')]}) |
| 476 |
|
| 477 |
# def followed by partial identifier |
| 478 |
self._assert_highlighting('def fo', {'KEYWORD': [('1.0', '1.3')], |
| 479 |
'DEFINITION': [('1.4', '1.6')]}) |
| 480 |
|
| 481 |
# def followed by non-keyword |
| 482 |
self._assert_highlighting('def ++', {'KEYWORD': [('1.0', '1.3')]}) |
| 483 |
|
| 484 |
def test_match_soft_keyword(self): |
| 485 |
# empty match |
| 486 |
self._assert_highlighting('match', {'KEYWORD': [('1.0', '1.5')]}) |
| 487 |
|
| 488 |
# match followed by partial identifier |
| 489 |
self._assert_highlighting('match fo', {'KEYWORD': [('1.0', '1.5')]}) |
| 490 |
|
| 491 |
# match followed by identifier and colon |
| 492 |
self._assert_highlighting('match foo:', {'KEYWORD': [('1.0', '1.5')]}) |
| 493 |
|
| 494 |
# match followed by keyword |
| 495 |
self._assert_highlighting('match and', {'KEYWORD': [('1.6', '1.9')]}) |
| 496 |
|
| 497 |
# match followed by builtin with keyword prefix |
| 498 |
self._assert_highlighting('match int:', {'KEYWORD': [('1.0', '1.5')], |
| 499 |
'BUILTIN': [('1.6', '1.9')]}) |
| 500 |
|
| 501 |
# match followed by non-text operator |
| 502 |
self._assert_highlighting('match^', {}) |
| 503 |
self._assert_highlighting('match @', {}) |
| 504 |
|
| 505 |
# match followed by colon |
| 506 |
self._assert_highlighting('match :', {}) |
| 507 |
|
| 508 |
# match followed by comma |
| 509 |
self._assert_highlighting('match\t,', {}) |
| 510 |
|
| 511 |
# match followed by a lone underscore |
| 512 |
self._assert_highlighting('match _:', {'KEYWORD': [('1.0', '1.5')]}) |
| 513 |
|
| 514 |
def test_case_soft_keyword(self): |
| 515 |
# empty case |
| 516 |
self._assert_highlighting('case', {'KEYWORD': [('1.0', '1.4')]}) |
| 517 |
|
| 518 |
# case followed by partial identifier |
| 519 |
self._assert_highlighting('case fo', {'KEYWORD': [('1.0', '1.4')]}) |
| 520 |
|
| 521 |
# case followed by identifier and colon |
| 522 |
self._assert_highlighting('case foo:', {'KEYWORD': [('1.0', '1.4')]}) |
| 523 |
|
| 524 |
# case followed by keyword |
| 525 |
self._assert_highlighting('case and', {'KEYWORD': [('1.5', '1.8')]}) |
| 526 |
|
| 527 |
# case followed by builtin with keyword prefix |
| 528 |
self._assert_highlighting('case int:', {'KEYWORD': [('1.0', '1.4')], |
| 529 |
'BUILTIN': [('1.5', '1.8')]}) |
| 530 |
|
| 531 |
# case followed by non-text operator |
| 532 |
self._assert_highlighting('case^', {}) |
| 533 |
self._assert_highlighting('case @', {}) |
| 534 |
|
| 535 |
# case followed by colon |
| 536 |
self._assert_highlighting('case :', {}) |
| 537 |
|
| 538 |
# case followed by comma |
| 539 |
self._assert_highlighting('case\t,', {}) |
| 540 |
|
| 541 |
# case followed by a lone underscore |
| 542 |
self._assert_highlighting('case _:', {'KEYWORD': [('1.0', '1.4'), |
| 543 |
('1.5', '1.6')]}) |
| 544 |
|
| 545 |
def test_long_multiline_string(self): |
| 546 |
source = textwrap.dedent('''\ |
| 547 |
"""a |
| 548 |
b |
| 549 |
c |
| 550 |
d |
| 551 |
e""" |
| 552 |
''') |
| 553 |
self._assert_highlighting(source, {'STRING': [('1.0', '5.4')]}) |
| 554 |
|
| 555 |
@run_in_tk_mainloop(delay=50) |
| 556 |
def test_incremental_editing(self): |
| 557 |
text = self.text |
| 558 |
eq = self.assertEqual |
| 559 |
|
| 560 |
# Simulate typing 'inte'. During this, the highlighting should |
| 561 |
# change from normal to keyword to builtin to normal. |
| 562 |
text.insert('insert', 'i') |
| 563 |
yield |
| 564 |
eq(text.tag_nextrange('BUILTIN', '1.0'), ()) |
| 565 |
eq(text.tag_nextrange('KEYWORD', '1.0'), ()) |
| 566 |
|
| 567 |
text.insert('insert', 'n') |
| 568 |
yield |
| 569 |
eq(text.tag_nextrange('BUILTIN', '1.0'), ()) |
| 570 |
eq(text.tag_nextrange('KEYWORD', '1.0'), ('1.0', '1.2')) |
| 571 |
|
| 572 |
text.insert('insert', 't') |
| 573 |
yield |
| 574 |
eq(text.tag_nextrange('BUILTIN', '1.0'), ('1.0', '1.3')) |
| 575 |
eq(text.tag_nextrange('KEYWORD', '1.0'), ()) |
| 576 |
|
| 577 |
text.insert('insert', 'e') |
| 578 |
yield |
| 579 |
eq(text.tag_nextrange('BUILTIN', '1.0'), ()) |
| 580 |
eq(text.tag_nextrange('KEYWORD', '1.0'), ()) |
| 581 |
|
| 582 |
# Simulate deleting three characters from the end of 'inte'. |
| 583 |
# During this, the highlighting should change from normal to |
| 584 |
# builtin to keyword to normal. |
| 585 |
text.delete('insert-1c', 'insert') |
| 586 |
yield |
| 587 |
eq(text.tag_nextrange('BUILTIN', '1.0'), ('1.0', '1.3')) |
| 588 |
eq(text.tag_nextrange('KEYWORD', '1.0'), ()) |
| 589 |
|
| 590 |
text.delete('insert-1c', 'insert') |
| 591 |
yield |
| 592 |
eq(text.tag_nextrange('BUILTIN', '1.0'), ()) |
| 593 |
eq(text.tag_nextrange('KEYWORD', '1.0'), ('1.0', '1.2')) |
| 594 |
|
| 595 |
text.delete('insert-1c', 'insert') |
| 596 |
yield |
| 597 |
eq(text.tag_nextrange('BUILTIN', '1.0'), ()) |
| 598 |
eq(text.tag_nextrange('KEYWORD', '1.0'), ()) |
| 599 |
|
| 600 |
@mock.patch.object(colorizer.ColorDelegator, 'recolorize') |
| 601 |
@mock.patch.object(colorizer.ColorDelegator, 'notify_range') |
| 602 |
def test_removecolors(self, mock_notify, mock_recolorize): |
| 603 |
text = self.text |
| 604 |
color = self.color |
| 605 |
text.insert('insert', source) |
| 606 |
|
| 607 |
color.recolorize_main() |
| 608 |
# recolorize_main doesn't add these tags. |
| 609 |
text.tag_add("ERROR", "1.0") |
| 610 |
text.tag_add("TODO", "1.0") |
| 611 |
text.tag_add("hit", "1.0") |
| 612 |
for tag in color.tagdefs: |
| 613 |
with self.subTest(tag=tag): |
| 614 |
self.assertNotEqual(text.tag_ranges(tag), ()) |
| 615 |
|
| 616 |
color.removecolors() |
| 617 |
for tag in color.tagdefs: |
| 618 |
with self.subTest(tag=tag): |
| 619 |
self.assertEqual(text.tag_ranges(tag), ()) |
| 620 |
|
| 621 |
|
| 622 |
if __name__ == '__main__': |
| 623 |
unittest.main(verbosity=2) |