From bd0bfaa95c599a90b828dab472a67342e57ffa0c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 12 Mar 2024 21:53:49 +0100 Subject: [PATCH] Add test for block imports access --- testing/templates/extend_and_import.html | 3 +++ testing/tests/extend.rs | 27 ++++++++++++++++++++++++ testing/tests/ui/block_and_vars.rs | 13 ++++++++++++ testing/tests/ui/block_and_vars.stderr | 7 ++++++ 4 files changed, 50 insertions(+) create mode 100644 testing/templates/extend_and_import.html create mode 100644 testing/tests/extend.rs create mode 100644 testing/tests/ui/block_and_vars.rs create mode 100644 testing/tests/ui/block_and_vars.stderr diff --git a/testing/templates/extend_and_import.html b/testing/templates/extend_and_import.html new file mode 100644 index 00000000..24cd9f81 --- /dev/null +++ b/testing/templates/extend_and_import.html @@ -0,0 +1,3 @@ +{% import "macro.html" as m1 %} + +{% block header %}{% endblock %} diff --git a/testing/tests/extend.rs b/testing/tests/extend.rs new file mode 100644 index 00000000..9261b3ce --- /dev/null +++ b/testing/tests/extend.rs @@ -0,0 +1,27 @@ +use askama::Template; + +#[derive(Template)] +#[template( + source = r#"{% extends "extend_and_import.html" %} +{%- import "macro.html" as m2 -%} + +{%- macro another(param) -%} + +--> {{ param }} + +{%- endmacro -%} + +{% block header -%} +{% call m1::twice(1) %} +{% call m2::twice(2) %} +{% call another(3) %} +{%- endblock -%} +"#, + ext = "txt" +)] +struct A; + +#[test] +fn test_macro_in_block_inheritance() { + assert_eq!(A.render().unwrap(), "\n\n1 1\n2 2\n--> 3"); +} diff --git a/testing/tests/ui/block_and_vars.rs b/testing/tests/ui/block_and_vars.rs new file mode 100644 index 00000000..b0f9d071 --- /dev/null +++ b/testing/tests/ui/block_and_vars.rs @@ -0,0 +1,13 @@ +use askama::Template; + +#[derive(Template)] +#[template(source = r#"{% extends "extend_and_import.html" %} + +{% let x = 12 %} +{% block header -%} +{{ x }} +{% endblock %}"#, ext = "html")] +struct A; + +fn main() { +} diff --git a/testing/tests/ui/block_and_vars.stderr b/testing/tests/ui/block_and_vars.stderr new file mode 100644 index 00000000..91a5f1b1 --- /dev/null +++ b/testing/tests/ui/block_and_vars.stderr @@ -0,0 +1,7 @@ +error[E0609]: no field `x` on type `&A` + --> tests/ui/block_and_vars.rs:3:10 + | +3 | #[derive(Template)] + | ^^^^^^^^ unknown field + | + = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)