aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2015-04-23 01:37:57 -0300
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2015-04-23 01:37:57 -0300
commit8d2b09e474dd0e3fdd8d3c03a0b520a398a9e82a (patch)
tree1e2e93d6b93a1d416fb80665612f8c145564e676
parent280f6199a03ebc17fc2dc515079a6c09b1bf13f9 (diff)
downloadblogc-8d2b09e474dd0e3fdd8d3c03a0b520a398a9e82a.tar.gz
blogc-8d2b09e474dd0e3fdd8d3c03a0b520a398a9e82a.tar.bz2
blogc-8d2b09e474dd0e3fdd8d3c03a0b520a398a9e82a.zip
added renderer tests
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am18
-rw-r--r--tests/check_renderer.c235
3 files changed, 254 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index c0943f3..bc5553c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -42,6 +42,7 @@ Makefile.in
# tests
/tests/check_error
/tests/check_loader
+/tests/check_renderer
/tests/check_source_parser
/tests/check_template_parser
/tests/check_utils
diff --git a/Makefile.am b/Makefile.am
index 75e68ea..0f5c5e8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -84,6 +84,7 @@ if USE_CMOCKA
check_PROGRAMS += \
tests/check_error \
tests/check_loader \
+ tests/check_renderer \
tests/check_source_parser \
tests/check_template_parser \
tests/check_utils \
@@ -123,6 +124,23 @@ tests_check_loader_LDADD = \
libblogc.la \
$(NULL)
+tests_check_renderer_SOURCES = \
+ tests/check_renderer.c \
+ $(NULL)
+
+tests_check_renderer_CFLAGS = \
+ $(CMOCKA_CFLAGS) \
+ $(NULL)
+
+tests_check_renderer_LDFLAGS = \
+ -no-install \
+ $(NULL)
+
+tests_check_renderer_LDADD = \
+ $(CMOCKA_LIBS) \
+ libblogc.la \
+ $(NULL)
+
tests_check_source_parser_SOURCES = \
tests/check_source_parser.c \
$(NULL)
diff --git a/tests/check_renderer.c b/tests/check_renderer.c
new file mode 100644
index 0000000..bbc2953
--- /dev/null
+++ b/tests/check_renderer.c
@@ -0,0 +1,235 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file COPYING.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <string.h>
+#include "../src/error.h"
+#include "../src/renderer.h"
+#include "../src/source-parser.h"
+#include "../src/template-parser.h"
+#include "../src/utils/utils.h"
+
+
+static b_slist_t*
+create_sources(unsigned int count)
+{
+ const char *s[] = {
+ "BOLA: asd\n"
+ "GUDA: zxc\n"
+ "-----\n"
+ "ahahahahahahahaha",
+ "BOLA: asd2\n"
+ "GUDA: zxc2\n"
+ "-----\n"
+ "ahahahahahahahaha2",
+ "BOLA: asd3\n"
+ "GUDA: zxc3\n"
+ "-----\n"
+ "ahahahahahahahaha3",
+ };
+ assert_false(count > 3);
+ blogc_error_t *err = NULL;
+ b_slist_t *l = NULL;
+ for (unsigned int i = 0; i < count; i++) {
+ l = b_slist_append(l, blogc_source_parse(s[i], strlen(s[i]), &err));
+ assert_null(err);
+ }
+ assert_int_equal(b_slist_length(l), count);
+ return l;
+}
+
+
+static void
+test_render_entry(void **state)
+{
+ const char *str =
+ "foo\n"
+ "{% block listing_once %}fuuu{% endblock %}\n"
+ "{% block entry %}\n"
+ "{% if GUDA %}{{ GUDA }}{% endif %}\n"
+ "{% if CHUNDA %}{{ CHUNDA }}{% endif %}\n"
+ "{% endblock %}\n"
+ "{% block listing %}lol{% 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, false);
+ assert_string_equal(out,
+ "foo\n"
+ "\n"
+ "\n"
+ "zxc\n"
+ "\n"
+ "\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_listing(void **state)
+{
+ const char *str =
+ "foo\n"
+ "{% block listing_once %}fuuu{% endblock %}\n"
+ "{% block entry %}\n"
+ "{% if GUDA %}{{ GUDA }}{% endif %}\n"
+ "{% if CHUNDA %}{{ CHUNDA }}{% endif %}\n"
+ "{% endblock %}\n"
+ "{% block listing %}\n"
+ "bola: {% if BOLA %}{{ BOLA }}{% 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(3);
+ assert_non_null(s);
+ char *out = blogc_render(l, s, true);
+ assert_string_equal(out,
+ "foo\n"
+ "fuuu\n"
+ "\n"
+ "\n"
+ "bola: asd\n"
+ "\n"
+ "bola: asd2\n"
+ "\n"
+ "bola: asd3\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_if(void **state)
+{
+ const char *str =
+ "{% block entry %}\n"
+ "{% if CHUNDA %}chunda\n"
+ "{% if GUDA %}guda\n"
+ "{% if 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);
+ char *out = blogc_render(l, s, false);
+ assert_string_equal(out,
+ "\n"
+ "\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_if2(void **state)
+{
+ const char *str =
+ "{% block entry %}\n"
+ "{% if GUDA %}guda\n"
+ "{% if CHUNDA %}chunda\n"
+ "{% if 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);
+ char *out = blogc_render(l, s, false);
+ assert_string_equal(out,
+ "\n"
+ "guda\n"
+ "\n"
+ "\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_if3(void **state)
+{
+ const char *str =
+ "{% block entry %}\n"
+ "{% if GUDA %}guda\n"
+ "{% if BOLA %}bola\n"
+ "{% if CHUNDA %}chunda\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);
+ char *out = blogc_render(l, s, false);
+ assert_string_equal(out,
+ "\n"
+ "guda\n"
+ "bola\n"
+ "\n"
+ "\n"
+ "\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, false));
+}
+
+
+int
+main(void)
+{
+ const UnitTest tests[] = {
+ unit_test(test_render_entry),
+ unit_test(test_render_listing),
+ unit_test(test_render_if),
+ unit_test(test_render_if2),
+ unit_test(test_render_if3),
+ unit_test(test_render_null),
+ };
+ return run_tests(tests);
+}