aboutsummaryrefslogtreecommitdiff
path: root/tests/test_template.c
blob: 244e788cc816d3494420391c3b245d8b4d11e4fd (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "test.h"
#include "template.h"

START_TESTS 

TEST(text_only) {
    char *input = "Hello world.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello world.");
    free(output);
}

TEST(expr_number) {
    char *input = "Hello {{ 5 }}.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello 5.");
    free(output);
}

TEST(expr_string) {
    char *input = "Hello {{ \"world\" }}.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello world.");
    free(output);
}

TEST(expr_symbol) {
    char *input = "Hello {{name}}.";
    struct hashmap *ctx = hashmap_new();
    hashmap_insert(ctx, "name", "world");
    char *output = template_string(input, ctx);
    assert_str(output, "Hello world.");
    hashmap_free(ctx);
    free(output);
}

TEST(expr_add) {
    struct {
        char *input;
        char *expected_output;
    } tests[] = {
        {"{{ 5 + 5 }}.", "10."},
        {"{{ 5 + foo }}.", "15."},
        {"{{ \"foo\" + \"bar\" }}", "foobar"},
        {"{{ \"Hello \" + name }}", "Hello Danny"},
    };

    struct hashmap *ctx = hashmap_new();
    hashmap_insert(ctx, "foo", "10");
    hashmap_insert(ctx, "name", "Danny");

    for (int i=0; i < ARRAY_SIZE(tests); i++) {
        char *output = template_string(tests[i].input, ctx);
        assert_str(output, tests[i].expected_output);
        free(output);
    }

    hashmap_free(ctx);
}

TEST(expr_subtract) {
    char *input = "Hello {{ 5 - 5 }}.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello 0.");
    free(output);
}

TEST(expr_divide) {
    char *input = "Hello {{ 5 / 5 }}.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello 1.");
    free(output);
}

TEST(expr_multiply) {
    char *input = "Hello {{ 5 * 5 }}.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello 25.");
    free(output);
}

TEST(expr_gt) {
    char *input = "Hello {{ 5 > 4 }}.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello 1.");
    free(output);

    input = "Hello {{ 5 > 6 }}.";
    output = template_string(input, NULL);
    assert_str(output, "Hello 0.");
    free(output);
}

TEST(expr_lt) {
    char *input = "Hello {{ 5 < 4 }}.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello 0.");
    free(output);

    input = "Hello {{ 4 < 5 }}.";
    output = template_string(input, NULL);
    assert_str(output, "Hello 1.");
    free(output);
}

TEST(expr_whitespace) {
    char *input = "Hello \n{{-name -}}\n.";
    struct hashmap *ctx = hashmap_new();
    hashmap_insert(ctx, "name", "world");
    char *output = template_string(input, ctx);
    assert_str(output, "Helloworld.");
    hashmap_free(ctx);
    free(output);
}

TEST(for_block) {
    char *input = "{% for n in names %}{{ n }}, {% endfor %}";
    struct hashmap *ctx = hashmap_new();

    struct vector *names = vector_new(9);
    vector_push(names, "John");
    vector_push(names, "Sally");
    vector_push(names, "Eric");
    hashmap_insert(ctx, "names", names);

    char *output = template_string(input, ctx);
    assert_str(output, "John, Sally, Eric, ");
    vector_free(names);
    hashmap_free(ctx);
    free(output);
}

TEST(var_dot_notation) {
    char *input = "Hello {{user.name}}!";
     struct hashmap *user = hashmap_new();
    hashmap_insert(user, "name", "Danny");

    struct hashmap *ctx = hashmap_new();
    hashmap_insert(ctx, "user", user);
    
    char *output = template_string(input, ctx);
    assert_str(output, "Hello Danny!");
    hashmap_free(ctx);
    free(output);
}

TEST(comments) {
    char *input = "Hello {# comment here #}world.";
    char *output = template_string(input, NULL);
    assert_str(output, "Hello world.");
    free(output);
}

TEST(if_block) {

    struct {
        char *input;
        char *expected_output;
    } tests[] = {
        {"{% if 5 > 10 %}1{% endif %}.", "."},
        {"{% if 10 > 5 %}1{% endif %}.", "1."},
        {"{% if foobar %}1{% endif %}.", "."},
        {"{% if name %}1{% endif %}.", "1."},
        {"{% if age > 10 %}1{% endif %}.", "1."},
    };

    struct hashmap *ctx = hashmap_new();
    hashmap_insert(ctx, "name", "Danny");
    hashmap_insert(ctx, "age", "29");
    for (int i=0; i < ARRAY_SIZE(tests); i++) {
        char *output = template_string(tests[i].input, ctx);
        assert_str(output, tests[i].expected_output);
        free(output);
    }

    hashmap_free(ctx);
}

TEST(if_else_block) {

    struct {
        char *input;
        char *expected_output;
    } tests[] = {
        {"{% if 5 > 10 %}1{% else %}2{% endif %}", "2"},
        {"{% if 10 > 5 %}1{% else %}2{% endif %}", "1"},
        {"{% if foobar %}1{% else %}2{% endif %}", "2"},
        {"{% if name %}1{% else %}2{% endif %}", "1"},
        {"{% if age < 10 %}1{% else %}2{% endif %}", "2"},
    };

    struct hashmap *ctx = hashmap_new();
    hashmap_insert(ctx, "name", "Danny");
    hashmap_insert(ctx, "age", "29");
    for (int i=0; i < ARRAY_SIZE(tests); i++) {
        char *output = template_string(tests[i].input, ctx);
        assert_str(output, tests[i].expected_output);
        free(output);
    }

    hashmap_free(ctx);
}

TEST(buffer_alloc) {
    /* Output a string so that output buffer is longer than template buffer, 
        to test dynamic allocation */
    char *input = "{{ n }}";
    struct hashmap *ctx = hashmap_new();
    char *text = "Lorem ipsum dolor sit amet.";
    hashmap_insert(ctx, "n", text);

    char *output = template_string(input, ctx);
    assert_str(output, text);
    hashmap_free(ctx);
    free(output);
}

TEST(inheritance) {
    struct env *env = env_new("./tests/data/01/");
    char *output = template(env, "child.tmpl", NULL);
    assert_str(output, "Header\nChild content\nFooter\n");
    free(output);
    env_free(env);
}

END_TESTS