File: test_table.py

package info (click to toggle)
lookatme 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,416 kB
  • sloc: python: 3,497; sh: 291; makefile: 16
file content (141 lines) | stat: -rw-r--r-- 3,935 bytes parent folder | download | duplicates (2)
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
"""
This module tests the Table widget in lookatme/widgets/table.py
"""


import pytest

import lookatme.widgets.table
import tests.utils as utils

TEST_STYLE = {
    "style": "monokai",
    "table": {
        "column_spacing": 3,
        "header_divider": "&",
    },
}


@pytest.fixture(autouse=True)
def table_setup(tmpdir, mocker):
    utils.setup_lookatme(tmpdir, mocker, style=TEST_STYLE)


def test_basic_render(tmpdir, mocker):
    """Test that a Table widget renders correctly
    """
    headers = ["H1", "H2", "H3"]
    aligns = ["left", "center", "right"]
    rows = [
        ["1", "22", "333"],
        ["*1*", "~~22~~", "**333**"],
    ]

    table = lookatme.widgets.table.Table(rows, headers=headers, aligns=aligns)
    canvas = table.render((20,))
    content = list(canvas.content())

    # four rows:
    #  1 headers
    #  2 DIVIDER
    #  3 row1
    #  4 row2
    assert len(content) == 4

    header_row = content[0]
    spec, text = utils.spec_and_text(header_row[0])
    assert "bold" in spec.foreground
    assert text == b"H1"
    spec, text = utils.spec_and_text(header_row[2])
    assert "bold" in spec.foreground
    assert text == b"H2"
    spec, text = utils.spec_and_text(header_row[5])
    assert "bold" in spec.foreground
    assert text == b"H3"

    divider_row = content[1]
    spec, text = utils.spec_and_text(divider_row[0])
    # no styling applied to the divider
    assert spec is None
    assert text == b"&&"
    spec, text = utils.spec_and_text(divider_row[2])
    # no styling applied to the divider
    assert spec is None
    assert text == b"&&"
    spec, text = utils.spec_and_text(divider_row[4])
    # no styling applied to the divider
    assert spec is None
    assert text == b"&&&"

    content_row1 = content[2]
    spec, text = utils.spec_and_text(content_row1[0])
    # no styling applied to this row
    assert spec is None
    assert text == b"1 "
    spec, text = utils.spec_and_text(content_row1[2])
    # no styling applied to this row
    assert spec is None
    assert text == b"22"
    spec, text = utils.spec_and_text(content_row1[4])
    # no styling applied to this row
    assert spec is None
    assert text == b"333"

    content_row1 = content[3]
    spec, text = utils.spec_and_text(content_row1[0])
    # no styling applied to this row
    assert "italics" in spec.foreground
    assert text == b"1"
    spec, text = utils.spec_and_text(content_row1[3])
    # no styling applied to this row
    assert "strikethrough" in spec.foreground
    assert text == b"22"
    spec, text = utils.spec_and_text(content_row1[5])
    # no styling applied to this row
    assert "underline" in spec.foreground
    assert text == b"333"


def test_table_no_headers(mocker):
    """This situation could never happen as parsed from Markdown. See
    https://stackoverflow.com/a/17543474.

    However this situation could happen manually when using the Table() class
    directly.
    """
    headers = None
    aligns = ["left", "center", "right"]
    rows = [
        ["1", "22", "333"],
        ["*1*", "~~22~~", "**333**"],
    ]

    table = lookatme.widgets.table.Table(rows, headers=headers, aligns=aligns)
    canvas = table.render((20,))
    content = list(canvas.content())

    assert len(content) == 2


def test_ignored_extra_column(mocker):
    """Test that extra columns beyond header values are ignored
    """
    headers = ["H1", "H2", "H3"]
    aligns = ["left", "center", "right"]
    rows = [
        ["1", "2", "3"],
        ["1", "2", "3", "4"],
        ["1", "2", "3", "4", "5"],
    ]

    table = lookatme.widgets.table.Table(rows, headers=headers, aligns=aligns)
    canvas = table.render((20,))
    content = list(canvas.content())

    # number of rows of output
    assert len(content) == 5
    assert b"4" not in utils.row_text(content[-2])

    assert b"4" not in utils.row_text(content[-1])
    assert b"5" not in utils.row_text(content[-1])