diff --git a/testing/templates/macro-import-callexpr.html b/testing/templates/macro-import-callexpr.html new file mode 100644 index 00000000..319262dd --- /dev/null +++ b/testing/templates/macro-import-callexpr.html @@ -0,0 +1,5 @@ +{% import "nested-macro-callexpr.html" as otherscope %} + +{% macro intermediate() %} + {{- otherscope::parent() -}} +{% endmacro %} \ No newline at end of file diff --git a/testing/templates/nested-macro-callexpr.html b/testing/templates/nested-macro-callexpr.html new file mode 100644 index 00000000..3e3cea91 --- /dev/null +++ b/testing/templates/nested-macro-callexpr.html @@ -0,0 +1,11 @@ +{%- macro child0() -%} + foo +{%- endmacro -%} + +{%- macro child1() -%} + {{ child0() }} +{%- endmacro -%} + +{%- macro parent() -%} + {{ child1() }} +{%- endmacro -%} diff --git a/testing/tests/macro.rs b/testing/tests/macro.rs index c31a6335..44605107 100644 --- a/testing/tests/macro.rs +++ b/testing/tests/macro.rs @@ -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" + ); +} diff --git a/testing/tests/ui/macro-caller-with-callexpr.rs b/testing/tests/ui/macro-caller-with-callexpr.rs new file mode 100644 index 00000000..fba5f2f3 --- /dev/null +++ b/testing/tests/ui/macro-caller-with-callexpr.rs @@ -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() {} diff --git a/testing/tests/ui/macro-caller-with-callexpr.stderr b/testing/tests/ui/macro-caller-with-callexpr.stderr new file mode 100644 index 00000000..5cbc08c6 --- /dev/null +++ b/testing/tests/ui/macro-caller-with-callexpr.stderr @@ -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 | | "#, + | |______^