diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-01-25 04:46:19 +0100 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-01-25 04:46:19 +0100 |
commit | 35c4f119ca188fd9e90a2bbada864f9d121459ba (patch) | |
tree | db9cf8adce3ad0b228e636d5b343b2466ac51012 | |
parent | c7297cb5e74727c136547f588503e488ca40dd30 (diff) | |
download | blogc-35c4f119ca188fd9e90a2bbada864f9d121459ba.tar.gz blogc-35c4f119ca188fd9e90a2bbada864f9d121459ba.tar.bz2 blogc-35c4f119ca188fd9e90a2bbada864f9d121459ba.zip |
renderer: fix bug when 'if' evals to false after 'if' evals to true
-rw-r--r-- | src/renderer.c | 10 | ||||
-rw-r--r-- | tests/check_renderer.c | 32 |
2 files changed, 37 insertions, 5 deletions
diff --git a/src/renderer.c b/src/renderer.c index 0f5d10a..5e07b0c 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -134,7 +134,6 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing char *defined = NULL; unsigned int if_count = 0; - unsigned int if_skip = 0; b_slist_t *foreach_var = NULL; b_slist_t *foreach_var_start = NULL; @@ -237,6 +236,7 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing case BLOGC_TEMPLATE_IF_STMT: case BLOGC_TEMPLATE_IFDEF_STMT: + if_count = 0; defined = NULL; if (stmt->value != NULL) defined = blogc_format_variable(stmt->value, config, @@ -283,7 +283,6 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing evaluate = true; } if (!evaluate) { - if_skip = if_count; // at this point we can just skip anything, counting the // number of 'if's, to know how many 'endif's we need to @@ -299,11 +298,11 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing continue; } if (stmt->type == BLOGC_TEMPLATE_ENDIF_STMT) { - if (if_count > if_skip) { + if (if_count > 0) { if_count--; continue; } - if (if_count == if_skip) + if (if_count == 0) break; } } @@ -314,7 +313,8 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing break; case BLOGC_TEMPLATE_ENDIF_STMT: - if_count--; + if (if_count > 0) + if_count--; break; case BLOGC_TEMPLATE_FOREACH_STMT: diff --git a/tests/check_renderer.c b/tests/check_renderer.c index 360f067..d6bc947 100644 --- a/tests/check_renderer.c +++ b/tests/check_renderer.c @@ -687,6 +687,37 @@ test_render_respect_variable_scope(void **state) static void +test_render_ifcount_bug(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% ifdef TITLE %}<h3>{{ TITLE }}</h3>{% endif %}\n" + "{% ifdef IS_POST %}\n" + "{% ifdef ASD %}ASD{% endif %}\n" + "{% endif %}\n" + "{% endblock %}\n"; + blogc_error_t *err = NULL; + b_slist_t *l = blogc_template_parse(str, strlen(str), &err); + assert_non_null(l); + assert_null(err); + b_slist_t *s = NULL; + s = b_slist_append(s, b_trie_new(free)); + b_trie_insert(s->data, "TITLE", b_strdup("bola")); + b_trie_t *c = b_trie_new(free); + char *out = blogc_render(l, s, c, false); + assert_string_equal(out, + "\n" + "<h3>bola</h3>\n" + "\n" + "\n"); + b_trie_free(c); + blogc_template_free_stmts(l); + b_slist_free_full(s, (b_free_func_t) b_trie_free); + free(out); +} + + +static void test_get_variable(void **state) { b_trie_t *g = b_trie_new(free); @@ -897,6 +928,7 @@ main(void) unit_test(test_render_outside_block), unit_test(test_render_prefer_local_variable), unit_test(test_render_respect_variable_scope), + unit_test(test_render_ifcount_bug), unit_test(test_get_variable), unit_test(test_get_variable_only_local), unit_test(test_get_variable_only_global), |