summaryrefslogtreecommitdiffstats
path: root/tests/test_component.py
blob: 9961b21dfb8fdc874579f740f097fcf2ed48cea1 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import pytest

from jinjax import Component, DuplicateDefDeclaration, InvalidArgument


def test_load_args():
    com = Component(
        name="Test.jinja",
        source='{#def message, lorem=4, ipsum="bar" -#}\n',
    )
    assert com.required == ["message"]
    assert com.optional == {
        "lorem": 4,
        "ipsum": "bar",
    }


def test_expression_args():
    com = Component(
        name="Test.jinja",
        source="{#def expr=1 + 2 + 3, a=1 -#}\n",
    )
    assert com.required == []
    assert com.optional == {
        "expr": 6,
        "a": 1,
    }


def test_dict_args():
    com = Component(
        name="Test.jinja",
        source="{#def expr={'a': 'b', 'c': 'd'} -#}\n",
    )
    assert com.optional == {
        "expr": {"a": "b", "c": "d"},
    }

    com = Component(
        name="Test.jinja",
        source='{#def a=1, expr={"a": "b", "c": "d"} -#}\n',
    )
    assert com.optional == {
        "a": 1,
        "expr": {"a": "b", "c": "d"},
    }


def test_lowercase_booleans():
    com = Component(
        name="Test.jinja",
        source="{#def a=false, b=true -#}\n",
    )
    assert com.optional == {
        "a": False,
        "b": True,
    }


def test_no_args():
    com = Component(
        name="Test.jinja",
        source="\n",
    )
    assert com.required == []
    assert com.optional == {}


def test_fails_when_invalid_name():
    with pytest.raises(InvalidArgument):
        source = "{#def 000abc -#}\n"
        co = Component(name="", source=source)
        print(co.required, co.optional)


def test_fails_when_missing_comma_between_args():
    with pytest.raises(InvalidArgument):
        source = "{#def lorem ipsum -#}\n"
        co = Component(name="", source=source)
        print(co.required, co.optional)


def test_fails_when_missing_quotes_arround_default_value():
    with pytest.raises(InvalidArgument):
        source = "{#def lorem=ipsum -#}\n"
        co = Component(name="", source=source)
        print(co.required, co.optional)


def test_fails_when_prop_is_expression():
    with pytest.raises(InvalidArgument):
        source = "{#def a-b -#}\n"
        co = Component(name="", source=source)
        print(co.required, co.optional)


def test_fails_when_extra_comma_between_args():
    with pytest.raises(InvalidArgument):
        source = "{#def a, , b -#}\n"
        co = Component(name="", source=source)
        print(co.required, co.optional)


def test_comma_in_default_value():
    com = Component(
        name="Test.jinja",
        source="{#def a='lorem, ipsum' -#}\n",
    )
    assert com.optional == {"a": "lorem, ipsum"}


def test_load_assets():
    com = Component(
        name="Test.jinja",
        url_prefix="/static/",
        source="""
        {#css a.css, "b.css", c.css -#}
        {#js a.js, b.js, c.js -#}
        """,
    )
    assert com.css == ["/static/a.css", "/static/b.css", "/static/c.css"]
    assert com.js == ["/static/a.js", "/static/b.js", "/static/c.js"]


def test_no_comma_in_assets_list_is_your_problem():
    com = Component(
        name="Test.jinja",
        source="{#js a.js b.js c.js -#}\n",
        url_prefix="/static/"
    )
    assert com.js == ["/static/a.js b.js c.js"]


def test_load_metadata_in_any_order():
    com = Component(
        name="Test.jinja",
        source="""
        {#css a.css #}
        {#def lorem, ipsum=4 #}
        {#js a.js #}
        """,
    )
    assert com.required == ["lorem"]
    assert com.optional == {"ipsum": 4}
    assert com.css == ["a.css"]
    assert com.js == ["a.js"]


def test_ignore_metadata_if_not_first():
    com = Component(
        name="Test.jinja",
        source="""
        I am content
        {#css a.css #}
        {#def lorem, ipsum=4 #}
        {#js a.js #}
        """,
    )
    assert com.required == []
    assert com.optional == {}
    assert com.css == []
    assert com.js == []


