aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests/tuple.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-01-14 15:32:53 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-01-28 11:21:06 +0100
commitda0b6ead0e75082bfffa24f8529d1f83961ba45c (patch)
tree58bbdb1de244e99c77b378a9a9d4eaf4dd873474 /testing/tests/tuple.rs
parent85ad2e6ba3a205e9b648431318aac0e75c027a82 (diff)
downloadaskama-da0b6ead0e75082bfffa24f8529d1f83961ba45c.tar.gz
askama-da0b6ead0e75082bfffa24f8529d1f83961ba45c.tar.bz2
askama-da0b6ead0e75082bfffa24f8529d1f83961ba45c.zip
Parse tuple expressions
Askama understands how to destructure tuples in let and match statements, but it does not understand how to build a tuple. This PR fixes this shortcoming.
Diffstat (limited to '')
-rw-r--r--testing/tests/tuple.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/testing/tests/tuple.rs b/testing/tests/tuple.rs
new file mode 100644
index 0000000..37d313c
--- /dev/null
+++ b/testing/tests/tuple.rs
@@ -0,0 +1,82 @@
+use askama::Template;
+
+struct Post {
+ id: u32,
+}
+
+struct Client<'a> {
+ can_post_ids: &'a [u32],
+ can_update_ids: &'a [u32],
+}
+
+impl Client<'_> {
+ fn can_post(&self, post: &Post) -> bool {
+ self.can_post_ids.contains(&post.id)
+ }
+
+ fn can_update(&self, post: &Post) -> bool {
+ self.can_update_ids.contains(&post.id)
+ }
+}
+
+#[derive(Template)]
+#[template(
+ source = r#"
+{%- match (client.can_post(post), client.can_update(post)) -%}
+ {%- when (false, false) -%}
+ No!
+ {%- when (can_post, can_update) -%}
+ <ul>
+ {%- if can_post -%}<li>post</li>{%- endif -%}
+ {%- if can_update -%}<li>update</li>{%- endif -%}
+ </ul>
+{%- endmatch -%}
+"#,
+ ext = "txt"
+)]
+struct TupleTemplate<'a> {
+ client: &'a Client<'a>,
+ post: &'a Post,
+}
+
+#[test]
+fn test_tuple() {
+ let template = TupleTemplate {
+ client: &Client {
+ can_post_ids: &[1, 2],
+ can_update_ids: &[2, 3],
+ },
+ post: &Post { id: 1 },
+ };
+ assert_eq!(template.render().unwrap(), "<ul><li>post</li></ul>");
+
+ let template = TupleTemplate {
+ client: &Client {
+ can_post_ids: &[1, 2],
+ can_update_ids: &[2, 3],
+ },
+ post: &Post { id: 2 },
+ };
+ assert_eq!(
+ template.render().unwrap(),
+ "<ul><li>post</li><li>update</li></ul>"
+ );
+
+ let template = TupleTemplate {
+ client: &Client {
+ can_post_ids: &[1, 2],
+ can_update_ids: &[2, 3],
+ },
+ post: &Post { id: 3 },
+ };
+ assert_eq!(template.render().unwrap(), "<ul><li>update</li></ul>");
+
+ let template = TupleTemplate {
+ client: &Client {
+ can_post_ids: &[1, 2],
+ can_update_ids: &[2, 3],
+ },
+ post: &Post { id: 4 },
+ };
+ assert_eq!(template.render().unwrap(), "No!");
+}