aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests/vars.rs
blob: d70a084920ad33d999a06ecada19a97a3da7e7ac (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
83
84
85
86
87
88
89
90
91
92
93
#![allow(clippy::useless_let_if_seq)]

use askama::Template;

#[derive(Template)]
#[template(source = "{% let v = s %}{{ v }}", ext = "txt")]
struct LetTemplate<'a> {
    s: &'a str,
}

#[test]
fn test_let() {
    let t = LetTemplate { s: "foo" };
    assert_eq!(t.render().unwrap(), "foo");
}

#[derive(Template)]
#[template(path = "let.html")]
struct LetTupleTemplate<'a> {
    s: &'a str,
    t: (&'a str, &'a str),
}

#[test]
fn test_let_tuple() {
    let t = LetTupleTemplate {
        s: "foo",
        t: ("bar", "bazz"),
    };
    assert_eq!(t.render().unwrap(), "foo\nbarbazz");
}

#[derive(Template)]
#[template(path = "let-decl.html")]
struct LetDeclTemplate<'a> {
    cond: bool,
    s: &'a str,
}

#[test]
fn test_let_decl() {
    let t = LetDeclTemplate {
        cond: false,
        s: "bar",
    };
    assert_eq!(t.render().unwrap(), "bar");
}

#[derive(Template)]
#[template(path = "let-shadow.html")]
struct LetShadowTemplate {
    cond: bool,
}

impl LetShadowTemplate {
    fn tuple() -> (i32, i32) {
        (4, 5)
    }
}

#[test]
fn test_let_shadow() {
    let t = LetShadowTemplate { cond: true };
    assert_eq!(t.render().unwrap(), "22-1-33-11-22");

    let t = LetShadowTemplate { cond: false };
    assert_eq!(t.render().unwrap(), "222-1-333-4-5-11-222");
}

#[derive(Template)]
#[template(source = "{% for v in self.0 %}{{ v }}{% endfor %}", ext = "txt")]
struct SelfIterTemplate(Vec<usize>);

#[test]
fn test_self_iter() {
    let t = SelfIterTemplate(vec![1, 2, 3]);
    assert_eq!(t.render().unwrap(), "123");
}

#[derive(Template)]
#[template(
    source = "{% if true %}{% let t = a.unwrap() %}{{ t }}{% endif %}",
    ext = "txt"
)]
struct IfLet {
    a: Option<&'static str>,
}

#[test]
fn test_if_let() {
    let t = IfLet { a: Some("foo") };
    assert_eq!(t.render().unwrap(), "foo");
}