From d7f5252a0d7c4c428bfbd560ce78b81073d3d6ec Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 17 May 2015 16:18:58 -0300 Subject: template-parser: minor fixes. tests --- src/template-parser.c | 17 +++++++++++++---- tests/check_template_parser.c | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/template-parser.c b/src/template-parser.c index 6eb65b6..47cb762 100644 --- a/src/template-parser.c +++ b/src/template-parser.c @@ -222,19 +222,25 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) if ((c >= 'a' && c <= 'z') || c == '_') break; if (c == ' ') { - if (0 == strncmp("entry", src + start, current - start)) { + if ((current - start == 5) && + (0 == strncmp("entry", src + start, current - start))) + { block_state = BLOCK_ENTRY; end = current; state = TEMPLATE_BLOCK_END; break; } - else if (0 == strncmp("listing", src + start, current - start)) { + else if ((current - start == 7) && + (0 == strncmp("listing", src + start, current - start))) + { block_state = BLOCK_LISTING; end = current; state = TEMPLATE_BLOCK_END; break; } - else if (0 == strncmp("listing_once", src + start, current - start)) { + else if ((current - start == 12) && + (0 == strncmp("listing_once", src + start, current - start))) + { block_state = BLOCK_LISTING_ONCE; end = current; state = TEMPLATE_BLOCK_END; @@ -434,7 +440,10 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) } if (*err == NULL) { - if (if_count != 0) + if (state == TEMPLATE_BLOCK_IF_OPERAND) + *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, + start2 - 1, "Found an open double-quoted string."); + else if (if_count != 0) *err = blogc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, "%d open 'ifdef' and/or 'ifndef' statements were not closed!", if_count); diff --git a/tests/check_template_parser.c b/tests/check_template_parser.c index 06161a7..8ce6e6d 100644 --- a/tests/check_template_parser.c +++ b/tests/check_template_parser.c @@ -61,7 +61,7 @@ test_template_parse(void **state) "{% endblock %}\n" "{% block listing %}{{ BOLA }}{% endblock %}\n" "{% block listing_once %}asd{% endblock %}\n" - "{% if BOLA == \"10\" %}aee{% endif %}"; + "{% if BOLA == \"1\\\"0\" %}aee{% endif %}"; blogc_error_t *err = NULL; b_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_null(err); @@ -106,7 +106,7 @@ test_template_parse(void **state) blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next->next, "\n", BLOGC_TEMPLATE_CONTENT_STMT); tmp = tmp->next->next->next->next->next->next->next->next->next->next; - blogc_assert_template_if_stmt(tmp, "BOLA", "==", "10"); + blogc_assert_template_if_stmt(tmp, "BOLA", "==", "1\\\"0"); blogc_assert_template_stmt(tmp->next, "aee", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT); @@ -440,6 +440,38 @@ test_template_parse_invalid_if_operator(void **state) } +static void +test_template_parse_invalid_if_operand(void **state) +{ + const char *a = "{% block entry %}{% if BOLA == asd %}\n"; + blogc_error_t *err = NULL; + b_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, + "Invalid 'if' operand. Must be double-quoted static string.\n" + "Error occurred near to 'asd %}'"); + blogc_error_free(err); +} + + +static void +test_template_parse_invalid_if_operand2(void **state) +{ + const char *a = "{% block entry %}{% if BOLA == \"asd %}\n"; + blogc_error_t *err = NULL; + b_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, + "Found an open double-quoted string.\n" + "Error occurred near to '\"asd %}'"); + blogc_error_free(err); +} + + static void test_template_parse_invalid_block_end(void **state) { @@ -582,6 +614,8 @@ main(void) unit_test(test_template_parse_invalid_ifdef_start), unit_test(test_template_parse_invalid_ifdef_variable), unit_test(test_template_parse_invalid_if_operator), + unit_test(test_template_parse_invalid_if_operand), + unit_test(test_template_parse_invalid_if_operand2), unit_test(test_template_parse_invalid_block_end), unit_test(test_template_parse_invalid_variable_name), unit_test(test_template_parse_invalid_variable_name2), -- cgit v1.2.3-18-g5258