def test_fail_with_more_than_one_args_declaration():
    with pytest.raises(DuplicateDefDeclaration):
        Component(
            name="Test.jinja",
            source="""
            {#def lorem, ipsum=4 #}
            {#def a, b, c, ipsum="nope" #}
            """,
        )


def test_merge_repeated_css_or_js_declarations():
    com = Component(
        name="Test.jinja",
        source="""
        {#css a.css #}
        {#def lorem, ipsum=4 #}
        {#css b.css #}
        {#js a.js #}
        {#js b.js #}
        """,
    )
    assert com.required == ["lorem"]
    assert com.optional == {"ipsum": 4}
    assert com.css == ["a.css", "b.css"]
    assert com.js == ["a.js", "b.js"]


def test_linejump_in_args_decl():
    com = Component(
        name="Test.jinja",
        source='{#def\n  message,\n  lorem=4,\n  ipsum="bar"\n#}\n',
    )
    assert com.required == ["message"]
    assert com.optional == {
        "lorem": 4,
        "ipsum": "bar",
    }


def test_global_assets():
    com = Component(
        name="Test.jinja",
        source="""
        {#css a.css, /static/shared/b.css, http://example.com/cdn.css #}
        {#js "http://example.com/cdn.js", a.js, /static/shared/b.js #}
        """,
    )
    assert com.css == ["a.css", "/static/shared/b.css", "http://example.com/cdn.css"]
    assert com.js == ["http://example.com/cdn.js", "a.js", "/static/shared/b.js"]


def test_types_in_args_decl():
    com = Component(
        name="Test.jinja",
        source="""{# def
  ring_class: str = "ring-1 ring-black",
  rounded_class: str = "rounded-2xl md:rounded-3xl",

  image: str | None = None,

  title: str = "",
  p_class: str = "px-5 md:px-6 py-5 md:py-6",
  gap_class: str = "gap-4",
  content_class: str = "",

  layer_class: str | None = None,
  layer_height: int = 4,
#}"""
    )
    assert com.required == []
    print(com.optional)
    assert com.optional == {
        "ring_class": "ring-1 ring-black",
        "rounded_class": "rounded-2xl md:rounded-3xl",
        "image": None,
        "title": "",
        "p_class": "px-5 md:px-6 py-5 md:py-6",
        "gap_class": "gap-4",
        "content_class": "",
        "layer_class": None,
        "layer_height": 4,
    }


def test_comments_in_args_decl():
    com = Component(
        name="Test.jinja",
        source="""{# def
  #
  # Card style
  ring_class: str = "ring-1 ring-black",
  rounded_class: str = "rounded-2xl md:rounded-3xl",
  #
  # Image
  image: str | None = None,
  #
  # Content
  title: str = "",
  p_class: str = "px-5 md:px-6 py-5 md:py-6",
  gap_class: str = "gap-4",
  content_class: str = "",
  #
  # Decorative layer
  layer_class: str | None = None,
  layer_height: int = 4,
#}"""
    )
    assert com.required == []
    print(com.optional)
    assert com.optional == {
        "ring_class": "ring-1 ring-black",
        "rounded_class": "rounded-2xl md:rounded-3xl",
        "image": None,
        "title": "",
        "p_class": "px-5 md:px-6 py-5 md:py-6",
        "gap_class": "gap-4",
        "content_class": "",
        "layer_class": None,
        "layer_height": 4,
    }


def test_comment_after_args_decl():
    com = Component(
        name="Test.jinja",
        source="""
{# def
  arg,
#}

{#
  Some comment.
#}
Hi
""".strip())
    assert com.required == ["arg"]
    assert com.optional == {}


def test_fake_decl():
    com = Component(
        name="Test.jinja",
        source="""
{# definitely not an args decl! #}
{# def arg #}
{# jsadfghkl are letters #}
{# csssssss #}
""".strip())
    assert com.required == ["arg"]
    assert com.optional == {}