summaryrefslogtreecommitdiffstats
path: root/tests/test_html_attrs.py
blob: bc1a68f6c1134bceae4526f09cdf8f1c404aa833 (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
277
278
279
280
281
import pytest

from jinjax.html_attrs import HTMLAttrs


def test_parse_initial_attrs():
    attrs = HTMLAttrs(
        {
            "title": "hi",
            "data-position": "top",
            "class": "z4 c3 a1 z4 b2",
            "open": True,
            "disabled": False,
            "value": 0,
            "foobar": None,
        }
    )
    assert attrs.classes == "a1 b2 c3 z4"
    assert attrs.get("class") == "a1 b2 c3 z4"
    assert attrs.get("data-position") == "top"
    assert attrs.get("data_position") == "top"
    assert attrs.get("title") == "hi"
    assert attrs.get("open") is True
    assert attrs.get("disabled", "meh") == "meh"
    assert attrs.get("value") == "0"

    assert attrs.get("disabled") is None
    assert attrs.get("foobar") is None

    attrs.set(data_value=0)
    attrs.set(data_position=False)
    assert attrs.get("data-value") == 0
    assert attrs.get("data-position") is None
    assert attrs.get("data_position") is None

def test_getattr():
    attrs = HTMLAttrs(
        {
            "title": "hi",
            "class": "z4 c3 a1 z4 b2",
            "open": True,
        }
    )
    assert attrs["class"] == "a1 b2 c3 z4"
    assert attrs["title"] == "hi"
    assert attrs["open"] is True
    assert attrs["lorem"] is None


def test_deltattr():
    attrs = HTMLAttrs(
        {
            "title": "hi",
            "class": "z4 c3 a1 z4 b2",
            "open": True,
        }
    )
    assert attrs["class"] == "a1 b2 c3 z4"
    del attrs["title"]
    assert attrs["title"] is None


def test_render():
    attrs = HTMLAttrs(
        {
            "title": "hi",
            "data-position": "top",
            "class": "z4 c3 a1 z4 b2",
            "open": True,
            "disabled": False,
        }
    )
    assert 'class="a1 b2 c3 z4" data-position="top" title="hi" open' == attrs.render()


def test_set():
    attrs = HTMLAttrs({})
    attrs.set(title="hi", data_position="top")
    attrs.set(open=True)
    assert 'data-position="top" title="hi" open' == attrs.render()

    attrs.set(title=False, open=False)
    assert 'data-position="top"' == attrs.render()


def test_class_management():
    attrs = HTMLAttrs(
        {
            "class": "z4 c3 a1 z4 b2",
        }
    )
    attrs.set(classes="lorem bipsum lorem a1")

    assert attrs.classes == "a1 b2 bipsum c3 lorem z4"

    attrs.remove_class("bipsum")
    assert attrs.classes == "a1 b2 c3 lorem z4"

    attrs.set(classes=None)
    attrs.set(classes="meh")
    assert attrs.classes == "meh"


def test_setdefault():
    attrs = HTMLAttrs(
        {
            "title": "hi",
        }
    )
    attrs.setdefault(
        title="default title",
        data_lorem="ipsum",
        open=True,
        disabled=False,
    )
    assert 'data-lorem="ipsum" title="hi"' == attrs.render()


def test_as_dict():
    attrs = HTMLAttrs(
        {
            "title": "hi",
            "data-position": "top",
            "class": "z4 c3 a1 z4 b2",
            "open": True,
            "disabled": False,
        }
    )
    assert attrs.as_dict == {
        "class": "a1 b2 c3 z4",
        "data-position": "top",
        "title": "hi",
        "open": True,
    }


def test_as_dict_no_classes():
    attrs = HTMLAttrs(
        {
            "title": "hi",
            "data-position": "top",
            "open": True,
        }
    )
    assert attrs.as_dict == {
        "data-position": "top",
        "title": "hi",
        "open": True,
    }


def test_render_attrs_lik_set():
    attrs = HTMLAttrs({"class": "lorem"})
    expected = 'class="ipsum lorem" data-position="top" title="hi" open'
    result = attrs.render(
        title="hi",
        data_position="top",
        classes="ipsum",
        open=True,
    )
    print(result)
    assert expected == result


def test_do_not_escape_tailwind_syntax():
    attrs = HTMLAttrs({"class": "lorem [&_a]:flex"})
    expected = 'class="[&_a]:flex ipsum lorem" title="Hi&Stuff"'
    result = attrs.render(
        **{
            "title": "Hi&Stuff",
            "class": "ipsum",
        }
    )
    print(result)
    assert expected == result


def test_do_escape_quotes_inside_attrs():
    attrs = HTMLAttrs(
        {
            "class": "lorem text-['red']",
            "title": 'I say "hey"',
            "open": True,
        }
    )
    expected = """class="lorem text-['red']" title='I say "hey"' open"""
    result = attrs.render()
    print(result)
    assert expected == result


def test_additional_attributes_are_lazily_evaluated_to_strings():
    class TestObject:
        def __str__(self):
            raise RuntimeError("Should not be called unless rendered.")

    attrs = HTMLAttrs(
        {
            "some_object": TestObject(),
        }
    )

    with pytest.raises(RuntimeError):
        attrs.render()


def test_additional_attributes_lazily_evaluated_has_string_methods():
    class TestObject:
        def __str__(self):
            return "test"

    attrs = HTMLAttrs({"some_object": TestObject()})

    assert attrs["some_object"].__str__
    assert attrs["some_object"].__repr__
    assert attrs["some_object"].__int__
    assert attrs["some_object"].__float__
    assert attrs["some_object"].__complex__
    assert attrs["some_object"].__hash__
    assert attrs["some_object"].__eq__
    assert attrs["some_object"].__lt__
    assert attrs["some_object"].__le__
    assert attrs["some_object"].__gt__
    assert attrs["some_object"].__ge__
    assert attrs["some_object"].__contains__
    assert attrs["some_object"].__len__
    assert attrs["some_object"].__getitem__
    assert attrs["some_object"].__add__
    assert attrs["some_object"].__radd__
    assert attrs["some_object"].__mul__
    assert attrs["some_object"].__mod__
    assert attrs["some_object"].__rmod__
    assert attrs["some_object"].capitalize
    assert attrs["some_object"].casefold
    assert attrs["some_object"].center
    assert attrs["some_object"].count
    assert attrs["some_object"].removeprefix
    assert attrs["some_object"].removesuffix
    assert attrs["some_object"].encode
    assert attrs["some_object"].endswith
    assert attrs["some_object"].expandtabs
    assert attrs["some_object"].find
    assert attrs["some_object"].format
    assert attrs["some_object"].format_map
    assert attrs["some_object"].index
    assert attrs["some_object"].isalpha
    assert attrs["some_object"].isalnum
    assert attrs["some_object"].isascii
    assert attrs["some_object"].isdecimal
    assert attrs["some_object"].isdigit
    assert attrs["some_object"].isidentifier
    assert attrs["some_object"].islower
    assert attrs["some_object"].isnumeric
    assert attrs["some_object"].isprintable
    assert attrs["some_object"].isspace
    assert attrs["some_object"].istitle
    assert attrs["some_object"].isupper
    assert attrs["some_object"].join
    assert attrs["some_object"].ljust
    assert attrs["some_object"].lower
    assert attrs["some_object"].lstrip
    assert attrs["some_object"].partition
    assert attrs["some_object"].replace
    assert attrs["some_object"].rfind
    assert attrs["some_object"].rindex
    assert attrs["some_object"].rjust
    assert attrs["some_object"].rpartition
    assert attrs["some_object"].rstrip
    assert attrs["some_object"].split
    assert attrs["some_object"].rsplit
    assert attrs["some_object"].splitlines
    assert attrs["some_object"].startswith
    assert attrs["some_object"].strip
    assert attrs["some_object"].swapcase
    assert attrs["some_object"].title
    assert attrs["some_object"].translate
    assert attrs["some_object"].upper
    assert attrs["some_object"].zfill

    assert attrs["some_object"].upper() == "TEST"
    assert attrs["some_object"].title() == "Test"