-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_component.py
253 lines (203 loc) · 5.51 KB
/
test_component.py
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
import re
import pytest
from django.template import Context, TemplateSyntaxError
from django.template.loader import render_to_string
from django.test import override_settings
from includecontents.django.base import Template
def test_basic():
assert render_to_string("test_component/index.html") == (
"""<main>
<section class="card">
<h3 >hello</h3>
<div>some content</div>
</section>
</main>
"""
)
@override_settings(DEBUG=True)
def test_debug_mode():
render_to_string("test_component/index.html")
def test_attrs():
assert render_to_string("test_component/attrs.html") == (
"""<main>
<section class="mycard" id="topcard" x-data>
<h3 class="large">hello</h3>
<div>
some content
</div>
</section>
</main>
"""
)
def test_missing_attr():
with pytest.raises(
TemplateSyntaxError,
match='Missing required attribute "title" in <include:card>',
):
render_to_string("test_component/missing_attr.html")
def test_missing_closing_tag():
with pytest.raises(
TemplateSyntaxError,
match=re.compile(r"Unclosed tag.*<include:card>.*</include:card>"),
):
render_to_string("test_component/missing_closing_tag.html")
def test_extend_class():
assert render_to_string("test_component/extend_class.html") == (
"""<main>
<section class="mycard card lg">
<h3 >hello</h3>
<div>
some content
</div>
</section>
</main>
"""
)
assert render_to_string("test_component/extend_class.html", {"red": True}) == (
"""<main>
<section class="mycard card lg">
<h3 class="text-red">hello</h3>
<div>
some content
</div>
</section>
</main>
"""
)
def test_empty_props():
output = Template(
"Attrs: <include:empty-props id='1' hello></include:empty-props>"
).render(Context())
assert output == 'Attrs: id="1" hello/'
def test_only_advanced_props():
output = Template(
"Attrs: <include:empty-props inner.id='1'></include:empty-props>"
).render(Context())
assert output == 'Attrs: /id="1"'
def test_nested_attrs():
output = Template(
"<include:nested-attrs inner.class='2' class='1'></include:nested-attrs>"
).render(Context())
assert (
output
== """\
<div class="1 main"></div>
<div class="2 inner"></div>"""
)
def test_self_closing():
output = Template("Attrs: <include:empty-props hello />").render(Context())
assert output == "Attrs: hello/"
def test_kebab_props():
output = Template(
"Attrs: "
'<include:empty-props x-data="{foo: bar}" inner.hx-swap="innerHTML">'
"</include:empty-props>"
).render(Context())
assert output == 'Attrs: x-data="{foo: bar}"/hx-swap="innerHTML"'
def test_safe_constants():
output = Template(
"""<include:escape-props test="2>1" another="3>2"></include:escape-props>"""
).render(Context())
assert output == 'test="2>1" another="3>2"'
def test_safe_constants_fallback():
output = Template("""<include:escape-props></include:escape-props>""").render(
Context()
)
assert output == 'test="5>4"'
def test_escape_variables():
output = Template(
"""<include:escape-props test=somevar another="3>2"></include:escape-props>"""
).render(Context({"somevar": '2>1"'}))
assert output == 'test="2>1"" another="3>2"'
def test_subdir():
output = Template("<include:inside:cat />").render(Context())
assert output == "Meow"
def test_template_caching(mocker):
template = Template("""
{% for i in '12345'|make_list %}
<include:empty-props i={i} />
{% endfor %}
""")
context = Context()
context.bind_template(template)
spy = mocker.spy(template.engine, "select_template")
template.render(context)
assert spy.call_count == 1
def test_context_passthrough():
output = Template("""<include:context food="fries" />""").render(
Context({"food": "fries"})
)
assert output == "fries"
output = Template("""<include:context food={food} />""").render(
Context({"food": "sushi"})
)
assert output == "sushi"
output = Template("""<include:context />""").render(Context({"food": "pizza"}))
assert output == ""
def test_shorthand_attrs():
output = Template("""<include:context {food} />""").render(
Context({"food": "pizza"})
)
assert output == "pizza"
def test_shorthand_required_attrs():
output = Template("""<include:card {title} />""").render(
Context({"title": "hello"})
)
assert (
output
== """\
<section class="card">
<h3 >hello</h3>
<div></div>
</section>"""
)
def test_multi():
output = render_to_string("test_component/multi.html")
assert output == (
"""<main>
<section class="card">
<h3 >hello</h3>
<div>Title</div>
</section>
<section class="card">
<h3 >1</h3>
<div>loop 1</div>
</section>
<section class="card">
<h3 >2</h3>
<div>loop 2</div>
</section>
<section class="card">
<h3 >3</h3>
<div>loop 3</div>
</section>
</main>
"""
)
def test_new_attr_syntax():
output = render_to_string(
"test_component/new_attr_syntax.html", {"myTitle": "Hello World"}
)
assert output == (
"""<main>
<section class="mycard">
<h3 >Hello World</h3>
<div>
content with new syntax
</div>
</section>
<section class="mycard">
<h3 >hello world</h3>
<div>
content with new syntax unquoted
</div>
</section>
<section class="mycard">
<h3 >Hello World</h3>
<div>
content with old syntax
</div>
</section>
</main>
"""
)