Skip to content

Commit

Permalink
Add type annotations to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Oct 10, 2023
1 parent 159bf75 commit ff8c975
Show file tree
Hide file tree
Showing 24 changed files with 355 additions and 334 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
v0.13.0 (in development)
v0.12.1 (in development)
------------------------
- Support Python 3.9 through 3.12
- Drop support for Python 3.6
- Add type annotations to tests
- Fix mypy's type inference for `headers`

v0.12.0 (2020-10-07)
--------------------
Expand Down
2 changes: 1 addition & 1 deletion src/txtble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
)
from .util import with_color_stripped

__version__ = "0.13.0.dev1"
__version__ = "0.12.1.dev1"
__author__ = "John Thorvald Wodder II"
__author_email__ = "[email protected]"
__license__ = "MIT"
Expand Down
11 changes: 8 additions & 3 deletions src/txtble/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def data_converter(value: Iterable[Iterable | Mapping]) -> list[list | Mapping]:
return data


def headers_converter(value: Iterable | None) -> list | None:
if value is not None:
return list(value)
else:
return None


@attr.s(auto_attribs=True)
class Txtble:
data: list[list | Mapping] = attr.ib(default=(), converter=data_converter)
Expand All @@ -48,9 +55,7 @@ class Txtble:
dict_fill: Any = DICT_FILL_RAISE
header_border: bool | BorderStyle | None = None
header_fill: Any = None
headers: Optional[list] = attr.ib(
default=None, converter=attr.converters.optional(list)
)
headers: Optional[list] = attr.ib(default=None, converter=headers_converter)
left_border: bool | BorderStyle | None = None
left_padding: int | str | None = None
len_func: Optional[LenFunc] = strwidth
Expand Down
28 changes: 14 additions & 14 deletions test/test_align.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from __future__ import annotations
import pytest
from test_data import DATA, HEADERS, TABLE
from txtble import Txtble


def test_align_lll():
def test_align_lll() -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["l", "l", "l"])
assert str(tbl) == TABLE


def test_align_ccc():
def test_align_ccc() -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["c", "c", "c"])
assert str(tbl) == (
"+---------+----------+------------------+\n"
Expand All @@ -30,7 +31,7 @@ def test_align_ccc():
)


def test_align_rrr():
def test_align_rrr() -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["r", "r", "r"])
assert str(tbl) == (
"+---------+----------+------------------+\n"
Expand All @@ -52,7 +53,7 @@ def test_align_rrr():
)


def test_align_rcl():
def test_align_rcl() -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["r", "c", "l"])
assert str(tbl) == (
"+---------+----------+------------------+\n"
Expand All @@ -74,7 +75,7 @@ def test_align_rcl():
)


def test_align_ccc_right_padding():
def test_align_ccc_right_padding() -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["c", "c", "c"], right_padding=2)
assert str(tbl) == (
"+-----------+------------+--------------------+\n"
Expand All @@ -96,7 +97,7 @@ def test_align_ccc_right_padding():
)


def test_align_extra_columns():
def test_align_extra_columns() -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["c", "c"])
assert str(tbl) == (
"+---------+----------+------------------+\n"
Expand All @@ -118,7 +119,7 @@ def test_align_extra_columns():
)


def test_align_extra_columns_align_fill():
def test_align_extra_columns_align_fill() -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["c", "c"], align_fill="r")
assert str(tbl) == (
"+---------+----------+------------------+\n"
Expand All @@ -140,7 +141,7 @@ def test_align_extra_columns_align_fill():
)


def test_align_extra_aligns():
def test_align_extra_aligns() -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["r", "c", "l", "c", "r"])
assert str(tbl) == (
"+---------+----------+------------------+\n"
Expand All @@ -163,22 +164,21 @@ def test_align_extra_aligns():


@pytest.mark.parametrize("align", ["q", "L", "left", "<"])
def test_bad_align(align):
def test_bad_align(align: str) -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["r", "c", align])
with pytest.raises(ValueError, match="invalid alignment specifier"):
str(tbl)


@pytest.mark.parametrize("align", ["q", "L", "left", "<"])
def test_bad_align_fill(align):
def test_bad_align_fill(align: str) -> None:
tbl = Txtble(DATA, headers=HEADERS, align=["c", "c"], align_fill=align)
with pytest.raises(ValueError, match="invalid alignment specifier"):
str(tbl)


@pytest.mark.parametrize("align_fill", [None, "l"])
def test_align_all_c(align_fill):
tbl = Txtble(DATA, headers=HEADERS, align="c", align_fill=align_fill)
def test_align_all_c() -> None:
tbl = Txtble(DATA, headers=HEADERS, align="c")
assert str(tbl) == (
"+---------+----------+------------------+\n"
"| Month |Birthstone| Birth Flower |\n"
Expand All @@ -199,7 +199,7 @@ def test_align_all_c(align_fill):
)


