diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2021-07-01 13:36:29 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-07-05 13:48:41 +0200 |
commit | 3055c4b52160f729f27009eae5649e8201cbeecb (patch) | |
tree | a5c4fe3a90ea431c44c7725123c607379bb39bc1 /testing/tests/loops.rs | |
parent | 44c0623a581413bba0dc59f8de72abc5dded843f (diff) | |
download | askama-3055c4b52160f729f27009eae5649e8201cbeecb.tar.gz askama-3055c4b52160f729f27009eae5649e8201cbeecb.tar.bz2 askama-3055c4b52160f729f27009eae5649e8201cbeecb.zip |
Add "destructoring tuple in loop" test
Diffstat (limited to '')
-rw-r--r-- | testing/tests/loops.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/testing/tests/loops.rs b/testing/tests/loops.rs index 7b008a2..fffb39d 100644 --- a/testing/tests/loops.rs +++ b/testing/tests/loops.rs @@ -1,3 +1,6 @@ +#![allow(clippy::type_complexity)] + +use std::fmt; use std::ops::Range; use askama::Template; @@ -235,3 +238,69 @@ fn test_for_vec_attr_slice_shadowing() { }; assert_eq!(t.render().unwrap(), "1 2 3 4 5 6 "); } + +struct NotClonable<T: fmt::Display>(T); + +impl<T: fmt::Display> fmt::Display for NotClonable<T> { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(formatter) + } +} + +#[derive(Template)] +#[template( + source = "{% for (((a,), b), c) in v %}{{a}}{{b}}{{c}}-{% endfor %}", + ext = "txt" +)] +struct ForDestructoringRefTupleTemplate<'a> { + v: &'a [(((char,), NotClonable<char>), &'a char)], +} + +#[test] +fn test_for_destructoring_ref_tuple() { + let v = [ + ((('a',), NotClonable('b')), &'c'), + ((('d',), NotClonable('e')), &'f'), + ((('g',), NotClonable('h')), &'i'), + ]; + let t = ForDestructoringRefTupleTemplate { v: &v }; + assert_eq!(t.render().unwrap(), "abc-def-ghi-"); +} + +#[derive(Template)] +#[template( + source = "{% for (((a,), b), c) in v %}{{a}}{{b}}{{c}}-{% endfor %}", + ext = "txt" +)] +struct ForDestructoringTupleTemplate<'a, const N: usize> { + v: [(((char,), NotClonable<char>), &'a char); N], +} + +#[test] +fn test_for_destructoring_tuple() { + let t = ForDestructoringTupleTemplate { + v: [ + ((('a',), NotClonable('b')), &'c'), + ((('d',), NotClonable('e')), &'f'), + ((('g',), NotClonable('h')), &'i'), + ], + }; + assert_eq!(t.render().unwrap(), "abc-def-ghi-"); +} + +#[derive(Template)] +#[template( + source = "{% for (i, msg) in messages.iter().enumerate() %}{{i}}={{msg}}-{% endfor %}", + ext = "txt" +)] +struct ForEnumerateTemplate<'a> { + messages: &'a [&'a str], +} + +#[test] +fn test_for_enumerate() { + let t = ForEnumerateTemplate { + messages: &["hello", "world", "!"], + }; + assert_eq!(t.render().unwrap(), "0=hello-1=world-2=!-"); +} |