aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests/tuple.rs
blob: 37d313c467ace1499b28d3c07e88cd64cc60be90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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!");
}