diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/check_content_parser.c | 22 | ||||
-rw-r--r-- | tests/check_datetime_parser.c | 2 | ||||
-rw-r--r-- | tests/check_error.c | 2 | ||||
-rw-r--r-- | tests/check_loader.c | 6 | ||||
-rw-r--r-- | tests/check_renderer.c | 166 | ||||
-rw-r--r-- | tests/check_source_parser.c | 2 | ||||
-rw-r--r-- | tests/check_template_parser.c | 241 | ||||
-rw-r--r-- | tests/check_utils.c | 57 |
8 files changed, 458 insertions, 40 deletions
diff --git a/tests/check_content_parser.c b/tests/check_content_parser.c index 817edba..165810d 100644 --- a/tests/check_content_parser.c +++ b/tests/check_content_parser.c @@ -1,6 +1,6 @@ /* * blogc: A blog compiler. - * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. @@ -50,6 +50,17 @@ test_slugify(void **state) static void +test_htmlentities(void **state) +{ + char *s = blogc_htmlentities(NULL); + assert_null(s); + s = blogc_htmlentities("asdxcv & < > \" 'sfd/gf"); + assert_string_equal(s, "asdxcv & < > " 'sfd/gf"); + free(s); +} + + +static void test_is_ordered_list_item(void **state) { assert_true(blogc_is_ordered_list_item("1.bola", 2)); @@ -88,7 +99,7 @@ test_content_parse(void **state) "> \n" "> asd\n" "\n" - " bola\n" + " <asd>bola</asd>\n" " asd\n" " qwewer\n" "\n" @@ -123,7 +134,7 @@ test_content_parse(void **state) "buga</p>\n" "<pre><code>asd</code></pre>\n" "</blockquote>\n" - "<pre><code>bola\n" + "<pre><code><asd>bola</asd>\n" " asd\n" "qwewer</code></pre>\n" "<hr />\n" @@ -166,7 +177,7 @@ test_content_parse_crlf(void **state) "> \r\n" "> asd\r\n" "\r\n" - " bola\r\n" + " <asd>bola</asd>\r\n" " asd\r\n" " qwewer\r\n" "\r\n" @@ -201,7 +212,7 @@ test_content_parse_crlf(void **state) "buga</p>\r\n" "<pre><code>asd</code></pre>\r\n" "</blockquote>\r\n" - "<pre><code>bola\r\n" + "<pre><code><asd>bola</asd>\r\n" " asd\r\n" "qwewer</code></pre>\r\n" "<hr />\r\n" @@ -2607,6 +2618,7 @@ main(void) { const UnitTest tests[] = { unit_test(test_slugify), + unit_test(test_htmlentities), unit_test(test_is_ordered_list_item), unit_test(test_content_parse), unit_test(test_content_parse_crlf), diff --git a/tests/check_datetime_parser.c b/tests/check_datetime_parser.c index 1ac976d..ba5a79d 100644 --- a/tests/check_datetime_parser.c +++ b/tests/check_datetime_parser.c @@ -1,6 +1,6 @@ /* * blogc: A blog compiler. - * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. diff --git a/tests/check_error.c b/tests/check_error.c index 17e1c40..d3af9c0 100644 --- a/tests/check_error.c +++ b/tests/check_error.c @@ -1,6 +1,6 @@ /* * blogc: A blog compiler. - * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. diff --git a/tests/check_loader.c b/tests/check_loader.c index f5be3e7..ac8bdb3 100644 --- a/tests/check_loader.c +++ b/tests/check_loader.c @@ -1,6 +1,6 @@ /* * blogc: A blog compiler. - * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. @@ -478,7 +478,7 @@ test_source_parse_from_files_filter_by_page_and_tag(void **state) will_return(__wrap_blogc_file_get_contents, b_strdup( "ASD: 789\n" "DATE: 2003-02-03 04:05:06\n" - "TAGS: chunda, bola\n" + "TAGS: chunda bola\n" "--------\n" "bola")); will_return(__wrap_blogc_file_get_contents, "bola4.txt"); @@ -505,7 +505,7 @@ test_source_parse_from_files_filter_by_page_and_tag(void **state) will_return(__wrap_blogc_file_get_contents, b_strdup( "ASD: 7894\n" "DATE: 2007-02-03 04:05:06\n" - "TAGS: yay, chunda\n" + "TAGS: yay chunda\n" "--------\n" "bola")); blogc_error_t *err = NULL; diff --git a/tests/check_renderer.c b/tests/check_renderer.c index cb8f8f1..d6bc947 100644 --- a/tests/check_renderer.c +++ b/tests/check_renderer.c @@ -1,6 +1,6 @@ /* * blogc: A blog compiler. - * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. @@ -31,6 +31,7 @@ create_sources(unsigned int count) "GUDA2: zxc\n" "DATE: 2015-01-02 03:04:05\n" "DATE_FORMAT: %R\n" + "TAGS: foo bar baz\n" "-----\n" "ahahahahahahahaha", "BOLA: asd2\n" @@ -74,7 +75,9 @@ test_render_entry(void **state) "{% if GUDA != \"bola\" %}HEHE{% endif %}\n" "{% if GUDA < \"zxd\" %}LOL2{% endif %}\n" "{% if GUDA > \"zxd\" %}LOL3{% endif %}\n" - "{% if GUDA <= \"zxc\" %}LOL4{% endif %}\n"; + "{% if GUDA <= \"zxc\" %}LOL4{% endif %}\n" + "{% foreach TAGS %}lol {{ FOREACH_ITEM }} haha {% endforeach %}\n" + "{% foreach TAGS_ASD %}yay{% endforeach %}\n"; blogc_error_t *err = NULL; b_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); @@ -97,7 +100,9 @@ test_render_entry(void **state) "HEHE\n" "LOL2\n" "\n" - "LOL4\n"); + "LOL4\n" + "lol foo haha lol bar haha lol baz haha \n" + "\n"); blogc_template_free_stmts(l); b_slist_free_full(s, (b_free_func_t) b_trie_free); free(out); @@ -117,6 +122,8 @@ test_render_listing(void **state) "{% block listing %}\n" "{% ifdef DATE_FORMATTED %}{{ DATE_FORMATTED }}{% endif %}\n" "bola: {% ifdef BOLA %}{{ BOLA }}{% endif %}\n" + "{% foreach TAGS %}lol {{ FOREACH_ITEM }} haha {% endforeach %}\n" + "{% foreach TAGS_ASD %}yay{% endforeach %}\n" "{% endblock %}\n"; blogc_error_t *err = NULL; b_slist_t *l = blogc_template_parse(str, strlen(str), &err); @@ -132,12 +139,18 @@ test_render_listing(void **state) "\n" "03:04\n" "bola: asd\n" + "lol foo haha lol bar haha lol baz haha \n" + "\n" "\n" "2014-02-03 04:05:06\n" "bola: asd2\n" "\n" + "\n" + "\n" "2013-01-02 03:04:05\n" "bola: asd3\n" + "\n" + "\n" "\n"); blogc_template_free_stmts(l); b_slist_free_full(s, (b_free_func_t) b_trie_free); @@ -158,6 +171,7 @@ test_render_listing_empty(void **state) "{% block listing %}\n" "{% ifdef DATE_FORMATTED %}{{ DATE_FORMATTED }}{% endif %}\n" "bola: {% ifdef BOLA %}{{ BOLA }}{% endif %}\n" + "{% foreach TAGS %}lol {{ FOREACH_ITEM }} haha {% endforeach %}\n" "{% endblock %}\n"; blogc_error_t *err = NULL; b_slist_t *l = blogc_template_parse(str, strlen(str), &err); @@ -519,6 +533,55 @@ test_render_if_gt_eq(void **state) static void +test_render_foreach(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% foreach TAGS %} {{ FOREACH_ITEM }} {% endforeach %}\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); + char *out = blogc_render(l, s, NULL, false); + assert_string_equal(out, + "\n" + " foo bar baz \n" + "\n"); + blogc_template_free_stmts(l); + b_slist_free_full(s, (b_free_func_t) b_trie_free); + free(out); +} + + +static void +test_render_foreach_if(void **state) +{ + const char *str = + "{% block entry %}\n" + "{% foreach TAGS %} {% if FOREACH_ITEM == \"bar\" %}{{ FOREACH_ITEM }}" + "{% endif %} {% endforeach %}\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); + char *out = blogc_render(l, s, NULL, false); + assert_string_equal(out, + "\n" + " bar \n" + "\n"); + blogc_template_free_stmts(l); + b_slist_free_full(s, (b_free_func_t) b_trie_free); + free(out); +} + + +static void test_render_null(void **state) { assert_null(blogc_render(NULL, NULL, NULL, false)); @@ -624,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); @@ -732,13 +826,13 @@ test_format_variable(void **state) b_trie_t *l = b_trie_new(free); b_trie_insert(l, "NAME", b_strdup("chunda")); b_trie_insert(l, "TITLE", b_strdup("chunda2")); - char *tmp = blogc_format_variable("NAME", g, l); + char *tmp = blogc_format_variable("NAME", g, l, NULL); assert_string_equal(tmp, "chunda"); free(tmp); - tmp = blogc_format_variable("TITLE", g, l); + tmp = blogc_format_variable("TITLE", g, l, NULL); assert_string_equal(tmp, "chunda2"); free(tmp); - assert_null(blogc_format_variable("BOLA", g, l)); + assert_null(blogc_format_variable("BOLA", g, l, NULL)); b_trie_free(g); b_trie_free(l); } @@ -752,7 +846,7 @@ test_format_variable_with_date(void **state) b_trie_insert(g, "DATE_FORMAT", b_strdup("%R")); b_trie_t *l = b_trie_new(free); b_trie_insert(l, "DATE", b_strdup("2011-12-13 14:15:16")); - char *tmp = blogc_format_variable("DATE_FORMATTED", g, l); + char *tmp = blogc_format_variable("DATE_FORMATTED", g, l, NULL); assert_string_equal(tmp, "14:15"); free(tmp); b_trie_free(g); @@ -760,6 +854,57 @@ test_format_variable_with_date(void **state) } +static void +test_format_variable_foreach(void **state) +{ + b_slist_t *l = NULL; + l = b_slist_append(l, b_strdup("asd")); + l = b_slist_append(l, b_strdup("qwe")); + char *tmp = blogc_format_variable("FOREACH_ITEM", NULL, NULL, l->next); + assert_string_equal(tmp, "qwe"); + free(tmp); + b_slist_free_full(l, free); +} + + +static void +test_format_variable_foreach_empty(void **state) +{ + assert_null(blogc_format_variable("FOREACH_ITEM", NULL, NULL, NULL)); +} + + +static void +test_split_list_variable(void **state) +{ + b_trie_t *g = b_trie_new(free); + b_trie_insert(g, "TAGS", b_strdup("asd lol hehe")); + b_trie_t *l = b_trie_new(free); + b_trie_insert(l, "TAGS", b_strdup("asd lol XD")); + b_slist_t *tmp = blogc_split_list_variable("TAGS", g, l); + assert_string_equal(tmp->data, "asd"); + assert_string_equal(tmp->next->data, "lol"); + assert_string_equal(tmp->next->next->data, "XD"); + b_slist_free_full(tmp, free); + b_trie_free(g); + b_trie_free(l); +} + + +static void +test_split_list_variable_not_found(void **state) +{ + b_trie_t *g = b_trie_new(free); + b_trie_insert(g, "TAGS", b_strdup("asd lol hehe")); + b_trie_t *l = b_trie_new(free); + b_trie_insert(l, "TAGS", b_strdup("asd lol XD")); + b_slist_t *tmp = blogc_split_list_variable("TAG", g, l); + assert_null(tmp); + b_trie_free(g); + b_trie_free(l); +} + + int main(void) { @@ -777,10 +922,13 @@ main(void) unit_test(test_render_if_gt), unit_test(test_render_if_lt_eq), unit_test(test_render_if_gt_eq), + unit_test(test_render_foreach), + unit_test(test_render_foreach_if), unit_test(test_render_null), 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), @@ -790,6 +938,10 @@ main(void) unit_test(test_format_date_without_date), unit_test(test_format_variable), unit_test(test_format_variable_with_date), + unit_test(test_format_variable_foreach), + unit_test(test_format_variable_foreach_empty), + unit_test(test_split_list_variable), + unit_test(test_split_list_variable_not_found), }; return run_tests(tests); } diff --git a/tests/check_source_parser.c b/tests/check_source_parser.c index 4d8518e..8d6c039 100644 --- a/tests/check_source_parser.c +++ b/tests/check_source_parser.c @@ -1,6 +1,6 @@ /* * blogc: A blog compiler. - * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. diff --git a/tests/check_template_parser.c b/tests/check_template_parser.c index b712f22..f655896 100644 --- a/tests/check_template_parser.c +++ b/tests/check_template_parser.c @@ -1,6 +1,6 @@ /* * blogc: A blog compiler. - * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. @@ -51,26 +51,27 @@ test_template_parse(void **state) const char *a = "Test\n" "\n" - " {% block entry %}\n" + " {%- block entry -%}\n" "{% ifdef CHUNDA %}\n" "bola\n" "{% endif %}\n" "{% ifndef BOLA %}\n" "bolao\n" - "{% endif %}\n" + "{%- endif %}\n" "{% endblock %}\n" "{% block listing %}{{ BOLA }}{% endblock %}\n" "{% block listing_once %}asd{% endblock %}\n" + "{%- foreach BOLA %}hahaha{% endforeach %}\n" "{% 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); assert_non_null(stmts); - blogc_assert_template_stmt(stmts, "Test\n\n ", + blogc_assert_template_stmt(stmts, "Test", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next, "entry", BLOGC_TEMPLATE_BLOCK_STMT); - blogc_assert_template_stmt(stmts->next->next, "\n", + blogc_assert_template_stmt(stmts->next->next, "", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next->next->next, "CHUNDA", BLOGC_TEMPLATE_IFDEF_STMT); @@ -82,7 +83,7 @@ test_template_parse(void **state) BLOGC_TEMPLATE_CONTENT_STMT); b_slist_t *tmp = stmts->next->next->next->next->next->next->next; blogc_assert_template_stmt(tmp, "BOLA", BLOGC_TEMPLATE_IFNDEF_STMT); - blogc_assert_template_stmt(tmp->next, "\nbolao\n", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next, "\nbolao", 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); @@ -104,13 +105,22 @@ test_template_parse(void **state) blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next->next, - "\n", BLOGC_TEMPLATE_CONTENT_STMT); + "", BLOGC_TEMPLATE_CONTENT_STMT); tmp = tmp->next->next->next->next->next->next->next->next->next->next; - blogc_assert_template_if_stmt(tmp, "BOLA", BLOGC_TEMPLATE_OP_EQ, "\"1\\\"0\""); - blogc_assert_template_stmt(tmp->next, "aee", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp, "BOLA", BLOGC_TEMPLATE_FOREACH_STMT); + blogc_assert_template_stmt(tmp->next, "hahaha", + BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next, NULL, + BLOGC_TEMPLATE_ENDFOREACH_STMT); + blogc_assert_template_stmt(tmp->next->next->next, "\n", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_if_stmt(tmp->next->next->next->next, "BOLA", + BLOGC_TEMPLATE_OP_EQ, "\"1\\\"0\""); + 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); + assert_null(tmp->next->next->next->next->next->next->next); blogc_template_free_stmts(stmts); } @@ -121,26 +131,27 @@ test_template_parse_crlf(void **state) const char *a = "Test\r\n" "\r\n" - " {% block entry %}\r\n" + " {%- block entry -%}\r\n" "{% ifdef CHUNDA %}\r\n" "bola\r\n" "{% endif %}\r\n" "{% ifndef BOLA %}\r\n" "bolao\r\n" - "{% endif %}\r\n" + "{%- endif %}\r\n" "{% endblock %}\r\n" "{% 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 %}"; 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, "Test\r\n\r\n ", + blogc_assert_template_stmt(stmts, "Test", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next, "entry", BLOGC_TEMPLATE_BLOCK_STMT); - blogc_assert_template_stmt(stmts->next->next, "\r\n", + blogc_assert_template_stmt(stmts->next->next, "", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next->next->next, "CHUNDA", BLOGC_TEMPLATE_IFDEF_STMT); @@ -152,7 +163,7 @@ test_template_parse_crlf(void **state) BLOGC_TEMPLATE_CONTENT_STMT); b_slist_t *tmp = stmts->next->next->next->next->next->next->next; blogc_assert_template_stmt(tmp, "BOLA", BLOGC_TEMPLATE_IFNDEF_STMT); - blogc_assert_template_stmt(tmp->next, "\r\nbolao\r\n", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp->next, "\r\nbolao", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT); blogc_assert_template_stmt(tmp->next->next->next, "\r\n", BLOGC_TEMPLATE_CONTENT_STMT); @@ -174,13 +185,22 @@ test_template_parse_crlf(void **state) blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next->next, - "\r\n", BLOGC_TEMPLATE_CONTENT_STMT); + "", BLOGC_TEMPLATE_CONTENT_STMT); tmp = tmp->next->next->next->next->next->next->next->next->next->next; - blogc_assert_template_if_stmt(tmp, "BOLA", BLOGC_TEMPLATE_OP_EQ, "\"1\\\"0\""); - blogc_assert_template_stmt(tmp->next, "aee", BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(tmp, "BOLA", BLOGC_TEMPLATE_FOREACH_STMT); + blogc_assert_template_stmt(tmp->next, "hahaha", + BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next, NULL, + BLOGC_TEMPLATE_ENDFOREACH_STMT); + blogc_assert_template_stmt(tmp->next->next->next, "\r\n", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_if_stmt(tmp->next->next->next->next, "BOLA", + BLOGC_TEMPLATE_OP_EQ, "\"1\\\"0\""); + 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); + assert_null(tmp->next->next->next->next->next->next->next); blogc_template_free_stmts(stmts); } @@ -361,6 +381,26 @@ test_template_parse_invalid_block_start(void **state) "Invalid statement syntax. Must begin with lowercase letter.\n" "Error occurred near line 1, position 4: {% ASD %}"); blogc_error_free(err); + a = "{%-- block entry %}\n"; + err = NULL; + 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 statement syntax. Duplicated whitespace cleaner before statement.\n" + "Error occurred near line 1, position 4: {%-- block entry %}"); + blogc_error_free(err); + a = "{% block entry --%}\n"; + err = NULL; + 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 statement syntax. Duplicated whitespace cleaner after statement.\n" + "Error occurred near line 1, position 17: {% block entry --%}"); + blogc_error_free(err); } @@ -383,6 +423,24 @@ test_template_parse_invalid_block_nested(void **state) static void +test_template_parse_invalid_foreach_nested(void **state) +{ + const char *a = + "{% foreach A %}\n" + "{% foreach B %}\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, + "'foreach' statements can't be nested.\n" + "Error occurred near line 2, position 11: {% foreach B %}"); + blogc_error_free(err); +} + + +static void test_template_parse_invalid_block_not_open(void **state) { const char *a = "{% endblock %}\n"; @@ -416,6 +474,22 @@ test_template_parse_invalid_endif_not_open(void **state) static void +test_template_parse_invalid_endforeach_not_open(void **state) +{ + const char *a = "{% endforeach %}\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, + "'endforeach' statement without an open 'foreach' statement.\n" + "Error occurred near line 1, position 14: {% endforeach %}"); + blogc_error_free(err); +} + + +static void test_template_parse_invalid_block_name(void **state) { const char *a = "{% chunda %}\n"; @@ -426,7 +500,7 @@ test_template_parse_invalid_block_name(void **state) assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "Invalid statement type: Allowed types are: 'block', 'endblock', 'ifdef', " - "'ifndef' and 'endif'.\n" + "'ifndef', 'endif', 'foreach' and 'endforeach'.\n" "Error occurred near line 1, position 10: {% chunda %}"); blogc_error_free(err); } @@ -482,6 +556,23 @@ test_template_parse_invalid_ifdef_start(void **state) static void +test_template_parse_invalid_foreach_start(void **state) +{ + const char *a = "{% block entry %}{% foreach guda %}\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 foreach variable name. Must begin with uppercase letter.\n" + "Error occurred near line 1, position 29: " + "{% block entry %}{% foreach guda %}"); + blogc_error_free(err); +} + + +static void test_template_parse_invalid_ifdef_variable(void **state) { const char *a = "{% block entry %}{% ifdef BoLA %}\n"; @@ -499,6 +590,57 @@ test_template_parse_invalid_ifdef_variable(void **state) static void +test_template_parse_invalid_ifdef_variable2(void **state) +{ + const char *a = "{% block entry %}{% ifdef 0123 %}\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 variable name. Must begin with uppercase letter.\n" + "Error occurred near line 1, position 27: " + "{% block entry %}{% ifdef 0123 %}"); + blogc_error_free(err); +} + + +static void +test_template_parse_invalid_foreach_variable(void **state) +{ + const char *a = "{% block entry %}{% foreach 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, + "Invalid foreach variable name. Must be uppercase letter, number or '_'.\n" + "Error occurred near line 1, position 30: " + "{% block entry %}{% foreach BoLA %}"); + blogc_error_free(err); +} + + +static void +test_template_parse_invalid_foreach_variable2(void **state) +{ + const char *a = "{% block entry %}{% foreach 0123 %}\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 foreach variable name. Must begin with uppercase letter.\n" + "Error occurred near line 1, position 29: {% block entry %}" + "{% foreach 0123 %}"); + blogc_error_free(err); +} + + +static void test_template_parse_invalid_if_operator(void **state) { const char *a = "{% block entry %}{% if BOLA = \"asd\" %}\n"; @@ -525,7 +667,7 @@ test_template_parse_invalid_if_operand(void **state) 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" + "Invalid 'if' operand. Must be double-quoted static string or variable.\n" "Error occurred near line 1, position 32: " "{% block entry %}{% if BOLA == asd %}"); blogc_error_free(err); @@ -550,6 +692,23 @@ test_template_parse_invalid_if_operand2(void **state) static void +test_template_parse_invalid_if_operand3(void **state) +{ + const char *a = "{% block entry %}{% if BOLA == 0123 %}\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 or variable.\n" + "Error occurred near line 1, position 32: " + "{% block entry %}{% if BOLA == 0123 %}"); + blogc_error_free(err); +} + + +static void test_template_parse_invalid_block_end(void **state) { const char *a = "{% block entry }}\n"; @@ -600,6 +759,23 @@ test_template_parse_invalid_variable_name2(void **state) static void +test_template_parse_invalid_variable_name3(void **state) +{ + const char *a = "{% block entry %}{{ 0123 }}{% 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, + "Invalid variable name. Must begin with uppercase letter.\n" + "Error occurred near line 1, position 21: {% block entry %}{{ 0123 }}" + "{% endblock %}"); + blogc_error_free(err); +} + + +static void test_template_parse_invalid_variable_end(void **state) { const char *a = "{% block entry %}{{ BOLA %}{% endblock %}\n"; @@ -678,6 +854,20 @@ test_template_parse_invalid_block_not_closed(void **state) } +static void +test_template_parse_invalid_foreach_not_closed(void **state) +{ + const char *a = "{% foreach 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, "An open 'foreach' statement was not closed!"); + blogc_error_free(err); +} + + int main(void) { @@ -688,24 +878,33 @@ main(void) unit_test(test_template_parse_ifdef_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_foreach_nested), unit_test(test_template_parse_invalid_block_not_open), unit_test(test_template_parse_invalid_endif_not_open), + unit_test(test_template_parse_invalid_endforeach_not_open), 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), unit_test(test_template_parse_invalid_ifdef_start), + unit_test(test_template_parse_invalid_foreach_start), unit_test(test_template_parse_invalid_ifdef_variable), + unit_test(test_template_parse_invalid_ifdef_variable2), + unit_test(test_template_parse_invalid_foreach_variable), + unit_test(test_template_parse_invalid_foreach_variable2), 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_if_operand3), unit_test(test_template_parse_invalid_block_end), unit_test(test_template_parse_invalid_variable_name), unit_test(test_template_parse_invalid_variable_name2), + unit_test(test_template_parse_invalid_variable_name3), unit_test(test_template_parse_invalid_variable_end), unit_test(test_template_parse_invalid_close), unit_test(test_template_parse_invalid_close2), unit_test(test_template_parse_invalid_if_not_closed), unit_test(test_template_parse_invalid_block_not_closed), + unit_test(test_template_parse_invalid_foreach_not_closed), }; return run_tests(tests); } diff --git a/tests/check_utils.c b/tests/check_utils.c index a42c75a..a511dda 100644 --- a/tests/check_utils.c +++ b/tests/check_utils.c @@ -1,6 +1,6 @@ /* * blogc: A blog compiler. - * Copyright (C) 2014-2015 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * Copyright (C) 2014-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br> * * This program can be distributed under the terms of the BSD License. * See the file LICENSE. @@ -129,6 +129,50 @@ test_str_ends_with(void **state) static void +test_str_lstrip(void **state) +{ + char *str = b_strdup(" \tbola\n \t"); + assert_string_equal(b_str_lstrip(str), "bola\n \t"); + free(str); + str = b_strdup("guda"); + assert_string_equal(b_str_lstrip(str), "guda"); + free(str); + str = b_strdup("\n"); + assert_string_equal(b_str_lstrip(str), ""); + free(str); + str = b_strdup("\t \n"); + assert_string_equal(b_str_lstrip(str), ""); + free(str); + str = b_strdup(""); + assert_string_equal(b_str_lstrip(str), ""); + free(str); + assert_null(b_str_lstrip(NULL)); +} + + +static void +test_str_rstrip(void **state) +{ + char *str = b_strdup(" \tbola\n \t"); + assert_string_equal(b_str_rstrip(str), " \tbola"); + free(str); + str = b_strdup("guda"); + assert_string_equal(b_str_rstrip(str), "guda"); + free(str); + str = b_strdup("\n"); + assert_string_equal(b_str_rstrip(str), ""); + free(str); + str = b_strdup("\t \n"); + assert_string_equal(b_str_rstrip(str), ""); + free(str); + str = b_strdup(""); + assert_string_equal(b_str_rstrip(str), ""); + free(str); + assert_null(b_str_rstrip(NULL)); +} + + +static void test_str_strip(void **state) { char *str = b_strdup(" \tbola\n \t"); @@ -137,6 +181,15 @@ test_str_strip(void **state) str = b_strdup("guda"); assert_string_equal(b_str_strip(str), "guda"); free(str); + str = b_strdup("\n"); + assert_string_equal(b_str_strip(str), ""); + free(str); + str = b_strdup("\t \n"); + assert_string_equal(b_str_strip(str), ""); + free(str); + str = b_strdup(""); + assert_string_equal(b_str_strip(str), ""); + free(str); assert_null(b_str_strip(NULL)); } @@ -799,6 +852,8 @@ main(void) unit_test(test_strdup_printf), unit_test(test_str_starts_with), unit_test(test_str_ends_with), + unit_test(test_str_lstrip), + unit_test(test_str_rstrip), unit_test(test_str_strip), unit_test(test_str_split), unit_test(test_str_replace), |