summaryrefslogtreecommitdiffstats
path: root/tests/test_base_table/test_table.py
blob: c5b5a89bfaef2b3609c50ca23b4433978a75c6bd (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
# coding: utf-8
"""Test property in BaseTable class."""

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

from terminaltables.base_table import BaseTable


def test_ascii():
    """Test with ASCII characters."""
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
    ]
    table = BaseTable(table_data)
    actual = table.table

    expected = (
        '+---------+-------+-----------+\n'
        '| Name    | Color | Type      |\n'
        '+---------+-------+-----------+\n'
        '| Avocado | green | nut       |\n'
        '| Tomato  | red   | fruit     |\n'
        '| Lettuce | green | vegetable |\n'
        '+---------+-------+-----------+'
    )

    assert actual == expected


def test_int():
    """Test with integers instead of strings."""
    table_data = [
        [100, 10, 1],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
    ]
    table = BaseTable(table_data, 1234567890)
    actual = table.table

    expected = (
        '+1234567890+---+\n'
        '| 100 | 10 | 1 |\n'
        '+-----+----+---+\n'
        '| 0   | 3  | 6 |\n'
        '| 1   | 4  | 7 |\n'
        '| 2   | 5  | 8 |\n'
        '+-----+----+---+'
    )

    assert actual == expected


def test_float():
    """Test with floats instead of strings."""
    table_data = [
        [1.0, 22.0, 333.0],
        [0.1, 3.1, 6.1],
        [1.1, 4.1, 7.1],
        [2.1, 5.1, 8.1],
    ]
    table = BaseTable(table_data, 0.12345678)
    actual = table.table

    expected = (
        '+0.12345678--+-------+\n'
        '| 1.0 | 22.0 | 333.0 |\n'
        '+-----+------+-------+\n'
        '| 0.1 | 3.1  | 6.1   |\n'
        '| 1.1 | 4.1  | 7.1   |\n'
        '| 2.1 | 5.1  | 8.1   |\n'
        '+-----+------+-------+'
    )

    assert actual == expected


def test_bool_none():
    """Test with NoneType/boolean instead of strings."""
    table_data = [
        [True, False, None],
        [True, False, None],
        [False, None, True],
        [None, True, False],
    ]
    table = BaseTable(table_data, True)
    actual = table.table

    expected = (
        '+True---+-------+-------+\n'
        '| True  | False | None  |\n'
        '+-------+-------+-------+\n'
        '| True  | False | None  |\n'
        '| False | None  | True  |\n'
        '| None  | True  | False |\n'
        '+-------+-------+-------+'
    )

    assert actual == expected


def test_cjk():
    """Test with CJK characters."""
    table_data = [
        ['CJK'],
        ['蓝色'],
        ['世界你好'],
    ]
    table = BaseTable(table_data)
    actual = table.table

    expected = (
        '+----------+\n'
        '| CJK      |\n'
        '+----------+\n'
        '| 蓝色     |\n'
        '| 世界你好 |\n'
        '+----------+'
    )

    assert actual == expected


def test_rtl():
    """Test with RTL characters."""
    table_data = [
        ['RTL'],
        ['שלום'],
        ['معرب'],
    ]
    table = BaseTable(table_data)
    actual = table.table

    expected = (
        '+------+\n'
        '| RTL  |\n'
        '+------+\n'
        '| שלום |\n'
        '| معرب |\n'
        '+------+'
    )

    assert actual == expected


def test_rtl_large():
    """Test large table of RTL characters."""
    table_data = [
        ['اكتب', 'اللون', 'اسم'],
        ['البندق', 'أخضر', 'أفوكادو'],
        ['ثمرة', 'أحمر', 'بندورة'],
        ['الخضروات', 'أخضر', 'الخس'],
    ]
    table = BaseTable(table_data, 'جوجل المترجم')
    actual = table.table

    expected = (
        '+جوجل المترجم------+---------+\n'
        '| اكتب     | اللون | اسم     |\n'
        '+----------+-------+---------+\n'
        '| البندق   | أخضر  | أفوكادو |\n'
        '| ثمرة     | أحمر  | بندورة  |\n'
        '| الخضروات | أخضر  | الخس    |\n'
        '+----------+-------+---------+'
    )

    assert actual == expected


def test_color():
    """Test with color characters."""
    table_data = [
        ['ansi', '\033[31mRed\033[39m', '\033[32mGreen\033[39m', '\033[34mBlue\033[39m'],
        ['colorclass', Color('{red}Red{/red}'), Color('{green}Green{/green}'), Color('{blue}Blue{/blue}')],
        ['colorama', Fore.RED + 'Red' + Fore.RESET, Fore.GREEN + 'Green' + Fore.RESET, Fore.BLUE + 'Blue' + Fore.RESET],
        ['termcolor', colored('Red', 'red'), colored('Green', 'green'), colored('Blue', 'blue')],
    ]
    table = BaseTable(table_data)
    table.inner_heading_row_border = False
    actual = table.table

    expected = (
        u'+------------+-----+-------+------+\n'
        u'| ansi       | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n'
        u'| colorclass | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n'
        u'| colorama   | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n'
        u'| termcolor  | \033[31mRed\033[0m | \033[32mGreen\033[0m | \033[34mBlue\033[0m |\n'
        u'+------------+-----+-------+------+'
    )

    assert actual == expected