From 91783b7c1e06a45e93fd89dbdb6aa3d1a9c2e990 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 27 Apr 2022 13:55:29 +0100 Subject: [PATCH] Testing for help text --- src/textual/css/_help_text.py | 32 +++++---- tests/css/test_help_text.py | 123 ++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 17 deletions(-) create mode 100644 tests/css/test_help_text.py diff --git a/src/textual/css/_help_text.py b/src/textual/css/_help_text.py index e8738cb22..5fc2683a8 100644 --- a/src/textual/css/_help_text.py +++ b/src/textual/css/_help_text.py @@ -46,7 +46,7 @@ class ContextSpecificBullets: inline: Iterable[Bullet] css: Iterable[Bullet] - def get_by_context(self, context: StylingContext | None) -> list[Bullet]: + def get_by_context(self, context: StylingContext) -> list[Bullet]: """Get the information associated with the given context Args: @@ -54,10 +54,8 @@ class ContextSpecificBullets: """ if context == "inline": return list(self.inline) - elif context == "css": - return list(self.css) else: - return list(self.inline) + list(self.css) + return list(self.css) def _python_name(property_name: str) -> str: @@ -85,7 +83,8 @@ def _css_name(property_name: str) -> str: def _contextualize_property_name( - property_name: str, context: StylingContext | None + property_name: str, + context: StylingContext, ) -> str: """Convert a property name to CSS or inline by replacing '-' with '_' or vice-versa @@ -97,13 +96,7 @@ def _contextualize_property_name( Returns: str: The property name converted to the given context. """ - if context: - return ( - _css_name(property_name) - if context == "css" - else _python_name(property_name) - ) - return property_name + return _css_name(property_name) if context == "css" else _python_name(property_name) def _spacing_examples(property_name: str) -> ContextSpecificBullets: @@ -144,7 +137,7 @@ def _spacing_examples(property_name: str) -> ContextSpecificBullets: def spacing_wrong_number_of_values( property_name: str, num_values_supplied: int, - context: StylingContext | None = None, + context: StylingContext, ) -> HelpText: """Help text to show when the user supplies the wrong number of values for a spacing property (e.g. padding or margin). @@ -173,7 +166,8 @@ def spacing_wrong_number_of_values( def spacing_invalid_value( - property_name: str, context: StylingContext | None = None + property_name: str, + context: StylingContext, ) -> HelpText: """Help text to show when the user supplies an invalid value for a spacing property. @@ -193,7 +187,8 @@ def spacing_invalid_value( def scalar_help_text( - property_name: str, context: StylingContext | None = None + property_name: str, + context: StylingContext, ) -> HelpText: """Help text to show when the user supplies an invalid value for a scalar property. @@ -240,7 +235,9 @@ def scalar_help_text( def string_enum_help_text( - property_name: str, valid_values: list[str], context: StylingContext | None = None + property_name: str, + valid_values: list[str], + context: StylingContext, ) -> HelpText: """Help text to show when the user supplies an invalid value for a string enum property. @@ -285,7 +282,8 @@ def string_enum_help_text( def color_property_help_text( - property_name: str, context: StylingContext | None = None + property_name: str, + context: StylingContext, ) -> HelpText: """Help text to show when the user supplies an invalid value for a color property. For example, an unparseable color string. diff --git a/tests/css/test_help_text.py b/tests/css/test_help_text.py new file mode 100644 index 000000000..5c34c3974 --- /dev/null +++ b/tests/css/test_help_text.py @@ -0,0 +1,123 @@ +import pytest + +from tests.utilities.render import render +from textual.css._help_text import spacing_wrong_number_of_values, spacing_invalid_value, scalar_help_text, \ + string_enum_help_text, color_property_help_text, border_property_help_text, layout_property_help_text, \ + docks_property_help_text, dock_property_help_text, fractional_property_help_text, offset_property_help_text, \ + align_help_text, offset_single_axis_help_text, style_flags_property_help_text + + +@pytest.fixture(params=["css", "inline"]) +def styling_context(request): + return request.param + + +def test_help_text_examples_are_contextualized(): + """Ensure that if the user is using CSS, they see CSS-specific examples + and if they're using inline styles they see inline-specific examples.""" + rendered_inline = render(spacing_invalid_value("padding", "inline")) + assert "widget.styles.padding" in rendered_inline + + rendered_css = render(spacing_invalid_value("padding", "css")) + assert "padding:" in rendered_css + + +def test_spacing_wrong_number_of_values(styling_context): + rendered = render(spacing_wrong_number_of_values("margin", 3, styling_context)) + assert "Invalid number of values" in rendered + assert "margin" in rendered + assert "3" in rendered + + +def test_spacing_invalid_value(styling_context): + rendered = render(spacing_invalid_value("padding", styling_context)) + assert "Invalid value for" in rendered + assert "padding" in rendered + + +def test_scalar_help_text(styling_context): + rendered = render(scalar_help_text("max-width", styling_context)) + assert "Invalid value for" in rendered + + # Ensure property name is contextualised to inline/css styling + if styling_context == "css": + assert "max-width" in rendered + elif styling_context == "inline": + assert "max_width" in rendered + + +def test_string_enum_help_text(styling_context): + rendered = render(string_enum_help_text("display", ["none", "hidden"], styling_context)) + assert "Invalid value for" in rendered + + # Ensure property name is mentioned + assert "display" in rendered + + # Ensure each valid value is mentioned + assert "hidden" in rendered + assert "none" in rendered + + +def test_color_property_help_text(styling_context): + rendered = render(color_property_help_text("background", styling_context)) + assert "Invalid value for" in rendered + assert "background" in rendered + + +def test_border_property_help_text(styling_context): + rendered = render(border_property_help_text("border", styling_context)) + assert "Invalid value for" in rendered + assert "border" in rendered + + +def test_layout_property_help_text(styling_context): + rendered = render(layout_property_help_text("layout", styling_context)) + assert "Invalid value for" in rendered + assert "layout" in rendered + + +def test_docks_property_help_text(styling_context): + rendered = render(docks_property_help_text("docks", styling_context)) + assert "Invalid value for" in rendered + assert "docks" in rendered + + +def test_dock_property_help_text(styling_context): + rendered = render(dock_property_help_text("dock", styling_context)) + assert "Invalid value for" in rendered + assert "dock" in rendered + + +def test_fractional_property_help_text(styling_context): + rendered = render(fractional_property_help_text("opacity", styling_context)) + assert "Invalid value for" in rendered + assert "opacity" in rendered + + +def test_offset_property_help_text(styling_context): + rendered = render(offset_property_help_text(styling_context)) + assert "Invalid value for" in rendered + assert "offset" in rendered + + +def test_align_help_text(): + rendered = render(align_help_text()) + assert "Invalid value for" in rendered + assert "align" in rendered + + +def test_offset_single_axis_help_text(): + rendered = render(offset_single_axis_help_text("offset-x")) + assert "Invalid value for" in rendered + assert "offset-x" in rendered + + +def test_style_flags_property_help_text(styling_context): + rendered = render(style_flags_property_help_text("text-style", "notavalue b", styling_context)) + assert "Invalid value" in rendered + assert "notavalue" in rendered + + if styling_context == "css": + assert "text-style" in rendered + else: + assert "text_style" in rendered