Skip to content

Commit f59af27

Browse files
author
Sergi Pons Freixes
committed
Add tests for first-child and last-child pseudo-classes
1 parent 1267403 commit f59af27

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
Lines changed: 156 additions & 0 deletions
Loading

tests/snapshot_tests/test_snapshots.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,38 @@ def on_mount(self) -> None:
25542554

25552555
assert snap_compare(PSApp())
25562556

2557+
def test_child_pseudo_classes(snap_compare):
2558+
"""Test pseudo classes added in https://github.com/Textualize/textual/pull/XXXX
2559+
2560+
You should see 2 labels and 3 buttons
2561+
2562+
The first label should have a red border.
2563+
2564+
The last button should have a green border.
2565+
"""
2566+
2567+
class CPSApp(App):
2568+
CSS = """
2569+
Label { width: 1fr; height: 1fr; }
2570+
Button { width: 1fr; height: 1fr; }
2571+
Label:first-child { border:heavy red; }
2572+
Label:last-child { border:heavy orange; }
2573+
Button:first-child { border:heavy yellow; }
2574+
Button:last-child { border:heavy green; }
2575+
"""
2576+
2577+
def compose(self) -> ComposeResult:
2578+
yield Label("Label 1")
2579+
yield Label("Label 2")
2580+
yield Button("Button 1")
2581+
yield Button("Button 2")
2582+
2583+
def on_mount(self) -> None:
2584+
# Mounting a new widget should update previous widgets, as the last child has changed
2585+
self.mount(Button("HELLO"))
2586+
2587+
assert snap_compare(CPSApp())
2588+
25572589

25582590
def test_split_segments_infinite_loop(snap_compare):
25592591
"""Regression test for https://github.com/Textualize/textual/issues/5151

tests/test_app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ async def test_hover_update_styles():
4343
"enabled",
4444
"first-of-type",
4545
"last-of-type",
46+
"last-child",
4647
"even",
4748
}
4849

@@ -59,6 +60,7 @@ async def test_hover_update_styles():
5960
"hover",
6061
"first-of-type",
6162
"last-of-type",
63+
"last-child",
6264
"even",
6365
}
6466
assert button.styles.background != initial_background

tests/test_widget.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,26 +646,36 @@ def compose(self) -> ComposeResult:
646646
labels = list(app.query(Label))
647647
assert labels[0].first_of_type
648648
assert not labels[0].last_of_type
649+
assert labels[0].first_child
650+
assert not labels[0].last_child
649651
assert labels[0].is_odd
650652
assert not labels[0].is_even
651653

652654
assert not labels[1].first_of_type
653655
assert not labels[1].last_of_type
656+
assert not labels[1].first_child
657+
assert not labels[1].last_child
654658
assert not labels[1].is_odd
655659
assert labels[1].is_even
656660

657661
assert not labels[2].first_of_type
658662
assert not labels[2].last_of_type
663+
assert not labels[2].first_child
664+
assert not labels[2].last_child
659665
assert labels[2].is_odd
660666
assert not labels[2].is_even
661667

662668
assert not labels[3].first_of_type
663669
assert not labels[3].last_of_type
670+
assert not labels[3].first_child
671+
assert not labels[3].last_child
664672
assert not labels[3].is_odd
665673
assert labels[3].is_even
666674

667675
assert not labels[4].first_of_type
668676
assert labels[4].last_of_type
677+
assert not labels[4].first_child
678+
assert labels[4].last_child
669679
assert labels[4].is_odd
670680
assert not labels[4].is_even
671681

0 commit comments

Comments
 (0)