summaryrefslogtreecommitdiffstats
path: root/tests/test_width_and_alignment/test_align_and_pad_cell.py
blob: e0a928e77859f8acb0f38b2aa41f005ca5cad5d8 (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
# coding: utf-8
"""Test function in module."""

import pytest
from colorama import Fore
from colorclass import Color
from termcolor import colored

from terminaltables.width_and_alignment import align_and_pad_cell


@pytest.mark.parametrize('string,align,width,expected', [
    ('test', '', 4, ['test']),
    (123, '', 3, ['123']),
    (0.9, '', 3, ['0.9']),
    (None, '', 4, ['None']),
    (True, '', 4, ['True']),
    (False, '', 5, ['False']),
    (Color('{blue}Test{/blue}'), '', 4, ['\x1b[34mTest\x1b[39m']),
    (Fore.BLUE + 'Test' + Fore.RESET, '', 4, ['\x1b[34mTest\x1b[39m']),
    (colored('Test', 'blue'), '', 4, ['\x1b[34mTest\x1b[0m']),
    ('蓝色', '', 4, ['蓝色']),
    (u'שלום', '', 4, [u'\u05e9\u05dc\u05d5\u05dd']),
    (u'معرب', '', 4, [u'\u0645\u0639\u0631\u0628']),

    ('test', '', 5, ['test ']),
    (123, '', 4, ['123 ']),
    (0.9, '', 4, ['0.9 ']),
    (None, '', 5, ['None ']),
    (True, '', 5, ['True ']),
    (False, '', 6, ['False ']),
    (Color('{blue}Test{/blue}'), '', 5, ['\x1b[34mTest\x1b[39m ']),
    (Fore.BLUE + 'Test' + Fore.RESET, '', 5, ['\x1b[34mTest\x1b[39m ']),
    (colored('Test', 'blue'), '', 5, ['\x1b[34mTest\x1b[0m ']),
    ('蓝色', '', 5, ['蓝色 ']),
    (u'שלום', '', 5, [u'\u05e9\u05dc\u05d5\u05dd ']),
    (u'معرب', '', 5, [u'\u0645\u0639\u0631\u0628 ']),

    ('test', 'left', 5, ['test ']),
    (123, 'left', 4, ['123 ']),
    (0.9, 'left', 4, ['0.9 ']),
    (None, 'left', 5, ['None ']),
    (True, 'left', 5, ['True ']),
    (False, 'left', 6, ['False ']),
    (Color('{blue}Test{/blue}'), 'left', 5, ['\x1b[34mTest\x1b[39m ']),
    (Fore.BLUE + 'Test' + Fore.RESET, 'left', 5, ['\x1b[34mTest\x1b[39m ']),
    (colored('Test', 'blue'), 'left', 5, ['\x1b[34mTest\x1b[0m ']),
    ('蓝色', 'left', 5, ['蓝色 ']),
    (u'שלום', 'left', 5, [u'\u05e9\u05dc\u05d5\u05dd ']),
    (u'معرب', 'left', 5, [u'\u0645\u0639\u0631\u0628 ']),

    ('test', 'right', 5, [' test']),
    (123, 'right', 4, [' 123']),
    (0.9, 'right', 4, [' 0.9']),
    (None, 'right', 5, [' None']),
    (True, 'right', 5, [' True']),
    (False, 'right', 6, [' False']),
    (Color('{blue}Test{/blue}'), 'right', 5, [' \x1b[34mTest\x1b[39m']),
    (Fore.BLUE + 'Test' + Fore.RESET, 'right', 5, [' \x1b[34mTest\x1b[39m']),
    (colored('Test', 'blue'), 'right', 5, [' \x1b[34mTest\x1b[0m']),
    ('蓝色', 'right', 5, [' 蓝色']),
    (u'שלום', 'right', 5, [u' \u05e9\u05dc\u05d5\u05dd']),
    (u'معرب', 'right', 5, [u' \u0645\u0639\u0631\u0628']),

    ('test', 'center', 6, [' test ']),
    (123, 'center', 5, [' 123 ']),
    (0.9, 'center', 5, [' 0.9 ']),
    (None, 'center', 6, [' None ']),
    (True, 'center', 6, [' True ']),
    (False, 'center', 7, [' False ']),
    (Color('{blue}Test{/blue}'), 'center', 6, [' \x1b[34mTest\x1b[39m ']),
    (Fore.BLUE + 'Test' + Fore.RESET, 'center', 6, [' \x1b[34mTest\x1b[39m ']),
    (colored('Test', 'blue'), 'center', 6, [' \x1b[34mTest\x1b[0m ']),
    ('蓝色', 'center', 6, [' 蓝色 ']),
    (u'שלום', 'center', 6, [u' \u05e9\u05dc\u05d5\u05dd ']),
    (u'معرب', 'center', 6, [u' \u0645\u0639\u0631\u0628 ']),
])
def test_width(string, align, width, expected):
    """Test width and horizontal alignment.

    :param str string: String to test.
    :param str align: Horizontal alignment.
    :param int width: Expand string to this width without padding.
    :param list expected: Expected output string.
    """
    actual = align_and_pad_cell(string, (align,), (width, 1), (0, 0, 0, 0))
    assert actual == expected


@pytest.mark.parametrize('string,align,height,expected', [
    ('test', '', 1, ['test']),
    (Color('{blue}Test{/blue}'), '', 1, ['\x1b[34mTest\x1b[39m']),
    (Fore.BLUE + 'Test' + Fore.RESET, '', 1, ['\x1b[34mTest\x1b[39m']),
    (colored('Test', 'blue'), '', 1, ['\x1b[34mTest\x1b[0m']),
    ('蓝色', '', 1, ['蓝色']),
    (u'שלום', '', 1, [u'\u05e9\u05dc\u05d5\u05dd']),
    (u'معرب', '', 1, [u'\u0645\u0639\u0631\u0628']),

    ('test', '', 2, ['test', '    ']),
    (Color('{blue}Test{/blue}'), '', 2, ['\x1b[34mTest\x1b[39m', '    ']),
    (Fore.BLUE + 'Test' + Fore.RESET, '', 2, ['\x1b[34mTest\x1b[39m', '    ']),
    (colored('Test', 'blue'), '', 2, ['\x1b[34mTest\x1b[0m', '    ']),
    ('蓝色', '', 2, ['蓝色', '    ']),
    (u'שלום', '', 2, [u'\u05e9\u05dc\u05d5\u05dd', '    ']),
    (u'معرب', '', 2, [u'\u0645\u0639\u0631\u0628', '    ']),

    ('test', 'top', 2, ['test', '    ']),
    (Color('{blue}Test{/blue}'), 'top', 2, ['\x1b[34mTest\x1b[39m', '    ']),
    (Fore.BLUE + 'Test' + Fore.RESET, 'top', 2, ['\x1b[34mTest\x1b[39m', '    ']),
    (colored('Test', 'blue'), 'top', 2, ['\x1b[34mTest\x1b[0m', '    ']),
    ('蓝色', 'top', 2, ['蓝色', '    ']),
    (u'שלום', 'top', 2, [u'\u05e9\u05dc\u05d5\u05dd', '    ']),
    (u'معرب', 'top', 2, [u'\u0645\u0639\u0631\u0628', '    ']),

    ('test', 'bottom', 2, ['    ', 'test']),
    (Color('{blue}Test{/blue}'), 'bottom', 2, ['    ', '\x1b[34mTest\x1b[39m']),
    (Fore.BLUE + 'Test' + Fore.RESET, 'bottom', 2, ['    ', '\x1b[34mTest\x1b[39m']),
    (colored('Test', 'blue'), 'bottom', 2, ['    ', '\x1b[34mTest\x1b[0m']),
    ('蓝色', 'bottom', 2, ['    ', '蓝色']),
    (u'שלום', 'bottom', 2, ['    ', u'\u05e9\u05dc\u05d5\u05dd']),
    (u'معرب', 'bottom', 2, ['    ', u'\u0645\u0639\u0631\u0628']),

    ('test', 'middle', 3, ['    ', 'test', '    ']),
    (Color('{blue}Test{/blue}'), 'middle', 3, ['    ', '\x1b[34mTest\x1b[39m', '    ']),
    (Fore.BLUE + 'Test' + Fore.RESET, 'middle', 3, ['    ', '\x1b[34mTest\x1b[39m', '    ']),
    (colored('Test', 'blue'), 'middle', 3, ['    ', '\x1b[34mTest\x1b[0m', '    ']),
    ('蓝色', 'middle', 3, ['    ', '蓝色', '    ']),
    (u'שלום', 'middle', 3, ['    ', u'\u05e9\u05dc\u05d5\u05dd', '    ']),
    (u'معرب', 'middle', 3, ['    ', u'\u0645\u0639\u0631\u0628', '    ']),
])
def test_height(string, align, height, expected):
    """Test height and vertical alignment.

    :param str string: String to test.
    :param str align: Horizontal alignment.
    :param int height: Expand string to this height without padding.
    :param list expected: Expected output string.
    """
    actual = align_and_pad_cell(string, (align,), (4, height), (0, 0, 0, 0))
    assert actual == expected


@pytest.mark.parametrize('string,align,expected', [
    ('', '', ['.......', '.......', '.......', '.......', '.......']),
    ('\n', '', ['.......', '.......', '.......', '.......', '.......']),
    ('a\nb\nc', '', ['.......', '.a.....', '.b.....', '.c.....', '.......']),
    ('test', '', ['.......', '.test..', '.......', '.......', '.......']),

    ('', 'left', ['.......', '.......', '.......', '.......', '.......']),
    ('\n', 'left', ['.......', '.......', '.......', '.......', '.......']),
    ('a\nb\nc', 'left', ['.......', '.a.....', '.b.....', '.c.....', '.......']),
    ('test', 'left', ['.......', '.test..', '.......', '.......', '.......']),

    ('', 'right', ['.......', '.......', '.......', '.......', '.......']),
    ('\n', 'right', ['.......', '.......', '.......', '.......', '.......']),
    ('a\nb\nc', 'right', ['.......', '.....a.', '.....b.', '.....c.', '.......']),
    ('test', 'right', ['.......', '..test.', '.......', '.......', '.......']),

    ('', 'center', ['.......', '.......', '.......', '.......', '.......']),
    ('\n', 'center', ['.......', '.......', '.......', '.......', '.......']),
    ('a\nb\nc', 'center', ['.......', '...a...', '...b...', '...c...', '.......']),
    ('test', 'center', ['.......', '..test.', '.......', '.......', '.......']),

    ('', 'top', ['.......', '.......', '.......', '.......', '.......']),
    ('\n', 'top', ['.......', '.......', '.......', '.......', '.......']),
    ('a\nb\nc', 'top', ['.......', '.a.....', '.b.....', '.c.....', '.......']),
    ('test', 'top', ['.......', '.test..', '.......', '.......', '.......']),

    ('', 'bottom', ['.......', '.......', '.......', '.......', '.......']),
    ('\n', 'bottom', ['.......', '.......', '.......', '.......', '.......']),
    ('a\nb\nc', 'bottom', ['.......', '.a.....', '.b.....', '.c.....', '.......']),
    ('test', 'bottom', ['.......', '.......', '.......', '.test..', '.......']),

    ('', 'middle', ['.......', '.......', '.......', '.......', '.......']),
    ('\n', 'middle', ['.......', '.......', '.......', '.......', '.......']),
    ('a\nb\nc', 'middle', ['.......', '.a.....', '.b.....', '.c.....', '.......']),
    ('test', 'middle', ['.......', '.......', '.test..', '.......', '.......']),

    (
        u'蓝色\nשלום\nمعرب',
        '',
        ['.......', u'.蓝色..', u'.\u05e9\u05dc\u05d5\u05dd..', u'.\u0645\u0639\u0631\u0628..', '.......']
    ),

    (
        '\n'.join((Color('{blue}Test{/blue}'), Fore.BLUE + 'Test' + Fore.RESET, colored('Test', 'blue'))),
        '',
        ['.......', '.\x1b[34mTest\x1b[39m..', '.\x1b[34mTest\x1b[39m..', '.\x1b[34mTest\x1b[0m..', '.......']
    ),

    # (Color('{blue}A\nB{/blue}'), '', '.......\n.\x1b[34mA\x1b[39m.....\n.\x1b[34mB\x1b[39m.....\n.......\n.......'),

])
def test_odd_width_height_pad_space(string, align, expected):
    """Test odd number width, height, padding, and dots for whitespaces.

    :param str string: String to test.
    :param str align: Alignment in any dimension but one at a time.
    :param list expected: Expected output string.
    """
    actual = align_and_pad_cell(string, (align,), (5, 3), (1, 1, 1, 1), '.')
    assert actual == expected