-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_alignments.py
156 lines (139 loc) · 7.55 KB
/
test_alignments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import pytest
from table2ascii import Alignment, PresetStyle, table2ascii as t2a
from table2ascii.exceptions import AlignmentCountMismatchError, InvalidAlignmentError
def test_first_left_four_right():
text = t2a(
header=["#", "G", "H", "R", "S"],
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
footer=["SUM", "130", "140", "135", "130"],
first_col_heading=True,
alignments=[Alignment.LEFT] + [Alignment.RIGHT] * 4,
)
expected = (
"╔═════╦═══════════════════════╗\n"
"║ # ║ G H R S ║\n"
"╟─────╫───────────────────────╢\n"
"║ 1 ║ 30 40 35 30 ║\n"
"║ 2 ║ 30 40 35 30 ║\n"
"╟─────╫───────────────────────╢\n"
"║ SUM ║ 130 140 135 130 ║\n"
"╚═════╩═══════════════════════╝"
)
assert text == expected
def test_wrong_number_alignments():
with pytest.raises(AlignmentCountMismatchError):
t2a(
header=["#", "G", "H", "R", "S"],
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
footer=["SUM", "130", "140", "135", "130"],
first_col_heading=True,
alignments=[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT],
)
def test_invalid_alignments():
with pytest.raises(InvalidAlignmentError):
t2a(
header=["#", "G", "H", "R", "S"],
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
footer=["SUM", "130", "140", "135", "130"],
first_col_heading=True,
alignments=[9999, -1, Alignment.RIGHT, Alignment.CENTER, Alignment.RIGHT], # type: ignore
)
def test_alignment_numeric_data():
text = t2a(
header=[1, "G", "H", "R", "S"],
body=[[1, 2, 3, 4, 5]],
footer=["A", "B", 1, 2, 3],
column_widths=[4, 5, 5, 4, 5],
alignments=[Alignment.RIGHT] + [Alignment.CENTER] * 4,
first_col_heading=True,
)
expected = (
"╔════╦══════════════════════╗\n"
"║ 1 ║ G H R S ║\n"
"╟────╫──────────────────────╢\n"
"║ 1 ║ 2 3 4 5 ║\n"
"╟────╫──────────────────────╢\n"
"║ A ║ B 1 2 3 ║\n"
"╚════╩══════════════════════╝"
)
assert text == expected
def test_alignments_multiline_data():
text = t2a(
header=["Multiline\nHeader\nCell", "G", "Two\nLines", "R", "S"],
body=[[1, "Alpha\nBeta\nGamma", 3, 4, "One\nTwo"]],
footer=["A", "Footer\nBreak", 1, "Second\nCell\nBroken", 3],
alignments=[
Alignment.LEFT,
Alignment.RIGHT,
Alignment.CENTER,
Alignment.LEFT,
Alignment.CENTER,
],
)
expected = (
"╔═══════════════════════════════════════════╗\n"
"║ Multiline G Two R S ║\n"
"║ Header Lines ║\n"
"║ Cell ║\n"
"╟───────────────────────────────────────────╢\n"
"║ 1 Alpha 3 4 One ║\n"
"║ Beta Two ║\n"
"║ Gamma ║\n"
"╟───────────────────────────────────────────╢\n"
"║ A Footer 1 Second 3 ║\n"
"║ Break Cell ║\n"
"║ Broken ║\n"
"╚═══════════════════════════════════════════╝"
)
assert text == expected
def test_decimal_alignment():
text = t2a(
header=["1.1.1", "G", "Long Header", "H", "R", "3.8"],
body=[[100.00001, 2, 3.14, 33, "AB", "1.5"], [10.0001, 22.0, 2.718, 3, "CD", "3.03"]],
footer=[10000.01, "123", 10.0, 0, "E", "A"],
alignments=[Alignment.DECIMAL] * 6,
first_col_heading=True,
style=PresetStyle.double_thin_box,
)
expected = (
"╔═════════════╦═══════╤═════════════╤════╤════╤═════════╗\n"
"║ 1.1.1 ║ G │ Long Header │ H │ R │ 3.8 ║\n"
"╠═════════════╬═══════╪═════════════╪════╪════╪═════════╣\n"
"║ 100.00001 ║ 2 │ 3.14 │ 33 │ AB │ 1.5 ║\n"
"╟─────────────╫───────┼─────────────┼────┼────┼─────────╢\n"
"║ 10.0001 ║ 22.0 │ 2.718 │ 3 │ CD │ 3.03 ║\n"
"╠═════════════╬═══════╪═════════════╪════╪════╪═════════╣\n"
"║ 10000.01 ║ 123 │ 10.0 │ 0 │ E │ A ║\n"
"╚═════════════╩═══════╧═════════════╧════╧════╧═════════╝"
)
assert text == expected
def test_single_decimal_alignment():
text = t2a(
header=["1.1.1", "G", "Long Header"],
body=[[100.00001, 2, 3.14], [10.0001, 22.0, 2.718]],
alignments=Alignment.DECIMAL,
)
expected = (
"╔════════════════════════════════╗\n"
"║ 1.1.1 G Long Header ║\n"
"╟────────────────────────────────╢\n"
"║ 100.00001 2 3.14 ║\n"
"║ 10.0001 22.0 2.718 ║\n"
"╚════════════════════════════════╝"
)
assert text == expected
def test_single_left_alignment():
text = t2a(
header=["1.1.1", "G", "Long Header"],
body=[[100.00001, 2, 3.14], [10.0001, 22.0, 2.718]],
alignments=Alignment.LEFT,
)
expected = (
"╔════════════════════════════════╗\n"
"║ 1.1.1 G Long Header ║\n"
"╟────────────────────────────────╢\n"
"║ 100.00001 2 3.14 ║\n"
"║ 10.0001 22.0 2.718 ║\n"
"╚════════════════════════════════╝"
)
assert text == expected