gplate/functions/tests/test-gplate-for-function.c
changeset 402 b9a2a060c766
parent 394 ad26d98e20e6
child 406 9b903d04907a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gplate/functions/tests/test-gplate-for-function.c	Sun Jul 04 03:40:27 2010 -0500
     1.3 @@ -0,0 +1,188 @@
     1.4 +/*
     1.5 + * Copyright (C) 2007-2010 Gary Kramlich <grim@reaperworld.com>
     1.6 + *
     1.7 + * This program is free software: you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation, either version 3 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.19 + */
    1.20 +#include <gplate/gplate.h>
    1.21 +#include <gplate/variables/gplate-dictionary-variable.h>
    1.22 +
    1.23 +#include <glib.h>
    1.24 +
    1.25 +/******************************************************************************
    1.26 + * Structs
    1.27 + *****************************************************************************/
    1.28 +typedef struct {
    1.29 +	const gchar *name;
    1.30 +	const gchar *value;
    1.31 +} GPlateForFunctionTestData;
    1.32 +
    1.33 +typedef struct {
    1.34 +	GPlateTemplate *template;
    1.35 +
    1.36 +	GPlateVariable *dict;
    1.37 +
    1.38 +	const gchar *template_string;
    1.39 +
    1.40 +	gchar *actual;
    1.41 +	const gchar *expected;
    1.42 +
    1.43 +	GError *error;
    1.44 +
    1.45 +	GPlateForFunctionTestData *data;
    1.46 +} GPlateForFunctionTestFixture;
    1.47 +
    1.48 +/******************************************************************************
    1.49 + * Fixtures
    1.50 + *****************************************************************************/
    1.51 +static void
    1.52 +gplate_for_function_test_setup(GPlateForFunctionTestFixture *fixture,
    1.53 +                               gconstpointer d)
    1.54 +{
    1.55 +	fixture->template = gplate_template_new();
    1.56 +	fixture->dict = gplate_dictionary_variable_new("list");
    1.57 +
    1.58 +	gplate_collection_add_variable(GPLATE_COLLECTION(fixture->template),
    1.59 +	                               fixture->dict);
    1.60 +}
    1.61 +
    1.62 +static void
    1.63 +gplate_for_function_test_teardown(GPlateForFunctionTestFixture *fixture,
    1.64 +                                  gconstpointer d)
    1.65 +{
    1.66 +	g_object_unref(fixture->template);
    1.67 +	fixture->template = NULL;
    1.68 +
    1.69 +	g_object_unref(fixture->dict);
    1.70 +	fixture->dict = NULL;
    1.71 +
    1.72 +	g_free(fixture->actual);
    1.73 +	fixture->actual = NULL;
    1.74 +
    1.75 +	fixture->expected = NULL;
    1.76 +
    1.77 +	if(fixture->error) {
    1.78 +		g_error_free(fixture->error);
    1.79 +		fixture->error = NULL;
    1.80 +	}
    1.81 +}
    1.82 +
    1.83 +/******************************************************************************
    1.84 + * Helpers
    1.85 + *****************************************************************************/
    1.86 +static void
    1.87 +gplate_for_function_test(GPlateForFunctionTestFixture *fixture) {
    1.88 +	gint i = 0;
    1.89 +
    1.90 +	/* add the variables */
    1.91 +	for(i = 0; fixture->data[i].name; i++) {
    1.92 +		gplate_collection_add_string(fixture->dict,
    1.93 +		                             fixture->data[i].name,
    1.94 +		                             fixture->data[i].value);
    1.95 +	}
    1.96 +
    1.97 +	fixture->actual = gplate_template_render(fixture->template,
    1.98 +	                                         fixture->template_string,
    1.99 +	                                         &fixture->error);
   1.100 +
   1.101 +	g_assert(fixture->error == NULL);
   1.102 +
   1.103 +	g_assert_cmpstr(fixture->expected, ==, fixture->actual);
   1.104 +}
   1.105 +
   1.106 +/******************************************************************************
   1.107 + * Simple For's
   1.108 + *****************************************************************************/
   1.109 +static void
   1.110 +gplate_for_function_test_zero_elements(GPlateForFunctionTestFixture *fixture,
   1.111 +                                       gconstpointer user_data)
   1.112 +{
   1.113 +	GPlateForFunctionTestData data[] = {
   1.114 +		{ NULL, NULL },
   1.115 +	};
   1.116 +
   1.117 +	fixture->data = data;
   1.118 +	fixture->template_string = "{% for in in line %}{{ i }}{% endfor %}";
   1.119 +	fixture->expected = "";
   1.120 +
   1.121 +	gplate_for_function_test(fixture);
   1.122 +}
   1.123 +
   1.124 +static void
   1.125 +gplate_for_function_test_one_element(GPlateForFunctionTestFixture *fixture,
   1.126 +                                     gconstpointer user_data)
   1.127 +{
   1.128 +	GPlateForFunctionTestData data[] = {
   1.129 +		{ "one", "1" },
   1.130 +		{ NULL, NULL },
   1.131 +	};
   1.132 +
   1.133 +	fixture->data = data;
   1.134 +	fixture->template_string = "{% for i in list %}{{ i }}{% endfor %}";
   1.135 +	fixture->expected = "1";
   1.136 +
   1.137 +	gplate_for_function_test(fixture);
   1.138 +}
   1.139 +
   1.140 +static void
   1.141 +gplate_for_function_test_two_elements(GPlateForFunctionTestFixture *fixture,
   1.142 +                                      gconstpointer user_data)
   1.143 +{
   1.144 +	GPlateForFunctionTestData data[] = {
   1.145 +		{ "a", "A" },
   1.146 +		{ "b", "B" },
   1.147 +		{ NULL, NULL },
   1.148 +	};
   1.149 +
   1.150 +	fixture->data = data;
   1.151 +	fixture->template_string = "{% for i in list %}{{ i }}{% endfor %}";
   1.152 +	fixture->expected = "AB";
   1.153 +
   1.154 +	gplate_for_function_test(fixture);
   1.155 +}
   1.156 +
   1.157 +/******************************************************************************
   1.158 + * Main!
   1.159 + *****************************************************************************/
   1.160 +gint
   1.161 +main(gint argc, gchar **argv) {
   1.162 +	g_test_init(&argc, &argv, NULL);
   1.163 +
   1.164 +	g_type_init();
   1.165 +
   1.166 +	gplate_config_load_default();
   1.167 +
   1.168 +	g_test_add("/functions/foo/zero_elements",
   1.169 +	           GPlateForFunctionTestFixture,
   1.170 +	           NULL,
   1.171 +	           gplate_for_function_test_setup,
   1.172 +	           gplate_for_function_test_zero_elements,
   1.173 +	           gplate_for_function_test_teardown);
   1.174 +
   1.175 +	g_test_add("/functions/foo/one_element",
   1.176 +	           GPlateForFunctionTestFixture,
   1.177 +	           NULL,
   1.178 +	           gplate_for_function_test_setup,
   1.179 +	           gplate_for_function_test_one_element,
   1.180 +	           gplate_for_function_test_teardown);
   1.181 +
   1.182 +	g_test_add("/functions/foo/two_elements",
   1.183 +	           GPlateForFunctionTestFixture,
   1.184 +	           NULL,
   1.185 +	           gplate_for_function_test_setup,
   1.186 +	           gplate_for_function_test_two_elements,
   1.187 +	           gplate_for_function_test_teardown);
   1.188 +
   1.189 +	return g_test_run();
   1.190 +}
   1.191 +