From 66fa0020e8ccc0a48e06b7b8f6c115f64d1be9bc Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 14 Feb 2022 10:28:52 +0000 Subject: [PATCH] Improve a docstring, add tests for CSS opacity parsing --- src/textual/css/_styles_builder.py | 3 ++- tests/css/test_parse.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/textual/css/_styles_builder.py b/src/textual/css/_styles_builder.py index c3dc6cf6f..005e5f2ea 100644 --- a/src/textual/css/_styles_builder.py +++ b/src/textual/css/_styles_builder.py @@ -21,10 +21,11 @@ from ..geometry import Spacing, SpacingDimensions, clamp def _join_tokens(tokens: Iterable[Token], joiner: str = "") -> str: - """Convert tokens into a string + """Convert tokens into a string by joining their values Args: tokens (Iterable[Token]): Tokens to join + joiner (str): String to join on, defaults to "" Returns: str: The tokens, joined together to form a string. diff --git a/tests/css/test_parse.py b/tests/css/test_parse.py index 5142d5cc9..10b54b502 100644 --- a/tests/css/test_parse.py +++ b/tests/css/test_parse.py @@ -314,3 +314,29 @@ class TestParseTransition: assert len(stylesheet_errors) == 1 assert stylesheet_errors[0][0].value == invalid_func_name assert ex.value.errors is not None + + +class TestParseOpacity: + @pytest.mark.parametrize("css_value, styles_value", [ + ["-0.2", 0.0], + ["0.4", 0.4], + ["1.3", 1.0], + ["-20%", 0.0], + ["25%", 0.25], + ["128%", 1.0], + ]) + def test_opacity_to_styles(self, css_value, styles_value): + css = f"#some-widget {{ opacity: {css_value} }}" + stylesheet = Stylesheet() + stylesheet.parse(css) + + assert stylesheet.rules[0].styles.opacity == styles_value + assert not stylesheet.rules[0].errors + + def test_opacity_invalid_value(self): + css = "#some-widget { opacity: 123x }" + stylesheet = Stylesheet() + + with pytest.raises(StylesheetParseError): + stylesheet.parse(css) + assert stylesheet.rules[0].errors