From 5205db347cb9f56187c2ad5e767281a7a4016533 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 27 Apr 2015 00:10:26 -0300 Subject: added global variable support --- tests/check_renderer.c | 82 ++++++++++++++++++++++--- tests/check_template_parser.c | 137 +++++++++++------------------------------- 2 files changed, 110 insertions(+), 109 deletions(-) (limited to 'tests') diff --git a/tests/check_renderer.c b/tests/check_renderer.c index 539d122..f8acc14 100644 --- a/tests/check_renderer.c +++ b/tests/check_renderer.c @@ -68,7 +68,7 @@ test_render_entry(void **state) assert_null(err); b_slist_t *s = create_sources(1); assert_non_null(s); - char *out = blogc_render(l, s, false); + char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "foo\n" "\n" @@ -102,7 +102,7 @@ test_render_listing(void **state) assert_null(err); b_slist_t *s = create_sources(3); assert_non_null(s); - char *out = blogc_render(l, s, true); + char *out = blogc_render(l, s, NULL, true); assert_string_equal(out, "foo\n" "fuuu\n" @@ -138,7 +138,7 @@ test_render_if(void **state) assert_null(err); b_slist_t *s = create_sources(1); assert_non_null(s); - char *out = blogc_render(l, s, false); + char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" "\n" @@ -167,7 +167,7 @@ test_render_if2(void **state) assert_null(err); b_slist_t *s = create_sources(1); assert_non_null(s); - char *out = blogc_render(l, s, false); + char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" "guda\n" @@ -198,7 +198,7 @@ test_render_if3(void **state) assert_null(err); b_slist_t *s = create_sources(1); assert_non_null(s); - char *out = blogc_render(l, s, false); + char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" "guda\n" @@ -231,7 +231,7 @@ test_render_if_not(void **state) assert_null(err); b_slist_t *s = create_sources(1); assert_non_null(s); - char *out = blogc_render(l, s, false); + char *out = blogc_render(l, s, NULL, false); assert_string_equal(out, "\n" "chunda\n" @@ -249,7 +249,73 @@ test_render_if_not(void **state) static void test_render_null(void **state) { - assert_null(blogc_render(NULL, NULL, false)); + assert_null(blogc_render(NULL, NULL, NULL, false)); +} + + +static void +test_render_outside_block(void **state) +{ + const char *str = + "{% if GUDA %}bola{% endif %}\n" + "{{ BOLA }}\n" + "{% if not CHUNDA %}lol{% endif %}\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 = create_sources(1); + assert_non_null(s); + b_trie_t *c = b_trie_new(free); + b_trie_insert(c, "GUDA", b_strdup("asd")); + char *out = blogc_render(l, s, c, false); + assert_string_equal(out, + "bola\n" + "\n" + "lol\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_render_prefer_local_variable(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% if LOL %}{{ LOL }}{% endif %}\n" + "{% if not CHUNDA %}chunda\n" + "{% if GUDA %}{{ GUDA }}\n" + "{% if not BOLA %}bola\n" + "{% endif %}\n" + "{% 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 = create_sources(1); + assert_non_null(s); + b_trie_t *c = b_trie_new(free); + b_trie_insert(c, "GUDA", b_strdup("hehe")); + b_trie_insert(c, "LOL", b_strdup("hmm")); + char *out = blogc_render(l, s, c, false); + assert_string_equal(out, + "\n" + "hmm\n" + "chunda\n" + "zxc\n" + "\n" + "\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); } @@ -263,7 +329,9 @@ main(void) unit_test(test_render_if2), unit_test(test_render_if3), unit_test(test_render_if_not), + unit_test(test_render_outside_block), unit_test(test_render_null), + unit_test(test_render_prefer_local_variable), }; return run_tests(tests); } diff --git a/tests/check_template_parser.c b/tests/check_template_parser.c index 7d0436e..bbef763 100644 --- a/tests/check_template_parser.c +++ b/tests/check_template_parser.c @@ -226,6 +226,40 @@ test_template_parse_html(void **state) } +static void +test_template_parse_if_and_var_outside_block(void **state) +{ + const char *a = + "{% if GUDA %}bola{% endif %}\n" + "{{ BOLA }}\n" + "{% if not CHUNDA %}{{ CHUNDA }}{% endif %}\n"; + blogc_error_t *err = NULL; + b_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_IF_STMT); + blogc_assert_template_stmt(stmts->next, "bola", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(stmts->next->next, NULL, + BLOGC_TEMPLATE_ENDIF_STMT); + blogc_assert_template_stmt(stmts->next->next->next, "\n", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(stmts->next->next->next->next, "BOLA", + BLOGC_TEMPLATE_VARIABLE_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_IF_NOT_STMT); + b_slist_t *tmp = stmts->next->next->next->next->next->next->next; + blogc_assert_template_stmt(tmp, "CHUNDA", BLOGC_TEMPLATE_VARIABLE_STMT); + blogc_assert_template_stmt(tmp->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT); + blogc_assert_template_stmt(tmp->next->next, "\n", + BLOGC_TEMPLATE_CONTENT_STMT); + assert_null(tmp->next->next->next); + blogc_template_free_stmts(stmts); +} + + static void test_template_parse_invalid_block_start(void **state) { @@ -276,38 +310,6 @@ test_template_parse_invalid_block_not_open(void **state) } -static void -test_template_parse_invalid_if_outside_block(void **state) -{ - const char *a = "{% if BOLA %}"; - 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, - "'if' statements only allowed inside 'entry' and 'listing' blocks.\n" - "Error occurred near to ' BOLA %}'"); - blogc_error_free(err); -} - - -static void -test_template_parse_invalid_if_inside_listing_once_block(void **state) -{ - const char *a = "{% block listing_once %}{% if BOLA %}{% endblock %}"; - 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, - "'if' statements only allowed inside 'entry' and 'listing' blocks.\n" - "Error occurred near to ' BOLA %}{% endblock %}'"); - blogc_error_free(err); -} - - static void test_template_parse_invalid_endif_not_open(void **state) { @@ -324,38 +326,6 @@ test_template_parse_invalid_endif_not_open(void **state) } -static void -test_template_parse_invalid_endif_outside_block(void **state) -{ - const char *a = "{% endif %}\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, - "'endif' statements only allowed inside 'entry' and 'listing' blocks.\n" - "Error occurred near to ' %}'"); - blogc_error_free(err); -} - - -static void -test_template_parse_invalid_endif_inside_listing_once_block(void **state) -{ - const char *a = "{% block listing_once %}{% endif %}{% endblock %}\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, - "'endif' statements only allowed inside 'entry' and 'listing' blocks.\n" - "Error occurred near to ' %}{% endblock %}'"); - blogc_error_free(err); -} - - static void test_template_parse_invalid_block_name(void **state) { @@ -468,38 +438,6 @@ test_template_parse_invalid_block_end(void **state) } -static void -test_template_parse_invalid_variable_outside_block(void **state) -{ - const char *a = "{{ BOLA }}\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, - "Variable statements only allowed inside 'entry' and 'listing' blocks.\n" - "Error occurred near to 'BOLA }}'"); - blogc_error_free(err); -} - - -static void -test_template_parse_invalid_variable_inside_listing_once_block(void **state) -{ - const char *a = "{% block listing_once %}{{ BOLA }}{% endblock %}\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, - "Variable statements only allowed inside 'entry' and 'listing' blocks.\n" - "Error occurred near to 'BOLA }}{% endblock %}'"); - blogc_error_free(err); -} - - static void test_template_parse_invalid_variable_name(void **state) { @@ -614,14 +552,11 @@ main(void) const UnitTest tests[] = { unit_test(test_template_parse), unit_test(test_template_parse_html), + unit_test(test_template_parse_if_and_var_outside_block), unit_test(test_template_parse_invalid_block_start), unit_test(test_template_parse_invalid_block_nested), unit_test(test_template_parse_invalid_block_not_open), - unit_test(test_template_parse_invalid_if_outside_block), - unit_test(test_template_parse_invalid_if_inside_listing_once_block), unit_test(test_template_parse_invalid_endif_not_open), - unit_test(test_template_parse_invalid_endif_outside_block), - unit_test(test_template_parse_invalid_endif_inside_listing_once_block), unit_test(test_template_parse_invalid_block_name), unit_test(test_template_parse_invalid_block_type_start), unit_test(test_template_parse_invalid_block_type), @@ -629,8 +564,6 @@ main(void) unit_test(test_template_parse_invalid_if_condition), unit_test(test_template_parse_invalid_if_variable), unit_test(test_template_parse_invalid_block_end), - unit_test(test_template_parse_invalid_variable_outside_block), - unit_test(test_template_parse_invalid_variable_inside_listing_once_block), unit_test(test_template_parse_invalid_variable_name), unit_test(test_template_parse_invalid_variable_name2), unit_test(test_template_parse_invalid_variable_end), -- cgit v1.2.3-18-g5258