-
Notifications
You must be signed in to change notification settings - Fork 873
/
Copy pathtest_snapshots.py
2376 lines (1644 loc) · 71.8 KB
/
test_snapshots.py
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from pathlib import Path
import pytest
from rich.panel import Panel
from rich.text import Text
from tests.snapshot_tests.language_snippets import SNIPPETS
from textual import events, on
from textual.app import App, ComposeResult
from textual.binding import Binding, Keymap
from textual.containers import Center, Container, Grid, Middle, Vertical, VerticalScroll
from textual.pilot import Pilot
from textual.renderables.gradient import LinearGradient
from textual.screen import ModalScreen, Screen
from textual.widgets import (
Button,
DataTable,
Footer,
Header,
Input,
Label,
Log,
OptionList,
Placeholder,
ProgressBar,
RadioSet,
RichLog,
SelectionList,
Static,
Switch,
TextArea,
)
from textual.widgets.text_area import BUILTIN_LANGUAGES, Selection, TextAreaTheme
# These paths should be relative to THIS directory.
WIDGET_EXAMPLES_DIR = Path("../../docs/examples/widgets")
LAYOUT_EXAMPLES_DIR = Path("../../docs/examples/guide/layout")
STYLES_EXAMPLES_DIR = Path("../../docs/examples/styles")
EXAMPLES_DIR = Path("../../examples")
SNAPSHOT_APPS_DIR = Path("./snapshot_apps")
# --- Layout related stuff ---
def test_grid_layout_basic(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "grid_layout1.py")
def test_grid_layout_basic_overflow(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "grid_layout2.py")
def test_grid_layout_gutter(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "grid_layout7_gutter.py")
def test_layers(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "layers.py")
def test_horizontal_layout(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "horizontal_layout.py")
def test_horizontal_layout_width_auto_dock(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "horizontal_auto_width.py")
def test_vertical_layout(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "vertical_layout.py")
def test_dock_layout_sidebar(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "dock_layout2_sidebar.py")
def test_layout_containers(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "layout_containers.py")
def test_alignment_containers(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "alignment_containers.py")
# --- Widgets - rendering and basic interactions ---
# Each widget should have a canonical example that is display in the docs.
# When adding a new widget, ideally we should also create a snapshot test
# from these examples which test rendering and simple interactions with it.
def test_switches(snap_compare):
"""Tests switches but also acts a regression test for using
width: auto in a Horizontal layout context."""
press = [
"shift+tab",
"enter", # toggle off
"shift+tab",
"wait:20",
"enter", # toggle on
"wait:20",
]
assert snap_compare(WIDGET_EXAMPLES_DIR / "switch.py", press=press)
def test_input_and_focus(snap_compare):
press = [
*"Darren", # Write "Darren"
"tab",
*"Burns", # Focus second input, write "Burns"
]
assert snap_compare(WIDGET_EXAMPLES_DIR / "input.py", press=press)
def test_input_validation(snap_compare):
"""Checking that invalid styling is applied. The snapshot app itself
also adds styling for -valid which gives a green border."""
press = [
*"-2", # -2 is invalid, so -invalid should be applied
"tab",
"3", # This is valid, so -valid should be applied
"tab",
*"-2",
# -2 is invalid, so -invalid should be applied (and :focus, since we stop here)
]
assert snap_compare(SNAPSHOT_APPS_DIR / "input_validation.py", press=press)
def test_input_suggestions(snap_compare):
async def run_before(pilot):
pilot.app.query(Input).first().cursor_blink = False
assert snap_compare(
SNAPSHOT_APPS_DIR / "input_suggestions.py", press=["b"], run_before=run_before
)
def test_masked_input(snap_compare):
async def run_before(pilot):
pilot.app.query(Input).first().cursor_blink = False
assert snap_compare(
SNAPSHOT_APPS_DIR / "masked_input.py",
press=["A", "B", "C", "0", "1", "-", "D", "E"],
run_before=run_before,
)
def test_buttons_render(snap_compare):
# Testing button rendering. We press tab to focus the first button too.
assert snap_compare(WIDGET_EXAMPLES_DIR / "button.py", press=["tab"])
def test_placeholder_render(snap_compare):
# Testing the rendering of the multiple placeholder variants and labels.
assert snap_compare(WIDGET_EXAMPLES_DIR / "placeholder.py")
def test_datatable_render(snap_compare):
press = ["tab", "down", "down", "right", "up", "left"]
assert snap_compare(WIDGET_EXAMPLES_DIR / "data_table.py", press=press)
def test_datatable_row_cursor_render(snap_compare):
press = ["up", "left", "right", "down", "down"]
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_row_cursor.py", press=press)
def test_datatable_column_cursor_render(snap_compare):
press = ["left", "up", "down", "right", "right"]
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_column_cursor.py", press=press)
def test_datatable_sort_multikey(snap_compare):
press = ["down", "right", "s"] # Also checks that sort doesn't move cursor.
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_sort.py", press=press)
def test_datatable_remove_row(snap_compare):
press = ["r"]
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_remove_row.py", press=press)
def test_datatable_labels_and_fixed_data(snap_compare):
# Ensure that we render correctly when there are fixed rows/cols and labels.
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_row_labels.py")
def test_datatable_style_ordering(snap_compare):
# Regression test for https://github.com/Textualize/textual/issues/2061
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_style_order.py")
def test_datatable_add_column(snap_compare):
# Checking adding columns after adding rows
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_add_column.py")
def test_datatable_add_row_auto_height(snap_compare):
# Check that rows added with auto height computation look right.
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_add_row_auto_height.py")
def test_datatable_add_row_auto_height_sorted(snap_compare):
# Check that rows added with auto height computation look right.
assert snap_compare(
SNAPSHOT_APPS_DIR / "data_table_add_row_auto_height.py", press=["s"]
)
def test_datatable_auto_height_future_updates(snap_compare):
"""https://github.com/Textualize/textual/issues/4928 meant that when height=None,
in add_row, future updates to the table would be incorrect.
In this test, every 2nd row is auto height and every other row is height 2.
The table is cleared then fully repopulated with the same 4 rows. All 4 rows
should be visible and rendered at heights 2, 1, 2, 1.
"""
ROWS = [
("foo", "bar"),
(1, "abc"),
(2, "def"),
(3, "ghi"),
(4, "jkl"),
]
class ExampleApp(App[None]):
CSS = "DataTable { border: solid red; }"
def compose(self) -> ComposeResult:
yield DataTable()
def on_mount(self) -> None:
table = self.query_one(DataTable)
table.add_columns(*ROWS[0])
self.populate_table()
def key_r(self) -> None:
self.populate_table()
def populate_table(self) -> None:
table = self.query_one(DataTable)
table.clear()
for i, row in enumerate(ROWS[1:]):
table.add_row(
*row,
height=None if i % 2 == 1 else 2,
)
assert snap_compare(ExampleApp(), press=["r"])
def test_datatable_cell_padding(snap_compare):
# Check that horizontal cell padding is respected.
assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_cell_padding.py")
def test_datatable_change_cell_padding(snap_compare):
# Check that horizontal cell padding is respected.
assert snap_compare(
SNAPSHOT_APPS_DIR / "data_table_cell_padding.py", press=["a", "b"]
)
def test_footer_render(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "footer.py")
def test_header_render(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "header.py")
def test_list_view(snap_compare):
assert snap_compare(
WIDGET_EXAMPLES_DIR / "list_view.py", press=["tab", "down", "down", "up"]
)
def test_log_write_lines(snap_compare):
assert snap_compare("snapshot_apps/log_write_lines.py")
def test_log_write(snap_compare):
assert snap_compare("snapshot_apps/log_write.py")
def test_fr_units(snap_compare):
assert snap_compare("snapshot_apps/fr_units.py")
def test_visibility(snap_compare):
assert snap_compare("snapshot_apps/visibility.py")
def test_tree_example(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "tree.py")
def test_markdown_example(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "markdown.py")
def test_markdown_viewer_example(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "markdown_viewer.py")
def test_markdown_theme_switching(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "markdown_theme_switcher.py", press=["t"])
def test_markdown_dark_theme_override(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "markdown_theme_switcher.py", press=["d", "wait:100"]
)
def test_markdown_light_theme_override(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "markdown_theme_switcher.py", press=["l", "t", "wait:100"]
)
def test_checkbox_example(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "checkbox.py")
def test_radio_button_example(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "radio_button.py")
def test_radio_set_example(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "radio_set.py")
def test_radio_set_is_scrollable(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/5100"""
class RadioSetApp(App):
CSS = """
RadioSet {
height: 5;
}
"""
def compose(self) -> ComposeResult:
yield RadioSet(*[(f"This is option #{n}") for n in range(10)])
app = RadioSetApp()
assert snap_compare(app, press=["up"])
def test_content_switcher_example_initial(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "content_switcher.py")
def test_content_switcher_example_switch(snap_compare):
assert snap_compare(
WIDGET_EXAMPLES_DIR / "content_switcher.py",
press=["tab", "enter", "wait:500"],
terminal_size=(50, 50),
)
def test_tabbed_content(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "tabbed_content.py")
def test_tabbed_content_with_modified_tabs(snap_compare):
# Tabs enabled and hidden.
assert snap_compare(SNAPSHOT_APPS_DIR / "modified_tabs.py")
def test_tabbed_content_styling_not_leaking(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "tabbed_content_style_leak_test.py")
def test_option_list_strings(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "option_list_strings.py")
def test_option_list_options(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "option_list_options.py")
def test_option_list_tables(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "option_list_tables.py")
def test_option_list_build(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "option_list.py")
def test_option_list_replace_prompt_from_single_line_to_single_line(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "option_list_multiline_options.py", press=["1"]
)
def test_option_list_replace_prompt_from_single_line_to_two_lines(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "option_list_multiline_options.py", press=["2"]
)
def test_option_list_replace_prompt_from_two_lines_to_three_lines(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "option_list_multiline_options.py", press=["3"]
)
def test_option_list_scrolling_in_long_list(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "option_list_long.py", press=["up"])
def test_progress_bar_indeterminate(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "progress_bar_isolated_.py", press=["f"])
def test_progress_bar_indeterminate_styled(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "progress_bar_styled_.py", press=["f"])
def test_progress_bar_halfway(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "progress_bar_isolated_.py", press=["t"])
def test_progress_bar_halfway_styled(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "progress_bar_styled_.py", press=["t"])
def test_progress_bar_completed(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "progress_bar_isolated_.py", press=["u"])
def test_progress_bar_completed_styled(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "progress_bar_styled_.py", press=["u"])
def test_rule_horizontal_rules(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "horizontal_rules.py")
def test_rule_vertical_rules(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "vertical_rules.py")
def test_select(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "select_widget.py")
def test_selection_list_selected(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "selection_list_selected.py")
def test_selection_list_selections(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "selection_list_selections.py")
def test_selection_list_tuples(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "selection_list_tuples.py")
def test_select_expanded(snap_compare):
assert snap_compare(
WIDGET_EXAMPLES_DIR / "select_widget.py", press=["tab", "enter"]
)
def test_select_from_values_expanded(snap_compare):
assert snap_compare(
WIDGET_EXAMPLES_DIR / "select_from_values_widget.py", press=["tab", "enter"]
)
def test_select_expanded_changed(snap_compare):
assert snap_compare(
WIDGET_EXAMPLES_DIR / "select_widget.py",
press=["tab", "enter", "down", "enter"],
)
def test_select_no_blank_has_default_value(snap_compare):
"""Make sure that the first value is selected by default if allow_blank=False."""
assert snap_compare(WIDGET_EXAMPLES_DIR / "select_widget_no_blank.py")
def test_select_set_options(snap_compare):
assert snap_compare(
WIDGET_EXAMPLES_DIR / "select_widget_no_blank.py",
press=["s"],
)
def test_sparkline_render(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "sparkline.py")
def test_sparkline_component_classes_colors(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "sparkline_colors.py")
def test_collapsible_render(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "collapsible.py")
def test_collapsible_collapsed(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "collapsible.py", press=["c"])
def test_collapsible_expanded(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "collapsible.py", press=["e"])
def test_collapsible_nested(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "collapsible_nested.py")
def test_collapsible_custom_symbol(snap_compare):
assert snap_compare(WIDGET_EXAMPLES_DIR / "collapsible_custom_symbol.py")
def test_directory_tree_reloading(snap_compare, tmp_path):
async def run_before(pilot):
await pilot.app.setup(tmp_path)
await pilot.press(
"down", "e", "e", "down", "down", "down", "down", "e", "down", "d", "r"
)
assert snap_compare(
SNAPSHOT_APPS_DIR / "directory_tree_reload.py",
run_before=run_before,
)
# --- CSS properties ---
# We have a canonical example for each CSS property that is shown in their docs.
# If any of these change, something has likely broken, so snapshot each of them.
PATHS = [
path.name
for path in (Path(__file__).parent / STYLES_EXAMPLES_DIR).iterdir()
if path.suffix == ".py"
]
@pytest.mark.parametrize("file_name", PATHS)
def test_css_property(file_name, snap_compare):
path_to_app = STYLES_EXAMPLES_DIR / file_name
assert snap_compare(path_to_app)
def test_viewport_height_and_width_properties(snap_compare):
path_to_app = SNAPSHOT_APPS_DIR / "viewport_units.py"
assert snap_compare(path_to_app)
def test_multiple_css(snap_compare):
# Interaction between multiple CSS files and app-level/classvar CSS
assert snap_compare("snapshot_apps/multiple_css/multiple_css.py")
def test_order_independence(snap_compare):
assert snap_compare("snapshot_apps/layer_order_independence.py")
def test_order_independence_toggle(snap_compare):
assert snap_compare("snapshot_apps/layer_order_independence.py", press="t")
def test_columns_height(snap_compare):
# Interaction with height auto, and relative heights to make columns
assert snap_compare("snapshot_apps/columns_height.py")
def test_offsets(snap_compare):
"""Test offsets of containers"""
assert snap_compare("snapshot_apps/offsets.py")
def test_nested_auto_heights(snap_compare):
"""Test refreshing widget within a auto sized container"""
assert snap_compare("snapshot_apps/nested_auto_heights.py", press=["1", "2"])
def test_programmatic_scrollbar_gutter_change(snap_compare):
"""Regression test for #1607 https://github.com/Textualize/textual/issues/1607
See also tests/css/test_programmatic_style_changes.py for other related regression tests.
"""
assert snap_compare(
"snapshot_apps/programmatic_scrollbar_gutter_change.py", press=["s"]
)
# --- Other ---
def test_key_display(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "key_display.py")
def test_demo(snap_compare):
"""Test the demo app (python -m textual)"""
assert snap_compare(
Path("../../src/textual/demo.py"),
terminal_size=(100, 30),
)
def test_label_widths(snap_compare):
"""Test renderable widths are calculate correctly."""
assert snap_compare(SNAPSHOT_APPS_DIR / "label_widths.py")
def test_border_alpha(snap_compare):
"""Test setting a border alpha."""
assert snap_compare(SNAPSHOT_APPS_DIR / "border_alpha.py")
def test_auto_width_input(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "auto_width_input.py", press=["tab", *"Hello"]
)
def test_screen_switch(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "screen_switch.py", press=["a", "b"])
def test_disabled_widgets(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "disable_widgets.py")
def test_focus_component_class(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "focus_component_class.py", press=["tab"])
def test_line_api_scrollbars(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "line_api_scrollbars.py")
def test_remove_with_auto_height(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "remove_auto.py", press=["a", "a", "a", "d", "d"]
)
def test_auto_table(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "auto-table.py", terminal_size=(120, 40))
def test_table_markup(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "table_markup.py")
def test_richlog_max_lines(snap_compare):
assert snap_compare("snapshot_apps/richlog_max_lines.py", press=[*"abcde"])
def test_richlog_scroll(snap_compare):
"""Ensure `RichLog.auto_scroll` causes the log to scroll to the end when new content is written."""
assert snap_compare(SNAPSHOT_APPS_DIR / "richlog_scroll.py")
def test_richlog_width(snap_compare):
"""Check that the width of RichLog is respected, even when it's not visible."""
assert snap_compare(SNAPSHOT_APPS_DIR / "richlog_width.py", press=["p"])
def test_richlog_min_width(snap_compare):
"""The available space of this RichLog is less than the minimum width, so written
content should be rendered at `min_width`. This snapshot should show the renderable
clipping at the right edge, as there's not enough space to satisfy the minimum width.
"""
class RichLogMinWidth20(App[None]):
def compose(self) -> ComposeResult:
rich_log = RichLog(min_width=20)
text = Text("0123456789", style="on red", justify="right")
rich_log.write(text)
yield rich_log
assert snap_compare(RichLogMinWidth20(), terminal_size=(20, 6))
def test_richlog_deferred_render_no_expand(snap_compare):
"""Check we can write to a RichLog before its size is known i.e. in `compose`."""
class RichLogNoExpand(App[None]):
def compose(self) -> ComposeResult:
rich_log = RichLog(min_width=10)
text = Text("0123456789", style="on red", justify="right")
# Perform the write in compose - it'll be deferred until the size is known
rich_log.write(text)
yield rich_log
assert snap_compare(RichLogNoExpand(), terminal_size=(20, 6))
def test_richlog_deferred_render_expand(snap_compare):
"""Check we can write to a RichLog before its size is known i.e. in `compose`.
The renderable should expand to fill full the width of the RichLog.
"""
class RichLogExpand(App[None]):
def compose(self) -> ComposeResult:
rich_log = RichLog(min_width=10)
text = Text("0123456789", style="on red", justify="right")
# Perform the write in compose - it'll be deferred until the size is known
rich_log.write(text, expand=True)
yield rich_log
assert snap_compare(RichLogExpand(), terminal_size=(20, 6))
def test_richlog_markup(snap_compare):
"""Check that Rich markup is rendered in RichLog when markup=True."""
class RichLogWidth(App[None]):
def compose(self) -> ComposeResult:
rich_log = RichLog(min_width=10, markup=True)
rich_log.write("[black on red u]black text on red, underlined")
rich_log.write("normal text, no markup")
yield rich_log
assert snap_compare(RichLogWidth(), terminal_size=(42, 6))
def test_richlog_shrink(snap_compare):
"""Ensure that when shrink=True, the renderable shrinks to fit the width of the RichLog."""
class RichLogShrink(App[None]):
CSS = "RichLog { width: 20; background: red;}"
def compose(self) -> ComposeResult:
rich_log = RichLog(min_width=4)
panel = Panel("lorem ipsum dolor sit amet lorem ipsum dolor sit amet")
rich_log.write(panel)
yield rich_log
assert snap_compare(RichLogShrink(), terminal_size=(24, 6))
def test_richlog_write_at_specific_width(snap_compare):
"""Ensure we can write renderables at a specific width.
`min_width` should be respected, but `width` should override.
The green label at the bottom should be equal in width to the bottom
renderable (equal to min_width).
"""
class RichLogWriteAtSpecificWidth(App[None]):
CSS = """
RichLog { width: 1fr; height: auto; }
#width-marker { background: green; width: 50; }
"""
def compose(self) -> ComposeResult:
rich_log = RichLog(min_width=50)
rich_log.write(Panel("width=20", style="black on red"), width=20)
rich_log.write(Panel("width=40", style="black on red"), width=40)
rich_log.write(Panel("width=60", style="black on red"), width=60)
rich_log.write(Panel("width=120", style="black on red"), width=120)
rich_log.write(
Panel("width=None (fallback to min_width)", style="black on red")
)
yield rich_log
width_marker = Label(
f"this label is width 50 (same as min_width)", id="width-marker"
)
yield width_marker
assert snap_compare(RichLogWriteAtSpecificWidth())
def test_richlog_highlight(snap_compare):
"""Check that RichLog.highlight correctly highlights with the ReprHighlighter.
Also ensures that interaction between CSS and highlighting is as expected -
non-highlighted text should have the CSS styles applied, but highlighted text
should ignore the CSS (and use the highlights returned from the highlighter).
"""
class RichLogHighlight(App[None]):
# Add some CSS to check interaction with highlighting.
CSS = """
RichLog { color: red; background: dodgerblue 40%; }
"""
def compose(self) -> ComposeResult:
rich_log = RichLog(highlight=True)
rich_log.write("Foo('bar', x=1, y=[1, 2, 3])")
yield rich_log
assert snap_compare(RichLogHighlight(), terminal_size=(30, 3))
def test_tabs_invalidate(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "tabs_invalidate.py",
press=["tab", "right"],
)
def test_scrollbar_thumb_height(snap_compare):
assert snap_compare(
SNAPSHOT_APPS_DIR / "scrollbar_thumb_height.py",
)
def test_pilot_resize_terminal(snap_compare):
async def run_before(pilot):
await pilot.resize_terminal(35, 20)
await pilot.resize_terminal(20, 10)
assert snap_compare(
SNAPSHOT_APPS_DIR / "pilot_resize_terminal.py",
run_before=run_before,
terminal_size=(80, 25),
)
def test_css_hot_reloading(snap_compare, monkeypatch):
"""Regression test for https://github.com/Textualize/textual/issues/2063."""
monkeypatch.setenv(
"TEXTUAL", "debug"
) # This will make sure we create a file monitor.
async def run_before(pilot):
css_file = pilot.app.CSS_PATH
with open(css_file, "w") as f:
f.write("/* This file is purposefully empty. */\n") # Clear all the CSS.
await pilot.app._on_css_change()
assert snap_compare(
SNAPSHOT_APPS_DIR / "hot_reloading_app.py", run_before=run_before
)
def test_css_hot_reloading_on_screen(snap_compare, monkeypatch):
"""Regression test for https://github.com/Textualize/textual/issues/3454."""
monkeypatch.setenv(
"TEXTUAL", "debug"
) # This will make sure we create a file monitor.
async def run_before(pilot):
css_file = pilot.app.screen.CSS_PATH
with open(css_file, "w") as f:
f.write("/* This file is purposefully empty. */\n") # Clear all the CSS.
await pilot.app._on_css_change()
assert snap_compare(
SNAPSHOT_APPS_DIR / "hot_reloading_app_with_screen_css.py",
run_before=run_before,
)
def test_datatable_hot_reloading(snap_compare, monkeypatch):
"""Regression test for https://github.com/Textualize/textual/issues/3312."""
monkeypatch.setenv(
"TEXTUAL", "debug"
) # This will make sure we create a file monitor.
async def run_before(pilot):
css_file = pilot.app.CSS_PATH
with open(css_file, "w") as f:
f.write("/* This file is purposefully empty. */\n") # Clear all the CSS.
await pilot.app._on_css_change()
assert snap_compare(
SNAPSHOT_APPS_DIR / "datatable_hot_reloading.py", run_before=run_before
)
def test_markdown_component_classes_reloading(snap_compare, monkeypatch):
"""Tests all markdown component classes reload correctly.
See https://github.com/Textualize/textual/issues/3464."""
monkeypatch.setenv(
"TEXTUAL", "debug"
) # This will make sure we create a file monitor.
async def run_before(pilot):
css_file = pilot.app.CSS_PATH
with open(css_file, "w") as f:
f.write("/* This file is purposefully empty. */\n") # Clear all the CSS.
await pilot.app._on_css_change()
assert snap_compare(
SNAPSHOT_APPS_DIR / "markdown_component_classes_reloading.py",
run_before=run_before,
)
def test_markdown_space_squashing(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "markdown_whitespace.py")
def test_layer_fix(snap_compare):
# Check https://github.com/Textualize/textual/issues/1358
assert snap_compare(SNAPSHOT_APPS_DIR / "layer_fix.py", press=["d"])
def test_modal_dialog_bindings_input(snap_compare):
# Check https://github.com/Textualize/textual/issues/2194
assert snap_compare(
SNAPSHOT_APPS_DIR / "modal_screen_bindings.py",
press=["enter", "h", "!", "left", "i", "tab"],
)
def test_modal_dialog_bindings(snap_compare):
# Check https://github.com/Textualize/textual/issues/2194
assert snap_compare(
SNAPSHOT_APPS_DIR / "modal_screen_bindings.py",
press=["enter", "tab", "h", "i", "tab", "enter"],
)
def test_dock_scroll(snap_compare):
# https://github.com/Textualize/textual/issues/2188
assert snap_compare(SNAPSHOT_APPS_DIR / "dock_scroll.py", terminal_size=(80, 25))
def test_dock_scroll2(snap_compare):
# https://github.com/Textualize/textual/issues/2525
assert snap_compare(SNAPSHOT_APPS_DIR / "dock_scroll2.py", terminal_size=(80, 25))
def test_dock_scroll_off_by_one(snap_compare):
# https://github.com/Textualize/textual/issues/2525
assert snap_compare(
SNAPSHOT_APPS_DIR / "dock_scroll_off_by_one.py",
terminal_size=(80, 25),
press=["_"],
)
def test_dock_none(snap_compare):
"""Checking that `dock:none` works in CSS and Python.
The label should appear at the top here, since we've undocked both
the header and footer.
"""
class DockNone(App[None]):
CSS = "Header { dock: none; }"
def compose(self) -> ComposeResult:
yield Label("Hello")
yield Header()
footer = Footer()
footer.styles.dock = "none"
yield footer
assert snap_compare(DockNone(), terminal_size=(30, 5))
def test_scroll_to(snap_compare):
# https://github.com/Textualize/textual/issues/2525
assert snap_compare(
SNAPSHOT_APPS_DIR / "scroll_to.py", terminal_size=(80, 25), press=["_"]
)
def test_auto_fr(snap_compare):
# https://github.com/Textualize/textual/issues/2220
assert snap_compare(SNAPSHOT_APPS_DIR / "auto_fr.py", terminal_size=(80, 25))
def test_fr_margins(snap_compare):
# https://github.com/Textualize/textual/issues/2220
assert snap_compare(SNAPSHOT_APPS_DIR / "fr_margins.py", terminal_size=(80, 25))
def test_scroll_visible(snap_compare):
# https://github.com/Textualize/textual/issues/2181
assert snap_compare(SNAPSHOT_APPS_DIR / "scroll_visible.py", press=["t"])
def test_scroll_to_center(snap_compare):
# READ THIS IF THIS TEST FAILS:
# While https://github.com/Textualize/textual/issues/2254 is open, the snapshot
# this is being compared against is INCORRECT.
# The correct output for this snapshot test would show a couple of containers
# scrolled so that the red string >>bullseye<< is centered on the screen.
# When this snapshot "breaks" because #2254 is fixed, this snapshot can be updated.
assert snap_compare(SNAPSHOT_APPS_DIR / "scroll_to_center.py", press=["s"])
def test_quickly_change_tabs(snap_compare):
# https://github.com/Textualize/textual/issues/2229
assert snap_compare(SNAPSHOT_APPS_DIR / "quickly_change_tabs.py", press=["p"])
def test_fr_unit_with_min(snap_compare):