From dc864486ec8c258388b4e006d9517e6e30c34a4d Mon Sep 17 00:00:00 2001 From: Andrew Dona-Couch -- GitHub drop ICE Date: Mon, 6 Mar 2023 16:18:45 -0500 Subject: Propogate size_hint from sub-blocks (#788) Closes #786 --- testing/templates/size-child-super.txt | 2 ++ testing/templates/size-child.txt | 2 ++ testing/templates/size-parent.txt | 1 + testing/tests/size_hint.rs | 47 ++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 testing/templates/size-child-super.txt create mode 100644 testing/templates/size-child.txt create mode 100644 testing/templates/size-parent.txt create mode 100644 testing/tests/size_hint.rs (limited to 'testing') diff --git a/testing/templates/size-child-super.txt b/testing/templates/size-child-super.txt new file mode 100644 index 0000000..77c2916 --- /dev/null +++ b/testing/templates/size-child-super.txt @@ -0,0 +1,2 @@ +{% extends "size-parent.txt" %} +{% block main %}{% call super() %}{% endblock %} diff --git a/testing/templates/size-child.txt b/testing/templates/size-child.txt new file mode 100644 index 0000000..86adbf8 --- /dev/null +++ b/testing/templates/size-child.txt @@ -0,0 +1,2 @@ +{% extends "size-parent.txt" %} +{% block main %}{% if true %}123{% endif %}{% endblock %} diff --git a/testing/templates/size-parent.txt b/testing/templates/size-parent.txt new file mode 100644 index 0000000..e277a24 --- /dev/null +++ b/testing/templates/size-parent.txt @@ -0,0 +1 @@ +{% block main %}{% if true %}12345{% endif %}{% endblock %} diff --git a/testing/tests/size_hint.rs b/testing/tests/size_hint.rs new file mode 100644 index 0000000..a9f8428 --- /dev/null +++ b/testing/tests/size_hint.rs @@ -0,0 +1,47 @@ +use askama::Template; + +macro_rules! test_size { + ($source:literal, $expected:expr) => {{ + #[derive(Template)] + #[template(source = $source, ext = "txt")] + struct T(bool); + + assert_eq!(T::SIZE_HINT, $expected); + }}; +} + +#[test] +fn test_cond_size_hint() { + test_size!("{% if self.0 %}12345{% else %}12345{% endif %}", 10); +} + +#[test] +fn test_match_size_hint() { + test_size!( + "{% match self.0 %}{% when true %}12345{% else %}12345{% endmatch %}", + 5 + ); +} + +#[test] +fn test_loop_size_hint() { + test_size!("{% for i in 0..1 %}12345{% endfor %}", 7); +} + +#[test] +fn test_block_size_hint() { + #[derive(Template)] + #[template(path = "size-child.txt")] + struct T; + + assert_eq!(T::SIZE_HINT, 3); +} + +#[test] +fn test_super_size_hint() { + #[derive(Template)] + #[template(path = "size-child-super.txt")] + struct T; + + assert_eq!(T::SIZE_HINT, 5); +} -- cgit