Skip to content

Commit 1f50bbb

Browse files
committed
add test cases and rework spaces
Signed-off-by: Michael Pollind <[email protected]>
1 parent 7471232 commit 1f50bbb

File tree

6 files changed

+190
-23
lines changed

6 files changed

+190
-23
lines changed

askama_derive/src/generator/node.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ impl<'a> Generator<'a, '_> {
596596
call: &'a WithSpan<'a, Call<'_>>,
597597
) -> Result<usize, CompileError> {
598598
let Call {
599-
ws,
599+
ws1,
600600
scope,
601601
name,
602602
ref args,
@@ -638,7 +638,7 @@ impl<'a> Generator<'a, '_> {
638638
self.seen_macros.push((def, ctx.file_info_of(call.span())));
639639
}
640640
self.seen_callers.push(call);
641-
self.flush_ws(ws); // Cannot handle_ws() here: whitespace from macro definition comes first
641+
self.flush_ws(ws1); // Cannot handle_ws() here: whitespace from macro definition comes first
642642
let size_hint = self.push_locals(|this| {
643643
macro_call_ensure_arg_count(call, def, ctx)?;
644644

@@ -759,7 +759,7 @@ impl<'a> Generator<'a, '_> {
759759
buf.write('}');
760760
Ok(size_hint)
761761
})?;
762-
self.prepare_ws(ws);
762+
self.prepare_ws(ws1);
763763
self.seen_macros.pop();
764764
self.seen_callers.pop();
765765
Ok(size_hint)
@@ -1107,7 +1107,7 @@ impl<'a> Generator<'a, '_> {
11071107
if ***path == Expr::Var("super") {
11081108
return self.write_block(ctx, buf, None, ws, s.span());
11091109
} else if ***path == Expr::Var("caller") {
1110-
let def = self.seen_callers.last().ok_or_else(|| {
1110+
let def = self.seen_callers.pop().ok_or_else(|| {
11111111
ctx.generate_error(format_args!("block is not defined for caller"), s.span())
11121112
})?;
11131113
let size_hint = self.push_locals(|this| {
@@ -1168,14 +1168,14 @@ impl<'a> Generator<'a, '_> {
11681168
}
11691169
}
11701170
}
1171-
1172-
let mut size_hint = this.handle(ctx, &def.nodes, buf, AstLevel::Nested)?;
11731171
this.flush_ws(def.ws2);
1172+
let mut size_hint = this.handle(ctx, &def.nodes, buf, AstLevel::Nested)?;
11741173
size_hint += this.write_buf_writable(ctx, buf)?;
11751174
buf.write('}');
11761175
Ok(size_hint)
11771176
})?;
1178-
self.prepare_ws(ws);
1177+
self.handle_ws(ws);
1178+
self.seen_callers.push(def);
11791179
return Ok(size_hint);
11801180
}
11811181
}

