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
|
#include "test.h"
#include "template.h"
START_TESTS
TEST(text_only) {
char *input = "Hello world.";
char *output = template(input, NULL);
assert_str(output, "Hello world.");
free(output);
}
TEST(var) {
char *input = "Hello {{name}}.";
struct hashmap *ctx = hashmap_new();
hashmap_insert(ctx, "name", "world");
char *output = template(input, ctx);
assert_str(output, "Hello world.");
hashmap_free(ctx);
free(output);
}
TEST(var_whitespace) {
char *input = "Hello \n{{-name -}}\n.";
struct hashmap *ctx = hashmap_new();
hashmap_insert(ctx, "name", "world");
char *output = template(input, ctx);
assert_str(output, "Helloworld.");
hashmap_free(ctx);
free(output);
}
TEST(multiline) {
char *input = "Hello {{name}}.\nL2";
struct hashmap *ctx = hashmap_new();
hashmap_insert(ctx, "name", "world");
char *output = template(input, ctx);
assert_str(output, "Hello world.\nL2");
hashmap_free(ctx);
free(output);
}
TEST(for_block) {
char *input = "{% for n in numbers %}{{ n }}, {% endfor %}";
struct hashmap *ctx = hashmap_new();
struct vector *numbers = vector_new(3);
vector_push(numbers, "1");
vector_push(numbers, "2");
vector_push(numbers, "3");
hashmap_insert(ctx, "numbers", numbers);
char *output = template(input, ctx);
assert_str(output, "1, 2, 3, ");
vector_free(numbers);
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(input, ctx);
assert_str(output, "Hello Danny!");
hashmap_free(ctx);
free(output);
}
TEST(comments) {
char *input = "Hello {# comment here #} world.";
char *output = template(input, NULL);
assert_str(output, "Hello world.");
free(output);
}
END_TESTS
|