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
|
from __future__ import annotations
import pytest
from textual.app import App, ComposeResult
from textual.color import Color
from textual.containers import Vertical
from textual.css.parse import parse
from textual.css.tokenizer import EOFError, TokenError
from textual.widgets import Button, Label
class NestedApp(App):
CSS = """
Screen {
& > #foo {
background: red;
#egg {
background: green;
}
.paul {
background: blue;
}
&.jessica {
color: magenta;
}
}
}
"""
def compose(self) -> ComposeResult:
with Vertical(id="foo", classes="jessica"):
yield Label("Hello", id="egg")
yield Label("World", classes="paul")
async def test_nest_app():
"""Test nested CSS works as expected."""
app = NestedApp()
async with app.run_test():
assert app.query_one("#foo").styles.background == Color.parse("red")
assert app.query_one("#foo").styles.color == Color.parse("magenta")
assert app.query_one("#egg").styles.background == Color.parse("green")
assert app.query_one("#foo .paul").styles.background == Color.parse("blue")
class ListOfNestedSelectorsApp(App[None]):
CSS = """
Label {
&.foo, &.bar {
background: red;
}
}
"""
def compose(self) -> ComposeResult:
yield Label("one", classes="foo")
yield Label("two", classes="bar")
yield Label("three", classes="heh")
async def test_lists_of_selectors_in_nested_css() -> None:
"""Regression test for https://github.com/Textualize/textual/issues/3969."""
app = ListOfNestedSelectorsApp()
red = Color.parse("red")
async with app.run_test():
assert app.query_one(".foo").styles.background == red
assert app.query_one(".bar").styles.background == red
assert app.query_one(".heh").styles.background != red
class DeclarationAfterNestedApp(App[None]):
CSS = """
Screen {
background: green;
Label {
background: red;
}
}
"""
def compose(self) -> ComposeResult:
yield Label("one")
async def test_rule_declaration_after_nested() -> None:
"""Regression test for https://github.com/Textualize/textual/issues/3999."""
app = DeclarationAfterNestedApp()
async with app.run_test():
assert app.screen.styles.background == Color.parse("green")
assert app.query_one(Label).styles.background == Color.parse("red")
@pytest.mark.parametrize(
("css", "exception"),
[
("Selector {", EOFError),
("Selector{ Foo {", EOFError),
("Selector{ Foo {}", EOFError),
("> {}", TokenError),
("&", TokenError),
("&&", TokenError),
("&.foo", TokenError),
("& .foo", TokenError),
("{", TokenError),
("*{", EOFError),
],
)
def test_parse_errors(css: str, exception: type[Exception]) -> None:
"""Check some CSS which should fail."""
with pytest.raises(exception):
list(parse("", css, ("foo", "")))
class PseudoClassesInNestedApp(App[None]):
CSS = """
Vertical {
Button:light, Button:dark {
background: red;
}
min-height: 3; # inconsequential rule to add entropy.
#two, *:focus {
background: green !important;
}
height: auto; # inconsequential rule to add entropy.
Label {
background: yellow;
&:light, &:dark {
background: red;
}
&:hover {
background: green !important;
}
}
}
"""
AUTO_FOCUS = "Button"
def compose(self) -> ComposeResult:
with Vertical():
yield Button(id="one", classes="first_half")
yield Button(id="two", classes="first_half")
with Vertical():
yield Label("Hello, world!", id="three", classes="first_half")
yield Label("Hello, world!", id="four", classes="first_half")
with Vertical():
yield Button(id="five", classes="second_half")
yield Button(id="six", classes="second_half")
yield Label("Hello, world!", id="seven", classes="second_half")
yield Label("Hello, world!", id="eight", classes="second_half")
async def test_pseudo_classes_work_in_nested_css() -> None:
"""Makes sure pseudo-classes are correctly understood in nested TCSS.
Regression test for https://github.com/Textualize/textual/issues/4039.
"""
app = PseudoClassesInNestedApp()
green = Color.parse("green")
red = Color.parse("red")
async with app.run_test() as pilot:
assert app.query_one("#one").styles.background == green
assert app.query_one("#two").styles.background == green
assert app.query_one("#five").styles.background == red
assert app.query_one("#six").styles.background == red
assert app.query_one("#three").styles.background == red
assert app.query_one("#four").styles.background == red
assert app.query_one("#seven").styles.background == red
assert app.query_one("#eight").styles.background == red
await pilot.hover("#eight")
assert app.query_one("#three").styles.background == red
assert app.query_one("#four").styles.background == red
assert app.query_one("#seven").styles.background == red
assert app.query_one("#eight").styles.background == green
|