From 3c18e47466a644fc323798de0d64f2b386c26c1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 15 Feb 2024 14:08:42 +0000 Subject: [PATCH] Test nested CSS worked at higher level. Addresses review feedback. See the three comments starting at https://github.com/Textualize/textual/pull/4040#issuecomment-1934279826. --- tests/css/test_nested_css.py | 48 ++++++++++ tests/css/test_tokenize.py | 181 ----------------------------------- 2 files changed, 48 insertions(+), 181 deletions(-) diff --git a/tests/css/test_nested_css.py b/tests/css/test_nested_css.py index 821c655ff3..20424ad297 100644 --- a/tests/css/test_nested_css.py +++ b/tests/css/test_nested_css.py @@ -44,6 +44,54 @@ async def test_nest_app(): 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{Label{background:red;}background:green;}" + CSS = """ + Screen { + Label { + background: red; + } + background: green; + } + """ + + 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"), [ diff --git a/tests/css/test_tokenize.py b/tests/css/test_tokenize.py index fab79ed4f6..d4dfba888e 100644 --- a/tests/css/test_tokenize.py +++ b/tests/css/test_tokenize.py @@ -898,184 +898,3 @@ def test_allow_new_lines(): ), ] assert list(tokenize(css, ("", ""))) == expected - - -def test_nested_css_selector_list_with_ampersand(): - """Regression test for https://github.com/Textualize/textual/issues/3969.""" - css = "Label{&.foo,&.bar{border:solid red;}}" - tokens = list(tokenize(css, ("", ""))) - assert tokens == [ - Token( - name="selector_start", - value="Label", - read_from=("", ""), - code=css, - location=(0, 0), - ), - Token( - name="declaration_set_start", - value="{", - read_from=("", ""), - code=css, - location=(0, 5), - ), - Token(name="nested", value="&", read_from=("", ""), code=css, location=(0, 6)), - Token( - name="selector_class", - value=".foo", - read_from=("", ""), - code=css, - location=(0, 7), - ), - Token( - name="new_selector", - value=",", - read_from=("", ""), - code=css, - location=(0, 11), - ), - Token(name="nested", value="&", read_from=("", ""), code=css, location=(0, 12)), - Token( - name="selector_class", - value=".bar", - read_from=("", ""), - code=css, - location=(0, 13), - ), - Token( - name="declaration_set_start", - value="{", - read_from=("", ""), - code=css, - location=(0, 17), - ), - Token( - name="declaration_name", - value="border:", - read_from=("", ""), - code=css, - location=(0, 18), - ), - Token( - name="token", value="solid", read_from=("", ""), code=css, location=(0, 25) - ), - Token( - name="whitespace", value=" ", read_from=("", ""), code=css, location=(0, 30) - ), - Token( - name="token", value="red", read_from=("", ""), code=css, location=(0, 31) - ), - Token( - name="declaration_end", - value=";", - read_from=("", ""), - code=css, - location=(0, 34), - ), - Token( - name="declaration_set_end", - value="}", - read_from=("", ""), - code=css, - location=(0, 35), - ), - Token( - name="declaration_set_end", - value="}", - read_from=("", ""), - code=css, - location=(0, 36), - ), - ] - - -def test_declaration_after_nested_declaration_set(): - """Regression test for https://github.com/Textualize/textual/issues/3999.""" - css = "Screen{Label{background:red;}background:green;}" - tokens = list(tokenize(css, ("", ""))) - assert tokens == [ - Token( - name="selector_start", - value="Screen", - read_from=("", ""), - code=css, - location=(0, 0), - ), - Token( - name="declaration_set_start", - value="{", - read_from=("", ""), - code=css, - location=(0, 6), - ), - Token( - name="selector_start", - value="Label", - read_from=("", ""), - code=css, - location=(0, 7), - ), - Token( - name="declaration_set_start", - value="{", - read_from=("", ""), - code=css, - location=(0, 12), - ), - Token( - name="declaration_name", - value="background:", - read_from=("", ""), - code=css, - location=(0, 13), - ), - Token( - name="token", - value="red", - read_from=("", ""), - code=css, - location=(0, 24), - ), - Token( - name="declaration_end", - value=";", - read_from=("", ""), - code=css, - location=(0, 27), - ), - Token( - name="declaration_set_end", - value="}", - read_from=("", ""), - code=css, - location=(0, 28), - ), - Token( - name="declaration_name", - value="background:", - read_from=("", ""), - code=css, - location=(0, 29), - ), - Token( - name="token", - value="green", - read_from=("", ""), - code=css, - location=(0, 40), - ), - Token( - name="declaration_end", - value=";", - read_from=("", ""), - code=css, - location=(0, 45), - ), - Token( - name="declaration_set_end", - value="}", - read_from=("", ""), - code=css, - location=(0, 46), - ), - ]