/* * blogc: A blog compiler. * Copyright (C) 2015 Rafael G. Martins * * This program can be distributed under the terms of the BSD License. * See the file COPYING. */ #ifdef HAVE_CONFIG_H #include #endif /* HAVE_CONFIG_H */ #include #include #include #include #include #include "../src/template-parser.h" #include "../src/error.h" #include "../src/utils/utils.h" static void blogc_assert_template_stmt(b_slist_t *l, const char *value, const blogc_template_stmt_type_t type) { blogc_template_stmt_t *stmt = l->data; if (value == NULL) assert_null(stmt->value); else assert_string_equal(stmt->value, value); assert_int_equal(stmt->type, type); } static void test_template_parse(void **state) { const char *a = "Test\n" "\n" " {% block entry %}\n" "{% if CHUNDA %}\n" "bola\n" "{% endif %}\n" "{% endblock %}\n" "{% block listing %}{{ BOLA }}{% endblock %}\n" "{% block listing_once %}asd{% endblock %}\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, "Test\n\n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next, "entry", BLOGC_TEMPLATE_BLOCK_STMT); blogc_assert_template_stmt(stmts->next->next, "\n", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next->next->next, "CHUNDA", BLOGC_TEMPLATE_IF_STMT); blogc_assert_template_stmt(stmts->next->next->next->next, "\nbola\n", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next->next->next->next->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT); blogc_assert_template_stmt(stmts->next->next->next->next->next->next, "\n", BLOGC_TEMPLATE_CONTENT_STMT); b_slist_t *tmp = stmts->next->next->next->next->next->next->next; blogc_assert_template_stmt(tmp, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT); blogc_assert_template_stmt(tmp->next, "\n", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next, "listing", BLOGC_TEMPLATE_BLOCK_STMT); blogc_assert_template_stmt(tmp->next->next->next, "BOLA", BLOGC_TEMPLATE_VARIABLE_STMT); blogc_assert_template_stmt(tmp->next->next->next->next, NULL, BLOGC_TEMPLATE_ENDBLOCK_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, "listing_once", BLOGC_TEMPLATE_BLOCK_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next, "asd", BLOGC_TEMPLATE_CONTENT_STMT); 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); assert_null(tmp->next->next->next->next->next->next->next->next->next->next); blogc_template_free_stmts(stmts); } static void test_template_parse_html(void **state) { const char *a = "\n" " \n" " {% block entry %}\n" " My cool blog >> {{ TITLE }}\n" " {% endblock %}\n" " {% block listing_once %}\n" " My cool blog - Main page\n" " {% endblock %}\n" " \n" " \n" "

My cool blog

\n" " {% block entry %}\n" "

{{ TITLE }}

\n" " {% if DATE %}

Published in: {{ DATE }}

{% endif %}\n" "
{{ CONTENT }}
\n" " {% endblock %}\n" " {% block listing_once %}
    {% endblock %}\n" " {% block listing %}

    " "{{ TITLE }}{% if DATE %} - {{ DATE }}{% endif %}

    {% endblock %}\n" " {% block listing_once %}
{% endblock %}\n" " \n" "\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, "\n \n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next, "entry", BLOGC_TEMPLATE_BLOCK_STMT); blogc_assert_template_stmt(stmts->next->next, "\n My cool blog >> ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next->next->next, "TITLE", BLOGC_TEMPLATE_VARIABLE_STMT); blogc_assert_template_stmt(stmts->next->next->next->next, "\n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next->next->next->next->next, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT); blogc_assert_template_stmt(stmts->next->next->next->next->next->next, "\n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(stmts->next->next->next->next->next->next->next, "listing_once", BLOGC_TEMPLATE_BLOCK_STMT); b_slist_t *tmp = stmts->next->next->next->next->next->next->next->next; blogc_assert_template_stmt(tmp, "\n My cool blog - Main page\n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT); blogc_assert_template_stmt(tmp->next->next, "\n \n \n

My cool blog

\n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next->next, "entry", BLOGC_TEMPLATE_BLOCK_STMT); blogc_assert_template_stmt(tmp->next->next->next->next, "\n

", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next, "TITLE", BLOGC_TEMPLATE_VARIABLE_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next->next, "

\n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next, "DATE", BLOGC_TEMPLATE_IF_STMT); tmp = tmp->next->next->next->next->next->next->next->next; blogc_assert_template_stmt(tmp, "

Published in: ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next, "DATE", BLOGC_TEMPLATE_VARIABLE_STMT); blogc_assert_template_stmt(tmp->next->next, "

", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT); blogc_assert_template_stmt(tmp->next->next->next->next, "\n
",
        BLOGC_TEMPLATE_CONTENT_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next,
        "CONTENT", BLOGC_TEMPLATE_VARIABLE_STMT);
    blogc_assert_template_stmt(tmp->next->next->next->next->next->next,
        "
\n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT); tmp = tmp->next->next->next->next->next->next->next->next; blogc_assert_template_stmt(tmp, "\n ", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next, "listing_once", BLOGC_TEMPLATE_BLOCK_STMT); blogc_assert_template_stmt(tmp->next->next, "", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next->next->next, NULL, BLOGC_TEMPLATE_ENDBLOCK_STMT); blogc_assert_template_stmt(tmp->next->next->next->next->next, "\n \n\n", BLOGC_TEMPLATE_CONTENT_STMT); assert_null(tmp->next->next->next->next->next->next); blogc_template_free_stmts(stmts); } int main(void) { const UnitTest tests[] = { unit_test(test_template_parse), unit_test(test_template_parse_html), }; return run_tests(tests); }