From 047e4e3753c597628024847a524d44ca67fa1382 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Fri, 17 Apr 2015 01:47:41 -0300 Subject: replaced leg-based parser with handmade parser for source files --- tests/check_source_grammar.c | 74 ---------------------------------------- tests/check_source_parser.c | 77 ++++++++++++++++++++++++++++++++++++++++++ tests/check_template_grammar.c | 47 ++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 74 deletions(-) delete mode 100644 tests/check_source_grammar.c create mode 100644 tests/check_source_parser.c (limited to 'tests') diff --git a/tests/check_source_grammar.c b/tests/check_source_grammar.c deleted file mode 100644 index b2f581b..0000000 --- a/tests/check_source_grammar.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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 "../src/source-grammar.h" - - -static void -test_source_parse(void **state) -{ - blogc_source_t *source = blogc_source_parse( - "VAR1: asd asd\n" - "VAR2: 123chunda\n" - "----------\n" - "# This is a test\n" - "\n" - "bola\n"); - assert_non_null(source); - assert_int_equal(b_trie_size(source->config), 2); - assert_string_equal(b_trie_lookup(source->config, "VAR1"), "asd asd"); - assert_string_equal(b_trie_lookup(source->config, "VAR2"), "123chunda"); - assert_string_equal(source->content, - "# This is a test\n" - "\n" - "bola\n"); - blogc_source_free(source); -} - - -static void -test_source_parse_with_spaces(void **state) -{ - blogc_source_t *source = blogc_source_parse( - "\n \n" - "VAR1: chunda \t \n" - "\n\n" - "BOLA: guda\n" - "----------\n" - "# This is a test\n" - "\n" - "bola\n"); - assert_non_null(source); - assert_int_equal(b_trie_size(source->config), 2); - assert_string_equal(b_trie_lookup(source->config, "VAR1"), "chunda"); - assert_string_equal(b_trie_lookup(source->config, "BOLA"), "guda"); - assert_string_equal(source->content, - "# This is a test\n" - "\n" - "bola\n"); - blogc_source_free(source); -} - - -int -main(void) -{ - const UnitTest tests[] = { - unit_test(test_source_parse), - unit_test(test_source_parse_with_spaces), - }; - return run_tests(tests); -} diff --git a/tests/check_source_parser.c b/tests/check_source_parser.c new file mode 100644 index 0000000..2f5880a --- /dev/null +++ b/tests/check_source_parser.c @@ -0,0 +1,77 @@ +/* + * 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/source-parser.h" + + +static void +test_source_parse(void **state) +{ + const char *a = + "VAR1: asd asd\n" + "VAR2: 123chunda\n" + "----------\n" + "# This is a test\n" + "\n" + "bola\n"; + blogc_source_t *source = blogc_source_parse(a, strlen(a)); + assert_non_null(source); + assert_int_equal(b_trie_size(source->config), 2); + assert_string_equal(b_trie_lookup(source->config, "VAR1"), "asd asd"); + assert_string_equal(b_trie_lookup(source->config, "VAR2"), "123chunda"); + assert_string_equal(source->content, + "# This is a test\n" + "\n" + "bola\n"); + blogc_source_free(source); +} + + +static void +test_source_parse_with_spaces(void **state) +{ + const char *a = + "\n \n" + "VAR1: chunda \t \n" + "\n\n" + "BOLA: guda\n" + "----------\n" + "# This is a test\n" + "\n" + "bola\n"; + blogc_source_t *source = blogc_source_parse(a, strlen(a)); + assert_non_null(source); + assert_int_equal(b_trie_size(source->config), 2); + assert_string_equal(b_trie_lookup(source->config, "VAR1"), "chunda"); + assert_string_equal(b_trie_lookup(source->config, "BOLA"), "guda"); + assert_string_equal(source->content, + "# This is a test\n" + "\n" + "bola\n"); + blogc_source_free(source); +} + + +int +main(void) +{ + const UnitTest tests[] = { + unit_test(test_source_parse), + unit_test(test_source_parse_with_spaces), + }; + return run_tests(tests); +} diff --git a/tests/check_template_grammar.c b/tests/check_template_grammar.c index ffad8fb..3b4dcca 100644 --- a/tests/check_template_grammar.c +++ b/tests/check_template_grammar.c @@ -88,11 +88,58 @@ test_template_parse(void **state) } +static void +test_template_parse_html(void **state) +{ + b_slist_t *stmts = blogc_template_parse( + "\n" + " \n" + " {% block single_source %}\n" + " My cool blog >> {{ TITLE }}\n" + " {% endblock %}\n" + " {% block multiple_sources %}\n" + " My cool blog - Main page\n" + " {% endblock %}\n" + " \n" + " \n" + "

My cool blog

\n" + " {% block single_source %}\n" + "

{{ TITLE }}

\n" + " {% if DATE %}

Published in: {{ DATE }}

{% endif %}\n" + "
{{ CONTENT }}
\n" + " {% endblock %}\n" + " {% block multiple_sources_once %}
    {% endblock %}\n" + " {% block multiple_sources %}

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

    {% endblock %}\n" + " {% block multiple_sources_once %}
{% endblock %}\n" + " \n" + "\n"); + assert_non_null(stmts); + blogc_assert_template_stmt(stmts, "\n \n ", + BLOGC_TEMPLATE_CONTENT_STMT); + blogc_assert_template_stmt(stmts->next, "single_source", + 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_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); } -- cgit v1.2.3-18-g5258