aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-17 13:55:23 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-17 13:55:23 +0100
commit4624d6d369e8e5ce9a887acfe27be90fedf3d1ed (patch)
treecbfcb2447a01ce09c97f9ee0e126478694d3f9aa
parent2b2ddbdb94334d586cab8e4737062bb2e794d102 (diff)
downloadunja-4624d6d369e8e5ce9a887acfe27be90fedf3d1ed.tar.gz
unja-4624d6d369e8e5ce9a887acfe27be90fedf3d1ed.zip
add support for {% else %} statements
-rw-r--r--src/template.c7
-rw-r--r--tests/test_template.c36
2 files changed, 35 insertions, 8 deletions
diff --git a/src/template.c b/src/template.c
index 1f6a34f..d3765a1 100644
--- a/src/template.c
+++ b/src/template.c
@@ -231,6 +231,10 @@ int eval(char *dest, mpc_ast_t* t, struct hashmap *ctx) {
if (object_is_truthy(result)) {
eval(dest, t->children[4], ctx);
+ } else {
+ if (t->children_num > 8) {
+ eval(dest, t->children[8], ctx);
+ }
}
object_free(result);
@@ -293,8 +297,7 @@ mpc_parser_t *parser_init() {
" for : <statement_open> \"for \" <symbol> \"in\" <symbol> <statement_close> <body> <statement_open> \"endfor\" <statement_close> ;"
" block : <statement_open> \"block \" <statement_close> <statement_open> \"endblock\" <statement_close>;"
" extends : <statement_open> \"extends \" <statement_close>;"
- /* TODO: Extend parser to include expression */
- " if : <statement_open> \"if \" <expression> <statement_close> <body> <statement_open> \"endif\" <statement_close> ;"
+ " if : <statement_open> \"if \" <expression> <statement_close> <body> (<statement_open> \"else\" <statement_close> <body>)? <statement_open> \"endif\" <statement_close> ;"
" statement : <for> | <block> | <extends> | <if> ;"
" content : <print> | <statement> | <text> | <comment>;"
" body : <content>* ;"
diff --git a/tests/test_template.c b/tests/test_template.c
index 329075e..0567995 100644
--- a/tests/test_template.c
+++ b/tests/test_template.c
@@ -151,18 +151,42 @@ TEST(comments) {
free(output);
}
-
TEST(if_block) {
struct {
char *input;
char *expected_output;
} tests[] = {
- {"{% if 5 > 10 %}OK{% endif %}.", "."},
- {"{% if 10 > 5 %}OK{% endif %}.", "OK."},
- {"{% if foobar %}OK{% endif %}.", "."},
- {"{% if name %}OK{% endif %}.", "OK."},
- {"{% if age > 10 %}OK{% endif %}.", "OK."},
+ {"{% 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(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();