Back to Repositories

Testing Column Alignment Rendering Features in Textualize/rich

This test suite validates the column alignment functionality in Rich’s Columns component, focusing on different alignment options (none, left, center, right) for panel content rendering.

Test Coverage Overview

The test suite provides comprehensive coverage of the Columns alignment feature in Rich.

Key areas tested include:
  • Default alignment behavior (no align specified)
  • Left alignment of column content
  • Center alignment of column content
  • Right alignment of column content
The test verifies exact output formatting including spacing and borders.

Implementation Analysis

The testing approach uses a render() function to generate formatted output with different alignment settings. It leverages Rich’s Console, Panel, and Columns components to create a controlled test environment.

The implementation uses string comparison for verification, checking exact character-level output matching including whitespace and box-drawing characters.

Technical Details

Testing tools and configuration:
  • Rich’s Console with controlled width (100 chars)
  • StringIO for output capture
  • Panel.fit with SQUARE box style
  • Columns component with expand=True
  • Python’s built-in assert for output validation

Best Practices Demonstrated

The test exhibits several quality testing practices:
  • Isolated test environment with fixed console width
  • Comprehensive coverage of all alignment options
  • Exact output validation
  • Clear separation of rendering and assertion logic
  • Representative test data with visual verification

textualize/rich

tests/test_columns_align.py

            
# encoding=utf-8

import io

from rich import box
from rich.columns import Columns
from rich.console import Console
from rich.panel import Panel


def render():
    console = Console(file=io.StringIO(), width=100, legacy_windows=False)
    panel = Panel.fit("foo", box=box.SQUARE, padding=0)
    columns = Columns([panel] * 4)
    columns.expand = True
    console.rule("no align")
    console.print(columns)

    columns.align = "left"
    console.rule("left align")
    console.print(columns)

    columns.align = "center"
    console.rule("center align")
    console.print(columns)

    columns.align = "right"
    console.rule("right align")
    console.print(columns)

    return console.file.getvalue()


def test_align():
    result = render()
    expected = "───────────────────────────────────────────── no align ─────────────────────────────────────────────
┌───┐                      ┌───┐                     ┌───┐                     ┌───┐                
│foo│                      │foo│                     │foo│                     │foo│                
└───┘                      └───┘                     └───┘                     └───┘                
──────────────────────────────────────────── left align ────────────────────────────────────────────
┌───┐                      ┌───┐                     ┌───┐                     ┌───┐                
│foo│                      │foo│                     │foo│                     │foo│                
└───┘                      └───┘                     └───┘                     └───┘                
─────────────────────────────────────────── center align ───────────────────────────────────────────
          ┌───┐                      ┌───┐                     ┌───┐                   ┌───┐        
          │foo│                      │foo│                     │foo│                   │foo│        
          └───┘                      └───┘                     └───┘                   └───┘        
─────────────────────────────────────────── right align ────────────────────────────────────────────
                     ┌───┐                     ┌───┐                     ┌───┐                 ┌───┐
                     │foo│                     │foo│                     │foo│                 │foo│
                     └───┘                     └───┘                     └───┘                 └───┘
"
    assert result == expected


if __name__ == "__main__":
    rendered = render()
    print(rendered)
    print(repr(rendered))