summaryrefslogtreecommitdiffstats
path: root/tests/test_width_and_alignment/test_table_width.py
blob: 58187896d5cbc45d36e9fb52b57551eecf98c208 (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
"""Test function in module."""

from terminaltables.width_and_alignment import max_dimensions, table_width


def test_empty():
    """Test with zero-length cells."""
    assert table_width(max_dimensions([['']])[2], 0, 0) == 0
    assert table_width(max_dimensions([['', '', '']])[2], 0, 0) == 0
    assert table_width(max_dimensions([['', '', ''], ['', '', '']])[2], 0, 0) == 0

    assert table_width(max_dimensions([['']], 1, 1)[2], 2, 1) == 4
    assert table_width(max_dimensions([['', '', '']], 1, 1)[2], 2, 1) == 10
    assert table_width(max_dimensions([['', '', ''], ['', '', '']], 1, 1)[2], 2, 1) == 10


def test_single_line():
    """Test with single-line cells."""
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
    ]

    # '| Lettuce | green | vegetable |'
    outer, inner, outer_widths = 2, 1, max_dimensions(table_data, 1, 1)[2]
    assert table_width(outer_widths, outer, inner) == 31

    # ' Lettuce | green | vegetable '
    outer = 0
    assert table_width(outer_widths, outer, inner) == 29

    # '| Lettuce  green  vegetable |'
    outer, inner = 2, 0
    assert table_width(outer_widths, outer, inner) == 29

    # ' Lettuce  green  vegetable '
    outer = 0
    assert table_width(outer_widths, outer, inner) == 27

    # '|Lettuce |green |vegetable |'
    outer, inner, outer_widths = 2, 1, max_dimensions(table_data, 1)[2]
    assert table_width(outer_widths, outer, inner) == 28

    # '|Lettuce     |green     |vegetable     |'
    outer_widths = max_dimensions(table_data, 3, 2)[2]
    assert table_width(outer_widths, outer, inner) == 40

    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
        ['Watermelon', 'green', 'fruit'],
    ]
    outer, inner, outer_widths = 2, 1, max_dimensions(table_data, 1, 1)[2]
    assert table_width(outer_widths, outer, inner) == 34


def test_multi_line():
    """Test with multi-line cells."""
    table_data = [
        ['Show', 'Characters'],
        ['Rugrats', ('Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles,\n'
                     'Susie Carmichael, Dil Pickles, Kimi Finster, Spike')],
        ['South Park', 'Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick']
    ]
    outer, inner, outer_widths = 2, 1, max_dimensions(table_data, 1, 1)[2]
    assert table_width(outer_widths, outer, inner) == 100