2 * Copyright (C) 2007-2010 Gary Kramlich <grim@reaperworld.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <gplate/gplate.h>
18 #include <gplate/variables/gplate-dictionary-variable.h>
22 /******************************************************************************
24 *****************************************************************************/
28 } TestGPlateForFunctionData;
31 GPlateTemplate *template;
35 const gchar *template_string;
38 const gchar *expected;
42 TestGPlateForFunctionData *data;
43 } TestGPlateForFunctionFixture;
45 /******************************************************************************
47 *****************************************************************************/
49 test_gplate_for_function_setup(TestGPlateForFunctionFixture *fixture,
52 fixture->template = gplate_template_new();
53 fixture->dict = gplate_dictionary_variable_new("list");
55 gplate_collection_add_variable(GPLATE_COLLECTION(fixture->template),
60 test_gplate_for_function_teardown(TestGPlateForFunctionFixture *fixture,
63 g_object_unref(fixture->template);
64 fixture->template = NULL;
66 g_object_unref(fixture->dict);
69 g_free(fixture->actual);
70 fixture->actual = NULL;
72 fixture->expected = NULL;
75 g_error_free(fixture->error);
76 fixture->error = NULL;
80 /******************************************************************************
82 *****************************************************************************/
84 test_gplate_for_function(TestGPlateForFunctionFixture *fixture) {
87 /* add the variables */
88 for(i = 0; fixture->data[i].name; i++) {
89 gplate_collection_add_string(fixture->dict,
90 fixture->data[i].name,
91 fixture->data[i].value);
94 fixture->actual = gplate_template_render(fixture->template,
95 fixture->template_string,
98 g_assert(fixture->error == NULL);
100 g_assert_cmpstr(fixture->expected, ==, fixture->actual);
103 /******************************************************************************
105 *****************************************************************************/
107 test_gplate_for_function_zero_elements(TestGPlateForFunctionFixture *fixture,
108 gconstpointer user_data)
110 TestGPlateForFunctionData data[] = {
114 fixture->data = data;
115 fixture->template_string = "{% for in in line %}{{ i }}{% endfor %}";
116 fixture->expected = "";
118 test_gplate_for_function(fixture);
122 test_gplate_for_function_one_element(TestGPlateForFunctionFixture *fixture,
123 gconstpointer user_data)
125 TestGPlateForFunctionData data[] = {
130 fixture->data = data;
131 fixture->template_string = "{% for i in list %}{{ i }}{% endfor %}";
132 fixture->expected = "1";
134 test_gplate_for_function(fixture);
138 test_gplate_for_function_two_elements(TestGPlateForFunctionFixture *fixture,
139 gconstpointer user_data)
141 TestGPlateForFunctionData data[] = {
147 fixture->data = data;
148 fixture->template_string = "{% for i in list %}{{ i }}{% endfor %}";
149 fixture->expected = "AB";
151 test_gplate_for_function(fixture);
154 /******************************************************************************
156 *****************************************************************************/
158 main(gint argc, gchar **argv) {
159 g_test_init(&argc, &argv, NULL);
163 gplate_config_load_default();
165 g_test_add("/functions/foo/zero_elements",
166 TestGPlateForFunctionFixture,
168 test_gplate_for_function_setup,
169 test_gplate_for_function_zero_elements,
170 test_gplate_for_function_teardown);
172 g_test_add("/functions/foo/one_element",
173 TestGPlateForFunctionFixture,
175 test_gplate_for_function_setup,
176 test_gplate_for_function_one_element,
177 test_gplate_for_function_teardown);
179 g_test_add("/functions/foo/two_elements",
180 TestGPlateForFunctionFixture,
182 test_gplate_for_function_setup,
183 test_gplate_for_function_two_elements,
184 test_gplate_for_function_teardown);