Skip to content

Commit a6c2078

Browse files
committed
AppTests: Full test cov for shape
Signed-off-by: Gabe Goodhart <[email protected]>
1 parent e7e12cb commit a6c2078

File tree

1 file changed

+138
-2
lines changed

1 file changed

+138
-2
lines changed

tests/test_shape.py

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,141 @@ def test_progress_bar():
2727
shape.progress_bar(0.5, 10, done_char="xx")
2828

2929

30-
def test_box():
31-
"""Test various"""
30+
@pytest.mark.parametrize(
31+
["x", "kwargs", "exp_text_lines"],
32+
[
33+
("Hello World", {}, 1),
34+
("Hello World", {"char": "*"}, 1),
35+
("Hello World", {"width": 9}, 2),
36+
],
37+
)
38+
def test_box(x, kwargs, exp_text_lines):
39+
"""Test various configs for boxes"""
40+
box = shape.box(x, **kwargs)
41+
box_lines = box.strip().split("\n")
42+
assert len(box_lines) == exp_text_lines + 2
43+
assert box_lines[0]
44+
frame_char = box_lines[0][0]
45+
assert all(
46+
set([ch for ch in line]) == {frame_char}
47+
for line in [box_lines[0], box_lines[-1]]
48+
)
49+
assert all(
50+
line[0] == frame_char and line[-1] == frame_char for line in box_lines[1:-1]
51+
)
52+
53+
54+
@pytest.mark.parametrize(
55+
["columns", "kwargs"],
56+
[
57+
# Simple 2x3
58+
([["First", "Gabe", "Me"], ["Last", "Goodhart", "You"]], {}),
59+
# Empty element
60+
([["First", "Gabe", "Me"], ["Last", "", "You"]], {}),
61+
# Empty header
62+
([["", "Gabe", "Me"], ["Last", "", "You"]], {}),
63+
# No header
64+
([["First", "Gabe", "Me"], ["Last", "Goodhart", "You"]], {"header": False}),
65+
# No row div
66+
(
67+
[["First", "Gabe", "Me"], ["Last", "Goodhart", "You"]],
68+
{"row_dividers": False},
69+
),
70+
# No row div or header
71+
(
72+
[["First", "Gabe", "Me"], ["Last", "Goodhart", "You"]],
73+
{"row_dividers": False, "header": False},
74+
),
75+
# Custom chars
76+
(
77+
[["First", "Gabe", "Me"], ["Last", "Goodhart", "You"]],
78+
{
79+
"hframe_char": "H",
80+
"vframe_char": "V",
81+
"corner_char": "*",
82+
"header_char": "$",
83+
},
84+
),
85+
# Empty table
86+
([], {}),
87+
# Word wrapping
88+
([["Name", "My name is Gabe Goodhart"], ["Foo Bar", "Y"]], {"max_width": 15}),
89+
],
90+
)
91+
def test_table_valid(columns, kwargs):
92+
"""Test various valid configs for tables"""
93+
table = shape.table(columns, **kwargs)
94+
table_lines = table.strip().split("\n")
95+
assert len(table_lines) >= 2
96+
top_frame = table_lines[0]
97+
bottom_frame = table_lines[-1]
98+
assert len(top_frame) > 1
99+
assert len(bottom_frame) > 1
100+
corner = top_frame[0]
101+
assert kwargs.get("corner_char", corner) == corner
102+
assert {top_frame[-1], bottom_frame[0], bottom_frame[-1]} == {corner}
103+
if columns:
104+
assert len(top_frame) > 2
105+
hline_char = top_frame[1]
106+
assert kwargs.get("hframe_char", hline_char) == hline_char
107+
assert set([ch for ch in top_frame[1:-1]]) == {hline_char}
108+
assert set([ch for ch in bottom_frame[1:-1]]) == {hline_char}
109+
assert len(table_lines) > 2
110+
vline_char = table_lines[1][0]
111+
assert kwargs.get("vframe_char", vline_char) == vline_char
112+
113+
# Test the horizontal dividers
114+
do_row_divs = kwargs.get("row_dividers", True)
115+
do_header = kwargs.get("header", True)
116+
hlines = [
117+
line for line in table_lines[1:-1] if len(line) >= 2 and line[1] != " "
118+
]
119+
if not do_row_divs and not do_header:
120+
assert not hlines
121+
else:
122+
exp_hlines = len(columns[0]) - 1 if do_row_divs else 1
123+
assert len(hlines) == exp_hlines
124+
assert all(set([hline[0], hline[-1]]) == {vline_char} for hline in hlines)
125+
if do_header:
126+
header_hline = hlines[0]
127+
row_divs = hlines[1:]
128+
header_char = header_hline[2]
129+
assert kwargs.get("header_char", header_char) == header_char
130+
assert set([ch for ch in header_hline[1:-1]]) == {header_char}
131+
else:
132+
row_divs = hlines
133+
if row_divs:
134+
assert len(set(row_divs)) == 1
135+
row_div = row_divs[0]
136+
assert set([ch for ch in row_div[1:-1]]) == {hline_char}
137+
138+
# Test the vertical dividers
139+
body_lines = [
140+
line for line in table_lines[1:-1] if len(line) >= 2 and line[1] == " "
141+
]
142+
exp_vlines = len(columns) + 1
143+
assert all(
144+
line.count(vline_char) == exp_vlines and line[0] == line[-1] == vline_char
145+
for line in body_lines
146+
)
147+
148+
149+
@pytest.mark.parametrize(
150+
["columns", "kwargs"],
151+
[
152+
# No valid columns
153+
([[]], {}),
154+
# Some invalid columns
155+
([["Hi", "there"], ["", ""]], {}),
156+
# Uneven columns
157+
([["Hi", "there"], ["Hey"]], {}),
158+
# Invalid char overrides
159+
([["Hi"]], {"hframe_char": "--"}),
160+
# Column collapse
161+
([["Hi", "This is a test to see how long"], ["X", "X"]], {"max_width": 5}),
162+
],
163+
)
164+
def test_table_invalid(columns, kwargs):
165+
"""Make sure all invalid kwarg options raise ValueError"""
166+
with pytest.raises(ValueError):
167+
shape.table(columns, **kwargs)

0 commit comments

Comments
 (0)