grim/gplate

1213fc842268
Parents ec39b68558fa
Children bb3ede62e68b
added functions to the new testing stuff
--- a/tests/CMakeLists.txt Mon Sep 03 22:33:34 2012 -0500
+++ b/tests/CMakeLists.txt Tue Sep 04 01:26:16 2012 -0500
@@ -1,3 +1,4 @@
+add_subdirectory(functions)
add_subdirectory(tags)
add_executable(test-gplate test-gplate.c)
@@ -5,6 +6,7 @@
target_link_libraries(test-gplate
${GLIB_LIBRARIES}
gplate
+ test-gplate-functions
test-gplate-tags
)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/functions/CMakeLists.txt Tue Sep 04 01:26:16 2012 -0500
@@ -0,0 +1,15 @@
+set(TEST_GPLATE_FUNCTIONS_HEADERS
+ test-gplate-functions.h
+)
+
+set(TEST_GPLATE_FUNCTIONS_SOURCES
+ test-gplate-functions.c
+ test-gplate-for-function.c
+ test-gplate-include-function.c
+)
+
+add_library(test-gplate-functions STATIC
+ ${TEST_GPLATE_FUNCTIONS_HEADERS}
+ ${TEST_GPLATE_FUNCTIONS_SOURCES}
+)
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/functions/test-gplate-for-function.c Tue Sep 04 01:26:16 2012 -0500
@@ -0,0 +1,181 @@
+/*
+ * GPlate - GObject based templating library
+ * Copyright (C) 2007-2012 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <gplate/gplate.h>
+#include <gplate/variables/gplate-dictionary-variable.h>
+
+#include <glib.h>
+
+/******************************************************************************
+ * Structs
+ *****************************************************************************/
+typedef struct {
+ const gchar *name;
+ const gchar *value;
+} TestGPlateForFunctionData;
+
+typedef struct {
+ GPlateTemplate *template;
+
+ GPlateVariable *dict;
+
+ const gchar *template_string;
+
+ gchar *actual;
+ const gchar *expected;
+
+ GError *error;
+
+ TestGPlateForFunctionData *data;
+} TestGPlateForFunctionFixture;
+
+/******************************************************************************
+ * Fixtures
+ *****************************************************************************/
+static void
+test_gplate_for_function_setup(TestGPlateForFunctionFixture *fixture,
+ gconstpointer d)
+{
+ fixture->template = gplate_template_new();
+ fixture->dict = gplate_dictionary_variable_new("list");
+
+ gplate_collection_add_variable(GPLATE_COLLECTION(fixture->template),
+ fixture->dict);
+}
+
+static void
+test_gplate_for_function_teardown(TestGPlateForFunctionFixture *fixture,
+ gconstpointer d)
+{
+ g_object_unref(fixture->template);
+ fixture->template = NULL;
+
+ g_object_unref(fixture->dict);
+ fixture->dict = NULL;
+
+ g_free(fixture->actual);
+ fixture->actual = NULL;
+
+ fixture->expected = NULL;
+
+ if(fixture->error) {
+ g_error_free(fixture->error);
+ fixture->error = NULL;
+ }
+}
+
+/******************************************************************************
+ * Helpers
+ *****************************************************************************/
+static void
+test_gplate_for_function(TestGPlateForFunctionFixture *fixture) {
+ gint i = 0;
+
+ /* add the variables */
+ for(i = 0; fixture->data[i].name; i++) {
+ gplate_collection_add_string(fixture->dict,
+ fixture->data[i].name,
+ fixture->data[i].value);
+ }
+
+ fixture->actual = gplate_template_render(fixture->template,
+ fixture->template_string,
+ &fixture->error);
+
+ g_assert(fixture->error == NULL);
+
+ g_assert_cmpstr(fixture->expected, ==, fixture->actual);
+}
+
+/******************************************************************************
+ * Simple For's
+ *****************************************************************************/
+static void
+test_gplate_for_function_zero_elements(TestGPlateForFunctionFixture *fixture,
+ gconstpointer user_data)
+{
+ TestGPlateForFunctionData data[] = {
+ { NULL, NULL },
+ };
+
+ fixture->data = data;
+ fixture->template_string = "{% for in in line %}{{ i }}{% endfor %}";
+ fixture->expected = "";
+
+ test_gplate_for_function(fixture);
+}
+
+static void
+test_gplate_for_function_one_element(TestGPlateForFunctionFixture *fixture,
+ gconstpointer user_data)
+{
+ TestGPlateForFunctionData data[] = {
+ { "one", "1" },
+ { NULL, NULL },
+ };
+
+ fixture->data = data;
+ fixture->template_string = "{% for i in list %}{{ i }}{% endfor %}";
+ fixture->expected = "1";
+
+ test_gplate_for_function(fixture);
+}
+
+static void
+test_gplate_for_function_two_elements(TestGPlateForFunctionFixture *fixture,
+ gconstpointer user_data)
+{
+ TestGPlateForFunctionData data[] = {
+ { "a", "A" },
+ { "b", "B" },
+ { NULL, NULL },
+ };
+
+ fixture->data = data;
+ fixture->template_string = "{% for i in list %}{{ i }}{% endfor %}";
+ fixture->expected = "AB";
+
+ test_gplate_for_function(fixture);
+}
+
+/******************************************************************************
+ * Main!
+ *****************************************************************************/
+void
+test_gplate_for_function_add_tests(void) {
+ g_test_add("/functions/foo/zero_elements",
+ TestGPlateForFunctionFixture,
+ NULL,
+ test_gplate_for_function_setup,
+ test_gplate_for_function_zero_elements,
+ test_gplate_for_function_teardown);
+
+ g_test_add("/functions/foo/one_element",
+ TestGPlateForFunctionFixture,
+ NULL,
+ test_gplate_for_function_setup,
+ test_gplate_for_function_one_element,
+ test_gplate_for_function_teardown);
+
+ g_test_add("/functions/foo/two_elements",
+ TestGPlateForFunctionFixture,
+ NULL,
+ test_gplate_for_function_setup,
+ test_gplate_for_function_two_elements,
+ test_gplate_for_function_teardown);
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/functions/test-gplate-functions.c Tue Sep 04 01:26:16 2012 -0500
@@ -0,0 +1,29 @@
+/*
+ * GPlate - GObject based templating library
+ * Copyright (C) 2007-2012 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "test-gplate-functions.h"
+
+/******************************************************************************
+ * API
+ *****************************************************************************/
+void
+test_gplate_functions_add_tests(void) {
+ test_gplate_for_function_add_tests();
+ test_gplate_include_function_add_tests();
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/functions/test-gplate-functions.h Tue Sep 04 01:26:16 2012 -0500
@@ -0,0 +1,36 @@
+/*
+ * GPlate - GObject based templating library
+ * Copyright (C) 2007-2012 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef TEST_GPLATE_FUNCTIONS_H
+#define TEST_GPLATE_FUNCTIONS_H
+
+#include <gplate/gplate.h>
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void test_gplate_functions_add_tests(void);
+
+/* external prototypes */
+void test_gplate_for_function_add_tests(void);
+void test_gplate_include_function_add_tests(void);
+
+G_END_DECLS
+
+#endif /* TEST_GPLATE_FUNCTIONS_H */
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/functions/test-gplate-include-function.c Tue Sep 04 01:26:16 2012 -0500
@@ -0,0 +1,124 @@
+/*
+ * GPlate - GObject based templating library
+ * Copyright (C) 2007-2012 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <gplate/gplate.h>
+
+#include <glib.h>
+
+/******************************************************************************
+ * Structs
+ *****************************************************************************/
+typedef struct {
+ GPlateTemplate *template;
+
+ const gchar *template_string;
+
+ gchar *actual;
+ const gchar *expected;
+
+ GError *error;
+} TestGPlateIncludeFunctionFixture;
+
+/******************************************************************************
+ * Fixtures
+ *****************************************************************************/
+static void
+test_gplate_include_function_setup(TestGPlateIncludeFunctionFixture *fixture,
+ gconstpointer d)
+{
+ fixture->template = gplate_template_new();
+}
+
+static void
+test_gplate_include_function_teardown(TestGPlateIncludeFunctionFixture *fixture,
+ gconstpointer d)
+{
+ g_object_unref(fixture->template);
+ fixture->template = NULL;
+
+ g_free(fixture->actual);
+ fixture->actual = NULL;
+
+ fixture->expected = NULL;
+
+ if(fixture->error) {
+ g_error_free(fixture->error);
+ fixture->error = NULL;
+ }
+}
+
+/******************************************************************************
+ * Tests
+ *****************************************************************************/
+static void
+test_gplate_include_function_simple(TestGPlateIncludeFunctionFixture *fixture,
+ gconstpointer data)
+{
+ fixture->expected = "including\nincluded!\ndone";
+
+ fixture->actual = gplate_template_render_file(fixture->template,
+ "templates/simple.gplate",
+ &fixture->error);
+
+ g_assert(fixture->error == NULL);
+
+ g_assert_cmpstr(fixture->expected, ==, fixture->actual);
+}
+
+static void
+test_gplate_include_function_double(TestGPlateIncludeFunctionFixture *fixture,
+ gconstpointer data)
+{
+ fixture->expected = " \
+<html>\
+ <head>\
+ <title>test</title>\
+ </head>\
+ <body>\
+in the body\
+ </body>\
+</html>";
+
+ fixture->actual = gplate_template_render_file(fixture->template,
+ "templates/html.gplate",
+ &fixture->error);
+
+ g_assert(fixture->error == NULL);
+
+ g_assert_cmpstr(fixture->expected, ==, fixture->actual);
+}
+
+/******************************************************************************
+ * Main!
+ *****************************************************************************/
+void
+test_gplate_include_function_add_tests(void) {
+ g_test_add("/functions/include/simple",
+ TestGPlateIncludeFunctionFixture,
+ NULL,
+ test_gplate_include_function_setup,
+ test_gplate_include_function_simple,
+ test_gplate_include_function_teardown);
+
+ g_test_add("/functions/include/double",
+ TestGPlateIncludeFunctionFixture,
+ NULL,
+ test_gplate_include_function_setup,
+ test_gplate_include_function_double,
+ test_gplate_include_function_teardown);
+}
+
--- a/tests/test-gplate.c Mon Sep 03 22:33:34 2012 -0500
+++ b/tests/test-gplate.c Tue Sep 04 01:26:16 2012 -0500
@@ -28,6 +28,7 @@
g_type_init();
+ test_gplate_functions_add_tests();
test_gplate_tag_add_tests();
return g_test_run();