diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-07-03 03:25:12 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-07-03 03:25:12 +0200 |
commit | 9acb03e669e20180b9bc95ca02b21460761fef87 (patch) | |
tree | 376d2c46c207daaf5b6c1939c805e4191f02a036 | |
parent | 82995655dca72297155c5632029284dacbcccc87 (diff) | |
download | blogc-9acb03e669e20180b9bc95ca02b21460761fef87.tar.gz blogc-9acb03e669e20180b9bc95ca02b21460761fef87.tar.bz2 blogc-9acb03e669e20180b9bc95ca02b21460761fef87.zip |
template-parser: renderer: implemented 'else' support in templates
yeah, this is stupid. after more than 320 commits and 26 releases, we
finally support the 'else' statement in the template engine. I don't
know if I'm dumb or what, but it took me that long to find a "simple"
solution to this basic issue. yep, no more
`{% ifdef FOO %}...{% endif %}{% ifndef FOO %}...{% endif %}` blocks.
but seriously, who cares?! :/
-rw-r--r-- | man/blogc-template.7.ronn | 14 | ||||
-rw-r--r-- | src/debug.c | 3 | ||||
-rw-r--r-- | src/renderer.c | 49 | ||||
-rw-r--r-- | src/template-parser.c | 31 | ||||
-rw-r--r-- | src/template-parser.h | 1 | ||||
-rw-r--r-- | tests/check_renderer.c | 189 | ||||
-rw-r--r-- | tests/check_template_parser.c | 150 |
7 files changed, 421 insertions, 16 deletions
diff --git a/man/blogc-template.7.ronn b/man/blogc-template.7.ronn index 3499d7c..1fdc479 100644 --- a/man/blogc-template.7.ronn +++ b/man/blogc-template.7.ronn @@ -143,7 +143,8 @@ based on the value and existence of variables in the current scope. The implementation of conditionals is simple, and each will just evaluate the value of a single variable. -The available conditionals are: `ifdef`, `ifndef` and `if`. +The available conditionals are: `ifdef`, `ifndef` and `if`. `else` statements +are supported. ### ifdef conditional @@ -154,9 +155,12 @@ This is how an `ifdef` conditional is defined in a template: {% ifdef TITLE %} This is title: {{ TITLE }} + {% else %} + Untitled entry {% endif %} -In this case, if the `TITLE` variable is defined, the content is included. +In this case, if the `TITLE` variable is defined, the content after the statement +is included. Otherwise, the content after `else` statement is included. ### ifndef conditional @@ -170,6 +174,8 @@ This is how an `ifndef` conditional is defined in a template: {% endif %} In this case, if the `TITLE` variable is not defined, the content is included. +`else` statements are supported here, even if it does not makes much sense to +be used this way. ### if conditional @@ -184,7 +190,9 @@ comparisions are strcmp(3)-like. This is how an `if` conditional is defined in a template: {% if TITLE == "My Title" %} - Title is "My Title" + Special description of "My Title" + {% else %} + Title is {{ TITLE }} {% endif %} Or: diff --git a/src/debug.c b/src/debug.c index 767e1a5..7039608 100644 --- a/src/debug.c +++ b/src/debug.c @@ -55,6 +55,9 @@ blogc_debug_template(sb_slist_t *stmts) fprintf(stderr, "IF: %s %s %s", data->value, get_operator(data->op), data->value2); break; + case BLOGC_TEMPLATE_ELSE_STMT: + fprintf(stderr, "ELSE"); + break; case BLOGC_TEMPLATE_ENDIF_STMT: fprintf(stderr, "ENDIF"); break; diff --git a/src/renderer.c b/src/renderer.c index 0f71ba7..351dfb1 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -188,6 +188,7 @@ blogc_render(sb_slist_t *tmpl, sb_slist_t *sources, sb_trie_t *config, bool list bool if_not = false; bool inside_block = false; bool evaluate = false; + bool valid_else = false; int cmp = 0; @@ -343,13 +344,23 @@ blogc_render(sb_slist_t *tmpl, sb_slist_t *sources, sb_trie_t *config, bool list if_count++; continue; } + if ((stmt->type == BLOGC_TEMPLATE_ELSE_STMT) && + (if_count == 0)) + { + // this is somewhat complex. only an else statement + // right after a non evaluated block should be considered + // valid, because all the inner conditionals were just + // skipped, and all the outter conditionals evaluated + // to true. + valid_else = true; + break; + } if (stmt->type == BLOGC_TEMPLATE_ENDIF_STMT) { if (if_count > 0) { if_count--; continue; } - if (if_count == 0) - break; + break; } } } @@ -358,7 +369,41 @@ blogc_render(sb_slist_t *tmpl, sb_slist_t *sources, sb_trie_t *config, bool list if_not = false; break; + case BLOGC_TEMPLATE_ELSE_STMT: + if_count = 0; + if (!valid_else) { + + // at this point we can just skip anything, counting the + // number of 'if's, to know how many 'endif's we need to + // skip as well. + while (1) { + tmp = tmp->next; + stmt = tmp->data; + if ((stmt->type == BLOGC_TEMPLATE_IF_STMT) || + (stmt->type == BLOGC_TEMPLATE_IFDEF_STMT) || + (stmt->type == BLOGC_TEMPLATE_IFNDEF_STMT)) + { + if_count++; + continue; + } + // no need to handle else statements here, because every + // if should have an endif. + if (stmt->type == BLOGC_TEMPLATE_ENDIF_STMT) { + if (if_count > 0) { + if_count--; + continue; + } + break; + } + } + } + valid_else = false; + break; + case BLOGC_TEMPLATE_ENDIF_STMT: + // any endif statement should invalidate valid_else, to avoid + // propagation to outter conditionals. + valid_else = false; if (if_count > 0) if_count--; break; diff --git a/src/template-parser.c b/src/template-parser.c index 525f5f5..344e398 100644 --- a/src/template-parser.c +++ b/src/template-parser.c @@ -69,6 +69,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) blogc_template_stmt_operator_t tmp_op = 0; unsigned int if_count = 0; + bool else_open = false; bool foreach_open = false; sb_slist_t *stmts = NULL; @@ -224,6 +225,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) type = BLOGC_TEMPLATE_IFDEF_STMT; start = current; if_count++; + else_open = false; break; } else if ((current - start == 6) && @@ -233,6 +235,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) type = BLOGC_TEMPLATE_IFNDEF_STMT; start = current; if_count++; + else_open = false; break; } else if ((current - start == 2) && @@ -242,6 +245,29 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) type = BLOGC_TEMPLATE_IF_STMT; start = current; if_count++; + else_open = false; + break; + } + else if ((current - start == 4) && + (0 == strncmp("else", src + start, 4))) + { + if (if_count > 0) { + if (!else_open) { + state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER; + type = BLOGC_TEMPLATE_ELSE_STMT; + else_open = true; + break; + } + *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + src, src_len, current, + "More than one 'else' statement for an open 'if', " + "'ifdef' or 'ifndef' statement."); + break; + } + *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + src, src_len, current, + "'else' statement without an open 'if', 'ifdef' or " + "'ifndef' statement."); break; } else if ((current - start == 5) && @@ -251,12 +277,13 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER; type = BLOGC_TEMPLATE_ENDIF_STMT; if_count--; + else_open = false; break; } *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, - "'endif' statement without an open 'ifdef' or 'ifndef' " - "statement."); + "'endif' statement without an open 'if', 'ifdef' or " + "'ifndef' statement."); break; } else if ((current - start == 7) && diff --git a/src/template-parser.h b/src/template-parser.h index 46223ec..fe2721e 100644 --- a/src/template-parser.h +++ b/src/template-parser.h @@ -21,6 +21,7 @@ typedef enum { BLOGC_TEMPLATE_IFDEF_STMT = 1, BLOGC_TEMPLATE_IFNDEF_STMT, BLOGC_TEMPLATE_IF_STMT, + BLOGC_TEMPLATE_ELSE_STMT, BLOGC_TEMPLATE_ENDIF_STMT, BLOGC_TEMPLATE_FOREACH_STMT, BLOGC_TEMPLATE_ENDFOREACH_STMT, diff --git a/tests/check_renderer.c b/tests/check_renderer.c index 0f17d23..cbd4a8e 100644 --- a/tests/check_renderer.c +++ b/tests/check_renderer.c @@ -74,7 +74,7 @@ test_render_entry(void **state) "{% if GUDA == \"zxc\" %}LOL{% endif %}\n" "{% if GUDA != \"bola\" %}HEHE{% endif %}\n" "{% if GUDA < \"zxd\" %}LOL2{% endif %}\n" - "{% if GUDA > \"zxd\" %}LOL3{% endif %}\n" + "{% if GUDA > \"zxd\" %}LOL3{% else %}ELSE{% endif %}\n" "{% if GUDA <= \"zxc\" %}LOL4{% endif %}\n" "{% foreach TAGS %}lol {{ FOREACH_ITEM }} haha {% endforeach %}\n" "{% foreach TAGS_ASD %}yay{% endforeach %}\n"; @@ -99,7 +99,7 @@ test_render_entry(void **state) "LOL\n" "HEHE\n" "LOL2\n" - "\n" + "ELSE\n" "LOL4\n" "lol foo haha lol bar haha lol baz haha \n" "\n"); @@ -282,6 +282,146 @@ test_render_ifdef3(void **state) static void +test_render_ifdef4(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% ifdef GUDA %}guda\n" + "{% ifdef BOLA %}bola\n" + "{% ifdef CHUNDA %}chunda\n" + "{% else %}else\n" + "{% endif %}\n" + "{% endif %}\n" + "{% else %}lol\n" + "{% endif %}\n" + "{% endblock %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *l = blogc_template_parse(str, strlen(str), &err); + assert_non_null(l); + assert_null(err); + sb_slist_t *s = create_sources(1); + assert_non_null(s); + char *out = blogc_render(l, s, NULL, false); + assert_string_equal(out, + "\n" + "guda\n" + "bola\n" + "else\n" + "\n" + "\n" + "\n" + "\n"); + blogc_template_free_stmts(l); + sb_slist_free_full(s, (sb_free_func_t) sb_trie_free); + free(out); +} + + +static void +test_render_ifdef5(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% ifdef GUDA %}guda\n" + "{% ifdef CHUNDA %}chunda\n" + "{% ifdef BOLA %}bola\n" + "{% endif %}\n" + "{% else %}else\n" + "{% endif %}\n" + "{% else %}lol\n" + "{% endif %}\n" + "{% endblock %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *l = blogc_template_parse(str, strlen(str), &err); + assert_non_null(l); + assert_null(err); + sb_slist_t *s = create_sources(1); + assert_non_null(s); + char *out = blogc_render(l, s, NULL, false); + assert_string_equal(out, + "\n" + "guda\n" + "else\n" + "\n" + "\n" + "\n"); + blogc_template_free_stmts(l); + sb_slist_free_full(s, (sb_free_func_t) sb_trie_free); + free(out); +} + + +static void +test_render_ifdef6(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% ifdef CHUNDA %}chunda\n" + "{% ifdef GUDA %}guda\n" + "{% ifdef BOLA %}bola\n" + "{% endif %}\n" + "{% else %}else\n" + "{% endif %}\n" + "{% else %}lol\n" + "{% endif %}\n" + "{% endblock %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *l = blogc_template_parse(str, strlen(str), &err); + assert_non_null(l); + assert_null(err); + sb_slist_t *s = create_sources(1); + assert_non_null(s); + char *out = blogc_render(l, s, NULL, false); + assert_string_equal(out, + "\n" + "lol\n" + "\n" + "\n"); + blogc_template_free_stmts(l); + sb_slist_free_full(s, (sb_free_func_t) sb_trie_free); + free(out); +} + + +static void +test_render_ifdef7(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% ifdef GUDA %}guda\n" + "{% ifdef BOLA %}bola\n" + "{% ifdef CHUNDA %}chunda\n" + "{% endif %}\n" + "{% endif %}\n" + "{% ifdef CHUNDA %}ch\n" + "{% else %}else\n" + "{% endif %}\n" + "{% endif %}\n" + "{% endblock %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *l = blogc_template_parse(str, strlen(str), &err); + assert_non_null(l); + assert_null(err); + sb_slist_t *s = create_sources(1); + assert_non_null(s); + char *out = blogc_render(l, s, NULL, false); + assert_string_equal(out, + "\n" + "guda\n" + "bola\n" + "\n" + "\n" + "else\n" + "\n" + "\n" + "\n"); + blogc_template_free_stmts(l); + sb_slist_free_full(s, (sb_free_func_t) sb_trie_free); + free(out); +} + + +static void test_render_ifndef(void **state) { const char *str = @@ -289,6 +429,7 @@ test_render_ifndef(void **state) "{% ifndef CHUNDA %}chunda\n" "{% ifdef GUDA %}guda\n" "{% ifndef BOLA %}bola\n" + "{% else %}else\n" "{% endif %}\n" "{% endif %}\n" "{% endif %}\n" @@ -304,6 +445,7 @@ test_render_ifndef(void **state) "\n" "chunda\n" "guda\n" + "else\n" "\n" "\n" "\n" @@ -323,6 +465,7 @@ test_render_if_eq(void **state) "{% if GUDA == \"zxc\" %}guda\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" + "{% else %}else\n" "{% endif %}\n" "{% endif %}\n" "{% endif %}\n" @@ -339,6 +482,7 @@ test_render_if_eq(void **state) "gudabola\n" "guda\n" "bola\n" + "else\n" "\n" "\n" "\n" @@ -358,6 +502,7 @@ test_render_if_neq(void **state) "{% if GUDA != \"zxa\" %}guda\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" + "{% else %}else\n" "{% endif %}\n" "{% endif %}\n" "{% endif %}\n" @@ -374,6 +519,7 @@ test_render_if_neq(void **state) "gudabola\n" "guda\n" "bola\n" + "else\n" "\n" "\n" "\n" @@ -393,6 +539,7 @@ test_render_if_lt(void **state) "{% if GUDA < \"zxe\" %}guda\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" + "{% else %}else\n" "{% endif %}\n" "{% endif %}\n" "{% endif %}\n" @@ -409,6 +556,7 @@ test_render_if_lt(void **state) "gudabola\n" "guda\n" "bola\n" + "else\n" "\n" "\n" "\n" @@ -428,6 +576,7 @@ test_render_if_gt(void **state) "{% if GUDA > \"zxa\" %}guda\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" + "{% else %}else\n" "{% endif %}\n" "{% endif %}\n" "{% endif %}\n" @@ -444,6 +593,7 @@ test_render_if_gt(void **state) "gudabola\n" "guda\n" "bola\n" + "else\n" "\n" "\n" "\n" @@ -464,6 +614,7 @@ test_render_if_lt_eq(void **state) "{% if GUDA <= \"zxe\" %}guda2\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" + "{% else %}else\n" "{% endif %}\n" "{% endif %}\n" "{% endif %}\n" @@ -482,6 +633,7 @@ test_render_if_lt_eq(void **state) "guda\n" "guda2\n" "bola\n" + "else\n" "\n" "\n" "\n" @@ -503,6 +655,7 @@ test_render_if_gt_eq(void **state) "{% if GUDA >= \"zxa\" %}guda2\n" "{% ifdef BOLA %}bola\n" "{% if GUDA > \"zxc\" %}asd\n" + "{% else %}else\n" "{% endif %}\n" "{% endif %}\n" "{% endif %}\n" @@ -521,6 +674,7 @@ test_render_if_gt_eq(void **state) "guda\n" "guda2\n" "bola\n" + "else\n" "\n" "\n" "\n" @@ -582,6 +736,32 @@ test_render_foreach_if(void **state) static void +test_render_foreach_if_else(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% foreach TAGS %}{% if FOREACH_ITEM == \"bar\" %}yay" + "{% else %}{{ FOREACH_ITEM }}" + "{% endif %} {% endforeach %}\n" + "{% endblock %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *l = blogc_template_parse(str, strlen(str), &err); + assert_non_null(l); + assert_null(err); + sb_slist_t *s = create_sources(1); + assert_non_null(s); + char *out = blogc_render(l, s, NULL, false); + assert_string_equal(out, + "\n" + "foo yay baz \n" + "\n"); + blogc_template_free_stmts(l); + sb_slist_free_full(s, (sb_free_func_t) sb_trie_free); + free(out); +} + + +static void test_render_null(void **state) { assert_null(blogc_render(NULL, NULL, NULL, false)); @@ -943,6 +1123,10 @@ main(void) unit_test(test_render_ifdef), unit_test(test_render_ifdef2), unit_test(test_render_ifdef3), + unit_test(test_render_ifdef4), + unit_test(test_render_ifdef5), + unit_test(test_render_ifdef6), + unit_test(test_render_ifdef7), unit_test(test_render_ifndef), unit_test(test_render_if_eq), unit_test(test_render_if_neq), @@ -952,6 +1136,7 @@ main(void) unit_test(test_render_if_gt_eq), unit_test(test_render_foreach), unit_test(test_render_foreach_if), + unit_test(test_render_foreach_if_else), unit_test(test_render_null), unit_test(test_render_outside_block), unit_test(test_render_prefer_local_variable), diff --git a/tests/check_template_parser.c b/tests/check_template_parser.c index 40a09be..e94b9c7 100644 --- a/tests/check_template_parser.c +++ b/tests/check_template_parser.c @@ -62,7 +62,7 @@ test_template_parse(void **state) "{% block listing %}{{ BOLA }}{% endblock %}\n" "{% block listing_once %}asd{% endblock %}\n" "{%- foreach BOLA %}hahaha{% endforeach %}\n" - "{% if BOLA == \"1\\\"0\" %}aee{% endif %}"; + "{% if BOLA == \"1\\\"0\" %}aee{% else %}fffuuuuuuu{% endif %}"; blogc_error_t *err = NULL; sb_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_null(err); @@ -119,8 +119,12 @@ test_template_parse(void **state) blogc_assert_template_stmt(tmp->next->next->next->next->next, "aee", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next->next, NULL, - BLOGC_TEMPLATE_ENDIF_STMT); - assert_null(tmp->next->next->next->next->next->next->next); + BLOGC_TEMPLATE_ELSE_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next, + "fffuuuuuuu", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next, + NULL, BLOGC_TEMPLATE_ENDIF_STMT); + assert_null(tmp->next->next->next->next->next->next->next->next->next); blogc_template_free_stmts(stmts); } @@ -142,7 +146,7 @@ test_template_parse_crlf(void **state) "{% block listing %}{{ BOLA }}{% endblock %}\r\n" "{% block listing_once %}asd{% endblock %}\r\n" "{%- foreach BOLA %}hahaha{% endforeach %}\r\n" - "{% if BOLA == \"1\\\"0\" %}aee{% endif %}"; + "{% if BOLA == \"1\\\"0\" %}aee{% else %}fffuuuuuuu{% endif %}"; blogc_error_t *err = NULL; sb_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_null(err); @@ -199,8 +203,12 @@ test_template_parse_crlf(void **state) blogc_assert_template_stmt(tmp->next->next->next->next->next, "aee", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next->next, NULL, - BLOGC_TEMPLATE_ENDIF_STMT); - assert_null(tmp->next->next->next->next->next->next->next); + BLOGC_TEMPLATE_ELSE_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next, + "fffuuuuuuu", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next, + NULL, BLOGC_TEMPLATE_ENDIF_STMT); + assert_null(tmp->next->next->next->next->next->next->next->next->next); blogc_template_free_stmts(stmts); } @@ -369,6 +377,74 @@ test_template_parse_ifdef_and_var_outside_block(void **state) static void +test_template_parse_nested_else(void **state) +{ + const char *a = + "{% ifdef GUDA %}\n" + "{% ifdef BOLA %}\n" + "asd\n" + "{% else %}\n" + "{% ifdef CHUNDA %}\n" + "qwe\n" + "{% else %}\n" + "rty\n" + "{% endif %}\n" + "{% endif %}\n" + "{% ifdef LOL %}\n" + "zxc\n" + "{% else %}\n" + "bnm\n" + "{% endif %}\n" + "{% endif %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); + assert_null(err); + assert_non_null(stmts); + blogc_assert_template_stmt(stmts, "GUDA", BLOGC_TEMPLATE_IFDEF_STMT); + blogc_assert_template_stmt(stmts->next, "\n", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(stmts->next->next, "BOLA", BLOGC_TEMPLATE_IFDEF_STMT); + blogc_assert_template_stmt(stmts->next->next->next, "\nasd\n", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(stmts->next->next->next->next, NULL, + BLOGC_TEMPLATE_ELSE_STMT); + blogc_assert_template_stmt(stmts->next->next->next->next->next, "\n", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(stmts->next->next->next->next->next->next, + "CHUNDA", BLOGC_TEMPLATE_IFDEF_STMT); + blogc_assert_template_stmt(stmts->next->next->next->next->next->next->next, + "\nqwe\n", BLOGC_TEMPLATE_CONTENT_STMT); + sb_slist_t *tmp = stmts->next->next->next->next->next->next->next->next; + blogc_assert_template_stmt(tmp, NULL, BLOGC_TEMPLATE_ELSE_STMT); + blogc_assert_template_stmt(tmp->next, "\nrty\n", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next->next, NULL, + BLOGC_TEMPLATE_ENDIF_STMT); + blogc_assert_template_stmt(tmp->next->next->next, "\n", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next, NULL, + BLOGC_TEMPLATE_ENDIF_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next->next, "\n", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next->next->next, + "LOL", BLOGC_TEMPLATE_IFDEF_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next, + "\nzxc\n", BLOGC_TEMPLATE_CONTENT_STMT); + tmp = tmp->next->next->next->next->next->next->next->next; + blogc_assert_template_stmt(tmp, NULL, BLOGC_TEMPLATE_ELSE_STMT); + blogc_assert_template_stmt(tmp->next, "\nbnm\n", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next->next, NULL, + BLOGC_TEMPLATE_ENDIF_STMT); + blogc_assert_template_stmt(tmp->next->next->next, "\n", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next, NULL, + BLOGC_TEMPLATE_ENDIF_STMT); + blogc_assert_template_stmt(tmp->next->next->next->next->next, "\n", + BLOGC_TEMPLATE_CONTENT_STMT); + assert_null(tmp->next->next->next->next->next->next); + blogc_template_free_stmts(stmts); +} + + +static void test_template_parse_invalid_block_start(void **state) { const char *a = "{% ASD %}\n"; @@ -466,7 +542,7 @@ test_template_parse_invalid_endif_not_open(void **state) assert_null(stmts); assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, - "'endif' statement without an open 'ifdef' or 'ifndef' statement.\n" + "'endif' statement without an open 'if', 'ifdef' or 'ifndef' statement.\n" "Error occurred near line 1, position 28: " "{% block listing %}{% endif %}{% endblock %}"); blogc_error_free(err); @@ -709,6 +785,62 @@ test_template_parse_invalid_if_operand3(void **state) static void +test_template_parse_invalid_else1(void **state) +{ + const char *a = "{% else %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); + assert_non_null(err); + assert_null(stmts); + assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); + assert_string_equal(err->msg, + "'else' statement without an open 'if', 'ifdef' or 'ifndef' statement.\n" + "Error occurred near line 1, position 8: {% else %}"); + blogc_error_free(err); +} + + +static void +test_template_parse_invalid_else2(void **state) +{ + const char *a = "{% if BOLA == \"123\" %}{% if GUDA == \"1\" %}{% else %}{% else %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); + assert_non_null(err); + assert_null(stmts); + assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); + assert_string_equal(err->msg, + "More than one 'else' statement for an open 'if', 'ifdef' or 'ifndef' " + "statement.\nError occurred near line 1, position 60: {% if BOLA == \"123\" " + "%}{% if GUDA == \"1\" %}{% else %}{% else %}"); + blogc_error_free(err); +} + + +static void +test_template_parse_invalid_else3(void **state) +{ + const char *a = + "{% if BOLA == \"123\" %}\n" + "{% if GUDA == \"1\" %}\n" + "{% else %}\n" + "asd\n" + "{% endif %}\n" + "{% else %}\n" + "{% else %}\n"; + blogc_error_t *err = NULL; + sb_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); + assert_non_null(err); + assert_null(stmts); + assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); + assert_string_equal(err->msg, + "More than one 'else' statement for an open 'if', 'ifdef' or 'ifndef' " + "statement.\nError occurred near line 7, position 8: {% else %}"); + blogc_error_free(err); +} + + +static void test_template_parse_invalid_block_end(void **state) { const char *a = "{% block entry }}\n"; @@ -876,6 +1008,7 @@ main(void) unit_test(test_template_parse_crlf), unit_test(test_template_parse_html), unit_test(test_template_parse_ifdef_and_var_outside_block), + unit_test(test_template_parse_nested_else), unit_test(test_template_parse_invalid_block_start), unit_test(test_template_parse_invalid_block_nested), unit_test(test_template_parse_invalid_foreach_nested), @@ -895,6 +1028,9 @@ main(void) unit_test(test_template_parse_invalid_if_operand), unit_test(test_template_parse_invalid_if_operand2), unit_test(test_template_parse_invalid_if_operand3), + unit_test(test_template_parse_invalid_else1), + unit_test(test_template_parse_invalid_else2), + unit_test(test_template_parse_invalid_else3), unit_test(test_template_parse_invalid_block_end), unit_test(test_template_parse_invalid_variable_name), unit_test(test_template_parse_invalid_variable_name2), |