mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-27 13:00:57 +00:00
Add unit-tests for #517
This commit is contained in:
parent
fcf793c35a
commit
1b1d604dbc
5
testing/templates/macro-import-callexpr.html
Normal file
5
testing/templates/macro-import-callexpr.html
Normal file
@ -0,0 +1,5 @@
|
||||
{% import "nested-macro-callexpr.html" as otherscope %}
|
||||
|
||||
{% macro intermediate() %}
|
||||
{{- otherscope::parent() -}}
|
||||
{% endmacro %}
|
11
testing/templates/nested-macro-callexpr.html
Normal file
11
testing/templates/nested-macro-callexpr.html
Normal file
@ -0,0 +1,11 @@
|
||||
{%- macro child0() -%}
|
||||
foo
|
||||
{%- endmacro -%}
|
||||
|
||||
{%- macro child1() -%}
|
||||
{{ child0() }}
|
||||
{%- endmacro -%}
|
||||
|
||||
{%- macro parent() -%}
|
||||
{{ child1() }}
|
||||
{%- endmacro -%}
|
@ -494,3 +494,114 @@ fn test_macro_default_value_generics() {
|
||||
|
||||
assert_eq!(Example.render().unwrap(), "---> 3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expr_macro_call_same_ctx() {
|
||||
#[derive(Template)]
|
||||
#[template(
|
||||
source = "
|
||||
{%- macro samescope(optarg=1+2) -%}
|
||||
{{ optarg }}
|
||||
{% endmacro -%}
|
||||
|
||||
{{- samescope() -}}
|
||||
{{- samescope(1) -}}
|
||||
",
|
||||
ext = "html"
|
||||
)]
|
||||
struct ExprMacroCall;
|
||||
|
||||
// primarily checking for compilation
|
||||
assert_eq!(ExprMacroCall.render().unwrap(), "3\n1\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expr_macro_call_imported() {
|
||||
#[derive(Template)]
|
||||
#[template(
|
||||
source = r#"
|
||||
{%- import "macro.html" as otherscope -%}
|
||||
{{- otherscope::twice("xxx") -}}
|
||||
"#,
|
||||
ext = "html"
|
||||
)]
|
||||
struct ExprMacroCall;
|
||||
|
||||
// primarily checking for compilation
|
||||
assert_eq!(ExprMacroCall.render().unwrap(), "xxx xxx");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expr_macro_call_imported_nested() {
|
||||
#[derive(Template)]
|
||||
#[template(
|
||||
source = r#"
|
||||
{%- import "nested-macro.html" as otherscope -%}
|
||||
{{- otherscope::parent() -}}
|
||||
"#,
|
||||
ext = "html"
|
||||
)]
|
||||
struct ExprMacroCall;
|
||||
|
||||
// primarily checking for compilation
|
||||
assert_eq!(ExprMacroCall.render().unwrap(), "foo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expr_macro_call_imported_nested2() {
|
||||
#[derive(Template)]
|
||||
#[template(
|
||||
source = r#"
|
||||
{%- import "nested-macro-callexpr.html" as otherscope -%}
|
||||
{% let child1 = "Test: Macros scoped by file" %}
|
||||
{{- otherscope::parent() -}}
|
||||
"#,
|
||||
ext = "html"
|
||||
)]
|
||||
struct ExprMacroCall;
|
||||
|
||||
// primarily checking for compilation
|
||||
assert_eq!(ExprMacroCall.render().unwrap(), "foo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expr_macro_call_importchain_nested() {
|
||||
#[derive(Template)]
|
||||
#[template(
|
||||
source = r#"
|
||||
{%- import "macro-import-callexpr.html" as otherscope -%}
|
||||
{{- otherscope::intermediate() -}}
|
||||
"#,
|
||||
ext = "html"
|
||||
)]
|
||||
struct ExprMacroCall;
|
||||
|
||||
// primarily checking for compilation
|
||||
assert_eq!(ExprMacroCall.render().unwrap(), "foo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_macro_caller_is_defined_check() {
|
||||
#[derive(Template)]
|
||||
#[template(
|
||||
source = r#"
|
||||
{%- macro testmacro() -%}
|
||||
{%- if caller is defined -%}
|
||||
{{- caller() -}}
|
||||
{%- else -%}
|
||||
no caller defined
|
||||
{%- endif -%}
|
||||
{%- endmacro -%}
|
||||
{{- testmacro() -}}
|
||||
{%- call testmacro() -%}|this time with caller{%- endcall -%}
|
||||
"#,
|
||||
ext = "html"
|
||||
)]
|
||||
struct ExprMacroCall;
|
||||
|
||||
// primarily checking for compilation
|
||||
assert_eq!(
|
||||
ExprMacroCall.render().unwrap(),
|
||||
"no caller defined|this time with caller"
|
||||
);
|
||||
}
|
||||
|
18
testing/tests/ui/macro-caller-with-callexpr.rs
Normal file
18
testing/tests/ui/macro-caller-with-callexpr.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use askama::Template;
|
||||
|
||||
// Calling into a macro that expects content (`caller`) using
|
||||
// the call-expr syntax should fail
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(
|
||||
source = r#"
|
||||
{% macro testmacro() %}
|
||||
{{caller()}}
|
||||
{% endmacro %}
|
||||
{{ testmacro() }}
|
||||
"#,
|
||||
ext = "txt"
|
||||
)]
|
||||
struct MacroCallerWithCallExpr;
|
||||
|
||||
fn main() {}
|
13
testing/tests/ui/macro-caller-with-callexpr.stderr
Normal file
13
testing/tests/ui/macro-caller-with-callexpr.stderr
Normal file
@ -0,0 +1,13 @@
|
||||
error: block is not defined for `caller`
|
||||
--> MacroCallerWithCallExpr.txt:3:12
|
||||
"()}}\n{% endmacro %}\n{{ testmacro() }}\n "
|
||||
--> tests/ui/macro-caller-with-callexpr.rs:8:14
|
||||
|
|
||||
8 | source = r#"
|
||||
| ______________^
|
||||
9 | | {% macro testmacro() %}
|
||||
10 | | {{caller()}}
|
||||
11 | | {% endmacro %}
|
||||
12 | | {{ testmacro() }}
|
||||
13 | | "#,
|
||||
| |______^
|
Loading…
x
Reference in New Issue
Block a user