]> git.proxmox.com Git - rustc.git/blob - vendor/tera/src/renderer/tests/macros.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / vendor / tera / src / renderer / tests / macros.rs
1 use crate::context::Context;
2 use crate::tera::Tera;
3
4 use super::NestedObject;
5
6 #[test]
7 fn render_macros() {
8 let mut tera = Tera::default();
9 tera.add_raw_templates(vec![
10 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
11 (
12 "tpl",
13 "{% import \"macros\" as macros %}{% block hey %}{{macros::hello()}}{% endblock hey %}",
14 ),
15 ])
16 .unwrap();
17
18 let result = tera.render("tpl", &Context::new());
19
20 assert_eq!(result.unwrap(), "Hello".to_string());
21 }
22
23 #[test]
24 fn render_macros_expression_arg() {
25 let mut context = Context::new();
26 context.insert("pages", &vec![1, 2, 3, 4, 5]);
27 let mut tera = Tera::default();
28 tera.add_raw_templates(vec![
29 ("macros", "{% macro hello(val)%}{{val}}{% endmacro hello %}"),
30 ("tpl", "{% import \"macros\" as macros %}{{macros::hello(val=pages|last)}}"),
31 ])
32 .unwrap();
33
34 let result = tera.render("tpl", &context);
35
36 assert_eq!(result.unwrap(), "5".to_string());
37 }
38
39 #[test]
40 fn render_macros_in_child_templates_same_namespace() {
41 let mut tera = Tera::default();
42 tera.add_raw_templates(vec![
43 ("grandparent", "{% block hey %}hello{% endblock hey %}"),
44 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
45 ("macros2", "{% macro hi()%}Hi{% endmacro hi %}"),
46 ("parent", "{% extends \"grandparent\" %}{% import \"macros\" as macros %}{% block hey %}{{macros::hello()}}{% endblock hey %}"),
47 ("child", "{% extends \"parent\" %}{% import \"macros2\" as macros %}{% block hey %}{{super()}}/{{macros::hi()}}{% endblock hey %}"),
48 ]).unwrap();
49
50 let result = tera.render("child", &Context::new());
51
52 assert_eq!(result.unwrap(), "Hello/Hi".to_string());
53 }
54
55 #[test]
56 fn render_macros_in_child_templates_different_namespace() {
57 let mut tera = Tera::default();
58 tera.add_raw_templates(vec![
59 ("grandparent", "{% block hey %}hello{% endblock hey %}"),
60 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
61 ("macros2", "{% macro hi()%}Hi{% endmacro hi %}"),
62 ("parent", "{% extends \"grandparent\" %}{% import \"macros\" as macros %}{% block hey %}{{macros::hello()}}{% endblock hey %}"),
63 ("child", "{% extends \"parent\" %}{% import \"macros2\" as macros2 %}{% block hey %}{{super()}}/{{macros2::hi()}}{% endblock hey %}"),
64 ]).unwrap();
65
66 let result = tera.render("child", &Context::new());
67
68 assert_eq!(result.unwrap(), "Hello/Hi".to_string());
69 }
70
71 #[test]
72 fn render_macros_in_parent_template_with_inheritance() {
73 let mut tera = Tera::default();
74 tera.add_raw_templates(vec![
75 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
76 ("grandparent", "{% import \"macros\" as macros %}{% block hey %}{{macros::hello()}}{% endblock hey %}"),
77 ("child", "{% extends \"grandparent\" %}{% import \"macros\" as macros %}{% block hey %}{{super()}}/{{macros::hello()}}{% endblock hey %}"),
78 ]).unwrap();
79
80 let result = tera.render("child", &Context::new());
81
82 assert_eq!(result.unwrap(), "Hello/Hello".to_string());
83 }
84
85 #[test]
86 fn macro_param_arent_escaped() {
87 let mut tera = Tera::default();
88 tera.add_raw_templates(vec![
89 ("macros.html", r#"{% macro print(val) %}{{val|safe}}{% endmacro print %}"#),
90 ("hello.html", r#"{% import "macros.html" as macros %}{{ macros::print(val=my_var)}}"#),
91 ])
92 .unwrap();
93 let mut context = Context::new();
94 context.insert("my_var", &"&");
95 let result = tera.render("hello.html", &context);
96
97 assert_eq!(result.unwrap(), "&".to_string());
98 }
99
100 #[test]
101 fn render_set_tag_macro() {
102 let mut tera = Tera::default();
103 tera.add_raw_templates(vec![
104 ("macros", "{% macro hello()%}Hello{% endmacro hello %}"),
105 (
106 "hello.html",
107 "{% import \"macros\" as macros %}{% set my_var = macros::hello() %}{{my_var}}",
108 ),
109 ])
110 .unwrap();
111 let result = tera.render("hello.html", &Context::new());
112
113 assert_eq!(result.unwrap(), "Hello".to_string());
114 }
115
116 #[test]
117 fn render_macros_with_default_args() {
118 let mut tera = Tera::default();
119 tera.add_raw_templates(vec![
120 ("macros", "{% macro hello(val=1) %}{{val}}{% endmacro hello %}"),
121 ("hello.html", "{% import \"macros\" as macros %}{{macros::hello()}}"),
122 ])
123 .unwrap();
124 let result = tera.render("hello.html", &Context::new());
125
126 assert_eq!(result.unwrap(), "1".to_string());
127 }
128
129 #[test]
130 fn render_macros_override_default_args() {
131 let mut tera = Tera::default();
132 tera.add_raw_templates(vec![
133 ("macros", "{% macro hello(val=1) %}{{val}}{% endmacro hello %}"),
134 ("hello.html", "{% import \"macros\" as macros %}{{macros::hello(val=2)}}"),
135 ])
136 .unwrap();
137 let result = tera.render("hello.html", &Context::new());
138
139 assert_eq!(result.unwrap(), "2".to_string());
140 }
141
142 #[test]
143 fn render_recursive_macro() {
144 let mut tera = Tera::default();
145 tera.add_raw_templates(vec![
146 (
147 "macros",
148 "{% macro factorial(n) %}{% if n > 1 %}{{ n }} - {{ self::factorial(n=n-1) }}{% else %}1{% endif %}{{ n }}{% endmacro factorial %}",
149 ),
150 ("hello.html", "{% import \"macros\" as macros %}{{macros::factorial(n=7)}}"),
151 ]).unwrap();
152 let result = tera.render("hello.html", &Context::new());
153
154 assert_eq!(result.unwrap(), "7 - 6 - 5 - 4 - 3 - 2 - 11234567".to_string());
155 }
156
157 // https://github.com/Keats/tera/issues/202
158 #[test]
159 fn recursive_macro_with_loops() {
160 let parent = NestedObject { label: "Parent".to_string(), parent: None, numbers: vec![1, 2, 3] };
161 let child = NestedObject {
162 label: "Child".to_string(),
163 parent: Some(Box::new(parent)),
164 numbers: vec![1, 2, 3],
165 };
166 let mut context = Context::new();
167 context.insert("objects", &vec![child]);
168 let mut tera = Tera::default();
169
170 tera.add_raw_templates(vec![
171 (
172 "macros.html",
173 r#"
174 {% macro label_for(obj, sep) -%}
175 {%- if obj.parent -%}
176 {{ self::label_for(obj=obj.parent, sep=sep) }}{{sep}}
177 {%- endif -%}
178 {{obj.label}}
179 {%- for i in obj.numbers -%}{{ i }}{%- endfor -%}
180 {%- endmacro label_for %}
181 "#,
182 ),
183 (
184 "recursive",
185 r#"
186 {%- import "macros.html" as macros -%}
187 {%- for obj in objects -%}
188 {{ macros::label_for(obj=obj, sep="|") }}
189 {%- endfor -%}
190 "#,
191 ),
192 ])
193 .unwrap();
194
195 let result = tera.render("recursive", &context);
196
197 assert_eq!(result.unwrap(), "Parent123|Child123".to_string());
198 }
199
200 // https://github.com/Keats/tera/issues/250
201 #[test]
202 fn render_macros_in_included() {
203 let mut tera = Tera::default();
204 tera.add_raw_templates(vec![
205 ("macros", "{% macro my_macro() %}my macro{% endmacro %}"),
206 ("includeme", r#"{% import "macros" as macros %}{{ macros::my_macro() }}"#),
207 ("example", r#"{% include "includeme" %}"#),
208 ])
209 .unwrap();
210 let result = tera.render("example", &Context::new());
211
212 assert_eq!(result.unwrap(), "my macro".to_string());
213 }
214
215 // https://github.com/Keats/tera/issues/255
216 #[test]
217 fn import_macros_into_other_macro_files() {
218 let mut tera = Tera::default();
219 tera.add_raw_templates(vec![
220 ("submacros", "{% macro test() %}Success!{% endmacro %}"),
221 (
222 "macros",
223 r#"{% import "submacros" as sub %}{% macro test() %}{{ sub::test() }}{% endmacro %}"#,
224 ),
225 ("index", r#"{% import "macros" as macros %}{{ macros::test() }}"#),
226 ])
227 .unwrap();
228 let result = tera.render("index", &Context::new());
229
230 assert_eq!(result.unwrap(), "Success!".to_string());
231 }
232
233 #[test]
234 fn can_load_parent_macro_in_child() {
235 let mut tera = Tera::default();
236 tera.add_raw_templates(vec![
237 ("macros", "{% macro hello()%}{{ 1 }}{% endmacro hello %}"),
238 ("parent", "{% import \"macros\" as macros %}{{ macros::hello() }}{% block bob %}{% endblock bob %}"),
239 ("child", "{% extends \"parent\" %}{% block bob %}{{ super() }}Hey{% endblock bob %}"),
240 ]).unwrap();
241
242 let result = tera.render("child", &Context::new());
243
244 assert_eq!(result.unwrap(), "1Hey".to_string());
245 }
246
247 #[test]
248 fn can_load_macro_in_child() {
249 let mut tera = Tera::default();
250 tera.add_raw_templates(vec![
251 ("macros", "{% macro hello()%}{{ 1 }}{% endmacro hello %}"),
252 ("parent", "{% block bob %}{% endblock bob %}"),
253 ("child", "{% extends \"parent\" %}{% import \"macros\" as macros %}{% block bob %}{{ macros::hello() }}{% endblock bob %}"),
254 ]).unwrap();
255
256 let result = tera.render("child", &Context::new());
257
258 assert_eq!(result.unwrap(), "1".to_string());
259 }
260
261 // https://github.com/Keats/tera/issues/333
262 // this test fails in 0.11.14, worked in 0.11.10
263 #[test]
264 fn can_inherit_macro_import_from_parent() {
265 let mut tera = Tera::default();
266 tera.add_raw_templates(vec![
267 ("macros", "{% macro hello()%}HELLO{% endmacro hello %}"),
268 ("parent", "{% import \"macros\" as macros %}{% block bob %}parent{% endblock bob %}"),
269 ("child", "{% extends \"parent\" %}{% block bob %}{{macros::hello()}}{% endblock bob %}"),
270 ])
271 .unwrap();
272
273 let result = tera.render("child", &Context::default());
274 assert_eq!(result.unwrap(), "HELLO".to_string());
275 }
276
277 #[test]
278 fn can_inherit_macro_import_from_grandparent() {
279 let mut tera = Tera::default();
280 tera.add_raw_templates(vec![
281 ("macros", "{% macro hello()%}HELLO{% endmacro hello %}"),
282 ("grandparent", "{% import \"macros\" as macros %}{% block bob %}grandparent{% endblock bob %}"),
283 ("parent", "{% extends \"grandparent\" %}{% import \"macros\" as macros2 %}{% block bob %}parent{% endblock bob %}"),
284 ("child", "{% extends \"parent\" %}{% block bob %}{{macros::hello()}}-{{macros2::hello()}}{% endblock bob %}"),
285 ]).unwrap();
286
287 let result = tera.render("child", &Context::default());
288 assert_eq!(result.unwrap(), "HELLO-HELLO".to_string());
289 }
290
291 #[test]
292 fn can_load_macro_in_parent_with_grandparent() {
293 let mut tera = Tera::default();
294 tera.add_raw_templates(vec![
295 ("macros", "{% macro hello()%}{{ 1 }}{% endmacro hello %}"),
296 ("grandparent", "{% block bob %}{% endblock bob %}"),
297 ("parent", "{% extends \"grandparent\" %}{% import \"macros\" as macros %}{% block bob %}{{ macros::hello() }} - Hey{% endblock bob %}"),
298 ("child", "{% extends \"parent\" %}{% block bob %}{{ super() }}{% endblock bob %}"),
299 ]).unwrap();
300
301 let result = tera.render("child", &Context::new());
302
303 assert_eq!(result.unwrap(), "1 - Hey".to_string());
304 }
305
306 #[test]
307 fn macro_can_load_macro_from_macro_files() {
308 let mut tera = Tera::default();
309 tera.add_raw_templates(vec![
310 ("submacros", "{% macro emma() %}Emma{% endmacro emma %}"),
311 ("macros", "{% import \"submacros\" as submacros %}{% macro hommage() %}{{ submacros::emma() }} was an amazing person!{% endmacro hommage %}"),
312 ("parent", "{% block main %}Someone was a terrible person!{% endblock main %} Don't you think?"),
313 ("child", "{% extends \"parent\" %}{% import \"macros\" as macros %}{% block main %}{{ macros::hommage() }}{% endblock main %}")
314 ]).unwrap();
315
316 let result = tera.render("child", &Context::new());
317 //println!("{:#?}", result);
318 assert_eq!(result.unwrap(), "Emma was an amazing person! Don't you think?".to_string());
319 }
320
321 #[test]
322 fn macro_can_access_global_context() {
323 let mut tera = Tera::default();
324 tera.add_raw_templates(vec![
325 ("parent", r#"{% import "macros" as macros %}{{ macros::test_global() }}"#),
326 ("macros", r#"{% macro test_global() %}{% set_global value1 = "42" %}{% for i in range(end=1) %}{% set_global value2 = " is the truth." %}{% endfor %}{{ value1 }}{% endmacro test_global %}"#)
327 ]).unwrap();
328
329 let result = tera.render("parent", &Context::new());
330 assert_eq!(result.unwrap(), "42".to_string());
331 }
332
333 #[test]
334 fn template_cant_access_macros_context() {
335 let mut tera = Tera::default();
336 tera.add_raw_templates(vec![
337 ("parent", r#"{% import "macros" as macros %}{{ macros::empty() }}{{ quote | default(value="I'd rather have roses on my table than diamonds on my neck.") }}"#),
338 ("macros", r#"{% macro empty() %}{% set_global quote = "This should not reachable from the calling template!" %}{% endmacro empty %}"#)
339 ]).unwrap();
340
341 let result = tera.render("parent", &Context::new());
342 assert_eq!(result.unwrap(), "I'd rather have roses on my table than diamonds on my neck.");
343 }
344
345 #[test]
346 fn parent_macro_cant_access_child_macro_context() {
347 let mut tera = Tera::default();
348 tera.add_raw_templates(vec![
349 ("parent", "{% import \"macros\" as macros %}{{ macros::test_global() }}"),
350 ("macros", r#"{% import "moremacros" as moremacros %}{% macro test_global() %}{% set_global value1 = "ACAB" %}{{ moremacros::another_one() }}{{ value1 }}-{{ value2 | default(value="ACAB") }}{% endmacro test_global %}"#),
351 ("moremacros", r#"{% macro another_one() %}{% set_global value2 = "1312" %}{% endmacro another_one %}"#)
352 ]).unwrap();
353
354 let result = tera.render("parent", &Context::new());
355 assert_eq!(result.unwrap(), "ACAB-ACAB".to_string());
356 }