Skip to content

Commit d0f7e02

Browse files
committed
Put Align in enum
1 parent 325e8bf commit d0f7e02

File tree

2 files changed

+176
-8
lines changed

2 files changed

+176
-8
lines changed

table2ascii/__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import List, Optional, Union
22
from math import floor, ceil
3+
import enum
34

4-
# constants
5-
ALIGN_LEFT = 0
6-
ALIGN_CENTER = 1
7-
ALIGN_RIGHT = 2
5+
6+
class Align(enum.Enum):
7+
LEFT = 0
8+
RIGHT = 1
9+
CENTER = 2
810

911

1012
class TableToAscii:
@@ -100,17 +102,17 @@ def __get_column_widths(self) -> List[int]:
100102
col_counts.append(max(header_size, *body_size, footer_size) + 2)
101103
return col_counts
102104

103-
def __pad(self, text: str, width: int, alignment: int = ALIGN_CENTER):
105+
def __pad(self, text: str, width: int, alignment: Align = Align.CENTER):
104106
"""Pad a string of text to a given width with specified alignment"""
105-
if alignment == ALIGN_LEFT:
107+
if alignment == Align.LEFT:
106108
# pad with spaces on the end
107109
return f" {text} " + (" " * (width - len(text) - 2))
108-
if alignment == ALIGN_CENTER:
110+
if alignment == Align.CENTER:
109111
# pad with spaces, half on each side
110112
before = " " * floor((width - len(text) - 2) / 2)
111113
after = " " * ceil((width - len(text) - 2) / 2)
112114
return before + f" {text} " + after
113-
if alignment == ALIGN_RIGHT:
115+
if alignment == Align.RIGHT:
114116
# pad with spaces at the beginning
115117
return (" " * (width - len(text) - 2)) + f" {text} "
116118
raise ValueError(f"The value '{alignment}' is not valid for alignment.")

tests/test_heading.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
from table2ascii import table2ascii as t2a
2+
3+
import pytest
4+
5+
6+
def test_header_body_footer():
7+
text = t2a(
8+
header=["#", "G", "H", "R", "S"],
9+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
10+
footer=["SUM", "130", "140", "135", "130"],
11+
)
12+
expected = (
13+
"╔═════╦═══════════════════════╗\n"
14+
"║ # ║ G H R S ║\n"
15+
"╟─────╫───────────────────────╢\n"
16+
"║ 1 ║ 30 40 35 30 ║\n"
17+
"║ 2 ║ 30 40 35 30 ║\n"
18+
"╟─────╫───────────────────────╢\n"
19+
"║ SUM ║ 130 140 135 130 ║\n"
20+
"╚═════╩═══════════════════════╝\n"
21+
)
22+
assert text == expected
23+
24+
25+
def test_body_footer():
26+
text = t2a(
27+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
28+
footer=["SUM", "130", "140", "135", "130"],
29+
)
30+
expected = (
31+
"╔═════╦═══════════════════════╗\n"
32+
"║ 1 ║ 30 40 35 30 ║\n"
33+
"║ 2 ║ 30 40 35 30 ║\n"
34+
"╟─────╫───────────────────────╢\n"
35+
"║ SUM ║ 130 140 135 130 ║\n"
36+
"╚═════╩═══════════════════════╝\n"
37+
)
38+
assert text == expected
39+
40+
41+
def test_header_body():
42+
text = t2a(
43+
header=["#", "G", "H", "R", "S"],
44+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
45+
)
46+
expected = (
47+
"╔═══╦═══════════════════╗\n"
48+
"║ # ║ G H R S ║\n"
49+
"╟───╫───────────────────╢\n"
50+
"║ 1 ║ 30 40 35 30 ║\n"
51+
"║ 2 ║ 30 40 35 30 ║\n"
52+
"╚═══╩═══════════════════╝\n"
53+
)
54+
assert text == expected
55+
56+
57+
def test_header_footer():
58+
text = t2a(
59+
header=["#", "G", "H", "R", "S"],
60+
footer=["SUM", "130", "140", "135", "130"],
61+
)
62+
expected = (
63+
"╔═════╦═══════════════════════╗\n"
64+
"║ # ║ G H R S ║\n"
65+
"╟─────╫───────────────────────╢\n"
66+
"╟─────╫───────────────────────╢\n"
67+
"║ SUM ║ 130 140 135 130 ║\n"
68+
"╚═════╩═══════════════════════╝\n"
69+
)
70+
assert text == expected
71+
72+
73+
def test_header():
74+
text = t2a(
75+
header=["#", "G", "H", "R", "S"],
76+
)
77+
expected = (
78+
"╔═══╦═══════════════╗\n"
79+
"║ # ║ G H R S ║\n"
80+
"╟───╫───────────────╢\n"
81+
"╚═══╩═══════════════╝\n"
82+
)
83+
assert text == expected
84+
85+
86+
def test_body():
87+
text = t2a(
88+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
89+
)
90+
expected = (
91+
"╔═══╦═══════════════════╗\n"
92+
"║ 1 ║ 30 40 35 30 ║\n"
93+
"║ 2 ║ 30 40 35 30 ║\n"
94+
"╚═══╩═══════════════════╝\n"
95+
)
96+
assert text == expected
97+
98+
99+
def test_footer():
100+
text = t2a(
101+
footer=["SUM", "130", "140", "135", "130"],
102+
)
103+
expected = (
104+
"╔═════╦═══════════════════════╗\n"
105+
"╟─────╫───────────────────────╢\n"
106+
"║ SUM ║ 130 140 135 130 ║\n"
107+
"╚═════╩═══════════════════════╝\n"
108+
)
109+
assert text == expected
110+
111+
112+
def test_header_footer_unequal():
113+
with pytest.raises(ValueError):
114+
t2a(
115+
header=["H", "R", "S"],
116+
footer=["SUM", "130", "140", "135", "130"],
117+
)
118+
119+
120+
def test_header_body_unequal():
121+
with pytest.raises(ValueError):
122+
t2a(
123+
header=["#", "G", "H", "R", "S"],
124+
body=[
125+
["0", "45", "30", "32", "28"],
126+
["1", "30", "40", "35", "30", "36"],
127+
["2", "30", "40", "35", "30"],
128+
],
129+
)
130+
131+
132+
def test_footer_body_unequal():
133+
with pytest.raises(ValueError):
134+
t2a(
135+
body=[
136+
["0", "45", "30", "32", "28"],
137+
["1", "30", "40", "35", "30"],
138+
["2", "30", "40", "35", "30"],
139+
],
140+
footer=["SUM", "130", "140", "135", "130", "36"],
141+
)
142+
143+
144+
def test_empty_header():
145+
text = t2a(
146+
header=[],
147+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
148+
)
149+
expected = (
150+
"╔═══╦═══════════════════╗\n"
151+
"║ 1 ║ 30 40 35 30 ║\n"
152+
"║ 2 ║ 30 40 35 30 ║\n"
153+
"╚═══╩═══════════════════╝\n"
154+
)
155+
assert text == expected
156+
157+
158+
def test_empty_body():
159+
text = t2a(header=["#", "G", "H", "R", "S"], body=[])
160+
expected = (
161+
"╔═══╦═══════════════╗\n"
162+
"║ # ║ G H R S ║\n"
163+
"╟───╫───────────────╢\n"
164+
"╚═══╩═══════════════╝\n"
165+
)
166+
assert text == expected

0 commit comments

Comments
 (0)