def test_align_fill_c():
def test_align_fill_c() -> None:
tbl = Txtble(DATA, headers=HEADERS, align_fill="c")
assert str(tbl) == (
"+---------+----------+------------------+\n"
Expand Down
19 changes: 10 additions & 9 deletions test/test_ansi_colors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
import pytest
from wcwidth import wcswidth
from test_data import DATA, HEADERS
Expand All @@ -20,11 +21,11 @@


@pytest.mark.parametrize("colored,plain", COLORED_STRINGS)
def test_color_aware_len(colored, plain):
def test_color_aware_len(colored: str, plain: str) -> None:
assert strwidth(colored) == wcswidth(plain)


def test_bad_color_aware_len():
def test_bad_color_aware_len() -> None:
s = "\033[1mNo terminating sgr0"
with pytest.raises(UnterminatedColorError) as excinfo:
strwidth(s)
Expand All @@ -33,7 +34,7 @@ def test_bad_color_aware_len():


@pytest.mark.parametrize("colored,plain", COLORED_STRINGS)
def test_colored_text(colored, plain):
def test_colored_text(colored: str, plain: str) -> None:
tbl = Txtble(data=[[colored], [plain]])
w = wcswidth(plain)
assert str(tbl) == (
Expand All @@ -52,7 +53,7 @@ def test_colored_text(colored, plain):
)


def test_multiline_colors():
def test_multiline_colors() -> None:
tbl = Txtble(
[
[
Expand All @@ -79,7 +80,7 @@ def test_multiline_colors():
)


def test_multiline_long_colors():
def test_multiline_long_colors() -> None:
tbl = Txtble(
[
[
Expand All @@ -106,7 +107,7 @@ def test_multiline_long_colors():
)


def test_color_none_str():
def test_color_none_str() -> None:
tbl = Txtble(
headers=("repr", "value"),
data=[
Expand All @@ -128,7 +129,7 @@ def test_color_none_str():
)


def test_color_row_fill():
def test_color_row_fill() -> None:
tbl = Txtble(
[
["1", "1"],
Expand All @@ -153,7 +154,7 @@ def test_color_row_fill():
)


def test_color_header_fill():
def test_color_header_fill() -> None:
tbl = Txtble(
[
["1", "1"],
Expand All @@ -179,7 +180,7 @@ def test_color_header_fill():
)


def test_color_padding():
def test_color_padding() -> None:
tbl = Txtble(DATA, headers=HEADERS, padding="\033[31m-\033[m")
assert str(tbl) == (
"+-----------+------------+--------------------+\n"
Expand Down
9 changes: 5 additions & 4 deletions test/test_ansi_regexes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
import re
import pytest
from txtble.util import COLOR_BEGIN_RGX, COLOR_END_RGX
Expand Down Expand Up @@ -30,20 +31,20 @@


@pytest.mark.parametrize("s", BEGIN_COLORS)
def test_color_begin_rgx(s):
def test_color_begin_rgx(s: str) -> None:
assert bool(re.match("^" + COLOR_BEGIN_RGX + "$", s))


@pytest.mark.parametrize("s", END_COLORS)
def test_color_end_rgx(s):
def test_color_end_rgx(s: str) -> None:
assert bool(re.match("^" + COLOR_END_RGX + "$", s))


@pytest.mark.parametrize("s", NOT_COLORS + END_COLORS)
def test_bad_color_begin_rgx(s):
def test_bad_color_begin_rgx(s: str) -> None:
assert re.match("^" + COLOR_BEGIN_RGX + "$", s) is None


@pytest.mark.parametrize("s", NOT_COLORS + BEGIN_COLORS)
def test_bad_color_end_rgx(s):
def test_bad_color_end_rgx(s: str) -> None:
assert re.match("^" + COLOR_END_RGX + "$", s) is None
15 changes: 8 additions & 7 deletions test/test_bad_width.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations
import pytest
from txtble import IndeterminateWidthError, Txtble

BAD_STRING = "\x01"
ERRMSG = repr(BAD_STRING) + ": string has indeterminate width"
ERRMSG = f"{BAD_STRING!r}: string has indeterminate width"


@pytest.mark.parametrize(
Expand All @@ -21,7 +22,7 @@
"\033[?1049l", # altscreen off
],
)
def test_indeterminate_cell(s):
def test_indeterminate_cell(s: str) -> None:
tbl = Txtble(
headers=["Header", "Header"],
data=[["A", "B"], [s, "D"]],
Expand All @@ -32,7 +33,7 @@ def test_indeterminate_cell(s):
assert str(excinfo.value) == repr(s) + ": string has indeterminate width"


def test_indeterminate_header():
def test_indeterminate_header() -> None:
tbl = Txtble(
headers=["Header", BAD_STRING],
data=[["A", "B"], ["C", "D"]],
Expand All @@ -43,7 +44,7 @@ def test_indeterminate_header():
assert str(excinfo.value) == ERRMSG


def test_indeterminate_header_fill():
def test_indeterminate_header_fill() -> None:
tbl = Txtble(
headers=["Header"],
header_fill=BAD_STRING,
Expand All @@ -55,7 +56,7 @@ def test_indeterminate_header_fill():
assert str(excinfo.value) == ERRMSG


def test_indeterminate_row_fill():
def test_indeterminate_row_fill() -> None:
tbl = Txtble(
headers=["Header", "Header"],
data=[["A", "B"], ["C"]],
Expand All @@ -67,7 +68,7 @@ def test_indeterminate_row_fill():
assert str(excinfo.value) == ERRMSG


def test_indeterminate_none_str():
def test_indeterminate_none_str() -> None:
tbl = Txtble(
headers=["Header", "Header"],
data=[["A", "B"], ["C", None]],
Expand All @@ -79,7 +80,7 @@ def test_indeterminate_none_str():
assert str(excinfo.value) == ERRMSG


def test_indeterminate_padding():
def test_indeterminate_padding() -> None:
tbl = Txtble(
headers=["Header", "Header"],
data=[["A", "B"], ["C", "D"]],
Expand Down
Loading

0 comments on commit ff8c975

Please sign in to comment.