Files
pytest2/testing/io/test_wcwidth.py
Ran Benita aca534c67d Improve our own wcwidth implementation and remove dependency on wcwidth package
`TerminalWriter`, imported recently from `py`, contains its own
incomplete wcwidth (`char_with`/`get_line_width`) implementation. The
`TerminalReporter` also needs this, but uses the external `wcwidth`
package.

This commit brings the `TerminalWriter` implementation up-to-par with
`wcwidth`, moves to implementation to a new file `_pytest._io.wcwidth`
which is used everywhere, and removes the dependency.

The differences compared to the `wcwidth` package are:

- Normalizes the string before counting.

- Uses Python's `unicodedata` instead of vendored Unicode tables. This
  means the data corresponds to the Python's version Unicode version
  instead of the `wcwidth`'s package version.

- Apply some optimizations.
2020-05-26 17:14:01 +03:00

39 lines
809 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pytest
from _pytest._io.wcwidth import wcswidth
from _pytest._io.wcwidth import wcwidth
@pytest.mark.parametrize(
("c", "expected"),
[
("\0", 0),
("\n", -1),
("a", 1),
("1", 1),
("א", 1),
("\u200B", 0),
("\u1ABE", 0),
("\u0591", 0),
("🉐", 2),
("", 2),
],
)
def test_wcwidth(c: str, expected: int) -> None:
assert wcwidth(c) == expected
@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