Skip to content

Commit 7c7c579

Browse files
committed
Add more alignment tests
1 parent 7cf0565 commit 7c7c579

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

table2ascii/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ def __init__(
6969

7070
self.__alignments = alignments or [Alignment.CENTER] * self.__columns
7171

72+
# check if alignments specified have a different number of columns
73+
if alignments and len(alignments) != self.__columns:
74+
raise ValueError(
75+
"Length of `alignments` list must equal the number of columns"
76+
)
77+
7278
"""
7379
╔═════╦═══════════════════════╗ ABBBBBCBBBBBDBBBBBDBBBBBDBBBBBE
7480
║ # ║ G H R S ║ F G H H H F

tests/test_alignments.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from table2ascii import table2ascii as t2a, Alignment
22

3+
import pytest
4+
35

46
def test_first_left_four_right():
57
text = t2a(
@@ -20,3 +22,25 @@ def test_first_left_four_right():
2022
"╚═════╩═══════════════════════╝\n"
2123
)
2224
assert text == expected
25+
26+
27+
def test_wrong_number_alignments():
28+
with pytest.raises(ValueError):
29+
t2a(
30+
header=["#", "G", "H", "R", "S"],
31+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
32+
footer=["SUM", "130", "140", "135", "130"],
33+
first_col_heading=True,
34+
alignments=[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT],
35+
)
36+
37+
38+
def test_invalid_alignments():
39+
with pytest.raises(ValueError):
40+
t2a(
41+
header=["#", "G", "H", "R", "S"],
42+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
43+
footer=["SUM", "130", "140", "135", "130"],
44+
first_col_heading=True,
45+
alignments=[9999, -1, Alignment.RIGHT],
46+
)

0 commit comments

Comments
 (0)