-
-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathtest_curtsies_painting.py
More file actions
883 lines (795 loc) · 29.6 KB
/
test_curtsies_painting.py
File metadata and controls
883 lines (795 loc) · 29.6 KB
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
import itertools
import os
import pydoc
import string
import sys
from contextlib import contextmanager
from typing import cast
from curtsies.formatstringarray import (
fsarray,
assertFSArraysEqual,
assertFSArraysEqualIgnoringFormatting,
)
from curtsies.fmtfuncs import cyan, bold, green, yellow, on_magenta, red
from curtsies.window import CursorAwareWindow
from unittest import mock, skipIf
from bpython.curtsiesfrontend.events import RefreshRequestEvent
from bpython import config, inspection
from bpython.curtsiesfrontend.repl import BaseRepl
from bpython.curtsiesfrontend import replpainter
from bpython.curtsiesfrontend.repl import (
INCONSISTENT_HISTORY_MSG,
CONTIGUITY_BROKEN_MSG,
)
from bpython.test import FixLanguageTestCase as TestCase, TEST_CONFIG
def setup_config():
config_struct = config.Config(TEST_CONFIG)
config_struct.cli_suggestion_width = 1
return config_struct
class ClearEnviron(TestCase):
@classmethod
def setUpClass(cls):
cls.mock_environ = mock.patch.dict(
"os.environ",
{
"LC_ALL": os.environ.get("LC_ALL", "C.UTF-8"),
"LANG": os.environ.get("LANG", "C.UTF-8"),
},
clear=True,
)
cls.mock_environ.start()
TestCase.setUpClass()
@classmethod
def tearDownClass(cls):
cls.mock_environ.stop()
TestCase.tearDownClass()
class CurtsiesPaintingTest(ClearEnviron):
def setUp(self):
class TestRepl(BaseRepl):
def _request_refresh(inner_self):
pass
self.repl = TestRepl(setup_config(), cast(CursorAwareWindow, None))
self.repl.height, self.repl.width = (5, 10)
@property
def locals(self):
return self.repl.coderunner.interp.locals
def assert_paint(self, screen, cursor_row_col):
array, cursor_pos = self.repl.paint()
assertFSArraysEqual(array, screen)
self.assertEqual(cursor_pos, cursor_row_col)
def assert_paint_ignoring_formatting(
self, screen, cursor_row_col=None, **paint_kwargs
):
array, cursor_pos = self.repl.paint(**paint_kwargs)
assertFSArraysEqualIgnoringFormatting(array, screen)
if cursor_row_col is not None:
self.assertEqual(cursor_pos, cursor_row_col)
def process_box_characters(self, screen):
if not self.repl.config.unicode_box or not config.supports_box_chars():
return [
line.replace("┌", "+")
.replace("└", "+")
.replace("┘", "+")
.replace("┐", "+")
.replace("─", "-")
for line in screen
]
return screen
class TestCurtsiesPaintingTest(CurtsiesPaintingTest):
def test_history_is_cleared(self):
self.assertEqual(self.repl.rl_history.entries, [""])
class TestCurtsiesPaintingSimple(CurtsiesPaintingTest):
def test_startup(self):
screen = fsarray([cyan(">>> ")], width=10)
self.assert_paint(screen, (0, 4))
def test_enter_text(self):
[self.repl.add_normal_character(c) for c in "1 + 1"]
screen = fsarray(
[
cyan(">>> ")
+ bold(
green("1")
+ cyan(" ")
+ yellow("+")
+ cyan(" ")
+ green("1")
),
],
width=10,
)
self.assert_paint(screen, (0, 9))
def test_run_line(self):
orig_stdout = sys.stdout
try:
sys.stdout = self.repl.stdout
[self.repl.add_normal_character(c) for c in "1 + 1"]
self.repl.on_enter(new_code=False)
screen = fsarray([">>> 1 + 1", "2"])
self.assert_paint_ignoring_formatting(screen, (1, 1))
finally:
sys.stdout = orig_stdout
def test_completion(self):
self.repl.height, self.repl.width = (5, 32)
self.repl.current_line = "an"
self.cursor_offset = 2
screen = self.process_box_characters(
[
">>> an",
"┌──────────────────────────────┐",
"│ and anext( any( │",
"└──────────────────────────────┘",
]
)
self.assert_paint_ignoring_formatting(screen, (0, 4))
def test_argspec(self):
def foo(x, y, z=10):
"docstring!"
pass
argspec = inspection.getfuncprops("foo", foo)
array = replpainter.formatted_argspec(argspec, 1, 30, setup_config())
screen = [
bold(cyan("foo"))
+ cyan(":")
+ cyan(" ")
+ cyan("(")
+ cyan("x")
+ yellow(",")
+ yellow(" ")
+ bold(cyan("y"))
+ yellow(",")
+ yellow(" ")
+ cyan("z")
+ yellow("=")
+ bold(cyan("10"))
+ yellow(")")
]
assertFSArraysEqual(fsarray(array), fsarray(screen))
def test_formatted_docstring(self):
actual = replpainter.formatted_docstring(
"Returns the results\n\n" "Also has side effects",
40,
config=setup_config(),
)
expected = fsarray(["Returns the results", "", "Also has side effects"])
assertFSArraysEqualIgnoringFormatting(actual, expected)
def test_unicode_docstrings(self):
"A bit of a special case in Python 2"
# issue 653
def foo():
"åß∂ƒ"
actual = replpainter.formatted_docstring(
foo.__doc__, 40, config=setup_config()
)
expected = fsarray(["åß∂ƒ"])
assertFSArraysEqualIgnoringFormatting(actual, expected)
def test_nonsense_docstrings(self):
for docstring in [
123,
{},
[],
]:
try:
replpainter.formatted_docstring(
docstring, 40, config=setup_config()
)
except Exception:
self.fail(f"bad docstring caused crash: {docstring!r}")
def test_weird_boto_docstrings(self):
# Boto does something like this.
# botocore: botocore/docs/docstring.py
class WeirdDocstring(str):
# a mighty hack. See botocore/docs/docstring.py
def expandtabs(self, tabsize=8):
return "asdfåß∂ƒ".expandtabs(tabsize)
def foo():
pass
foo.__doc__ = WeirdDocstring()
wd = pydoc.getdoc(foo)
actual = replpainter.formatted_docstring(wd, 40, config=setup_config())
expected = fsarray(["asdfåß∂ƒ"])
assertFSArraysEqualIgnoringFormatting(actual, expected)
def test_paint_lasts_events(self):
actual = replpainter.paint_last_events(
4, 100, ["a", "b", "c"], config=setup_config()
)
if config.supports_box_chars():
expected = fsarray(["┌─┐", "│c│", "│b│", "└─┘"])
else:
expected = fsarray(["+-+", "|c|", "|b|", "+-+"])
assertFSArraysEqualIgnoringFormatting(actual, expected)
@contextmanager
def output_to_repl(repl):
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = repl.stdout, repl.stderr
yield
finally:
sys.stdout, sys.stderr = old_out, old_err
class HigherLevelCurtsiesPaintingTest(CurtsiesPaintingTest):
def refresh(self):
self.refresh_requests.append(RefreshRequestEvent())
def send_refreshes(self):
while self.refresh_requests:
self.repl.process_event(self.refresh_requests.pop())
_, _ = self.repl.paint()
def enter(self, line=None):
"""Enter a line of text, avoiding autocompletion windows
autocomplete could still happen if the entered line has
autocompletion that would happen then, but intermediate
stages won't happen"""
if line is not None:
self.repl._set_cursor_offset(len(line), update_completion=False)
self.repl.current_line = line
with output_to_repl(self.repl):
self.repl.on_enter(new_code=False)
self.assertEqual(self.repl.rl_history.entries, [""])
self.send_refreshes()
def undo(self):
with output_to_repl(self.repl):
self.repl.undo()
self.send_refreshes()
def setUp(self):
self.refresh_requests = []
class TestRepl(BaseRepl):
def _request_refresh(inner_self):
self.refresh()
self.repl = TestRepl(
setup_config(), cast(CursorAwareWindow, None), banner=""
)
self.repl.height, self.repl.width = (5, 32)
def send_key(self, key):
self.repl.process_event("<SPACE>" if key == " " else key)
self.repl.paint() # has some side effects we need to be wary of
class TestWidthAwareness(HigherLevelCurtsiesPaintingTest):
def test_cursor_position_with_fullwidth_char(self):
self.repl.add_normal_character("間")
cursor_pos = self.repl.paint()[1]
self.assertEqual(cursor_pos, (0, 6))
def test_cursor_position_with_padding_char(self):
# odd numbered so fullwidth chars don't wrap evenly
self.repl.width = 11
[self.repl.add_normal_character(c) for c in "width"]
cursor_pos = self.repl.paint()[1]
self.assertEqual(cursor_pos, (1, 4))
@skipIf(
sys.version_info[:2] >= (3, 11) and sys.version_info[:3] < (3, 11, 1),
"https://github.com/python/cpython/issues/98744",
)
def test_display_of_padding_chars(self):
self.repl.width = 11
[self.repl.add_normal_character(c) for c in "width"]
self.enter()
expected = [">>> wid ", "th"] # <--- note the added trailing space
result = [d.s for d in self.repl.display_lines[0:2]]
self.assertEqual(result, expected)
class TestCurtsiesRewindRedraw(HigherLevelCurtsiesPaintingTest):
def test_rewind(self):
self.repl.current_line = "1 + 1"
self.enter()
screen = [">>> 1 + 1", "2", ">>> "]
self.assert_paint_ignoring_formatting(screen, (2, 4))
self.repl.undo()
screen = [">>> "]
self.assert_paint_ignoring_formatting(screen, (0, 4))
def test_rewind_contiguity_loss(self):
self.enter("1 + 1")
self.enter("2 + 2")
self.enter("def foo(x):")
self.repl.current_line = " return x + 1"
screen = [
">>> 1 + 1",
"2",
">>> 2 + 2",
"4",
">>> def foo(x):",
"... return x + 1",
]
self.assert_paint_ignoring_formatting(screen, (5, 8))
self.repl.scroll_offset = 1
self.assert_paint_ignoring_formatting(screen[1:], (4, 8))
self.undo()
screen = ["2", ">>> 2 + 2", "4", ">>> "]
self.assert_paint_ignoring_formatting(screen, (3, 4))
self.undo()
screen = ["2", ">>> "]
self.assert_paint_ignoring_formatting(screen, (1, 4))
self.undo()
screen = [
CONTIGUITY_BROKEN_MSG[: self.repl.width],
">>> ",
"",
"",
"",
" ",
] # TODO why is that there? Necessary?
self.assert_paint_ignoring_formatting(screen, (1, 4))
screen = [">>> "]
self.assert_paint_ignoring_formatting(screen, (0, 4))
def test_inconsistent_history_doesnt_happen_if_onscreen(self):
self.enter("1 + 1")
screen = [">>> 1 + 1", "2", ">>> "]
self.assert_paint_ignoring_formatting(screen, (2, 4))
self.enter("2 + 2")
screen = [">>> 1 + 1", "2", ">>> 2 + 2", "4", ">>> "]
self.assert_paint_ignoring_formatting(screen, (4, 4))
self.repl.display_lines[0] = self.repl.display_lines[0] * 2
self.undo()
screen = [">>> 1 + 1", "2", ">>> "]
self.assert_paint_ignoring_formatting(screen, (2, 4))
def test_rewind_inconsistent_history(self):
self.enter("1 + 1")
self.enter("2 + 2")
self.enter("3 + 3")
screen = [">>> 1 + 1", "2", ">>> 2 + 2", "4", ">>> 3 + 3", "6", ">>> "]
self.assert_paint_ignoring_formatting(screen, (6, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[2:], (4, 4))
self.repl.display_lines[0] = self.repl.display_lines[0] * 2
self.undo()
screen = [
INCONSISTENT_HISTORY_MSG[: self.repl.width],
">>> 2 + 2",
"4",
">>> ",
"",
" ",
]
self.assert_paint_ignoring_formatting(screen, (3, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[1:-2], (2, 4))
self.assert_paint_ignoring_formatting(screen[1:-2], (2, 4))
def test_rewind_inconsistent_history_more_lines_same_screen(self):
self.repl.width = 60
sys.a = 5
self.enter("import sys")
self.enter("for i in range(sys.a):")
self.enter(" print(sys.a)")
self.enter("")
self.enter("1 + 1")
self.enter("2 + 2")
screen = [
">>> import sys",
">>> for i in range(sys.a):",
"... print(sys.a)",
"... ",
"5",
"5",
"5",
"5",
"5",
">>> 1 + 1",
"2",
">>> 2 + 2",
"4",
">>> ",
]
self.assert_paint_ignoring_formatting(screen, (13, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[9:], (4, 4))
sys.a = 6
self.undo()
screen = [
INCONSISTENT_HISTORY_MSG[: self.repl.width],
"6",
# everything will jump down a line - that's perfectly
# reasonable
">>> 1 + 1",
"2",
">>> ",
" ",
]
self.assert_paint_ignoring_formatting(screen, (4, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[1:-1], (3, 4))
def test_rewind_inconsistent_history_more_lines_lower_screen(self):
self.repl.width = 60
sys.a = 5
self.enter("import sys")
self.enter("for i in range(sys.a):")
self.enter(" print(sys.a)")
self.enter("")
self.enter("1 + 1")
self.enter("2 + 2")
screen = [
">>> import sys",
">>> for i in range(sys.a):",
"... print(sys.a)",
"... ",
"5",
"5",
"5",
"5",
"5",
">>> 1 + 1",
"2",
">>> 2 + 2",
"4",
">>> ",
]
self.assert_paint_ignoring_formatting(screen, (13, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[9:], (4, 4))
sys.a = 8
self.undo()
screen = [
INCONSISTENT_HISTORY_MSG[: self.repl.width],
"8",
"8",
"8",
">>> 1 + 1",
"2",
">>> ",
]
self.assert_paint_ignoring_formatting(screen)
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[-5:])
def test_rewind_inconsistent_history_more_lines_raise_screen(self):
self.repl.width = 60
sys.a = 5
self.enter("import sys")
self.enter("for i in range(sys.a):")
self.enter(" print(sys.a)")
self.enter("")
self.enter("1 + 1")
self.enter("2 + 2")
screen = [
">>> import sys",
">>> for i in range(sys.a):",
"... print(sys.a)",
"... ",
"5",
"5",
"5",
"5",
"5",
">>> 1 + 1",
"2",
">>> 2 + 2",
"4",
">>> ",
]
self.assert_paint_ignoring_formatting(screen, (13, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[9:], (4, 4))
sys.a = 1
self.undo()
screen = [
INCONSISTENT_HISTORY_MSG[: self.repl.width],
"1",
">>> 1 + 1",
"2",
">>> ",
" ",
]
self.assert_paint_ignoring_formatting(screen)
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[1:-1])
def test_rewind_history_not_quite_inconsistent(self):
self.repl.width = 50
sys.a = 5
self.enter("for i in range(__import__('sys').a):")
self.enter(" print(i)")
self.enter("")
self.enter("1 + 1")
self.enter("2 + 2")
screen = [
">>> for i in range(__import__('sys').a):",
"... print(i)",
"... ",
"0",
"1",
"2",
"3",
"4",
">>> 1 + 1",
"2",
">>> 2 + 2",
"4",
">>> ",
]
self.assert_paint_ignoring_formatting(screen, (12, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[8:], (4, 4))
sys.a = 6
self.undo()
screen = [
"5",
# everything will jump down a line - that's perfectly
# reasonable
">>> 1 + 1",
"2",
">>> ",
]
self.assert_paint_ignoring_formatting(screen, (3, 4))
def test_rewind_barely_consistent(self):
self.enter("1 + 1")
self.enter("2 + 2")
self.enter("3 + 3")
screen = [">>> 1 + 1", "2", ">>> 2 + 2", "4", ">>> 3 + 3", "6", ">>> "]
self.assert_paint_ignoring_formatting(screen, (6, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[2:], (4, 4))
self.repl.display_lines[2] = self.repl.display_lines[2] * 2
self.undo()
screen = [">>> 2 + 2", "4", ">>> "]
self.assert_paint_ignoring_formatting(screen, (2, 4))
def test_clear_screen(self):
self.enter("1 + 1")
self.enter("2 + 2")
screen = [">>> 1 + 1", "2", ">>> 2 + 2", "4", ">>> "]
self.assert_paint_ignoring_formatting(screen, (4, 4))
self.repl.request_paint_to_clear_screen = True
screen = [">>> 1 + 1", "2", ">>> 2 + 2", "4", ">>> ", "", "", "", ""]
self.assert_paint_ignoring_formatting(screen, (4, 4))
def test_scroll_down_while_banner_visible(self):
self.repl.status_bar.message("STATUS_BAR")
self.enter("1 + 1")
self.enter("2 + 2")
screen = [
">>> 1 + 1",
"2",
">>> 2 + 2",
"4",
">>> ",
"STATUS_BAR ",
]
self.assert_paint_ignoring_formatting(screen, (4, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[1:], (3, 4))
def test_clear_screen_while_banner_visible(self):
self.repl.status_bar.message("STATUS_BAR")
self.enter("1 + 1")
self.enter("2 + 2")
screen = [
">>> 1 + 1",
"2",
">>> 2 + 2",
"4",
">>> ",
"STATUS_BAR ",
]
self.assert_paint_ignoring_formatting(screen, (4, 4))
self.repl.scroll_offset += len(screen) - self.repl.height
self.assert_paint_ignoring_formatting(screen[1:], (3, 4))
self.repl.request_paint_to_clear_screen = True
screen = [
"2",
">>> 2 + 2",
"4",
">>> ",
"",
"",
"",
"STATUS_BAR ",
]
self.assert_paint_ignoring_formatting(screen, (3, 4))
def test_cursor_stays_at_bottom_of_screen(self):
"""infobox showing up during intermediate render was causing this to
fail, #371"""
self.repl.width = 50
self.repl.current_line = "__import__('random').__name__"
with output_to_repl(self.repl):
self.repl.on_enter(new_code=False)
screen = [">>> __import__('random').__name__", "'random'"]
self.assert_paint_ignoring_formatting(screen)
with output_to_repl(self.repl):
self.repl.process_event(self.refresh_requests.pop())
screen = [">>> __import__('random').__name__", "'random'", ""]
self.assert_paint_ignoring_formatting(screen)
with output_to_repl(self.repl):
self.repl.process_event(self.refresh_requests.pop())
screen = [">>> __import__('random').__name__", "'random'", ">>> "]
self.assert_paint_ignoring_formatting(screen, (2, 4))
def test_unhighlight_paren_bugs(self):
"""two previous bugs, parent didn't highlight until next render
and paren didn't unhighlight until enter"""
self.repl.width = 32
self.assertEqual(self.repl.rl_history.entries, [""])
self.enter("(")
self.assertEqual(self.repl.rl_history.entries, [""])
screen = [">>> (", "... "]
self.assertEqual(self.repl.rl_history.entries, [""])
self.assert_paint_ignoring_formatting(screen)
self.assertEqual(self.repl.rl_history.entries, [""])
with output_to_repl(self.repl):
self.assertEqual(self.repl.rl_history.entries, [""])
self.repl.process_event(")")
self.assertEqual(self.repl.rl_history.entries, [""])
screen = fsarray(
[
cyan(">>> ") + on_magenta(bold(red("("))),
green("... ") + on_magenta(bold(red(")"))),
],
width=32,
)
self.assert_paint(screen, (1, 5))
with output_to_repl(self.repl):
self.repl.process_event(" ")
screen = fsarray(
[
cyan(">>> ") + yellow("("),
green("... ") + yellow(")") + bold(cyan(" ")),
],
width=32,
)
self.assert_paint(screen, (1, 6))
def test_472(self):
[self.send_key(c) for c in "(1, 2, 3)"]
with output_to_repl(self.repl):
self.send_key("\n")
self.send_refreshes()
self.send_key("<UP>")
self.repl.paint()
[self.send_key("<LEFT>") for _ in range(4)]
self.send_key("<BACKSPACE>")
self.send_key("4")
self.repl.on_enter()
self.send_refreshes()
screen = [
">>> (1, 2, 3)",
"(1, 2, 3)",
">>> (1, 4, 3)",
"(1, 4, 3)",
">>> ",
]
self.assert_paint_ignoring_formatting(screen, (4, 4))
def completion_target(num_names, chars_in_first_name=1):
class Class:
pass
if chars_in_first_name < 1:
raise ValueError("need at least one char in each name")
elif chars_in_first_name == 1 and num_names > len(string.ascii_letters):
raise ValueError("need more chars to make so many names")
names = gen_names()
if num_names > 0:
setattr(Class, "a" * chars_in_first_name, 1)
next(names) # use the above instead of first name
for _, name in zip(range(num_names - 1), names):
setattr(Class, name, 0)
return Class()
def gen_names():
for letters in itertools.chain(
itertools.combinations_with_replacement(string.ascii_letters, 1),
itertools.combinations_with_replacement(string.ascii_letters, 2),
):
yield "".join(letters)
class TestCompletionHelpers(TestCase):
def test_gen_names(self):
self.assertEqual(
list(zip([1, 2, 3], gen_names())), [(1, "a"), (2, "b"), (3, "c")]
)
def test_completion_target(self):
target = completion_target(14)
self.assertEqual(
len([x for x in dir(target) if not x.startswith("_")]), 14
)
class TestCurtsiesInfoboxPaint(HigherLevelCurtsiesPaintingTest):
def test_simple(self):
self.repl.width, self.repl.height = (20, 30)
self.locals["abc"] = completion_target(3, 50)
self.repl.current_line = "abc"
self.repl.cursor_offset = 3
self.repl.process_event(".")
screen = self.process_box_characters(
[
">>> abc.",
"┌──────────────────┐",
"│ aaaaaaaaaaaaaaaa │",
"│ b │",
"│ c │",
"└──────────────────┘",
]
)
self.assert_paint_ignoring_formatting(screen, (0, 8))
def test_fill_screen(self):
self.repl.width, self.repl.height = (20, 15)
self.locals["abc"] = completion_target(20, 100)
self.repl.current_line = "abc"
self.repl.cursor_offset = 3
self.repl.process_event(".")
screen = self.process_box_characters(
[
">>> abc.",
"┌──────────────────┐",
"│ aaaaaaaaaaaaaaaa │",
"│ b │",
"│ c │",
"│ d │",
"│ e │",
"│ f │",
"│ g │",
"│ h │",
"│ i │",
"│ j │",
"│ k │",
"│ l │",
"└──────────────────┘",
]
)
self.assert_paint_ignoring_formatting(screen, (0, 8))
def test_lower_on_screen(self):
self.repl.get_top_usable_line = lambda: 10 # halfway down terminal
self.repl.width, self.repl.height = (20, 15)
self.locals["abc"] = completion_target(20, 100)
self.repl.current_line = "abc"
self.repl.cursor_offset = 3
self.repl.process_event(".")
screen = self.process_box_characters(
[
">>> abc.",
"┌──────────────────┐",
"│ aaaaaaaaaaaaaaaa │",
"│ b │",
"│ c │",
"│ d │",
"│ e │",
"│ f │",
"│ g │",
"│ h │",
"│ i │",
"│ j │",
"│ k │",
"│ l │",
"└──────────────────┘",
]
)
# behavior before issue #466
self.assert_paint_ignoring_formatting(
screen, try_preserve_history_height=0
)
self.assert_paint_ignoring_formatting(screen, min_infobox_height=100)
# behavior after issue #466
screen = self.process_box_characters(
[
">>> abc.",
"┌──────────────────┐",
"│ aaaaaaaaaaaaaaaa │",
"│ b │",
"│ c │",
"└──────────────────┘",
]
)
self.assert_paint_ignoring_formatting(screen)
def test_at_bottom_of_screen(self):
self.repl.get_top_usable_line = lambda: 17 # two lines from bottom
self.repl.width, self.repl.height = (20, 15)
self.locals["abc"] = completion_target(20, 100)
self.repl.current_line = "abc"
self.repl.cursor_offset = 3
self.repl.process_event(".")
screen = self.process_box_characters(
[
">>> abc.",
"┌──────────────────┐",
"│ aaaaaaaaaaaaaaaa │",
"│ b │",
"│ c │",
"│ d │",
"│ e │",
"│ f │",
"│ g │",
"│ h │",
"│ i │",
"│ j │",
"│ k │",
"│ l │",
"└──────────────────┘",
]
)
# behavior before issue #466
self.assert_paint_ignoring_formatting(
screen, try_preserve_history_height=0
)
self.assert_paint_ignoring_formatting(screen, min_infobox_height=100)
# behavior after issue #466
screen = self.process_box_characters(
[
">>> abc.",
"┌──────────────────┐",
"│ aaaaaaaaaaaaaaaa │",
"│ b │",
"│ c │",
"└──────────────────┘",
]
)
self.assert_paint_ignoring_formatting(screen)