summaryrefslogtreecommitdiffstats
path: root/tests/test_style.py
blob: d0a4790b8a833877ffb9759c118c2787219b80bf (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from __future__ import annotations

from prompt_toolkit.styles import Attrs, Style, SwapLightAndDarkStyleTransformation


def test_style_from_dict():
    style = Style.from_dict(
        {
            "a": "#ff0000 bold underline strike italic",
            "b": "bg:#00ff00 blink reverse",
        }
    )

    # Lookup of class:a.
    expected = Attrs(
        color="ff0000",
        bgcolor="",
        bold=True,
        underline=True,
        strike=True,
        italic=True,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:a") == expected

    # Lookup of class:b.
    expected = Attrs(
        color="",
        bgcolor="00ff00",
        bold=False,
        underline=False,
        strike=False,
        italic=False,
        blink=True,
        reverse=True,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:b") == expected

    # Test inline style.
    expected = Attrs(
        color="ff0000",
        bgcolor="",
        bold=False,
        underline=False,
        strike=False,
        italic=False,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("#ff0000") == expected

    # Combine class name and inline style (Whatever is defined later gets priority.)
    expected = Attrs(
        color="00ff00",
        bgcolor="",
        bold=True,
        underline=True,
        strike=True,
        italic=True,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:a #00ff00") == expected

    expected = Attrs(
        color="ff0000",
        bgcolor="",
        bold=True,
        underline=True,
        strike=True,
        italic=True,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("#00ff00 class:a") == expected


def test_class_combinations_1():
    # In this case, our style has both class 'a' and 'b'.
    # Given that the style for 'a b' is defined at the end, that one is used.
    style = Style(
        [
            ("a", "#0000ff"),
            ("b", "#00ff00"),
            ("a b", "#ff0000"),
        ]
    )
    expected = Attrs(
        color="ff0000",
        bgcolor="",
        bold=False,
        underline=False,
        strike=False,
        italic=False,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:a class:b") == expected
    assert style.get_attrs_for_style_str("class:a,b") == expected
    assert style.get_attrs_for_style_str("class:a,b,c") == expected

    # Changing the order shouldn't matter.
    assert style.get_attrs_for_style_str("class:b class:a") == expected
    assert style.get_attrs_for_style_str("class:b,a") == expected


def test_class_combinations_2():
    # In this case, our style has both class 'a' and 'b'.
    # The style that is defined the latest get priority.
    style = Style(
        [
            ("a b", "#ff0000"),
            ("b", "#00ff00"),
            ("a", "#0000ff"),
        ]
    )
    expected = Attrs(
        color="00ff00",
        bgcolor="",
        bold=False,
        underline=False,
        strike=False,
        italic=False,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:a class:b") == expected
    assert style.get_attrs_for_style_str("class:a,b") == expected
    assert style.get_attrs_for_style_str("class:a,b,c") == expected

    # Defining 'a' latest should give priority to 'a'.
    expected = Attrs(
        color="0000ff",
        bgcolor="",
        bold=False,
        underline=False,
        strike=False,
        italic=False,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:b class:a") == expected
    assert style.get_attrs_for_style_str("class:b,a") == expected


def test_substyles():
    style = Style(
        [
            ("a.b", "#ff0000 bold"),
            ("a", "#0000ff"),
            ("b", "#00ff00"),
            ("b.c", "#0000ff italic"),
        ]
    )

    # Starting with a.*
    expected = Attrs(
        color="0000ff",
        bgcolor="",
        bold=False,
        underline=False,
        strike=False,
        italic=False,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:a") == expected

    expected = Attrs(
        color="ff0000",
        bgcolor="",
        bold=True,
        underline=False,
        strike=False,
        italic=False,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:a.b") == expected
    assert style.get_attrs_for_style_str("class:a.b.c") == expected

    # Starting with b.*
    expected = Attrs(
        color="00ff00",
        bgcolor="",
        bold=False,
        underline=False,
        strike=False,
        italic=False,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:b") == expected
    assert style.get_attrs_for_style_str("class:b.a") == expected

    expected = Attrs(
        color="0000ff",
        bgcolor="",
        bold=False,
        underline=False,
        strike=False,
        italic=True,
        blink=False,
        reverse=False,
        hidden=False,
    )
    assert style.get_attrs_for_style_str("class:b.c") == expected
    assert style.get_attrs_for_style_str("class:b.c.d") == expected


def test_swap_light_and_dark_style_transformation():
    transformation = SwapLightAndDarkStyleTransformation()

    # Test with 6 digit hex colors.
    before = Attrs(
        color="440000",
        bgcolor="888844",
        bold=True,
        underline=True,
        strike=True,
        italic=True,
        blink=False,
        reverse=False,
        hidden=False,
    )
    after = Attrs(
        color="ffbbbb",
        bgcolor="bbbb76",
        bold=True,
        underline=True,
        strike=True,
        italic=True,
        blink=False,
        reverse=False,
        hidden=False,
    )

    assert transformation.transform_attrs(before) == after

    # Test with ANSI colors.
    before = Attrs(
        color="ansired",
        bgcolor="ansiblack",
        bold=True,
        underline=True,
        strike=True,
        italic=True,
        blink=False,
        reverse=False,
        hidden=False,
    )
    after = Attrs(
        color="ansibrightred",
        bgcolor="ansiwhite",
        bold=True,
        underline=True,
        strike=True,
        italic=True,
        blink=False,
        reverse=False,
        hidden=False,
    )

    assert transformation.transform_attrs(before) == after