diff options
| author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-12-29 00:12:51 +0100 | 
|---|---|---|
| committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-12-29 00:13:47 +0100 | 
| commit | 8d96c02e5118cf7bd656fde9100570a67115d19a (patch) | |
| tree | a3c638bab8f8e595a10ff9751bbe4c8a603a918d /tests | |
| parent | 14e9d7b2299f15efb695b0202f9c1f6a9f7a4ba6 (diff) | |
| download | blogc-8d96c02e5118cf7bd656fde9100570a67115d19a.tar.gz blogc-8d96c02e5118cf7bd656fde9100570a67115d19a.tar.bz2 blogc-8d96c02e5118cf7bd656fde9100570a67115d19a.zip  | |
man: renderer: template-parser: added foreach iterator support
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/check_renderer.c | 70 | ||||
| -rw-r--r-- | tests/check_template_parser.c | 121 | 
2 files changed, 182 insertions, 9 deletions
diff --git a/tests/check_renderer.c b/tests/check_renderer.c index cb8f8f1..cc04fdc 100644 --- a/tests/check_renderer.c +++ b/tests/check_renderer.c @@ -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,8 @@ 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";      blogc_error_t *err = NULL;      b_slist_t *l = blogc_template_parse(str, strlen(str), &err);      assert_non_null(l); @@ -97,7 +99,8 @@ test_render_entry(void **state)          "HEHE\n"          "LOL2\n"          "\n" -        "LOL4\n"); +        "LOL4\n" +        "lol foo haha lol bar haha lol baz haha \n");      blogc_template_free_stmts(l);      b_slist_free_full(s, (b_free_func_t) b_trie_free);      free(out); @@ -117,6 +120,7 @@ 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"          "{% endblock %}\n";      blogc_error_t *err = NULL;      b_slist_t *l = blogc_template_parse(str, strlen(str), &err); @@ -132,12 +136,15 @@ test_render_listing(void **state)          "\n"          "03:04\n"          "bola: asd\n" +        "lol foo haha lol bar haha lol baz haha \n"          "\n"          "2014-02-03 04:05:06\n"          "bola: asd2\n"          "\n" +        "\n"          "2013-01-02 03:04:05\n"          "bola: asd3\n" +        "\n"          "\n");      blogc_template_free_stmts(l);      b_slist_free_full(s, (b_free_func_t) b_trie_free); @@ -158,6 +165,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 +527,30 @@ 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_null(void **state)  {      assert_null(blogc_render(NULL, NULL, NULL, false)); @@ -760,6 +792,37 @@ test_format_variable_with_date(void **state)  } +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,6 +840,7 @@ 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_null),          unit_test(test_render_outside_block),          unit_test(test_render_prefer_local_variable), @@ -790,6 +854,8 @@ main(void)          unit_test(test_format_date_without_date),          unit_test(test_format_variable),          unit_test(test_format_variable_with_date), +        unit_test(test_split_list_variable), +        unit_test(test_split_list_variable_not_found),      };      return run_tests(tests);  } diff --git a/tests/check_template_parser.c b/tests/check_template_parser.c index b712f22..6aaceed 100644 --- a/tests/check_template_parser.c +++ b/tests/check_template_parser.c @@ -61,6 +61,7 @@ test_template_parse(void **state)          "{% 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); @@ -106,11 +107,20 @@ 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", 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);  } @@ -131,6 +141,7 @@ test_template_parse_crlf(void **state)          "{% 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); @@ -176,11 +187,20 @@ test_template_parse_crlf(void **state)      blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next->next,          "\r\n", 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);  } @@ -383,6 +403,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 +454,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 +480,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 +536,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 +570,23 @@ test_template_parse_invalid_ifdef_variable(void **state)  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_if_operator(void **state)  {      const char *a = "{% block entry %}{% if BOLA = \"asd\" %}\n"; @@ -678,6 +766,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,13 +790,17 @@ 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_foreach_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), @@ -706,6 +812,7 @@ main(void)          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);  }  | 
