|
| 1 | +from textwrap import dedent |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import terminaltables |
| 6 | +from terminaltables import AsciiTable, UnixTable |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize('cls', [AsciiTable, UnixTable]) |
| 10 | +def test_empty(cls): |
| 11 | + terminaltables.DEFAULT_TERMINAL_WIDTH = 80 |
| 12 | + table = cls([]) |
| 13 | + assert 2 == table.table_width |
| 14 | + assert table.ok is True |
| 15 | + |
| 16 | + table = cls([[]]) |
| 17 | + assert 2 == table.table_width |
| 18 | + assert table.ok is True |
| 19 | + |
| 20 | + table = cls([['']]) |
| 21 | + assert 4 == table.table_width |
| 22 | + assert table.ok is True |
| 23 | + |
| 24 | + table = cls([[' ']]) |
| 25 | + assert 5 == table.table_width |
| 26 | + assert table.ok is True |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize('cls', [AsciiTable, UnixTable]) |
| 30 | +def test_simple(cls): |
| 31 | + terminaltables.DEFAULT_TERMINAL_WIDTH = 80 |
| 32 | + table_data = [ |
| 33 | + ['Name', 'Color', 'Type'], |
| 34 | + ['Avocado', 'green', 'nut'], |
| 35 | + ['Tomato', 'red', 'fruit'], |
| 36 | + ['Lettuce', 'green', 'vegetable'], |
| 37 | + ] |
| 38 | + table = cls(table_data) |
| 39 | + |
| 40 | + assert 31 == table.table_width |
| 41 | + assert table.ok is True |
| 42 | + |
| 43 | + table_data.append(['Watermelon', 'green', 'fruit']) |
| 44 | + assert 34 == table.table_width |
| 45 | + assert table.ok is True |
| 46 | + |
| 47 | + terminaltables.DEFAULT_TERMINAL_WIDTH = 34 |
| 48 | + assert table.ok is True |
| 49 | + |
| 50 | + terminaltables.DEFAULT_TERMINAL_WIDTH = 33 |
| 51 | + assert table.ok is False |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize('cls', [AsciiTable, UnixTable]) |
| 55 | +def test_multi_line(cls): |
| 56 | + terminaltables.DEFAULT_TERMINAL_WIDTH = 100 |
| 57 | + table_data = [ |
| 58 | + ['Show', 'Characters'], |
| 59 | + ['Rugrats', dedent('Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles,\n' |
| 60 | + 'Susie Carmichael, Dil Pickles, Kimi Finster, Spike')], |
| 61 | + ['South Park', 'Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick'] |
| 62 | + ] |
| 63 | + table = cls(table_data) |
| 64 | + assert 100 == table.table_width |
| 65 | + assert table.ok is True |
| 66 | + |
| 67 | + |
| 68 | +def test_attributes(): |
| 69 | + table_data = [ |
| 70 | + ['Name', 'Color', 'Type'], |
| 71 | + ['Avocado', 'green', 'nut'], |
| 72 | + ['Tomato', 'red', 'fruit'], |
| 73 | + ['Lettuce', 'green', 'vegetable'], |
| 74 | + ] |
| 75 | + table = AsciiTable(table_data) |
| 76 | + |
| 77 | + assert 31 == max(len(r) for r in table.table.splitlines()) |
| 78 | + assert 31 == table.table_width |
| 79 | + |
| 80 | + table.outer_border = False |
| 81 | + assert 29 == max(len(r) for r in table.table.splitlines()) |
| 82 | + assert 29 == table.table_width |
| 83 | + |
| 84 | + table.inner_column_border = False |
| 85 | + assert 27 == max(len(r) for r in table.table.splitlines()) |
| 86 | + assert 27 == table.table_width |
| 87 | + |
| 88 | + table.padding_left = 0 |
| 89 | + assert 24 == max(len(r) for r in table.table.splitlines()) |
| 90 | + assert 24 == table.table_width |
| 91 | + |
| 92 | + table.padding_right = 0 |
| 93 | + assert 21 == max(len(r) for r in table.table.splitlines()) |
| 94 | + assert 21 == table.table_width |
0 commit comments