aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests/macro.rs
blob: 3d027d2dc180a59e6de817e5eafea5ec82813250 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use askama::Template;

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

#[test]
fn test_macro() {
    let t = MacroTemplate { s: "foo" };
    assert_eq!(t.render().unwrap(), "12foo foo foo34foo foo5");
}

#[derive(Template)]
#[template(path = "macro-no-args.html")]
struct MacroNoArgsTemplate;

#[test]
fn test_macro_no_args() {
    let t = MacroNoArgsTemplate;
    assert_eq!(t.render().unwrap(), "11the best thing111we've ever done11");
}

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

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

#[derive(Template)]
#[template(path = "deep-nested-macro.html")]
struct NestedTemplate;

#[test]
fn test_nested() {
    let t = NestedTemplate;
    assert_eq!(t.render().unwrap(), "foo");
}

#[derive(Template)]
#[template(path = "deep-import-parent.html")]
struct DeepImportTemplate;

#[test]
fn test_deep_import() {
    let t = DeepImportTemplate;
    assert_eq!(t.render().unwrap(), "foo");
}

#[derive(Template)]
#[template(path = "macro-short-circuit.html")]
struct ShortCircuitTemplate {}

#[test]
fn test_short_circuit() {
    let t = ShortCircuitTemplate {};
    assert_eq!(t.render().unwrap(), "truetruetruefalsetruetrue");
}

#[derive(Template)]
#[template(path = "nested-macro-args.html")]
struct NestedMacroArgsTemplate {}

#[test]
fn test_nested_macro_with_args() {
    let t = NestedMacroArgsTemplate {};
    assert_eq!(t.render().unwrap(), "first second");
}

#[derive(Template)]
#[template(path = "macro-import-str-cmp.html")]
struct StrCmpTemplate;

#[test]
fn str_cmp() {
    let t = StrCmpTemplate;
    assert_eq!(t.render().unwrap(), "AfooBotherCneitherD");
}

#[derive(Template)]
#[template(path = "macro-self-arg.html")]
struct MacroSelfArgTemplate<'a> {
    s: &'a str,
}

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

#[derive(Template)]
#[template(
    source = "{%- macro thrice(param1, param2) -%}
{{ param1 }} {{ param2 }}
{% endmacro -%}

{%- call thrice(param1=2, param2=3) -%}
{%- call thrice(param2=3, param1=2) -%}
{%- call thrice(3, param2=2) -%}
",
    ext = "html"
)]
struct MacroNamedArg;

#[test]
// We check that it's always the correct values passed to the
// expected argument.
fn test_named_argument() {
    assert_eq!(
        MacroNamedArg.render().unwrap(),
        "\
2 3
2 3
3 2
"
    );
}

#[derive(Template)]
#[template(
    source = r#"{% macro button(label) %}
{{- label -}}
{% endmacro %}

{%- call button(label="hi") -%}
"#,
    ext = "html"
)]
struct OnlyNamedArgument;

#[test]
fn test_only_named_argument() {
    assert_eq!(OnlyNamedArgument.render().unwrap(), "hi");
}

// Check for trailing commas.
#[derive(Template)]
#[template(
    source = r#"{% macro button(label , ) %}
{{- label -}}
{% endmacro %}
{%- macro button2(label ,) %}
{% endmacro %}
{%- macro button3(label,) %}
{% endmacro %}
{%- macro button4(label, ) %}
{% endmacro %}
{%- macro button5(label ) %}
{% endmacro %}

{%- call button(label="hi" , ) -%}
{%- call button(label="hi" ,) -%}
{%- call button(label="hi",) -%}
{%- call button(label="hi", ) -%}
{%- call button(label="hi" ) -%}
"#,
    ext = "html"
)]
struct TrailingComma;

#[test]
fn test_trailing_comma() {
    assert_eq!(TrailingComma.render().unwrap(), "hihihihihi");
}