Debian Codegraph @

tests/test_extension.py

raw
1 import jinja2
2 import pytest
3 from markupsafe import Markup
4
5 from jinjax import Catalog, JinjaX
6
7
8 VALID_DATA = (
9 # Simple case
10 (
11 """<Foo bar="baz">content</Foo>""",
12 """{% call(_slot="") catalog.irender("Foo", __prefix=__prefix, **{"bar":"baz"}) -%}content{%- endcall %}""",
13 ),
14 # Self-closing tag
15 (
16 """<Alert type="success" message="Success!" />""",
17 """{{ catalog.irender("Alert", __prefix=__prefix, **{"type":"success", "message":"Success!"}) }}""",
18 ),
19 # No attributes
20 (
21 """<Foo>content</Foo>""",
22 """{% call(_slot="") catalog.irender("Foo", __prefix=__prefix, **{}) -%}content{%- endcall %}""",
23 ),
24 # No attributes, self-closing tag
25 (
26 """<Foo />""",
27 """{{ catalog.irender("Foo", __prefix=__prefix, **{}) }}""",
28 ),
29 # Line breaks
30 (
31 """<Foo
32 bar="baz"
33 lorem="ipsum"
34 >content</Foo>""",
35 """{% call(_slot="") catalog.irender("Foo", __prefix=__prefix, **{"bar":"baz", "lorem":"ipsum"}) -%}content{%- endcall %}""",
36 ),
37 # Line breaks, self-closing tag
38 (
39 """<Foo
40 bar="baz"
41 lorem="ipsum"
42 green
43 />""",
44 """{{ catalog.irender("Foo", __prefix=__prefix, **{"bar":"baz", "lorem":"ipsum", "green":True}) }}""",
45 ),
46 # Subfolder in tag name
47 (
48 """<sub.Alert type="success">content</sub.Alert>""",
49 """{% call(_slot="") catalog.irender("sub.Alert", __prefix=__prefix, **{"type":"success"}) -%}content{%- endcall %}""",
50 ),
51 # Python expression in attribute and boolean attributes
52 (
53 """<Foo bar={{ 42 + 4 }} green large>content</Foo>""",
54 """{% call(_slot="") catalog.irender("Foo", __prefix=__prefix, **{"bar":42 + 4, "green":True, "large":True}) -%}content{%- endcall %}""",
55 ),
56 # Prefix in tag name and `'}}'` in attribute
57 (
58 """<ui:Button lorem={{ 'ipsum }}' }} foo="bar">content</ui:Button>""",
59 """{% call(_slot="") catalog.irender("ui:Button", __prefix=__prefix, **{"lorem":'ipsum }}', "foo":"bar"}) -%}content{%- endcall %}""",
60 ),
61 # `>` in expression
62 (
63 """<CloseBtn disabled={{ num > 4 }} />""",
64 """{{ catalog.irender("CloseBtn", __prefix=__prefix, **{"disabled":num > 4}) }}""",
65 ),
66 # `>` in attribute value
67 (
68 """<CloseBtn data-closer-action="click->closer#close" />""",
69 """{{ catalog.irender("CloseBtn", __prefix=__prefix, **{"data_closer_action":"click->closer#close"}) }}""",
70 ),
71 )
72
73
74 @pytest.mark.parametrize("source, expected", VALID_DATA)
75 def test_process_valid_tags(source, expected):
76 # Test the process_tags method of the JinjaX extension
77 env = jinja2.Environment()
78 jinjax = JinjaX(env)
79 result = jinjax.process_tags(source)
80 print(result)
81 assert result == expected
82
83
84 INVALID_DATA = (
85 # Tag not closed
86 (
87 """<Foo bar="baz">content aslasals ls,als,as""",
88 jinja2.TemplateSyntaxError,
89 "Unclosed component",
90 ),
91 # String attribute not closed
92 (
93 """<Foo bar="baz>content lorem ipsumsdsd""",
94 jinja2.TemplateSyntaxError,
95 "Syntax error",
96 ),
97 # Expression not closed
98 (
99 """<Foo bar={{ 42 + 4>content</Foo>""",
100 jinja2.TemplateSyntaxError,
101 "Syntax error",
102 ),
103 # Expression not opem
104 (
105 """<Foo bar=42 + 4}}>content</Foo>""",
106 jinja2.TemplateSyntaxError,
107 "Syntax error",
108 ),
109 )
110
111
112 @pytest.mark.parametrize("source, exception, match", INVALID_DATA)
113 def test_process_invalid_tags(source, exception, match):
114 # Test the process_tags method of the JinjaX extension
115 env = jinja2.Environment()
116 jinjax = JinjaX(env)
117 with pytest.raises(exception, match=f".*{match}.*"):
118 jinjax.process_tags(source)
119
120
121 def test_process_nested_Same_tag():
122 # Test the process_tags method for the same tag nested
123 env = jinja2.Environment()
124 jinjax = JinjaX(env)
125 source = """
126 <Card class="card">
127 WTF
128 <Card class="card-header">abc</Card>
129 <Card class="card-body">
130 <div><Card>Text</Card></div>
131 </Card>
132 </Card>
133 """
134 expected = """
135 {% call(_slot="") catalog.irender("Card", __prefix=__prefix, **{"class":"card"}) -%}
136 WTF
137 {% call(_slot="") catalog.irender("Card", __prefix=__prefix, **{"class":"card-header"}) -%}abc{%- endcall %}
138 {% call(_slot="") catalog.irender("Card", __prefix=__prefix, **{"class":"card-body"}) -%}
139 <div>{% call(_slot="") catalog.irender("Card", __prefix=__prefix, **{}) -%}Text{%- endcall %}</div>
140 {%- endcall %}
141 {%- endcall %}
142 """
143 result = jinjax.process_tags(source)
144 print(result)
145 assert result.strip() == expected.strip()
146
147
148 def test_finalizing_assets(folder):
149 # Test that finalizing assets works as expected when reusing Jinja env
150 env = jinja2.Environment(loader=jinja2.FileSystemLoader(folder))
151 env.add_extension(JinjaX)
152
153 catalog = Catalog(jinja_env=env)
154 catalog.add_folder(folder)
155
156 (folder / "Parent.jinja").write_text(
157 """
158 {{ catalog.render_assets() }}
159 {{ content }}""".strip()
160 )
161 (folder / "Comp1.jinja").write_text(
162 """
163 {#css "a.css" #}
164 {#js "a.js" #}
165 <Parent />""".strip()
166 )
167 (folder / "index.html").write_text("<Comp1 />".strip())
168
169 expected = """
170 <link rel="stylesheet" href="/static/components/a.css">
171 <script type="module" src="/static/components/a.js"></script>""".strip()
172
173 template = env.get_template("index.html")
174 html = template.render()
175 assert html == Markup(expected)
176
177
178 def test_finalizing_assets_autoescape(folder):
179 # Deferred assets must stay safe Markup so they are not escaped (issue #140)
180 env = jinja2.Environment(
181 loader=jinja2.FileSystemLoader(folder), autoescape=True
182 )
183 env.add_extension(JinjaX)
184
185 catalog = Catalog(jinja_env=env)
186 catalog.add_folder(folder)
187
188 (folder / "Parent.jinja").write_text(
189 """
190 {{ catalog.render_assets() }}
191 {{ content }}""".strip()
192 )
193 (folder / "Comp1.jinja").write_text(
194 """
195 {#css "a.css" #}
196 {#js "a.js" #}
197 <Parent />""".strip()
198 )
199 (folder / "index.html").write_text("<Comp1 />".strip())
200
201 expected = """
202 <link rel="stylesheet" href="/static/components/a.css">
203 <script type="module" src="/static/components/a.js"></script>""".strip()
204
205 template = env.get_template("index.html")
206 html = template.render()
207 assert html == Markup(expected)