askama_parser/src/node.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,11 @@ impl<'a> Import<'a> {
877877

878878
#[derive(Debug, PartialEq)]
879879
pub struct Call<'a> {
880-
pub ws: Ws,
880+
pub ws1: Ws,
881881
pub caller_args: Vec<&'a str>,
882882
pub scope: Option<&'a str>,
883883
pub name: &'a str,
884884
pub args: Vec<WithSpan<'a, Expr<'a>>>,
885-
pub ws1: Ws,
886885
pub nodes: Vec<Node<'a>>,
887886
pub ws2: Ws,
888887
}
@@ -947,12 +946,11 @@ impl<'a> Call<'a> {
947946

948947
Ok(WithSpan::new(
949948
Self {
950-
ws: Ws(pws, nws),
949+
ws1: Ws(pws, nws),
951950
caller_args: call_args.unwrap_or_default(),
952951
scope,
953952
name,
954953
args,
955-
ws1: Ws(pws, pws2),
956954
nodes,
957955
ws2: Ws(pws2, nws2),
958956
},

testing/tests/calls.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,18 @@ nested
125125
assert_eq!(x.render().unwrap(), "nested");
126126
}
127127

128+
128129
#[test]
129130
fn test_caller_struct() {
130131
struct TestInput<'a> {
131-
a: &'a str,
132-
b: &'a str
132+
a: &'a str,
133+
b: &'a str,
133134
}
134135
#[derive(Template)]
135136
#[template(
136137
source = r#"
137138
{%- macro test(a) -%}
138-
{{caller(a)}}
139+
{{- caller(a) -}}
139140
{%- endmacro -%}
140141
{%- call(value) test(a) -%}
141142
a: {{value.a}}
@@ -145,13 +146,10 @@ b: {{value.b}}
145146
ext = "txt"
146147
)]
147148
struct Tmpl<'a> {
148-
a: TestInput<'a>
149+
a: TestInput<'a>,
149150
}
150151
let x = Tmpl {
151-
a: TestInput {
152-
a: "one",
153-
b: "two"
154-
}
152+
a: TestInput { a: "one", b: "two" },
155153
};
156154
assert_eq!(x.render().unwrap(), "a: one\nb: two");
157155
}
@@ -162,18 +160,18 @@ fn test_caller_args() {
162160
#[template(
163161
source = r#"
164162
{%- macro test() -%}
165-
{{- caller("test") -}}
166-
{{- caller(1) -}}
163+
{{~ caller("test") ~}}
164+
{{~ caller(1) ~}}
167165
{%- endmacro -%}
168166
{%- call(value) test() -%}
169-
nested {{value}}
167+
nested {{value}}
170168
{%- endcall -%}
171169
"#,
172170
ext = "html"
173171
)]
174172
struct CallerEmpty {}
175173
let x = CallerEmpty {};
176-
assert_eq!(x.render().unwrap(), "nested testnested 1");
174+
assert_eq!(x.render().unwrap(), "nested test\nnested 1\n");
177175
}
178176

179177
// Ensures that fields are not moved when calling a jinja macro.

testing/tests/macro.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,48 @@ fn test_default_value3() {
238238
);
239239
}
240240

241+
// This test a caller expression with expressions in the arguments.
242+
#[test]
243+
fn test_caller_expr() {
244+
#[derive(Template)]
245+
#[template(
246+
source = "{%- macro thrice(a, b, c) -%}
247+
{{caller(a+10,b - 10,a+b+c)}}
248+
{% endmacro -%}
249+
{%- call(a,b,c) thrice(10,11,13) -%}{{a}} {{b}} {{c + 1}}{%- endcall -%}
250+
",
251+
ext = "html"
252+
)]
253+
struct MacroCallerExpr;
254+
assert_eq!(MacroCallerExpr.render().unwrap(), "20 1 35\n");
255+
256+
}
257+
258+
#[test]
259+
fn test_caller_in_caller() {
260+
#[derive(Template)]
261+
#[template(
262+
source = r#"
263+
{%- macro test2() -%}
264+
{{~ caller("bb") ~}}
265+
{%- endmacro -%}
266+
{%- macro test() -%}
267+
{{~ caller("a") ~}}
268+
{%- endmacro -%}
269+
{%- call(a) test() -%}
270+
{%- call(b) test2() -%}
271+
one: {{ b }}
272+
{%- endcall -%}
273+
two: {{- a -}}
274+
{%- endcall -%}
275+
"#,
276+
ext = "txt"
277+
)]
278+
struct CallerInCaller;
279+
assert_eq!(CallerInCaller.render().unwrap(), "one: bb\ntwo:a");
280+
}
281+
282+
241283
// This test ensures that we can use declared variables as default value for
242284
// macro arguments.
243285
#[test]

