Back to Repositories

Testing Rich Container Components for Console Text Formatting in Textualize/rich

This test suite validates the functionality of Rich library’s container components, specifically focusing on Lines and Renderables classes. The tests verify measurement, rendering, and text justification capabilities essential for Rich’s console output formatting.

Test Coverage Overview

The test suite provides comprehensive coverage of Rich’s container components with particular focus on measurement and rendering functionality.

Key areas tested include:
  • Renderables measurement and iteration
  • Empty container handling
  • Lines rendering to console
  • Text justification with different alignment options (left, center, right, full)

Implementation Analysis

The testing approach uses pytest framework with isolated test cases for each container feature. The implementation demonstrates proper unit testing patterns by:

  • Creating Console instances for each test
  • Validating both simple and complex text formatting scenarios
  • Testing edge cases like empty containers
  • Verifying text alignment and spacing calculations

Technical Details

Testing tools and components include:

  • Rich Console class for output handling
  • Text and Span classes for content formatting
  • Style class for text styling
  • Lines and Renderables container classes
  • pytest assertions for validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including:

  • Isolated test functions with clear naming
  • Comprehensive assertion checking
  • Testing of both normal and edge cases
  • Proper setup and teardown of test resources
  • Verification of visual formatting and measurements

textualize/rich

tests/test_containers.py

            
from rich.console import Console
from rich.containers import Lines, Renderables
from rich.text import Span, Text
from rich.style import Style


def test_renderables_measure():
    console = Console()
    text = Text("foo")
    renderables = Renderables([text])

    result = renderables.__rich_measure__(console, console.options)
    _min, _max = result
    assert _min == 3
    assert _max == 3

    assert list(renderables) == [text]


def test_renderables_empty():
    console = Console()
    renderables = Renderables()

    result = renderables.__rich_measure__(console, console.options)
    _min, _max = result
    assert _min == 1
    assert _max == 1


def test_lines_rich_console():
    console = Console()
    lines = Lines([Text("foo")])

    result = list(lines.__rich_console__(console, console.options))
    assert result == [Text("foo")]


def test_lines_justify():
    console = Console()
    lines1 = Lines([Text("foo", style="b"), Text("test", style="b")])
    lines1.justify(console, 10, justify="left")
    assert lines1._lines == [Text("foo       "), Text("test      ")]
    lines1.justify(console, 10, justify="center")
    assert lines1._lines == [Text("   foo    "), Text("   test   ")]
    lines1.justify(console, 10, justify="right")
    assert lines1._lines == [Text("       foo"), Text("      test")]

    lines2 = Lines([Text("foo bar", style="b"), Text("test", style="b")])
    lines2.justify(console, 7, justify="full")
    print(repr(lines2._lines[0].spans))
    assert lines2._lines == [
        Text(
            "foo bar",
            spans=[Span(0, 3, "b"), Span(3, 4, Style.parse("bold")), Span(4, 7, "b")],
        ),
        Text("test"),
    ]