summaryrefslogtreecommitdiffstats
path: root/tests/test_base_table/test_gen_table.py
blob: 54d5fe1a81630b6cc431990ebfbbb38263a576d9 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""Test method in BaseTable class."""

import pytest

from terminaltables.base_table import BaseTable
from terminaltables.build import flatten
from terminaltables.width_and_alignment import max_dimensions


@pytest.mark.parametrize('inner_heading_row_border', [True, False])
@pytest.mark.parametrize('inner_footing_row_border', [True, False])
@pytest.mark.parametrize('inner_row_border', [True, False])
def test_inner_row_borders(inner_heading_row_border, inner_footing_row_border, inner_row_border):
    """Test heading/footing/row borders.

    :param bool inner_heading_row_border: Passed to table.
    :param bool inner_footing_row_border: Passed to table.
    :param bool inner_row_border: Passed to table.
    """
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
    ]
    table = BaseTable(table_data)
    table.inner_heading_row_border = inner_heading_row_border
    table.inner_footing_row_border = inner_footing_row_border
    table.inner_row_border = inner_row_border
    inner_widths, inner_heights, outer_widths = max_dimensions(table_data, table.padding_left, table.padding_right)[:3]
    actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths))

    # Determine expected.
    if inner_row_border:
        expected = (
            '+---------+-------+-----------+\n'
            '| Name    | Color | Type      |\n'
            '+---------+-------+-----------+\n'
            '| Avocado | green | nut       |\n'
            '+---------+-------+-----------+\n'
            '| Tomato  | red   | fruit     |\n'
            '+---------+-------+-----------+\n'
            '| Lettuce | green | vegetable |\n'
            '+---------+-------+-----------+'
        )
    elif inner_heading_row_border and inner_footing_row_border:
        expected = (
            '+---------+-------+-----------+\n'
            '| Name    | Color | Type      |\n'
            '+---------+-------+-----------+\n'
            '| Avocado | green | nut       |\n'
            '| Tomato  | red   | fruit     |\n'
            '+---------+-------+-----------+\n'
            '| Lettuce | green | vegetable |\n'
            '+---------+-------+-----------+'
        )
    elif inner_heading_row_border:
        expected = (
            '+---------+-------+-----------+\n'
            '| Name    | Color | Type      |\n'
            '+---------+-------+-----------+\n'
            '| Avocado | green | nut       |\n'
            '| Tomato  | red   | fruit     |\n'
            '| Lettuce | green | vegetable |\n'
            '+---------+-------+-----------+'
        )
    elif inner_footing_row_border:
        expected = (
            '+---------+-------+-----------+\n'
            '| Name    | Color | Type      |\n'
            '| Avocado | green | nut       |\n'
            '| Tomato  | red   | fruit     |\n'
            '+---------+-------+-----------+\n'
            '| Lettuce | green | vegetable |\n'
            '+---------+-------+-----------+'
        )
    else:
        expected = (
            '+---------+-------+-----------+\n'
            '| Name    | Color | Type      |\n'
            '| Avocado | green | nut       |\n'
            '| Tomato  | red   | fruit     |\n'
            '| Lettuce | green | vegetable |\n'
            '+---------+-------+-----------+'
        )

    assert actual == expected


@pytest.mark.parametrize('outer_border', [True, False])
def test_outer_borders(outer_border):
    """Test left/right/top/bottom table borders.

    :param bool outer_border: Passed to table.
    """
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
    ]
    table = BaseTable(table_data, 'Example Table')
    table.outer_border = outer_border
    inner_widths, inner_heights, outer_widths = max_dimensions(table_data, table.padding_left, table.padding_right)[:3]
    actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths))

    # Determine expected.
    if outer_border:
        expected = (
            '+Example Table----+-----------+\n'
            '| Name    | Color | Type      |\n'
            '+---------+-------+-----------+\n'
            '| Avocado | green | nut       |\n'
            '| Tomato  | red   | fruit     |\n'
            '| Lettuce | green | vegetable |\n'
            '+---------+-------+-----------+'
        )
    else:
        expected = (
            ' Name    | Color | Type      \n'
            '---------+-------+-----------\n'
            ' Avocado | green | nut       \n'
            ' Tomato  | red   | fruit     \n'
            ' Lettuce | green | vegetable '
        )

    assert actual == expected


@pytest.mark.parametrize('mode', ['row', 'one', 'blank', 'empty', 'none'])
@pytest.mark.parametrize('bare', [False, True])
def test_one_no_rows(mode, bare):
    """Test with one or no rows.

    :param str mode: Type of table contents to test.
    :param bool bare: Disable padding/borders.
    """
    if mode == 'row':
        table_data = [
            ['Avocado', 'green', 'nut'],
        ]
    elif mode == 'one':
        table_data = [
            ['Avocado'],
        ]
    elif mode == 'blank':
        table_data = [
            [''],
        ]
    elif mode == 'empty':
        table_data = [
            [],
        ]
    else:
        table_data = [
        ]
    table = BaseTable(table_data)
    if bare:
        table.inner_column_border = False
        table.inner_footing_row_border = False
        table.inner_heading_row_border = False
        table.inner_row_border = False
        table.outer_border = False
        table.padding_left = 0
        table.padding_right = 0
    inner_widths, inner_heights, outer_widths = max_dimensions(table_data, table.padding_left, table.padding_right)[:3]
    actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths))

    # Determine expected.
    if mode == 'row':
        if bare:
            expected = (
                'Avocadogreennut'
            )
        else:
            expected = (
                '+---------+-------+-----+\n'
                '| Avocado | green | nut |\n'
                '+---------+-------+-----+'
            )
    elif mode == 'one':
        if bare:
            expected = (
                'Avocado'
            )
        else:
            expected = (
                '+---------+\n'
                '| Avocado |\n'
                '+---------+'
            )
    elif mode == 'blank':  # Remember there's still padding.
        if bare:
            expected = (
                ''
            )
        else:
            expected = (
                '+--+\n'
                '|  |\n'
                '+--+'
            )
    elif mode == 'empty':
        if bare:
            expected = (
                ''
            )
        else:
            expected = (
                '++\n'
                '||\n'
                '++'
            )
    else:
        if bare:
            expected = (
                ''
            )
        else:
            expected = (
                '++\n'
                '++'
            )

    assert actual == expected