Testing for help text

This commit is contained in:
Darren Burns
2022-04-27 13:55:29 +01:00
parent c2c29222e5
commit 91783b7c1e
2 changed files with 138 additions and 17 deletions

View File

@@ -46,7 +46,7 @@ class ContextSpecificBullets:
inline: Iterable[Bullet] inline: Iterable[Bullet]
css: 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 """Get the information associated with the given context
Args: Args:
@@ -54,10 +54,8 @@ class ContextSpecificBullets:
""" """
if context == "inline": if context == "inline":
return list(self.inline) return list(self.inline)
elif context == "css":
return list(self.css)
else: else:
return list(self.inline) + list(self.css) return list(self.css)
def _python_name(property_name: str) -> str: def _python_name(property_name: str) -> str:
@@ -85,7 +83,8 @@ def _css_name(property_name: str) -> str:
def _contextualize_property_name( def _contextualize_property_name(
property_name: str, context: StylingContext | None property_name: str,
context: StylingContext,
) -> str: ) -> str:
"""Convert a property name to CSS or inline by replacing """Convert a property name to CSS or inline by replacing
'-' with '_' or vice-versa '-' with '_' or vice-versa
@@ -97,13 +96,7 @@ def _contextualize_property_name(
Returns: Returns:
str: The property name converted to the given context. 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 (
_css_name(property_name)
if context == "css"
else _python_name(property_name)
)
return property_name
def _spacing_examples(property_name: str) -> ContextSpecificBullets: def _spacing_examples(property_name: str) -> ContextSpecificBullets:
@@ -144,7 +137,7 @@ def _spacing_examples(property_name: str) -> ContextSpecificBullets:
def spacing_wrong_number_of_values( def spacing_wrong_number_of_values(
property_name: str, property_name: str,
num_values_supplied: int, num_values_supplied: int,
context: StylingContext | None = None, context: StylingContext,
) -> HelpText: ) -> HelpText:
"""Help text to show when the user supplies the wrong number of values """Help text to show when the user supplies the wrong number of values
for a spacing property (e.g. padding or margin). for a spacing property (e.g. padding or margin).
@@ -173,7 +166,8 @@ def spacing_wrong_number_of_values(
def spacing_invalid_value( def spacing_invalid_value(
property_name: str, context: StylingContext | None = None property_name: str,
context: StylingContext,
) -> HelpText: ) -> HelpText:
"""Help text to show when the user supplies an invalid value for a spacing """Help text to show when the user supplies an invalid value for a spacing
property. property.
@@ -193,7 +187,8 @@ def spacing_invalid_value(
def scalar_help_text( def scalar_help_text(
property_name: str, context: StylingContext | None = None property_name: str,
context: StylingContext,
) -> HelpText: ) -> HelpText:
"""Help text to show when the user supplies an invalid value for """Help text to show when the user supplies an invalid value for
a scalar property. a scalar property.
@@ -240,7 +235,9 @@ def scalar_help_text(
def string_enum_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: ) -> HelpText:
"""Help text to show when the user supplies an invalid value for a string """Help text to show when the user supplies an invalid value for a string
enum property. enum property.
@@ -285,7 +282,8 @@ def string_enum_help_text(
def color_property_help_text( def color_property_help_text(
property_name: str, context: StylingContext | None = None property_name: str,
context: StylingContext,
) -> HelpText: ) -> HelpText:
"""Help text to show when the user supplies an invalid value for a color """Help text to show when the user supplies an invalid value for a color
property. For example, an unparseable color string. property. For example, an unparseable color string.

123
tests/css/test_help_text.py Normal file
View File

@@ -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