Back to Repositories

Testing Rich Text Padding Implementation in Textualize/rich

This test suite validates the Padding functionality in the Rich library, focusing on padding text and renderables with customizable spacing. The tests cover core padding operations, styling, and console rendering capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of the Padding class functionality in Rich.

Key areas tested include:
  • Padding representation and string conversion
  • Text indentation operations
  • Padding value unpacking logic
  • Console rendering with expand options
  • Rich console integration with styling

Implementation Analysis

The testing approach uses pytest fixtures and assertions to validate padding behavior. Tests implement both basic padding operations and complex console rendering scenarios.

Notable patterns include:
  • Direct method testing with various input combinations
  • Console capture for output verification
  • Style and rendering integration tests
  • Exception handling validation

Technical Details

Testing infrastructure includes:
  • pytest as the primary testing framework
  • Rich’s Console class for output capture
  • ConsoleOptions and ConsoleDimensions for rendering tests
  • Style and Segment classes for formatting validation

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test cases and comprehensive edge case coverage.

Notable practices include:
  • Separate test functions for distinct functionality
  • Explicit assertion messages
  • Exception testing with pytest.raises
  • Mock console testing environment

textualize/rich

tests/test_padding.py

            
import pytest

from rich.padding import Padding
from rich.console import Console, ConsoleDimensions, ConsoleOptions
from rich.style import Style
from rich.segment import Segment


def test_repr():
    padding = Padding("foo", (1, 2))
    assert isinstance(repr(padding), str)


def test_indent():
    indent_result = Padding.indent("test", 4)
    assert indent_result.top == 0
    assert indent_result.right == 0
    assert indent_result.bottom == 0
    assert indent_result.left == 4


def test_unpack():
    assert Padding.unpack(3) == (3, 3, 3, 3)
    assert Padding.unpack((3,)) == (3, 3, 3, 3)
    assert Padding.unpack((3, 4)) == (3, 4, 3, 4)
    assert Padding.unpack((3, 4, 5, 6)) == (3, 4, 5, 6)
    with pytest.raises(ValueError):
        Padding.unpack((1, 2, 3))


def test_expand_false():
    console = Console(width=100, color_system=None)
    console.begin_capture()
    console.print(Padding("foo", 1, expand=False))
    assert console.end_capture() == "     
 foo 
     
"


def test_rich_console():
    renderable = "test renderable"
    style = Style(color="red")
    options = ConsoleOptions(
        ConsoleDimensions(80, 25),
        max_height=25,
        legacy_windows=False,
        min_width=10,
        max_width=20,
        is_terminal=False,
        encoding="utf-8",
    )

    expected_outputs = [
        Segment(renderable, style=style),
        Segment(" " * (20 - len(renderable)), style=style),
        Segment("
", style=None),
    ]
    padding_generator = Padding(renderable, style=style).__rich_console__(
        Console(), options
    )
    for output, expected in zip(padding_generator, expected_outputs):
        assert output == expected