aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests/matches.rs
blob: 380a69c5c501ffc4e8f0fa856814671afc63e8cf (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#![allow(clippy::blacklisted_name)]

use askama::Template;

#[derive(Template)]
#[template(path = "match-opt.html")]
struct MatchOptTemplate<'a> {
    item: Option<&'a str>,
}

#[derive(Template)]
#[template(path = "match-opt.html")]
struct MatchOptRefTemplate<'a> {
    item: &'a Option<&'a str>,
}

#[test]
fn test_match_option() {
    let s = MatchOptTemplate { item: Some("foo") };
    assert_eq!(s.render().unwrap(), "\nFound literal foo\n");

    let s = MatchOptTemplate { item: Some("bar") };
    assert_eq!(s.render().unwrap(), "\nFound bar\n");

    let s = MatchOptTemplate { item: None };
    assert_eq!(s.render().unwrap(), "\nNot Found\n");
}

#[test]
fn test_match_ref_deref() {
    let s = MatchOptRefTemplate { item: &Some("foo") };
    assert_eq!(s.render().unwrap(), "\nFound literal foo\n");
}

#[derive(Template)]
#[template(path = "match-literal.html")]
struct MatchLitTemplate<'a> {
    item: &'a str,
}

#[test]
fn test_match_literal() {
    let s = MatchLitTemplate { item: "bar" };
    assert_eq!(s.render().unwrap(), "\nFound literal bar\n");

    let s = MatchLitTemplate { item: "qux" };
    assert_eq!(s.render().unwrap(), "\nElse found qux\n");
}

#[derive(Template)]
#[template(path = "match-literal-char.html")]
struct MatchLitCharTemplate {
    item: char,
}

#[test]
fn test_match_literal_char() {
    let s = MatchLitCharTemplate { item: 'b' };
    assert_eq!(s.render().unwrap(), "\nFound literal b\n");

    let s = MatchLitCharTemplate { item: 'c' };
    assert_eq!(s.render().unwrap(), "\nElse found c\n");
}

#[derive(Template)]
#[template(path = "match-literal-num.html")]
struct MatchLitNumTemplate {
    item: u32,
}

#[test]
fn test_match_literal_num() {
    let s = MatchLitNumTemplate { item: 42 };
    assert_eq!(s.render().unwrap(), "\nFound answer to everything\n");

    let s = MatchLitNumTemplate { item: 23 };
    assert_eq!(s.render().unwrap(), "\nElse found 23\n");
}

#[allow(dead_code)]
enum Color {
    Rgb { r: u32, g: u32, b: u32 },
    GrayScale(u32),
    Cmyk(u32, u32, u32, u32),
}

#[derive(Template)]
#[template(path = "match-custom-enum.html")]
struct MatchCustomEnumTemplate {
    color: Color,
}

#[test]
fn test_match_custom_enum() {
    let s = MatchCustomEnumTemplate {
        color: Color::Rgb {
            r: 160,
            g: 0,
            b: 255,
        },
    };
    assert_eq!(s.render().unwrap(), "\nColorful: #A000FF\n");
}

#[derive(Template)]
#[template(path = "match-no-ws.html")]
struct MatchNoWhitespace {
    foo: Option<usize>,
}

#[test]
fn test_match_no_whitespace() {
    let s = MatchNoWhitespace { foo: Some(1) };
    assert_eq!(s.render().unwrap(), "1");
}

#[derive(Template)]
#[template(
    source = "{% match foo %}{% when Some(bar) %}{{ bar }}{% when None %}{% endmatch %}",
    ext = "txt"
)]
struct MatchWithoutWithKeyword {
    foo: Option<usize>,
}

#[test]
fn test_match_without_with_keyword() {
    let s = MatchWithoutWithKeyword { foo: Some(1) };
    assert_eq!(s.render().unwrap(), "1");
    let s = MatchWithoutWithKeyword { foo: None };
    assert_eq!(s.render().unwrap(), "");
}