summaryrefslogtreecommitdiffstats
path: root/tests/test_all_tables_e2e/test_ascii_table.py
blob: 51ebc2aeaeb1b0a2bd1454d1f17c99d73feb16f7 (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
"""AsciiTable end to end testing."""

import sys
from textwrap import dedent

import py
import pytest

from terminaltables import AsciiTable
from terminaltables.terminal_io import IS_WINDOWS
from tests import PROJECT_ROOT
from tests.screenshot import RunNewConsole, screenshot_until_match

HERE = py.path.local(__file__).dirpath()


def test_single_line():
    """Test single-lined cells."""
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
        ['Watermelon', 'green'],
        [],
    ]
    table = AsciiTable(table_data, 'Example')
    table.inner_footing_row_border = True
    table.justify_columns[0] = 'left'
    table.justify_columns[1] = 'center'
    table.justify_columns[2] = 'right'
    actual = table.table

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


def test_multi_line():
    """Test multi-lined cells."""
    table_data = [
        ['Show', 'Characters'],
        ['Rugrats', 'Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles,\nDil Pickles'],
        ['South Park', 'Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick']
    ]
    table = AsciiTable(table_data)

    # Test defaults.
    actual = table.table
    expected = (
        '+------------+-------------------------------------------------------------------------------------+\n'
        '| Show       | Characters                                                                          |\n'
        '+------------+-------------------------------------------------------------------------------------+\n'
        '| Rugrats    | Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles, |\n'
        '|            | Dil Pickles                                                                         |\n'
        '| South Park | Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick                          |\n'
        '+------------+-------------------------------------------------------------------------------------+'
    )
    assert actual == expected

    # Test inner row border.
    table.inner_row_border = True
    actual = table.table
    expected = (
        '+------------+-------------------------------------------------------------------------------------+\n'
        '| Show       | Characters                                                                          |\n'
        '+------------+-------------------------------------------------------------------------------------+\n'
        '| Rugrats    | Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles, |\n'
        '|            | Dil Pickles                                                                         |\n'
        '+------------+-------------------------------------------------------------------------------------+\n'
        '| South Park | Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick                          |\n'
        '+------------+-------------------------------------------------------------------------------------+'
    )
    assert actual == expected

    # Justify right.
    table.justify_columns = {1: 'right'}
    actual = table.table
    expected = (
        '+------------+-------------------------------------------------------------------------------------+\n'
        '| Show       |                                                                          Characters |\n'
        '+------------+-------------------------------------------------------------------------------------+\n'
        '| Rugrats    | Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles, |\n'
        '|            |                                                                         Dil Pickles |\n'
        '+------------+-------------------------------------------------------------------------------------+\n'
        '| South Park |                          Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick |\n'
        '+------------+-------------------------------------------------------------------------------------+'
    )
    assert actual == expected


@pytest.mark.skipif(str(not IS_WINDOWS))
@pytest.mark.skipif('True')  # https://github.com/Robpol86/terminaltables/issues/30
def test_windows_screenshot(tmpdir):
    """Test on Windows in a new console window. Take a screenshot to verify it works.

    :param tmpdir: pytest fixture.
    """
    script = tmpdir.join('script.py')
    command = [sys.executable, str(script)]
    screenshot = PROJECT_ROOT.join('test_ascii_table.png')
    if screenshot.check():
        screenshot.remove()

    # Generate script.
    script_template = dedent(u"""\
    from __future__ import print_function
    import os, time
    from colorclass import Color, Windows
    from terminaltables import AsciiTable
    Windows.enable(auto_colors=True)
    stop_after = time.time() + 20

    table_data = [
        [Color('{b}Name{/b}'), Color('{b}Color{/b}'), Color('{b}Misc{/b}')],
        ['Avocado', Color('{autogreen}green{/fg}'), 100],
        ['Tomato', Color('{autored}red{/fg}'), 0.5],
        ['Lettuce', Color('{autogreen}green{/fg}'), None],
    ]
    print(AsciiTable(table_data).table)

    print('Waiting for screenshot_until_match()...')
    while not os.path.exists(r'%s') and time.time() < stop_after:
        time.sleep(0.5)
    """)
    script_contents = script_template % str(screenshot)
    script.write(script_contents.encode('utf-8'), mode='wb')

    # Setup expected.
    sub_images = [str(p) for p in HERE.listdir('sub_ascii_*.bmp')]
    assert sub_images

    # Run.
    with RunNewConsole(command) as gen:
        screenshot_until_match(str(screenshot), 15, sub_images, 1, gen)