Back to Repositories

Testing Bar Component Rendering and Measurement in Textualize/rich

This test suite validates the functionality of the Bar component in the Rich library, focusing on rendering progress bars and measuring their dimensions. It ensures proper bar visualization and handling of edge cases in terminal output.

Test Coverage Overview

The test suite provides comprehensive coverage of the Bar component’s core functionality.

  • Validates bar representation and string formatting
  • Tests bar rendering with different size and position parameters
  • Verifies measurement calculations for console display
  • Handles edge cases like zero-size bars and invalid ranges

Implementation Analysis

The testing approach employs pytest fixtures and assertion-based validation to verify Bar component behavior.

  • Uses render helper function for consistent output testing
  • Implements console width calculations and ANSI escape sequences
  • Validates both visual representation and internal measurements

Technical Details

  • Testing Framework: pytest
  • Rich Console integration for terminal rendering
  • ANSI escape sequence validation
  • Custom render utility for output verification
  • Console measurement calculations

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test cases and comprehensive validation.

  • Separate test cases for different functionality aspects
  • Edge case handling and zero-value testing
  • Expected output validation using predefined constants
  • Clear test method naming and organization

textualize/rich

tests/test_block_bar.py

            
from rich.bar import Bar
from rich.console import Console

from .render import render


expected = [
    "\x1b[39;49m     ▐█████████████████████████                   \x1b[0m
",
    "\x1b[39;49m      ██████████████████████▌                     \x1b[0m
",
    "\x1b[39;49m                                                  \x1b[0m
",
]


def test_repr():
    bar = Bar(size=100, begin=11, end=62, width=50)
    assert repr(bar) == "Bar(100, 11, 62)"


def test_render():
    bar = Bar(size=100, begin=11, end=62, width=50)
    bar_render = render(bar)
    assert bar_render == expected[0]
    bar = Bar(size=100, begin=12, end=57, width=50)
    bar_render = render(bar)
    assert bar_render == expected[1]
    # begin after end
    bar = Bar(size=100, begin=60, end=40, width=50)
    bar_render = render(bar)
    assert bar_render == expected[2]


def test_measure():
    console = Console(width=120)
    bar = Bar(size=100, begin=11, end=62)
    measurement = bar.__rich_measure__(console, console.options)
    assert measurement.minimum == 4
    assert measurement.maximum == 120


def test_zero_total():
    # Shouldn't throw zero division error
    bar = Bar(size=0, begin=0, end=0)
    render(bar)


if __name__ == "__main__":
    bar = Bar(size=100, begin=11, end=62, width=50)
    bar_render = render(bar)
    print(repr(bar_render))
    bar = Bar(size=100, begin=12, end=57, width=50)
    bar_render = render(bar)
    print(repr(bar_render))
    bar = Bar(size=100, begin=60, end=40, width=50)
    bar_render = render(bar)
    print(repr(bar_render))