test_wcwidth.py

Overview

The [test_wcwidth.py](/projects/286/67408) file contains automated tests for verifying the correctness of two functions, `wcwidth` and `wcswidth`, imported from the `_pytest._io.wcwidth` module. These functions are used to measure the display width of Unicode characters and strings, respectively. The file ensures that these functions correctly compute the terminal column width of individual characters and entire strings, including handling special cases like control characters, zero-width characters, and wide characters (e.g., East Asian full-width characters and emojis).

This test file leverages the `pytest` framework for parametrized testing, providing multiple input cases and their expected output widths to validate the behavior of the width calculation functions.


Detailed Explanation

Imports


Function: test_wcwidth

@pytest.mark.parametrize(
    ("c", "expected"),
    [
        ("\0", 0),
        ("\n", -1),
        ("a", 1),
        ("1", 1),
        ("א", 1),
        ("\u200b", 0),
        ("\u1abe", 0),
        ("\u0591", 0),
        ("🉐", 2),
        ("$", 2),  # noqa: RUF001
    ],
)
def test_wcwidth(c: str, expected: int) -> None:
    assert wcwidth(c) == expected

Function: test_wcswidth

@pytest.mark.parametrize(
    ("s", "expected"),
    [
        ("", 0),
        ("hello, world!", 13),
        ("hello, world!\n", -1),
        ("0123456789", 10),
        ("שלום, עולם!", 11),
        ("שְבֻעָיים", 6),
        ("🉐🉐🉐", 6),
    ],
)
def test_wcswidth(s: str, expected: int) -> None:
    assert wcswidth(s) == expected

Implementation Details

This file does not implement these functions but tests their correctness using representative samples.


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[Test_wcwidth.py]
    A --> B[wcwidth function]
    A --> C[wcswidth function]

    B --> D[Input: single character (str)]
    B --> E[Output: int (width or -1)]
    C --> F[Input: string (str)]
    C --> G[Output: int (total width or -1)]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333
    style C fill:#bbf,stroke:#333

Summary

[test_wcwidth.py](/projects/286/67408) is a utility test module that validates the behavior of Unicode character width calculation functions crucial for proper terminal display in pytest. By covering a wide range of character types, including control, combining, and wide characters, it ensures reliable width computations that help maintain a consistent and readable test output interface.


If you need further details or examples, feel free to ask!