Add unit-tests for #517

This commit is contained in:
Markus Ebner 2025-07-05 02:13:54 +02:00 committed by René Kijewski
parent fcf793c35a
commit 1b1d604dbc
5 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,5 @@
{% import "nested-macro-callexpr.html" as otherscope %}
{% macro intermediate() %}
{{- otherscope::parent() -}}
{% endmacro %}

View File

@ -0,0 +1,11 @@
{%- macro child0() -%}
foo
{%- endmacro -%}
{%- macro child1() -%}
{{ child0() }}
{%- endmacro -%}
{%- macro parent() -%}
{{ child1() }}
{%- endmacro -%}

View File

@ -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"
);
}

View 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() {}

View 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 | | "#,
| |______^