aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2021-07-01 18:29:48 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2021-07-05 14:17:49 +0200
commit7e227907fe5d293b2d7620602d48ffba94e35dc5 (patch)
tree3dcbe0d932741fefb7a6ebf20748a9d0e2747f14 /testing/tests
parent3055c4b52160f729f27009eae5649e8201cbeecb (diff)
downloadaskama-7e227907fe5d293b2d7620602d48ffba94e35dc5.tar.gz
askama-7e227907fe5d293b2d7620602d48ffba94e35dc5.tar.bz2
askama-7e227907fe5d293b2d7620602d48ffba94e35dc5.zip
Implement destructoring of structs
This PR implements the destructoring of structs on the lhs of "let" and "for" statements.
Diffstat (limited to 'testing/tests')
-rw-r--r--testing/tests/let_destructoring.rs107
1 files changed, 107 insertions, 0 deletions
diff --git a/testing/tests/let_destructoring.rs b/testing/tests/let_destructoring.rs
new file mode 100644
index 0000000..20f7fff
--- /dev/null
+++ b/testing/tests/let_destructoring.rs
@@ -0,0 +1,107 @@
+use askama::Template;
+
+#[derive(Template)]
+#[template(source = "{% let (a, b, c) = v %}{{a}}{{b}}{{c}}", ext = "txt")]
+struct LetDestructoringTuple {
+ v: (i32, i32, i32),
+}
+
+#[test]
+fn test_let_destruct_tuple() {
+ let t = LetDestructoringTuple { v: (1, 2, 3) };
+ assert_eq!(t.render().unwrap(), "123");
+}
+
+struct UnnamedStruct(i32, i32, i32);
+
+#[derive(Template)]
+#[template(
+ source = "{% let UnnamedStruct(a, b, c) = v %}{{a}}{{b}}{{c}}",
+ ext = "txt"
+)]
+struct LetDestructoringUnnamedStruct {
+ v: UnnamedStruct,
+}
+
+#[test]
+fn test_let_destruct_unnamed_struct() {
+ let t = LetDestructoringUnnamedStruct {
+ v: UnnamedStruct(1, 2, 3),
+ };
+ assert_eq!(t.render().unwrap(), "123");
+}
+
+#[derive(Template)]
+#[template(
+ source = "{% let UnnamedStruct(a, b, c) = v %}{{a}}{{b}}{{c}}",
+ ext = "txt"
+)]
+struct LetDestructoringUnnamedStructRef<'a> {
+ v: &'a UnnamedStruct,
+}
+
+#[test]
+fn test_let_destruct_unnamed_struct_ref() {
+ let v = UnnamedStruct(1, 2, 3);
+ let t = LetDestructoringUnnamedStructRef { v: &v };
+ assert_eq!(t.render().unwrap(), "123");
+}
+
+struct NamedStruct {
+ a: i32,
+ b: i32,
+ c: i32,
+}
+
+#[derive(Template)]
+#[template(
+ source = "{% let NamedStruct { a, b: d, c } = v %}{{a}}{{d}}{{c}}",
+ ext = "txt"
+)]
+struct LetDestructoringNamedStruct {
+ v: NamedStruct,
+}
+
+#[test]
+fn test_let_destruct_named_struct() {
+ let t = LetDestructoringNamedStruct {
+ v: NamedStruct { a: 1, b: 2, c: 3 },
+ };
+ assert_eq!(t.render().unwrap(), "123");
+}
+
+#[derive(Template)]
+#[template(
+ source = "{% let NamedStruct { a, b: d, c } = v %}{{a}}{{d}}{{c}}",
+ ext = "txt"
+)]
+struct LetDestructoringNamedStructRef<'a> {
+ v: &'a NamedStruct,
+}
+
+#[test]
+fn test_let_destruct_named_struct_ref() {
+ let v = NamedStruct { a: 1, b: 2, c: 3 };
+ let t = LetDestructoringNamedStructRef { v: &v };
+ assert_eq!(t.render().unwrap(), "123");
+}
+
+mod some {
+ pub mod path {
+ pub struct Struct<'a>(pub &'a str);
+ }
+}
+
+#[derive(Template)]
+#[template(source = "{% let some::path::Struct(v) = v %}{{v}}", ext = "txt")]
+struct LetDestructoringWithPath<'a> {
+ v: some::path::Struct<'a>,
+}
+
+#[test]
+fn test_let_destruct_with_path() {
+ let t = LetDestructoringWithPath {
+ v: some::path::Struct("hello"),
+ };
+ assert_eq!(t.render().unwrap(), "hello");
+}