1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
|
"""Test sidebar, coverage 85%"""
from textwrap import dedent
import sys
from itertools import chain
import unittest
import unittest.mock
from test.support import requires, swap_attr
import tkinter as tk
from idlelib.idle_test.tkinter_testing_utils import run_in_tk_mainloop
from idlelib.delegator import Delegator
from idlelib.editor import fixwordbreaks
from idlelib.percolator import Percolator
import idlelib.pyshell
from idlelib.pyshell import fix_x11_paste, PyShell, PyShellFileList
from idlelib.run import fix_scaling
import idlelib.sidebar
from idlelib.sidebar import get_end_linenumber, get_lineno
class Dummy_editwin:
def __init__(self, text):
self.text = text
self.text_frame = self.text.master
self.per = Percolator(text)
self.undo = Delegator()
self.per.insertfilter(self.undo)
def setvar(self, name, value):
pass
def getlineno(self, index):
return int(float(self.text.index(index)))
class LineNumbersTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
requires('gui')
cls.root = tk.Tk()
cls.root.withdraw()
cls.text_frame = tk.Frame(cls.root)
cls.text_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
cls.text_frame.rowconfigure(1, weight=1)
cls.text_frame.columnconfigure(1, weight=1)
cls.text = tk.Text(cls.text_frame, width=80, height=24, wrap=tk.NONE)
cls.text.grid(row=1, column=1, sticky=tk.NSEW)
cls.editwin = Dummy_editwin(cls.text)
cls.editwin.vbar = tk.Scrollbar(cls.text_frame)
@classmethod
def tearDownClass(cls):
cls.editwin.per.close()
cls.root.update()
cls.root.destroy()
del cls.text, cls.text_frame, cls.editwin, cls.root
def setUp(self):
self.linenumber = idlelib.sidebar.LineNumbers(self.editwin)
self.highlight_cfg = {"background": '#abcdef',
"foreground": '#123456'}
orig_idleConf_GetHighlight = idlelib.sidebar.idleConf.GetHighlight
def mock_idleconf_GetHighlight(theme, element):
if element == 'linenumber':
return self.highlight_cfg
return orig_idleConf_GetHighlight(theme, element)
GetHighlight_patcher = unittest.mock.patch.object(
idlelib.sidebar.idleConf, 'GetHighlight', mock_idleconf_GetHighlight)
GetHighlight_patcher.start()
self.addCleanup(GetHighlight_patcher.stop)
self.font_override = 'TkFixedFont'
def mock_idleconf_GetFont(root, configType, section):
return self.font_override
GetFont_patcher = unittest.mock.patch.object(
idlelib.sidebar.idleConf, 'GetFont', mock_idleconf_GetFont)
GetFont_patcher.start()
self.addCleanup(GetFont_patcher.stop)
def tearDown(self):
self.text.delete('1.0', 'end')
def get_selection(self):
return tuple(map(str, self.text.tag_ranges('sel')))
def get_line_screen_position(self, line):
bbox = self.linenumber.sidebar_text.bbox(f'{line}.end -1c')
x = bbox[0] + 2
y = bbox[1] + 2
return x, y
def assert_state_disabled(self):
state = self.linenumber.sidebar_text.config()['state']
self.assertEqual(state[-1], tk.DISABLED)
def get_sidebar_text_contents(self):
return self.linenumber.sidebar_text.get('1.0', tk.END)
def assert_sidebar_n_lines(self, n_lines):
expected = '\n'.join(chain(map(str, range(1, n_lines + 1)), ['']))
self.assertEqual(self.get_sidebar_text_contents(), expected)
def assert_text_equals(self, expected):
return self.assertEqual(self.text.get('1.0', 'end'), expected)
def test_init_empty(self):
self.assert_sidebar_n_lines(1)
def test_init_not_empty(self):
self.text.insert('insert', 'foo bar\n'*3)
self.assert_text_equals('foo bar\n'*3 + '\n')
self.assert_sidebar_n_lines(4)
def test_toggle_linenumbering(self):
self.assertEqual(self.linenumber.is_shown, False)
self.linenumber.show_sidebar()
self.assertEqual(self.linenumber.is_shown, True)
self.linenumber.hide_sidebar()
self.assertEqual(self.linenumber.is_shown, False)
self.linenumber.hide_sidebar()
self.assertEqual(self.linenumber.is_shown, False)
self.linenumber.show_sidebar()
self.assertEqual(self.linenumber.is_shown, True)
self.linenumber.show_sidebar()
self.assertEqual(self.linenumber.is_shown, True)
def test_insert(self):
self.text.insert('insert', 'foobar')
self.assert_text_equals('foobar\n')
self.assert_sidebar_n_lines(1)
self.assert_state_disabled()
self.text.insert('insert', '\nfoo')
self.assert_text_equals('foobar\nfoo\n')
self.assert_sidebar_n_lines(2)
self.assert_state_disabled()
self.text.insert('insert', 'hello\n'*2)
self.assert_text_equals('foobar\nfoohello\nhello\n\n')
self.assert_sidebar_n_lines(4)
self.assert_state_disabled()
self.text.insert('insert', '\nworld')
self.assert_text_equals('foobar\nfoohello\nhello\n\nworld\n')
self.assert_sidebar_n_lines(5)
self.assert_state_disabled()
def test_delete(self):
self.text.insert('insert', 'foobar')
self.assert_text_equals('foobar\n')
self.text.delete('1.1', '1.3')
self.assert_text_equals('fbar\n')
self.assert_sidebar_n_lines(1)
self.assert_state_disabled()
self.text.insert('insert', 'foo\n'*2)
self.assert_text_equals('fbarfoo\nfoo\n\n')
self.assert_sidebar_n_lines(3)
self.assert_state_disabled()
# Deleting up to "2.end" doesn't delete the final newline.
self.text.delete('2.0', '2.end')
self.assert_text_equals('fbarfoo\n\n\n')
self.assert_sidebar_n_lines(3)
self.assert_state_disabled()
self.text.delete('1.3', 'end')
self.assert_text_equals('fba\n')
self.assert_sidebar_n_lines(1)
self.assert_state_disabled()
# Text widgets always keep a single '\n' character at the end.
self.text.delete('1.0', 'end')
self.assert_text_equals('\n')
self.assert_sidebar_n_lines(1)
self.assert_state_disabled()
def test_sidebar_text_width(self):
"""
Test that linenumber text widget is always at the minimum
width
"""
def get_width():
return self.linenumber.sidebar_text.config()['width'][-1]
self.assert_sidebar_n_lines(1)
self.assertEqual(get_width(), 1)
self.text.insert('insert', 'foo')
self.assert_sidebar_n_lines(1)
self.assertEqual(get_width(), 1)
self.text.insert('insert', 'foo\n'*8)
self.assert_sidebar_n_lines(9)
self.assertEqual(get_width(), 1)
self.text.insert('insert', 'foo\n')
self.assert_sidebar_n_lines(10)
self.assertEqual(get_width(), 2)
self.text.insert('insert', 'foo\n')
self.assert_sidebar_n_lines(11)
self.assertEqual(get_width(), 2)
self.text.delete('insert -1l linestart', 'insert linestart')
self.assert_sidebar_n_lines(10)
self.assertEqual(get_width(), 2)
self.text.delete('insert -1l linestart', 'insert linestart')
self.assert_sidebar_n_lines(9)
self.assertEqual(get_width(), 1)
self.text.insert('insert', 'foo\n'*90)
self.assert_sidebar_n_lines(99)
self.assertEqual(get_width(), 2)
self.text.insert('insert', 'foo\n')
self.assert_sidebar_n_lines(100)
self.assertEqual(get_width(), 3)
self.text.insert('insert', 'foo\n')
self.assert_sidebar_n_lines(101)
self.assertEqual(get_width(), 3)
self.text.delete('insert -1l linestart', 'insert linestart')
self.assert_sidebar_n_lines(100)
self.assertEqual(get_width(), 3)
self.text.delete('insert -1l linestart', 'insert linestart')
self.assert_sidebar_n_lines(99)
self.assertEqual(get_width(), 2)
self.text.delete('50.0 -1c', 'end -1c')
self.assert_sidebar_n_lines(49)
self.assertEqual(get_width(), 2)
self.text.delete('5.0 -1c', 'end -1c')
self.assert_sidebar_n_lines(4)
self.assertEqual(get_width(), 1)
# Text widgets always keep a single '\n' character at the end.
self.text.delete('1.0', 'end -1c')
self.assert_sidebar_n_lines(1)
self.assertEqual(get_width(), 1)
# The following tests are temporarily disabled due to relying on
# simulated user input and inspecting which text is selected, which
# are fragile and can fail when several GUI tests are run in parallel
# or when the windows created by the test lose focus.
#
# TODO: Re-work these tests or remove them from the test suite.
@unittest.skip('test disabled')
def test_click_selection(self):
self.linenumber.show_sidebar()
self.text.insert('1.0', 'one\ntwo\nthree\nfour\n')
self.root.update()
# Click on the second line.
x, y = self.get_line_screen_position(2)
self.linenumber.sidebar_text.event_generate('<Button-1>', x=x, y=y)
self.linenumber.sidebar_text.update()
self.root.update()
self.assertEqual(self.get_selection(), ('2.0', '3.0'))
def simulate_drag(self, start_line, end_line):
start_x, start_y = self.get_line_screen_position(start_line)
end_x, end_y = self.get_line_screen_position(end_line)
self.linenumber.sidebar_text.event_generate('<Button-1>',
x=start_x, y=start_y)
self.root.update()
def lerp(a, b, steps):
"""linearly interpolate from a to b (inclusive) in equal steps"""
last_step = steps - 1
for i in range(steps):
yield ((last_step - i) / last_step) * a + (i / last_step) * b
for x, y in zip(
map(int, lerp(start_x, end_x, steps=11)),
map(int, lerp(start_y, end_y, steps=11)),
):
self.linenumber.sidebar_text.event_generate('<B1-Motion>', x=x, y=y)
self.root.update()
self.linenumber.sidebar_text.event_generate('<ButtonRelease-1>',
x=end_x, y=end_y)
self.root.update()
@unittest.skip('test disabled')
def test_drag_selection_down(self):
self.linenumber.show_sidebar()
self.text.insert('1.0', 'one\ntwo\nthree\nfour\nfive\n')
self.root.update()
# Drag from the second line to the fourth line.
self.simulate_drag(2, 4)
self.assertEqual(self.get_selection(), ('2.0', '5.0'))
@unittest.skip('test disabled')
def test_drag_selection_up(self):
self.linenumber.show_sidebar()
self.text.insert('1.0', 'one\ntwo\nthree\nfour\nfive\n')
self.root.update()
# Drag from the fourth line to the second line.
self.simulate_drag(4, 2)
self.assertEqual(self.get_selection(), ('2.0', '5.0'))
def test_scroll(self):
self.linenumber.show_sidebar()
self.text.insert('1.0', 'line\n' * 100)
self.root.update()
# Scroll down 10 lines.
self.text.yview_scroll(10, 'unit')
self.root.update()
self.assertEqual(self.text.index('@0,0'), '11.0')
self.assertEqual(self.linenumber.sidebar_text.index('@0,0'), '11.0')
# Generate a mouse-wheel event and make sure it scrolled up or down.
# The meaning of the "delta" is OS-dependant, so this just checks for
# any change.
self.linenumber.sidebar_text.event_generate('<MouseWheel>',
x=0, y=0,
delta=10)
self.root.update()
self.assertNotEqual(self.text.index('@0,0'), '11.0')
self.assertNotEqual(self.linenumber.sidebar_text.index('@0,0'), '11.0')
def test_font(self):
ln = self.linenumber
orig_font = ln.sidebar_text['font']
test_font = 'TkTextFont'
self.assertNotEqual(orig_font, test_font)
# Ensure line numbers aren't shown.
ln.hide_sidebar()
self.font_override = test_font
# Nothing breaks when line numbers aren't shown.
ln.update_font()
# Activate line numbers, previous font change is immediately effective.
ln.show_sidebar()
self.assertEqual(ln.sidebar_text['font'], test_font)
# Call the font update with line numbers shown, change is picked up.
self.font_override = orig_font
ln.update_font()
self.assertEqual(ln.sidebar_text['font'], orig_font)
def test_highlight_colors(self):
ln = self.linenumber
orig_colors = dict(self.highlight_cfg)
test_colors = {'background': '#222222', 'foreground': '#ffff00'}
def assert_colors_are_equal(colors):
self.assertEqual(ln.sidebar_text['background'], colors['background'])
self.assertEqual(ln.sidebar_text['foreground'], colors['foreground'])
# Ensure line numbers aren't shown.
ln.hide_sidebar()
self.highlight_cfg = test_colors
# Nothing breaks with inactive line numbers.
ln.update_colors()
# Show line numbers, previous colors change is immediately effective.
ln.show_sidebar()
assert_colors_are_equal(test_colors)
# Call colors update with no change to the configured colors.
ln.update_colors()
assert_colors_are_equal(test_colors)
# Call the colors update with line numbers shown, change is picked up.
self.highlight_cfg = orig_colors
ln.update_colors()
assert_colors_are_equal(orig_colors)
class ShellSidebarTest(unittest.TestCase):
root: tk.Tk = None
shell: PyShell = None
@classmethod
def setUpClass(cls):
requires('gui')
cls.root = root = tk.Tk()
root.withdraw()
fix_scaling(root)
fixwordbreaks(root)
fix_x11_paste(root)
cls.flist = flist = PyShellFileList(root)
# See #43981 about macosx.setupApp(root, flist) causing failure.
root.update_idletasks()
cls.init_shell()
@classmethod
def tearDownClass(cls):
if cls.shell is not None:
cls.shell.executing = False
cls.shell.close()
cls.shell = None
cls.flist = None
cls.root.update_idletasks()
cls.root.destroy()
cls.root = None
@classmethod
def init_shell(cls):
cls.shell = cls.flist.open_shell()
cls.shell.pollinterval = 10
cls.root.update()
cls.n_preface_lines = get_lineno(cls.shell.text, 'end-1c') - 1
@classmethod
def reset_shell(cls):
cls.shell.per.bottom.delete(f'{cls.n_preface_lines+1}.0', 'end-1c')
cls.shell.shell_sidebar.update_sidebar()
cls.root.update()
def setUp(self):
# In some test environments, e.g. Azure Pipelines (as of
# Apr. 2021), sys.stdout is changed between tests. However,
# PyShell relies on overriding sys.stdout when run without a
# sub-process (as done here; see setUpClass).
self._saved_stdout = None
if sys.stdout != self.shell.stdout:
self._saved_stdout = sys.stdout
sys.stdout = self.shell.stdout
self.reset_shell()
def tearDown(self):
if self._saved_stdout is not None:
sys.stdout = self._saved_stdout
def get_sidebar_lines(self):
canvas = self.shell.shell_sidebar.canvas
texts = list(canvas.find(tk.ALL))
texts_by_y_coords = {
canvas.bbox(text)[1]: canvas.itemcget(text, 'text')
for text in texts
}
line_y_coords = self.get_shell_line_y_coords()
return [texts_by_y_coords.get(y, None) for y in line_y_coords]
def assert_sidebar_lines_end_with(self, expected_lines):
self.shell.shell_sidebar.update_sidebar()
self.assertEqual(
self.get_sidebar_lines()[-len(expected_lines):],
expected_lines,
)
def get_shell_line_y_coords(self):
text = self.shell.text
y_coords = []
index = text.index("@0,0")
if index.split('.', 1)[1] != '0':
index = text.index(f"{index} +1line linestart")
while (lineinfo := text.dlineinfo(index)) is not None:
y_coords.append(lineinfo[1])
index = text.index(f"{index} +1line")
return y_coords
def get_sidebar_line_y_coords(self):
canvas = self.shell.shell_sidebar.canvas
texts = list(canvas.find(tk.ALL))
texts.sort(key=lambda text: canvas.bbox(text)[1])
return [canvas.bbox(text)[1] for text in texts]
def assert_sidebar_lines_synced(self):
self.assertLessEqual(
set(self.get_sidebar_line_y_coords()),
set(self.get_shell_line_y_coords()),
)
def do_input(self, input):
shell = self.shell
text = shell.text
for line_index, line in enumerate(input.split('\n')):
if line_index > 0:
text.event_generate('<<newline-and-indent>>')
text.insert('insert', line, 'stdin')
def test_initial_state(self):
sidebar_lines = self.get_sidebar_lines()
self.assertEqual(
sidebar_lines,
[None] * (len(sidebar_lines) - 1) + ['>>>'],
)
self.assert_sidebar_lines_synced()
@run_in_tk_mainloop()
def test_single_empty_input(self):
self.do_input('\n')
yield
self.assert_sidebar_lines_end_with(['>>>', '>>>'])
@run_in_tk_mainloop()
def test_single_line_statement(self):
self.do_input('1\n')
yield
self.assert_sidebar_lines_end_with(['>>>', None, '>>>'])
@run_in_tk_mainloop()
def test_multi_line_statement(self):
# Block statements are not indented because IDLE auto-indents.
self.do_input(dedent('''\
if True:
print(1)
'''))
yield
self.assert_sidebar_lines_end_with([
'>>>',
'...',
'...',
'...',
None,
'>>>',
])
@run_in_tk_mainloop()
def test_single_long_line_wraps(self):
self.do_input('1' * 200 + '\n')
yield
self.assert_sidebar_lines_end_with(['>>>', None, '>>>'])
self.assert_sidebar_lines_synced()
@run_in_tk_mainloop()
def test_squeeze_multi_line_output(self):
shell = self.shell
text = shell.text
self.do_input('print("a\\nb\\nc")\n')
yield
self.assert_sidebar_lines_end_with(['>>>', None, None, None, '>>>'])
text.mark_set('insert', f'insert -1line linestart')
text.event_generate('<<squeeze-current-text>>')
yield
self.assert_sidebar_lines_end_with(['>>>', None, '>>>'])
self.assert_sidebar_lines_synced()
shell.squeezer.expandingbuttons[0].expand()
yield
self.assert_sidebar_lines_end_with(['>>>', None, None, None, '>>>'])
self.assert_sidebar_lines_synced()
@run_in_tk_mainloop()
def test_interrupt_recall_undo_redo(self):
text = self.shell.text
# Block statements are not indented because IDLE auto-indents.
initial_sidebar_lines = self.get_sidebar_lines()
self.do_input(dedent('''\
if True:
print(1)
'''))
yield
self.assert_sidebar_lines_end_with(['>>>', '...', '...'])
with_block_sidebar_lines = self.get_sidebar_lines()
self.assertNotEqual(with_block_sidebar_lines, initial_sidebar_lines)
# Control-C
text.event_generate('<<interrupt-execution>>')
yield
self.assert_sidebar_lines_end_with(['>>>', '...', '...', None, '>>>'])
# Recall previous via history
text.event_generate('<<history-previous>>')
text.event_generate('<<interrupt-execution>>')
yield
self.assert_sidebar_lines_end_with(['>>>', '...', None, '>>>'])
# Recall previous via recall
text.mark_set('insert', text.index('insert -2l'))
text.event_generate('<<newline-and-indent>>')
yield
text.event_generate('<<undo>>')
yield
self.assert_sidebar_lines_end_with(['>>>'])
text.event_generate('<<redo>>')
yield
self.assert_sidebar_lines_end_with(['>>>', '...'])
text.event_generate('<<newline-and-indent>>')
text.event_generate('<<newline-and-indent>>')
yield
self.assert_sidebar_lines_end_with(
['>>>', '...', '...', '...', None, '>>>']
)
@run_in_tk_mainloop()
def test_very_long_wrapped_line(self):
with swap_attr(self.shell, 'squeezer', None):
self.do_input('x = ' + '1'*10_000 + '\n')
yield
self.assertEqual(self.get_sidebar_lines(), ['>>>'])
def test_font(self):
sidebar = self.shell.shell_sidebar
test_font = 'TkTextFont'
def mock_idleconf_GetFont(root, configType, section):
return test_font
GetFont_patcher = unittest.mock.patch.object(
idlelib.sidebar.idleConf, 'GetFont', mock_idleconf_GetFont)
GetFont_patcher.start()
def cleanup():
GetFont_patcher.stop()
sidebar.update_font()
self.addCleanup(cleanup)
def get_sidebar_font():
canvas = sidebar.canvas
texts = list(canvas.find(tk.ALL))
fonts = {canvas.itemcget(text, 'font') for text in texts}
self.assertEqual(len(fonts), 1)
return next(iter(fonts))
self.assertNotEqual(get_sidebar_font(), test_font)
sidebar.update_font()
self.assertEqual(get_sidebar_font(), test_font)
def test_highlight_colors(self):
sidebar = self.shell.shell_sidebar
test_colors = {"background": '#abcdef', "foreground": '#123456'}
orig_idleConf_GetHighlight = idlelib.sidebar.idleConf.GetHighlight
def mock_idleconf_GetHighlight(theme, element):
if element in ['linenumber', 'console']:
return test_colors
return orig_idleConf_GetHighlight(theme, element)
GetHighlight_patcher = unittest.mock.patch.object(
idlelib.sidebar.idleConf, 'GetHighlight',
mock_idleconf_GetHighlight)
GetHighlight_patcher.start()
def cleanup():
GetHighlight_patcher.stop()
sidebar.update_colors()
self.addCleanup(cleanup)
def get_sidebar_colors():
canvas = sidebar.canvas
texts = list(canvas.find(tk.ALL))
fgs = {canvas.itemcget(text, 'fill') for text in texts}
self.assertEqual(len(fgs), 1)
fg = next(iter(fgs))
bg = canvas.cget('background')
return {"background": bg, "foreground": fg}
self.assertNotEqual(get_sidebar_colors(), test_colors)
sidebar.update_colors()
self.assertEqual(get_sidebar_colors(), test_colors)
@run_in_tk_mainloop()
def test_mousewheel(self):
sidebar = self.shell.shell_sidebar
text = self.shell.text
# Enter a 100-line string to scroll the shell screen down.
self.do_input('x = """' + '\n'*100 + '"""\n')
yield
self.assertGreater(get_lineno(text, '@0,0'), 1)
last_lineno = get_end_linenumber(text)
self.assertIsNotNone(text.dlineinfo(text.index(f'{last_lineno}.0')))
# Scroll up using the <MouseWheel> event.
# The meaning delta is platform-dependant.
delta = -1 if sys.platform == 'darwin' else 120
sidebar.canvas.event_generate('<MouseWheel>', x=0, y=0, delta=delta)
yield
self.assertIsNone(text.dlineinfo(text.index(f'{last_lineno}.0')))
# Scroll back down using the <Button-5> event.
sidebar.canvas.event_generate('<Button-5>', x=0, y=0)
yield
self.assertIsNotNone(text.dlineinfo(text.index(f'{last_lineno}.0')))
@run_in_tk_mainloop()
def test_copy(self):
sidebar = self.shell.shell_sidebar
text = self.shell.text
first_line = get_end_linenumber(text)
self.do_input(dedent('''\
if True:
print(1)
'''))
yield
text.tag_add('sel', f'{first_line}.0', 'end-1c')
selected_text = text.get('sel.first', 'sel.last')
self.assertTrue(selected_text.startswith('if True:\n'))
self.assertIn('\n1\n', selected_text)
text.event_generate('<<copy>>')
self.addCleanup(text.clipboard_clear)
copied_text = text.clipboard_get()
self.assertEqual(copied_text, selected_text)
@run_in_tk_mainloop()
def test_copy_with_prompts(self):
sidebar = self.shell.shell_sidebar
text = self.shell.text
first_line = get_end_linenumber(text)
self.do_input(dedent('''\
if True:
print(1)
'''))
yield
text.tag_add('sel', f'{first_line}.3', 'end-1c')
selected_text = text.get('sel.first', 'sel.last')
self.assertTrue(selected_text.startswith('True:\n'))
selected_lines_text = text.get('sel.first linestart', 'sel.last')
selected_lines = selected_lines_text.split('\n')
selected_lines.pop() # Final '' is a split artifact, not a line.
# Expect a block of input and a single output line.
expected_prompts = \
['>>>'] + ['...'] * (len(selected_lines) - 2) + [None]
selected_text_with_prompts = '\n'.join(
line if prompt is None else prompt + ' ' + line
for prompt, line in zip(expected_prompts,
selected_lines,
strict=True)
) + '\n'
text.event_generate('<<copy-with-prompts>>')
self.addCleanup(text.clipboard_clear)
copied_text = text.clipboard_get()
self.assertEqual(copied_text, selected_text_with_prompts)
if __name__ == '__main__':
unittest.main(verbosity=2)
|