testing/tests/ui/caller_arguments.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use askama::Template;
2+
3+
#[derive(Template)]
4+
#[template(
5+
source = r#"
6+
{% macro test() %}
7+
{{- caller("a", "b") -}}
8+
{%- endmacro -%}
9+
{%- call(a,b,c) test() -%}
10+
{{- a -}}
11+
{%- endcall -%}
12+
"#,
13+
ext = "txt"
14+
)]
15+
struct InvalidNumberArguments {
16+
}
17+
18+
#[derive(Template)]
19+
#[template(
20+
source = r#"
21+
{% macro test() %}
22+
{{- caller("a") -}}
23+
{%- endmacro -%}
24+
{%- call(a test() -%}
25+
{{- a -}}
26+
{%- endcall -%}
27+
"#,
28+
ext = "txt"
29+
)]
30+
struct NoClosingParen {
31+
}
32+
33+
#[derive(Template)]
34+
#[template(
35+
source = r#"
36+
{% macro test() %}
37+
{{- caller("a") -}}
38+
{%- endmacro -%}
39+
{%- call(a) test() -%}
40+
{{- caller(a) -}}
41+
{%- endcall -%}
42+
"#,
43+
ext = "txt"
44+
)]
45+
struct CallerInCaller {
46+
}
47+
48+
#[derive(Template)]
49+
#[template(
50+
source = r#"
51+
{% macro test2() %}
52+
{{ caller("bb") }}
53+
{% endmacro %}
54+
{% macro test() %}
55+
{{ caller("a") }}
56+
{%- endmacro -%}
57+
{%- call(a) test() -%}
58+
{% call(b) test2() %}
59+
{{ caller("b") }}
60+
{% endcall %}
61+
{{- a -}}
62+
{%- endcall -%}
63+
"#,
64+
ext = "txt"
65+
)]
66+
struct CallerInCaller1 {
67+
}
68+
69+
fn main() {}
70+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error: missing `c` argument
2+
--> InvalidNumberArguments.txt:3:18
3+
"(\"a\", \"b\") -}}\n {%- endmacro -%}\n {%- call(a,b,c) test() -%}\n {{- a"...
4+
--> tests/ui/caller_arguments.rs:5:14
5+
|
6+
5 | source = r#"
7+
| ______________^
8+
6 | | {% macro test() %}
9+
7 | | {{- caller("a", "b") -}}
10+
8 | | {%- endmacro -%}
11+
... |
12+
11 | | {%- endcall -%}
13+
12 | | "#,
14+
| |______^
15+
16+
error: expected `)` to close call argument list
17+
--> <source attribute>:5:15
18+
"test() -%}\n {{- a -}}\n {%- endcall -%}\n "
19+
--> tests/ui/caller_arguments.rs:20:14
20+
|
21+
20 | source = r#"
22+
| ______________^
23+
21 | | {% macro test() %}
24+
22 | | {{- caller("a") -}}
25+
23 | | {%- endmacro -%}
26+
... |
27+
26 | | {%- endcall -%}
28+
27 | | "#,
29+
| |______^
30+
31+
error: block is not defined for caller
32+
--> CallerInCaller.txt:6:18
33+
"(a) -}}\n {%- endcall -%}\n "
34+
--> tests/ui/caller_arguments.rs:35:14
35+
|
36+
35 | source = r#"
37+
| ______________^
38+
36 | | {% macro test() %}
39+
37 | | {{- caller("a") -}}
40+
38 | | {%- endmacro -%}
41+
... |
42+
41 | | {%- endcall -%}
43+
42 | | "#,
44+
| |______^
45+
46+
error: block is not defined for caller
47+
--> CallerInCaller1.txt:10:21
48+
"(\"b\") }}\n {% endcall %}\n {{- a -}}\n {%- endcall -%}\n "
49+
--> tests/ui/caller_arguments.rs:50:14
50+
|
51+
50 | source = r#"
52+
| ______________^
53+
51 | | {% macro test2() %}
54+
52 | | {{ caller("bb") }}
55+
53 | | {% endmacro %}
56+
... |
57+
62 | | {%- endcall -%}
58+
63 | | "#,
59+
| |______^

0 commit comments

Comments
 (0)